safex11/src/xlib/event.rs

46 lines
1 KiB
Rust
Raw Normal View History

2020-06-28 09:43:22 +00:00
use x11::xlib;
use crate::errors::{Error, Result};
/// An x11 Event
#[derive(Debug)]
pub struct Event {
inner: *mut xlib::XEvent,
kind: EventKind,
}
/// Type of event
#[derive(Debug)]
pub enum EventKind {
KeyPress(KeyEvent),
KeyRelease(KeyEvent),
}
impl Event {
/// Returns the EventKind of this event
pub fn kind(&self) -> &EventKind {
&self.kind
}
pub(super) unsafe fn from_raw(event: *mut xlib::XEvent) -> Result<Self> {
let inner = event;
let event = unsafe { *event };
debug!("event type: {:?}", event.type_);
let kind = match event.type_ {
xlib::KeyPress => EventKind::KeyPress(KeyEvent(event.key)),
xlib::KeyRelease => EventKind::KeyRelease(KeyEvent(event.key)),
other => return Err(Error::InvalidEventType(other)),
};
Ok(Event { inner, kind })
}
}
impl Drop for Event {
fn drop(&mut self) {
unsafe { libc::free(self.inner as *mut libc::c_void) };
}
}
#[derive(Debug)]
pub struct KeyEvent(xlib::XKeyEvent);