pub use error::ProtocolError; use std::fmt::{self, Display}; pub mod error; pub mod proto; pub mod scancode; #[cfg(all(unix, feature = "libei", not(target_os = "macos")))] mod libei; // FIXME pub const BTN_LEFT: u32 = 0x110; pub const BTN_RIGHT: u32 = 0x111; pub const BTN_MIDDLE: u32 = 0x112; pub const BTN_BACK: u32 = 0x113; pub const BTN_FORWARD: u32 = 0x114; #[derive(Debug, PartialEq, Clone, Copy)] pub enum PointerEvent { /// relative motion event Motion { time: u32, dx: f64, dy: f64 }, /// mouse button event Button { time: u32, button: u32, state: u32 }, /// axis event, scroll event for touchpads Axis { time: u32, axis: u8, value: f64 }, /// discrete axis event, scroll event for mice - 120 = one scroll tick AxisDiscrete120 { axis: u8, value: i32 }, /// frame event Frame {}, } #[derive(Debug, PartialEq, Clone, Copy)] pub enum KeyboardEvent { /// a key press / release event Key { time: u32, key: u32, state: u8 }, /// modifiers changed state Modifiers { mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32, }, } #[derive(PartialEq, Debug, Clone, Copy)] pub enum Event { /// pointer event (motion / button / axis) Pointer(PointerEvent), /// keyboard events (key / modifiers) Keyboard(KeyboardEvent), /// enter event: request to enter a client. /// The client must release the pointer if it is grabbed /// and reply with a leave event, as soon as its ready to /// receive events Enter(), /// leave event: this client is now ready to receive events and will /// not send any events after until it sends an enter event Leave(), /// ping a client, to see if it is still alive. A client that does /// not respond with a pong event will be assumed to be offline. Ping(), /// response to a ping event: this event signals that a client /// is still alive but must otherwise be ignored Pong(), /// explicit disconnect request. The client will no longer /// send events until the next Enter event. All of its keys should be released. Disconnect(), } impl Display for PointerEvent { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { PointerEvent::Motion { time: _, dx, dy } => write!(f, "motion({dx},{dy})"), PointerEvent::Button { time: _, button, state, } => { let str = match *button { BTN_LEFT => Some("left"), BTN_RIGHT => Some("right"), BTN_MIDDLE => Some("middle"), BTN_FORWARD => Some("forward"), BTN_BACK => Some("back"), _ => None, }; if let Some(button) = str { write!(f, "button({button}, {state})") } else { write!(f, "button({button}, {state}") } } PointerEvent::Axis { time: _, axis, value, } => write!(f, "scroll({axis}, {value})"), PointerEvent::AxisDiscrete120 { axis, value } => { write!(f, "scroll-120 ({axis}, {value})") } PointerEvent::Frame {} => write!(f, "frame()"), } } } impl Display for KeyboardEvent { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { KeyboardEvent::Key { time: _, key, state, } => { let scan = scancode::Linux::try_from(*key); if let Ok(scan) = scan { write!(f, "key({scan:?}, {state})") } else { write!(f, "key({key}, {state})") } } KeyboardEvent::Modifiers { mods_depressed, mods_latched, mods_locked, group, } => write!( f, "modifiers({mods_depressed},{mods_latched},{mods_locked},{group})" ), } } } impl Display for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Event::Pointer(p) => write!(f, "{}", p), Event::Keyboard(k) => write!(f, "{}", k), Event::Enter() => write!(f, "enter"), Event::Leave() => write!(f, "leave"), Event::Ping() => write!(f, "ping"), Event::Pong() => write!(f, "pong"), Event::Disconnect() => write!(f, "disconnect"), } } }