45 lines
1 KiB
Rust
45 lines
1 KiB
Rust
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);
|