diff options
Diffstat (limited to 'src/event.rs')
-rw-r--r-- | src/event.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 0000000..e15ee18 --- /dev/null +++ b/src/event.rs @@ -0,0 +1,69 @@ +use x11::xlib::{ + XEvent, + XKeyEvent, + XButtonEvent, + XMotionEvent, + XCrossingEvent, + XCreateWindowEvent, + XDestroyWindowEvent, + XConfigureEvent, +}; + +use x11::xlib; +use x11::xlib::Display; +use std::fmt; + +use safe_x11::next_xevent; + +pub enum Event { + Key(XKeyEvent, bool), + Button(XButtonEvent, bool), + Drag(XMotionEvent), + Enter(XCrossingEvent), + Leave(XCrossingEvent), + Generic(XEvent), + Create(XCreateWindowEvent), + Destroy(XDestroyWindowEvent), + Configure(XConfigureEvent), + NoEvent +} + +impl fmt::Debug for Event { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + &Event::Key(ev, pressed) => format!("Key({}, {})", XEvent::from(ev).get_type(), pressed), + &Event::Button(ev, pressed) => format!("Button({}, {})", XEvent::from(ev).get_type(), pressed), + &Event::Drag(ev) => format!("Drag({})", XEvent::from(ev).get_type()), + &Event::Generic(ev) => format!("Generic({})", XEvent::from(ev).get_type()), + &Event::Enter(ev) => format!("Enter({})", XEvent::from(ev).get_type()), + &Event::Leave(ev) => format!("Enter({})", XEvent::from(ev).get_type()), + &Event::Create(ev) => format!("Create({})", XEvent::from(ev).get_type()), + &Event::Destroy(ev) => format!("Destroy({})", XEvent::from(ev).get_type()), + &Event::Configure(ev) => format!("Configure({})", XEvent::from(ev).get_type()), + &Event::NoEvent => format!("NoEvent"), + }; + + write!(f, "{}", s) + } +} + +pub fn next_event(display: *mut Display) -> Event { + let ev = next_xevent(display); + + match ev.get_type() { + 2 => Event::Key(xlib::XKeyEvent::from(ev), true), + 3 => Event::Key(xlib::XKeyEvent::from(ev), false), + 4 => Event::Button(xlib::XButtonEvent::from(ev), true), + 5 => Event::Button(xlib::XButtonEvent::from(ev), false), + 6 => Event::Drag(XMotionEvent::from(ev)), + 7 => Event::Enter(XCrossingEvent::from(ev)), + 8 => Event::Leave(XCrossingEvent::from(ev)), + 16 => Event::Create(XCreateWindowEvent::from(ev)), + 17 => Event::Destroy(XDestroyWindowEvent::from(ev)), + 22 => Event::Configure(XConfigureEvent::from(ev)), + e => { + println!("Unknown event {}", e); + Event::NoEvent + }, + } +} |