From c7fc13ed64e7807fd35419382003903a97efc078 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 11 Sep 2018 17:25:50 -0500 Subject: [PATCH] sigh --- src/gui.rs | 17 +++++++++++++---- src/lib.rs | 2 +- src/xlib/cursor.rs | 13 +++++++++++++ src/xlib/cursorfont.rs | 5 +++++ src/xlib/display.rs | 19 ++++++++++++++++++- src/xlib/errors.rs | 3 +++ src/xlib/event.rs | 5 ++++- src/xlib/mod.rs | 7 +++++++ 8 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/xlib/cursor.rs create mode 100644 src/xlib/cursorfont.rs diff --git a/src/gui.rs b/src/gui.rs index 01bae88..d02bbd8 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -4,8 +4,8 @@ use xlib::{Display, EventKind, Visual, Window}; use errors::ScreenshotError; use Options; use Rectangle; -use SelectWindow; use Region; +use SelectWindow; pub struct GUI { pub(crate) display: Display, @@ -36,8 +36,8 @@ impl GUI { y = region.y; width = region.width; height = region.height; - }, - _ => () + } + _ => (), } Image2::create_from_drawable(window, 0, x, y, width as i32, height as i32, true) @@ -60,10 +60,19 @@ impl GUI { let root_im = root.get_image(); let mut done = 0; + let mut button_press = false; while done == 0 && self.display.pending()? > 0 { let ev = self.display.next_event()?; match ev.kind() { - EventKind::ButtonPress => (), + EventKind::ButtonPress => { + button_press = true; + } + EventKind::ButtonRelease => { + if button_press { + done = 1; + } + button_press = false; + } _ => (), } } diff --git a/src/lib.rs b/src/lib.rs index 19d28fd..9e96c3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,9 +21,9 @@ mod window; pub mod imlib2; pub mod xlib; +use errors::ScreenshotError; use structopt::StructOpt; use xlib::Rectangle; -use errors::ScreenshotError; pub use capture::capture; pub use options::{Options, Region}; diff --git a/src/xlib/cursor.rs b/src/xlib/cursor.rs new file mode 100644 index 0000000..41bef77 --- /dev/null +++ b/src/xlib/cursor.rs @@ -0,0 +1,13 @@ +use x11::xlib as x; + +/// Mouse pointer +pub struct Cursor { + pub(crate) display: *mut x::Display, + pub(crate) inner: x::Cursor, +} + +impl Drop for Cursor { + fn drop(&mut self) { + unsafe { x::XFreeCursor(self.display, self.inner) }; + } +} diff --git a/src/xlib/cursorfont.rs b/src/xlib/cursorfont.rs new file mode 100644 index 0000000..4d96aba --- /dev/null +++ b/src/xlib/cursorfont.rs @@ -0,0 +1,5 @@ +pub const XC_crosshair: u32 = 34; +pub const XC_ll_angle: u32 = 76; +pub const XC_lr_angle: u32 = 78; +pub const XC_ul_angle: u32 = 144; +pub const XC_ur_angle: u32 = 148; diff --git a/src/xlib/display.rs b/src/xlib/display.rs index ae78063..60172db 100644 --- a/src/xlib/display.rs +++ b/src/xlib/display.rs @@ -3,7 +3,7 @@ use std::ffi::CString; use libc; use x11::xlib as x; -use xlib::{Event, Visual, Window, X11Error}; +use xlib::{Cursor, Event, Visual, Window, X11Error}; /// A connection to an X server. pub struct Display { @@ -30,6 +30,18 @@ impl Display { Ok(Display { inner }) } + /// Wrapper around XCreateFontCursor + pub fn create_font_cursor(&self, shape: u32) -> Result { + let cursor = unsafe { x::XCreateFontCursor(self.inner, shape) as x::Cursor }; + if cursor == 0 { + return Err(X11Error::CreateCursorError); + } + Ok(Cursor { + display: self.inner, + inner: cursor, + }) + } + /// Get the next event, blocks until an event is reached. pub fn next_event(&self) -> Result { let event = @@ -117,6 +129,11 @@ impl Display { Ok((rx, ry, child)) } + /// Sync + pub fn sync(&self, discard: bool) { + unsafe { x::XSync(self.inner, if discard { 1 } else { 0 }) }; + } + /// eturns the focus window and the current focus state. pub fn get_input_focus(&self) -> Result<(Window, i32), X11Error> { let mut focus_return: x::Window = 0; diff --git a/src/xlib/errors.rs b/src/xlib/errors.rs index d43daa0..8978708 100644 --- a/src/xlib/errors.rs +++ b/src/xlib/errors.rs @@ -2,6 +2,9 @@ #[allow(missing_docs)] #[derive(Debug, Fail)] pub enum X11Error { + #[fail(display = "failed to create cursor")] + CreateCursorError, + #[fail(display = "failed to open display")] DisplayOpenError, diff --git a/src/xlib/event.rs b/src/xlib/event.rs index 693d495..9ed9a22 100644 --- a/src/xlib/event.rs +++ b/src/xlib/event.rs @@ -11,9 +11,12 @@ pub struct Event { /// Type of event pub enum EventKind { - /// Was a button pressed + /// A mouse button was pressed ButtonPress, + /// A mouse button was released + ButtonRelease, + /// None event None, } diff --git a/src/xlib/mod.rs b/src/xlib/mod.rs index 1b7855c..b15d01d 100644 --- a/src/xlib/mod.rs +++ b/src/xlib/mod.rs @@ -2,6 +2,7 @@ //! //! I need this for my project. +mod cursor; mod display; mod drawable; mod errors; @@ -12,6 +13,12 @@ mod rect; mod visual; mod window; +#[allow(non_upper_case_globals)] +#[allow(missing_docs)] +mod cursorfont; + +pub use self::cursor::Cursor; +pub use self::cursorfont::*; pub use self::display::{Display, GetDisplay}; pub use self::drawable::Drawable; pub use self::errors::X11Error;