Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Jul 13, 2018
1 parent 215fc5d commit d9a440c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 85 deletions.
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ extern crate failure;
extern crate serde_derive;
extern crate termcolor;

pub mod window;
mod report;
pub mod window;

use report::{Method, Report};

Expand Down Expand Up @@ -102,8 +102,8 @@ pub struct Metadata {
#[macro_export]
macro_rules! setup_panic {
($meta:expr) => {
use $crate::{handle_dump, Metadata};
use std::panic::{self, PanicInfo};
use $crate::{handle_dump, Metadata};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);
Expand All @@ -116,13 +116,15 @@ macro_rules! setup_panic {
};

() => {
use $crate::{handle_dump, Metadata};
use std::panic::{self, PanicInfo};
use $crate::{handle_dump, Metadata};

let meta = Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
authors: env!("CARGO_PKG_AUTHORS")
.replace(":", ", ")
.into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};

Expand Down Expand Up @@ -159,8 +161,12 @@ pub fn write_msg<P: AsRef<Path>>(
meta: &Metadata,
buffer: &mut Write,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(&meta.version, &meta.name, &meta.authors, &meta.homepage);
let (_version, name, authors, homepage) = (
&meta.version,
&meta.name,
&meta.authors,
&meta.homepage,
);

writeln!(buffer, "Well, this is embarrassing.\n")?;
writeln!(
Expand Down
167 changes: 98 additions & 69 deletions src/window/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::os::raw::*;
use std::ptr;

use self::x11_dl::xlib;
use self::x11_dl::xlib::Xlib;
use self::x11_dl::xlib::XRectangle;
use self::x11_dl::xlib::Xlib;

const TEXT_MARGIN: i32 = 10;

Expand Down Expand Up @@ -39,24 +39,30 @@ pub(crate) fn create_window(message: String) {
attributes.background_pixel = (xlib.XWhitePixel)(display, screen);

// window height gets reset later
(xlib.XCreateWindow)(display,
root,
0,
0,
window_width,
window_height,
0,
0,
xlib::InputOutput as c_uint,
ptr::null_mut(),
xlib::CWBackPixel,
&mut attributes)
(xlib.XCreateWindow)(
display,
root,
0,
0,
window_width,
window_height,
0,
0,
xlib::InputOutput as c_uint,
ptr::null_mut(),
xlib::CWBackPixel,
&mut attributes,
)
};

// set window title.
let title_str = CString::new("Panic!").unwrap();
unsafe {
(xlib.XStoreName)(display, window, title_str.as_ptr() as *const c_char)
(xlib.XStoreName)(
display,
window,
title_str.as_ptr() as *const c_char,
)
};

// allow the window to be deleted by the window manager
Expand All @@ -66,46 +72,61 @@ pub(crate) fn create_window(message: String) {
(xlib.XInternAtom)(display, wm_protocols_str.as_ptr(), xlib::False)
};
let wm_delete_window = unsafe {
(xlib.XInternAtom)(display, wm_delete_window_str.as_ptr(), xlib::False)
(xlib.XInternAtom)(
display,
wm_delete_window_str.as_ptr(),
xlib::False,
)
};
let mut protocols = [wm_delete_window];
unsafe {
(xlib.XSetWMProtocols)(display,
window,
protocols.as_mut_ptr(),
protocols.len() as c_int)
(xlib.XSetWMProtocols)(
display,
window,
protocols.as_mut_ptr(),
protocols.len() as c_int,
)
};

// let the window manager know this is a dialog box.
let wm_window_type_str = CString::new("_NET_WM_WINDOW_TYPE").unwrap();
let wm_window_type_dialog_str = CString::new("_NET_WM_WINDOW_TYPE_DIALOG")
.unwrap();
let wm_window_type_dialog_str =
CString::new("_NET_WM_WINDOW_TYPE_DIALOG").unwrap();
let wm_window_type = unsafe {
(xlib.XInternAtom)(display, wm_window_type_str.as_ptr(), xlib::False)
(xlib.XInternAtom)(
display,
wm_window_type_str.as_ptr(),
xlib::False,
)
};
let wm_window_type_dialog = unsafe {
(xlib.XInternAtom)(
display,
wm_window_type_dialog_str.as_ptr(),
xlib::False,
)
};
let wm_window_type_dialog =
unsafe {
(xlib.XInternAtom)(display,
wm_window_type_dialog_str.as_ptr(),
xlib::False)
};
let wm_window_type_dialog = &wm_window_type_dialog as *const u64 as *const u8;
unsafe {
(xlib.XChangeProperty)(display,
window,
wm_window_type,
xlib::XA_ATOM,
32,
xlib::PropModeReplace,
wm_window_type_dialog,
1)
(xlib.XChangeProperty)(
display,
window,
wm_window_type,
xlib::XA_ATOM,
32,
xlib::PropModeReplace,
wm_window_type_dialog,
1,
)
};

// specify events to use
unsafe {
(xlib.XSelectInput)(display,
window,
xlib::ExposureMask | xlib::StructureNotifyMask)
(xlib.XSelectInput)(
display,
window,
xlib::ExposureMask | xlib::StructureNotifyMask,
)
};

// create graphics context
Expand All @@ -115,17 +136,19 @@ pub(crate) fn create_window(message: String) {
};

// create font set
let font_list = CString::new("-*-*-medium-r-normal--*-120-*-*-*-*-*-*")
.unwrap();
let font_list =
CString::new("-*-*-medium-r-normal--*-120-*-*-*-*-*-*").unwrap();
let mut missing = ptr::null_mut();
let mut num_missing = 0;
let mut foo = ptr::null_mut();
let font_set = unsafe {
(xlib.XCreateFontSet)(display,
font_list.as_ptr() as *const c_char,
&mut missing,
&mut num_missing,
&mut foo)
(xlib.XCreateFontSet)(
display,
font_list.as_ptr() as *const c_char,
&mut missing,
&mut num_missing,
&mut foo,
)
};

// show window.
Expand Down Expand Up @@ -169,7 +192,7 @@ pub(crate) fn create_window(message: String) {
xlib::ConfigureNotify => {
let configure_event: &xlib::XConfigureEvent = event.as_ref();
window_width = configure_event.width as u32;
#[allow(unused_assignments)]
#[allow(unused_assignments)]
{
window_height = configure_event.height as u32;
}
Expand All @@ -180,14 +203,16 @@ pub(crate) fn create_window(message: String) {
split_message(&xlib, font_set, &message, window_width as i32);
for (i, line) in message_lines.iter().enumerate() {
unsafe {
(xlib.Xutf8DrawString)(display,
window,
font_set,
gc,
TEXT_MARGIN,
(i as i32 + 1) * max_line_height,
line.as_ptr() as *const c_char,
line.to_bytes().len() as i32)
(xlib.Xutf8DrawString)(
display,
window,
font_set,
gc,
TEXT_MARGIN,
(i as i32 + 1) * max_line_height,
line.as_ptr() as *const c_char,
line.to_bytes().len() as i32,
)
};
}
}
Expand All @@ -199,10 +224,11 @@ pub(crate) fn create_window(message: String) {
unsafe { (xlib.XCloseDisplay)(display) };
}

fn line_width_height(xlib: &Xlib,
font_set: *mut xlib::_XOC,
text: &CString)
-> (u16, u16) {
fn line_width_height(
xlib: &Xlib,
font_set: *mut xlib::_XOC,
text: &CString,
) -> (u16, u16) {
let mut overall_ink = XRectangle {
x: 0,
y: 0,
Expand All @@ -211,21 +237,24 @@ fn line_width_height(xlib: &Xlib,
};
let mut overall_logical = overall_ink.clone();
unsafe {
(xlib.Xutf8TextExtents)(font_set,
text.as_ptr() as *const c_char,
text.to_bytes().len() as i32,
&mut overall_ink as *mut XRectangle,
&mut overall_logical as *mut XRectangle)
(xlib.Xutf8TextExtents)(
font_set,
text.as_ptr() as *const c_char,
text.to_bytes().len() as i32,
&mut overall_ink as *mut XRectangle,
&mut overall_logical as *mut XRectangle,
)
};

(overall_logical.width, overall_logical.height)
}

fn split_message(xlib: &Xlib,
font_set: *mut xlib::_XOC,
message: &String,
window_width: i32)
-> Vec<CString> {
fn split_message(
xlib: &Xlib,
font_set: *mut xlib::_XOC,
message: &String,
window_width: i32,
) -> Vec<CString> {
let mut processed_lines = vec![];
for line in message.lines() {
if line.is_empty() {
Expand Down
20 changes: 15 additions & 5 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains the create_window function
#[cfg(all(target_os = "windows", feature = "gui"))]
#[path = "windows.rs"]
mod window_impl;
Expand All @@ -8,25 +10,33 @@ mod window_impl;

#[cfg(not(any(target_os = "linux", target_os = "windows")))]
mod window_impl {
pub(crate) fn create_window(_: String) { }
pub(crate) fn create_window(_: String) {}
}

use std::path::Path;
use Metadata;
use std::path::Path;

/// Generate the human-panic mesage from the passed path and Metadata and
/// display it in a native OS window. GUI applications will need to make use
/// of this as they may not have a terminal to display in.
#[allow(unused)]
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn create_window<P: AsRef<Path>>(file_path: Option<P>, meta: &Metadata) {
#[cfg(feature = "gui")]
{
use std::io::{Cursor, Read};
use write_msg;

let mut buffer = Cursor::new(vec!());
write_msg(file_path, meta, &mut buffer).expect("human-panic: generating error message for GUI failed: write_msg");
let mut buffer = Cursor::new(vec![]);
write_msg(file_path, meta, &mut buffer).expect(
"human-panic: generating error message for GUI failed: write_msg",
);
buffer.set_position(0);

let mut message = String::new();
buffer.read_to_string(&mut message).expect("human-panic: generating error message for GUI failed: read_to_string");
buffer.read_to_string(&mut message).expect(
"human-panic: generating error message for GUI failed: read_to_string",
);

window_impl::create_window(message);
}
Expand Down
23 changes: 18 additions & 5 deletions src/window/windows.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
extern crate winapi;

use self::winapi::um::winuser::{MessageBoxW, MB_OK};
use std::ffi::OsStr;
use std::iter::once;
use std::os::windows::ffi::OsStrExt;
use std::ptr::null_mut;
use self::winapi::um::winuser::{MB_OK, MessageBoxW};

pub(crate) fn create_window(message: String) {
let message_wide: Vec<u16> = OsStr::new(message.as_str()).encode_wide().chain(once(0)).collect();
let panic_wide: Vec<u16> = OsStr::new("Panic!").encode_wide().chain(once(0)).collect();
let ret = unsafe { MessageBoxW(null_mut(), message_wide.as_ptr(), panic_wide.as_ptr(), MB_OK) };
let message_wide: Vec<u16> = OsStr::new(message.as_str())
.encode_wide()
.chain(once(0))
.collect();
let panic_wide: Vec<u16> = OsStr::new("Panic!")
.encode_wide()
.chain(once(0))
.collect();
let ret = unsafe {
MessageBoxW(
null_mut(),
message_wide.as_ptr(),
panic_wide.as_ptr(),
MB_OK,
)
};
if ret == 0 {
eprintln!("Failed to create error message window");
eprintln!("Failed to create error message window");
}
}

0 comments on commit d9a440c

Please sign in to comment.