miracle-plugin 0.0.10

Rust bindings for the miracle-wm plugin API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use super::bindings;
use bitflags::bitflags;

/// Event type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum EventType {
    /// Unused since Mir 0.26.
    Key = 0,
    /// Unused since Mir 0.26.
    Motion = 1,
    Window = 2,
    Resize = 3,
    PromptSessionStateChange = 4,
    Orientation = 5,
    CloseWindow = 6,
    Input = 7,
    /// Unused since Mir 0.26.
    InputConfiguration = 8,
    WindowOutput = 9,
    InputDeviceState = 10,
    WindowPlacement = 11,
}

impl From<EventType> for bindings::MirEventType {
    fn from(value: EventType) -> Self {
        value as bindings::MirEventType
    }
}

impl TryFrom<bindings::MirEventType> for EventType {
    type Error = ();

    fn try_from(value: bindings::MirEventType) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Key),
            1 => Ok(Self::Motion),
            2 => Ok(Self::Window),
            3 => Ok(Self::Resize),
            4 => Ok(Self::PromptSessionStateChange),
            5 => Ok(Self::Orientation),
            6 => Ok(Self::CloseWindow),
            7 => Ok(Self::Input),
            8 => Ok(Self::InputConfiguration),
            9 => Ok(Self::WindowOutput),
            10 => Ok(Self::InputDeviceState),
            11 => Ok(Self::WindowPlacement),
            _ => Err(()),
        }
    }
}

/// Input event type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum InputEventType {
    #[default]
    Key = 0,
    Touch = 1,
    Pointer = 2,
    KeyboardResync = 3,
}

impl From<InputEventType> for bindings::MirInputEventType {
    fn from(value: InputEventType) -> Self {
        value as bindings::MirInputEventType
    }
}

impl TryFrom<bindings::MirInputEventType> for InputEventType {
    type Error = ();

    fn try_from(value: bindings::MirInputEventType) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Key),
            1 => Ok(Self::Touch),
            2 => Ok(Self::Pointer),
            3 => Ok(Self::KeyboardResync),
            _ => Err(()),
        }
    }
}

bitflags! {
    /// Key modifier state flags.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct InputEventModifiers: u32 {
        const NONE         = 1 << 0;
        const ALT          = 1 << 1;
        const ALT_LEFT     = 1 << 2;
        const ALT_RIGHT    = 1 << 3;
        const SHIFT        = 1 << 4;
        const SHIFT_LEFT   = 1 << 5;
        const SHIFT_RIGHT  = 1 << 6;
        const SYM          = 1 << 7;
        const FUNCTION     = 1 << 8;
        const CTRL         = 1 << 9;
        const CTRL_LEFT    = 1 << 10;
        const CTRL_RIGHT   = 1 << 11;
        const META         = 1 << 12;
        const META_LEFT    = 1 << 13;
        const META_RIGHT   = 1 << 14;
        const CAPS_LOCK    = 1 << 15;
        const NUM_LOCK     = 1 << 16;
        const SCROLL_LOCK  = 1 << 17;
    }
}

impl From<InputEventModifiers> for bindings::MirInputEventModifiers {
    fn from(value: InputEventModifiers) -> Self {
        value.bits()
    }
}

impl From<bindings::MirInputEventModifiers> for InputEventModifiers {
    fn from(value: bindings::MirInputEventModifiers) -> Self {
        InputEventModifiers::from_bits_truncate(value)
    }
}

/// Keyboard action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum KeyboardAction {
    /// A key has been released.
    #[default]
    Up = 0,
    /// A key has been pressed.
    Down = 1,
    /// System policy triggered a key repeat on an already-down key.
    Repeat = 2,
    /// Modifiers updated without a key event; keysym, scan_code and text are not set.
    Modifiers = 3,
}

impl From<KeyboardAction> for bindings::MirKeyboardAction {
    fn from(value: KeyboardAction) -> Self {
        value as bindings::MirKeyboardAction
    }
}

impl TryFrom<bindings::MirKeyboardAction> for KeyboardAction {
    type Error = ();

    fn try_from(value: bindings::MirKeyboardAction) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Up),
            1 => Ok(Self::Down),
            2 => Ok(Self::Repeat),
            3 => Ok(Self::Modifiers),
            _ => Err(()),
        }
    }
}

/// Per-touch action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum TouchAction {
    /// This touch point is going up.
    #[default]
    Up = 0,
    /// This touch point is going down.
    Down = 1,
    /// Axis values have changed on this touch point.
    Change = 2,
}

impl From<TouchAction> for bindings::MirTouchAction {
    fn from(value: TouchAction) -> Self {
        value as bindings::MirTouchAction
    }
}

impl TryFrom<bindings::MirTouchAction> for TouchAction {
    type Error = ();

    fn try_from(value: bindings::MirTouchAction) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Up),
            1 => Ok(Self::Down),
            2 => Ok(Self::Change),
            _ => Err(()),
        }
    }
}

/// Touch axis identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum TouchAxis {
    /// X coordinate of the touch.
    #[default]
    X = 0,
    /// Y coordinate of the touch.
    Y = 1,
    /// Pressure of the touch.
    Pressure = 2,
    /// Length of the major axis of the touch ellipse.
    TouchMajor = 3,
    /// Length of the minor axis of the touch ellipse.
    TouchMinor = 4,
    /// Diameter of a circle centered on the touch point.
    Size = 5,
}

impl From<TouchAxis> for bindings::MirTouchAxis {
    fn from(value: TouchAxis) -> Self {
        value as bindings::MirTouchAxis
    }
}

impl TryFrom<bindings::MirTouchAxis> for TouchAxis {
    type Error = ();

    fn try_from(value: bindings::MirTouchAxis) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::X),
            1 => Ok(Self::Y),
            2 => Ok(Self::Pressure),
            3 => Ok(Self::TouchMajor),
            4 => Ok(Self::TouchMinor),
            5 => Ok(Self::Size),
            _ => Err(()),
        }
    }
}

/// Per-touch tool type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum TouchTooltype {
    /// Tool type could not be determined.
    #[default]
    Unknown = 0,
    /// Touch made with a finger.
    Finger = 1,
    /// Touch made with a stylus.
    Stylus = 2,
}

impl From<TouchTooltype> for bindings::MirTouchTooltype {
    fn from(value: TouchTooltype) -> Self {
        value as bindings::MirTouchTooltype
    }
}

impl TryFrom<bindings::MirTouchTooltype> for TouchTooltype {
    type Error = ();

    fn try_from(value: bindings::MirTouchTooltype) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Unknown),
            1 => Ok(Self::Finger),
            2 => Ok(Self::Stylus),
            _ => Err(()),
        }
    }
}

/// Pointer action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum PointerAction {
    /// A pointer button has been released.
    #[default]
    ButtonUp = 0,
    /// A pointer button has been pressed.
    ButtonDown = 1,
    /// The pointer entered the surface.
    Enter = 2,
    /// The pointer left the surface.
    Leave = 3,
    /// Axis values have changed.
    Motion = 4,
}

impl From<PointerAction> for bindings::MirPointerAction {
    fn from(value: PointerAction) -> Self {
        value as bindings::MirPointerAction
    }
}

impl TryFrom<bindings::MirPointerAction> for PointerAction {
    type Error = ();

    fn try_from(value: bindings::MirPointerAction) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::ButtonUp),
            1 => Ok(Self::ButtonDown),
            2 => Ok(Self::Enter),
            3 => Ok(Self::Leave),
            4 => Ok(Self::Motion),
            _ => Err(()),
        }
    }
}

/// Pointer axis identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum PointerAxis {
    /// Absolute X coordinate of the pointer.
    #[default]
    X = 0,
    /// Absolute Y coordinate of the pointer.
    Y = 1,
    /// Vertical scroll wheel ticks.
    VScroll = 2,
    /// Horizontal scroll wheel ticks.
    HScroll = 3,
    /// Last reported X differential from the pointer.
    RelativeX = 4,
    /// Last reported Y differential from the pointer.
    RelativeY = 5,
    /// Physical vertical scroll wheel clicks.
    VScrollDiscrete = 6,
    /// Physical horizontal scroll wheel clicks.
    HScrollDiscrete = 7,
    /// Fractional values of 120 for high-res vertical scrolling.
    VScrollValue120 = 8,
    /// Fractional values of 120 for high-res horizontal scrolling.
    HScrollValue120 = 9,
}

impl From<PointerAxis> for bindings::MirPointerAxis {
    fn from(value: PointerAxis) -> Self {
        value as bindings::MirPointerAxis
    }
}

impl TryFrom<bindings::MirPointerAxis> for PointerAxis {
    type Error = ();

    fn try_from(value: bindings::MirPointerAxis) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::X),
            1 => Ok(Self::Y),
            2 => Ok(Self::VScroll),
            3 => Ok(Self::HScroll),
            4 => Ok(Self::RelativeX),
            5 => Ok(Self::RelativeY),
            6 => Ok(Self::VScrollDiscrete),
            7 => Ok(Self::HScrollDiscrete),
            8 => Ok(Self::VScrollValue120),
            9 => Ok(Self::HScrollValue120),
            _ => Err(()),
        }
    }
}

bitflags! {
    /// Pointer button state flags.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct PointerButtons: u32 {
        const PRIMARY   = 1 << 0;
        const SECONDARY = 1 << 1;
        const TERTIARY  = 1 << 2;
        const BACK      = 1 << 3;
        const FORWARD   = 1 << 4;
        const SIDE      = 1 << 5;
        const EXTRA     = 1 << 6;
        const TASK      = 1 << 7;
    }
}

impl From<PointerButtons> for bindings::MirPointerButtons {
    fn from(value: PointerButtons) -> Self {
        value.bits()
    }
}

impl From<bindings::MirPointerButtons> for PointerButtons {
    fn from(value: bindings::MirPointerButtons) -> Self {
        PointerButtons::from_bits_truncate(value)
    }
}

/// Source of a pointer axis event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u32)]
pub enum PointerAxisSource {
    #[default]
    None = 0,
    Wheel = 1,
    Finger = 2,
    Continuous = 3,
    WheelTilt = 4,
}

impl From<PointerAxisSource> for bindings::MirPointerAxisSource {
    fn from(value: PointerAxisSource) -> Self {
        value as bindings::MirPointerAxisSource
    }
}

impl TryFrom<bindings::MirPointerAxisSource> for PointerAxisSource {
    type Error = ();

    fn try_from(value: bindings::MirPointerAxisSource) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::None),
            1 => Ok(Self::Wheel),
            2 => Ok(Self::Finger),
            3 => Ok(Self::Continuous),
            4 => Ok(Self::WheelTilt),
            _ => Err(()),
        }
    }
}

/// A keyboard event.
pub struct KeyboardEvent {
    /// The keyboard action.
    pub action: KeyboardAction,

    /// The xkeysym.
    ///
    /// You may use the xkeysym crate to match against this.
    pub keysym: u32,

    /// The raw scan code.
    ///
    /// Prefer using the keysym.
    pub scan_code: i32,

    /// The modifiers held during this event.
    pub modifiers: InputEventModifiers,
}

/// A pointer event.
pub struct PointerEvent {
    /// The x position of the pointer.
    pub x: f32,

    /// The y position of the pointer.
    pub y: f32,

    /// The pointer action.
    pub action: PointerAction,

    /// The modifiers held during the event.
    pub modifiers: InputEventModifiers,

    /// The buttons held during the event.
    pub buttons: PointerButtons,
}