Skip to main content

miracle_plugin/
input.rs

1use super::bindings;
2use bitflags::bitflags;
3
4/// Event type.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[repr(u32)]
7pub enum EventType {
8    /// Unused since Mir 0.26.
9    Key = 0,
10    /// Unused since Mir 0.26.
11    Motion = 1,
12    Window = 2,
13    Resize = 3,
14    PromptSessionStateChange = 4,
15    Orientation = 5,
16    CloseWindow = 6,
17    Input = 7,
18    /// Unused since Mir 0.26.
19    InputConfiguration = 8,
20    WindowOutput = 9,
21    InputDeviceState = 10,
22    WindowPlacement = 11,
23}
24
25impl From<EventType> for bindings::MirEventType {
26    fn from(value: EventType) -> Self {
27        value as bindings::MirEventType
28    }
29}
30
31impl TryFrom<bindings::MirEventType> for EventType {
32    type Error = ();
33
34    fn try_from(value: bindings::MirEventType) -> Result<Self, Self::Error> {
35        match value {
36            0 => Ok(Self::Key),
37            1 => Ok(Self::Motion),
38            2 => Ok(Self::Window),
39            3 => Ok(Self::Resize),
40            4 => Ok(Self::PromptSessionStateChange),
41            5 => Ok(Self::Orientation),
42            6 => Ok(Self::CloseWindow),
43            7 => Ok(Self::Input),
44            8 => Ok(Self::InputConfiguration),
45            9 => Ok(Self::WindowOutput),
46            10 => Ok(Self::InputDeviceState),
47            11 => Ok(Self::WindowPlacement),
48            _ => Err(()),
49        }
50    }
51}
52
53/// Input event type.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
55#[repr(u32)]
56pub enum InputEventType {
57    #[default]
58    Key = 0,
59    Touch = 1,
60    Pointer = 2,
61    KeyboardResync = 3,
62}
63
64impl From<InputEventType> for bindings::MirInputEventType {
65    fn from(value: InputEventType) -> Self {
66        value as bindings::MirInputEventType
67    }
68}
69
70impl TryFrom<bindings::MirInputEventType> for InputEventType {
71    type Error = ();
72
73    fn try_from(value: bindings::MirInputEventType) -> Result<Self, Self::Error> {
74        match value {
75            0 => Ok(Self::Key),
76            1 => Ok(Self::Touch),
77            2 => Ok(Self::Pointer),
78            3 => Ok(Self::KeyboardResync),
79            _ => Err(()),
80        }
81    }
82}
83
84bitflags! {
85    /// Key modifier state flags.
86    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87    pub struct InputEventModifiers: u32 {
88        const NONE         = 1 << 0;
89        const ALT          = 1 << 1;
90        const ALT_LEFT     = 1 << 2;
91        const ALT_RIGHT    = 1 << 3;
92        const SHIFT        = 1 << 4;
93        const SHIFT_LEFT   = 1 << 5;
94        const SHIFT_RIGHT  = 1 << 6;
95        const SYM          = 1 << 7;
96        const FUNCTION     = 1 << 8;
97        const CTRL         = 1 << 9;
98        const CTRL_LEFT    = 1 << 10;
99        const CTRL_RIGHT   = 1 << 11;
100        const META         = 1 << 12;
101        const META_LEFT    = 1 << 13;
102        const META_RIGHT   = 1 << 14;
103        const CAPS_LOCK    = 1 << 15;
104        const NUM_LOCK     = 1 << 16;
105        const SCROLL_LOCK  = 1 << 17;
106    }
107}
108
109impl From<InputEventModifiers> for bindings::MirInputEventModifiers {
110    fn from(value: InputEventModifiers) -> Self {
111        value.bits()
112    }
113}
114
115impl From<bindings::MirInputEventModifiers> for InputEventModifiers {
116    fn from(value: bindings::MirInputEventModifiers) -> Self {
117        InputEventModifiers::from_bits_truncate(value)
118    }
119}
120
121/// Keyboard action.
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
123#[repr(u32)]
124pub enum KeyboardAction {
125    /// A key has been released.
126    #[default]
127    Up = 0,
128    /// A key has been pressed.
129    Down = 1,
130    /// System policy triggered a key repeat on an already-down key.
131    Repeat = 2,
132    /// Modifiers updated without a key event; keysym, scan_code and text are not set.
133    Modifiers = 3,
134}
135
136impl From<KeyboardAction> for bindings::MirKeyboardAction {
137    fn from(value: KeyboardAction) -> Self {
138        value as bindings::MirKeyboardAction
139    }
140}
141
142impl TryFrom<bindings::MirKeyboardAction> for KeyboardAction {
143    type Error = ();
144
145    fn try_from(value: bindings::MirKeyboardAction) -> Result<Self, Self::Error> {
146        match value {
147            0 => Ok(Self::Up),
148            1 => Ok(Self::Down),
149            2 => Ok(Self::Repeat),
150            3 => Ok(Self::Modifiers),
151            _ => Err(()),
152        }
153    }
154}
155
156/// Per-touch action.
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
158#[repr(u32)]
159pub enum TouchAction {
160    /// This touch point is going up.
161    #[default]
162    Up = 0,
163    /// This touch point is going down.
164    Down = 1,
165    /// Axis values have changed on this touch point.
166    Change = 2,
167}
168
169impl From<TouchAction> for bindings::MirTouchAction {
170    fn from(value: TouchAction) -> Self {
171        value as bindings::MirTouchAction
172    }
173}
174
175impl TryFrom<bindings::MirTouchAction> for TouchAction {
176    type Error = ();
177
178    fn try_from(value: bindings::MirTouchAction) -> Result<Self, Self::Error> {
179        match value {
180            0 => Ok(Self::Up),
181            1 => Ok(Self::Down),
182            2 => Ok(Self::Change),
183            _ => Err(()),
184        }
185    }
186}
187
188/// Touch axis identifier.
189#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
190#[repr(u32)]
191pub enum TouchAxis {
192    /// X coordinate of the touch.
193    #[default]
194    X = 0,
195    /// Y coordinate of the touch.
196    Y = 1,
197    /// Pressure of the touch.
198    Pressure = 2,
199    /// Length of the major axis of the touch ellipse.
200    TouchMajor = 3,
201    /// Length of the minor axis of the touch ellipse.
202    TouchMinor = 4,
203    /// Diameter of a circle centered on the touch point.
204    Size = 5,
205}
206
207impl From<TouchAxis> for bindings::MirTouchAxis {
208    fn from(value: TouchAxis) -> Self {
209        value as bindings::MirTouchAxis
210    }
211}
212
213impl TryFrom<bindings::MirTouchAxis> for TouchAxis {
214    type Error = ();
215
216    fn try_from(value: bindings::MirTouchAxis) -> Result<Self, Self::Error> {
217        match value {
218            0 => Ok(Self::X),
219            1 => Ok(Self::Y),
220            2 => Ok(Self::Pressure),
221            3 => Ok(Self::TouchMajor),
222            4 => Ok(Self::TouchMinor),
223            5 => Ok(Self::Size),
224            _ => Err(()),
225        }
226    }
227}
228
229/// Per-touch tool type.
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
231#[repr(u32)]
232pub enum TouchTooltype {
233    /// Tool type could not be determined.
234    #[default]
235    Unknown = 0,
236    /// Touch made with a finger.
237    Finger = 1,
238    /// Touch made with a stylus.
239    Stylus = 2,
240}
241
242impl From<TouchTooltype> for bindings::MirTouchTooltype {
243    fn from(value: TouchTooltype) -> Self {
244        value as bindings::MirTouchTooltype
245    }
246}
247
248impl TryFrom<bindings::MirTouchTooltype> for TouchTooltype {
249    type Error = ();
250
251    fn try_from(value: bindings::MirTouchTooltype) -> Result<Self, Self::Error> {
252        match value {
253            0 => Ok(Self::Unknown),
254            1 => Ok(Self::Finger),
255            2 => Ok(Self::Stylus),
256            _ => Err(()),
257        }
258    }
259}
260
261/// Pointer action.
262#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
263#[repr(u32)]
264pub enum PointerAction {
265    /// A pointer button has been released.
266    #[default]
267    ButtonUp = 0,
268    /// A pointer button has been pressed.
269    ButtonDown = 1,
270    /// The pointer entered the surface.
271    Enter = 2,
272    /// The pointer left the surface.
273    Leave = 3,
274    /// Axis values have changed.
275    Motion = 4,
276}
277
278impl From<PointerAction> for bindings::MirPointerAction {
279    fn from(value: PointerAction) -> Self {
280        value as bindings::MirPointerAction
281    }
282}
283
284impl TryFrom<bindings::MirPointerAction> for PointerAction {
285    type Error = ();
286
287    fn try_from(value: bindings::MirPointerAction) -> Result<Self, Self::Error> {
288        match value {
289            0 => Ok(Self::ButtonUp),
290            1 => Ok(Self::ButtonDown),
291            2 => Ok(Self::Enter),
292            3 => Ok(Self::Leave),
293            4 => Ok(Self::Motion),
294            _ => Err(()),
295        }
296    }
297}
298
299/// Pointer axis identifier.
300#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
301#[repr(u32)]
302pub enum PointerAxis {
303    /// Absolute X coordinate of the pointer.
304    #[default]
305    X = 0,
306    /// Absolute Y coordinate of the pointer.
307    Y = 1,
308    /// Vertical scroll wheel ticks.
309    VScroll = 2,
310    /// Horizontal scroll wheel ticks.
311    HScroll = 3,
312    /// Last reported X differential from the pointer.
313    RelativeX = 4,
314    /// Last reported Y differential from the pointer.
315    RelativeY = 5,
316    /// Physical vertical scroll wheel clicks.
317    VScrollDiscrete = 6,
318    /// Physical horizontal scroll wheel clicks.
319    HScrollDiscrete = 7,
320    /// Fractional values of 120 for high-res vertical scrolling.
321    VScrollValue120 = 8,
322    /// Fractional values of 120 for high-res horizontal scrolling.
323    HScrollValue120 = 9,
324}
325
326impl From<PointerAxis> for bindings::MirPointerAxis {
327    fn from(value: PointerAxis) -> Self {
328        value as bindings::MirPointerAxis
329    }
330}
331
332impl TryFrom<bindings::MirPointerAxis> for PointerAxis {
333    type Error = ();
334
335    fn try_from(value: bindings::MirPointerAxis) -> Result<Self, Self::Error> {
336        match value {
337            0 => Ok(Self::X),
338            1 => Ok(Self::Y),
339            2 => Ok(Self::VScroll),
340            3 => Ok(Self::HScroll),
341            4 => Ok(Self::RelativeX),
342            5 => Ok(Self::RelativeY),
343            6 => Ok(Self::VScrollDiscrete),
344            7 => Ok(Self::HScrollDiscrete),
345            8 => Ok(Self::VScrollValue120),
346            9 => Ok(Self::HScrollValue120),
347            _ => Err(()),
348        }
349    }
350}
351
352bitflags! {
353    /// Pointer button state flags.
354    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
355    pub struct PointerButtons: u32 {
356        const PRIMARY   = 1 << 0;
357        const SECONDARY = 1 << 1;
358        const TERTIARY  = 1 << 2;
359        const BACK      = 1 << 3;
360        const FORWARD   = 1 << 4;
361        const SIDE      = 1 << 5;
362        const EXTRA     = 1 << 6;
363        const TASK      = 1 << 7;
364    }
365}
366
367impl From<PointerButtons> for bindings::MirPointerButtons {
368    fn from(value: PointerButtons) -> Self {
369        value.bits()
370    }
371}
372
373impl From<bindings::MirPointerButtons> for PointerButtons {
374    fn from(value: bindings::MirPointerButtons) -> Self {
375        PointerButtons::from_bits_truncate(value)
376    }
377}
378
379/// Source of a pointer axis event.
380#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
381#[repr(u32)]
382pub enum PointerAxisSource {
383    #[default]
384    None = 0,
385    Wheel = 1,
386    Finger = 2,
387    Continuous = 3,
388    WheelTilt = 4,
389}
390
391impl From<PointerAxisSource> for bindings::MirPointerAxisSource {
392    fn from(value: PointerAxisSource) -> Self {
393        value as bindings::MirPointerAxisSource
394    }
395}
396
397impl TryFrom<bindings::MirPointerAxisSource> for PointerAxisSource {
398    type Error = ();
399
400    fn try_from(value: bindings::MirPointerAxisSource) -> Result<Self, Self::Error> {
401        match value {
402            0 => Ok(Self::None),
403            1 => Ok(Self::Wheel),
404            2 => Ok(Self::Finger),
405            3 => Ok(Self::Continuous),
406            4 => Ok(Self::WheelTilt),
407            _ => Err(()),
408        }
409    }
410}
411
412/// A keyboard event.
413pub struct KeyboardEvent {
414    /// The keyboard action.
415    pub action: KeyboardAction,
416
417    /// The xkeysym.
418    ///
419    /// You may use the xkeysym crate to match against this.
420    pub keysym: u32,
421
422    /// The raw scan code.
423    ///
424    /// Prefer using the keysym.
425    pub scan_code: i32,
426
427    /// The modifiers held during this event.
428    pub modifiers: InputEventModifiers,
429}
430
431/// A pointer event.
432pub struct PointerEvent {
433    /// The x position of the pointer.
434    pub x: f32,
435
436    /// The y position of the pointer.
437    pub y: f32,
438
439    /// The pointer action.
440    pub action: PointerAction,
441
442    /// The modifiers held during the event.
443    pub modifiers: InputEventModifiers,
444
445    /// The buttons held during the event.
446    pub buttons: PointerButtons,
447}