input_event/
lib.rs

1use std::fmt::{self, Display};
2
3pub mod error;
4pub mod scancode;
5
6#[cfg(all(unix, feature = "libei", not(target_os = "macos")))]
7mod libei;
8
9// FIXME
10pub const BTN_LEFT: u32 = 0x110;
11pub const BTN_RIGHT: u32 = 0x111;
12pub const BTN_MIDDLE: u32 = 0x112;
13pub const BTN_BACK: u32 = 0x113;
14pub const BTN_FORWARD: u32 = 0x114;
15
16#[derive(Debug, PartialEq, Clone, Copy)]
17pub enum PointerEvent {
18    /// relative motion event
19    Motion { time: u32, dx: f64, dy: f64 },
20    /// mouse button event
21    Button { time: u32, button: u32, state: u32 },
22    /// axis event, scroll event for touchpads
23    Axis { time: u32, axis: u8, value: f64 },
24    /// discrete axis event, scroll event for mice - 120 = one scroll tick
25    AxisDiscrete120 { axis: u8, value: i32 },
26}
27
28#[derive(Debug, PartialEq, Clone, Copy)]
29pub enum KeyboardEvent {
30    /// a key press / release event
31    Key { time: u32, key: u32, state: u8 },
32    /// modifiers changed state
33    Modifiers {
34        depressed: u32,
35        latched: u32,
36        locked: u32,
37        group: u32,
38    },
39}
40
41#[derive(PartialEq, Debug, Clone, Copy)]
42pub enum Event {
43    /// pointer event (motion / button / axis)
44    Pointer(PointerEvent),
45    /// keyboard events (key / modifiers)
46    Keyboard(KeyboardEvent),
47}
48
49impl Display for PointerEvent {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            PointerEvent::Motion { time: _, dx, dy } => write!(f, "motion({dx},{dy})"),
53            PointerEvent::Button {
54                time: _,
55                button,
56                state,
57            } => {
58                let str = match *button {
59                    BTN_LEFT => Some("left"),
60                    BTN_RIGHT => Some("right"),
61                    BTN_MIDDLE => Some("middle"),
62                    BTN_FORWARD => Some("forward"),
63                    BTN_BACK => Some("back"),
64                    _ => None,
65                };
66                if let Some(button) = str {
67                    write!(f, "button({button}, {state})")
68                } else {
69                    write!(f, "button({button}, {state}")
70                }
71            }
72            PointerEvent::Axis {
73                time: _,
74                axis,
75                value,
76            } => write!(f, "scroll({axis}, {value})"),
77            PointerEvent::AxisDiscrete120 { axis, value } => {
78                write!(f, "scroll-120 ({axis}, {value})")
79            }
80        }
81    }
82}
83
84impl Display for KeyboardEvent {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        match self {
87            KeyboardEvent::Key {
88                time: _,
89                key,
90                state,
91            } => {
92                let scan = scancode::Linux::try_from(*key);
93                if let Ok(scan) = scan {
94                    write!(f, "key({scan:?}, {state})")
95                } else {
96                    write!(f, "key({key}, {state})")
97                }
98            }
99            KeyboardEvent::Modifiers {
100                depressed: mods_depressed,
101                latched: mods_latched,
102                locked: mods_locked,
103                group,
104            } => write!(
105                f,
106                "modifiers({mods_depressed},{mods_latched},{mods_locked},{group})"
107            ),
108        }
109    }
110}
111
112impl Display for Event {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        match self {
115            Event::Pointer(p) => write!(f, "{}", p),
116            Event::Keyboard(k) => write!(f, "{}", k),
117        }
118    }
119}