Skip to main content

xmirror_event/
event.rs

1#[repr(C)]
2#[derive(Debug, Clone, Copy)]
3pub enum Event {
4    KeyDown(Key),
5    KeyUp(Key),
6    Mods(Mods),
7
8    MouseDown(Mouse),
9    MouseUp(Mouse),
10    MouseMove { dx: i8, dy: i8 },
11    Scroll { dx: i8, dy: i8 },
12}
13
14#[rustfmt::skip]
15#[repr(u8)]
16#[derive(Debug, Clone, Copy)]
17pub enum Key {
18    A, B, C, D, E, F, G, H, I, J, K, L, M,
19    N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
20
21    Num1, Num2, Num3, Num4, Num5,
22    Num6, Num7, Num8, Num9, Num0,
23
24    Left,       Up,         Right,      Down,      Enter,
25    Esc,        Backspace,  Tab,        Space,     Minus,
26    Equal,      LeftBrace,  RightBrace, Backslash, SemiColon,
27    Apostrophe, Grave,      Comma,      Dot,       Slash,
28    CapsLock,   ScrollLock, Pause,      Print,     Insert,
29    Delete,     Home,       End,        PageUp,    PageDown,
30
31    F1, F2, F3, F4,  F5,  F6,
32    F7, F8, F9, F10, F11, F12,
33}
34
35bitflags::bitflags! {
36    #[rustfmt::skip]
37    #[repr(C)]
38    #[derive(Debug, Clone, Copy)]
39    pub struct Mods: u8 {
40        const Shift = 1 << 0;
41        const Ctrl = 1 << 1;
42        const Alt = 1 << 2;
43        const Super = 1 << 3;
44    }
45}
46
47#[repr(u8)]
48#[derive(Debug, Clone, Copy)]
49pub enum Mouse {
50    Left,
51    Right,
52    Middle,
53}
54
55#[repr(u8)]
56#[derive(Debug, Clone, Copy)]
57pub enum Scroll {
58    Up,
59    Down,
60    Left,
61    Right,
62}
63
64macro_rules! impl_conversions {
65    ( $to_fn:ident, $from_fn:ident, $( $variant:ident => $id:expr ),+ $(,)? ) => {
66        pub fn $to_fn(self) -> u32 {
67            match self {
68                $( Self::$variant => $id, )+
69            }
70        }
71        pub fn $from_fn(id: u32) -> Option<Self> {
72            match id {
73                $( $id => Some(Self::$variant), )+
74                _ => None,
75            }
76        }
77    };
78}
79macro_rules! impl_platform {
80    (
81        $to_fn:ident, $from_fn:ident,
82        Key { $( $key:ident => $vk:expr ),+ $(,)? }
83        Mods { $( $mods:ident => $vmod:expr ),+ $(,)? }
84        Mouse { $( $mouse:ident => $vm:expr ),+ $(,)? }
85        Scroll { $( $scroll:ident => $vs:expr ),+ $(,)? }
86    ) => {
87        impl Key { impl_conversions!{ $to_fn, $from_fn, $( $key => $vk ),* } }
88        impl Mouse { impl_conversions!{ $to_fn, $from_fn, $( $mouse => $vm ),* } }
89        impl Scroll { impl_conversions!{ $to_fn, $from_fn, $( $scroll => $vs ),* } }
90        impl Mods {
91            pub fn $to_fn(self) -> u32 {
92                $( (if self.contains(Self::$mods) { $vmod } else { 0 }) )|*
93            }
94            pub fn $from_fn(id: u32) -> Self {
95                $( (if id & $vmod > 0 { Self::$mods } else { Self::empty() }) )|*
96            }
97        }
98    }
99}
100
101impl_platform! {
102    to_x11, from_x11,
103    Key {
104        A => 38,  B => 56,  C => 54,  D => 40,  E => 26,  F => 41,  G => 42,  H => 43,  I => 31,  J => 44,  K => 45,  L => 46,  M => 58,
105        N => 57,  O => 32,  P => 33,  Q => 24,  R => 27,  S => 39,  T => 28,  U => 30,  V => 55,  W => 25,  X => 53,  Y => 29,  Z => 52,
106
107        Num1 => 10,  Num2 => 11,  Num3 => 12,  Num4 => 13,  Num5 => 14,
108        Num6 => 15,  Num7 => 16,  Num8 => 17,  Num9 => 18,  Num0 => 19,
109
110        Left       => 113,  Up         => 111,  Right      => 114,  Down      => 116,  Enter      =>  36,
111        Esc        =>   9,  Backspace  =>  22,  Tab        =>  23,  Space     =>  65,  Minus      =>  20,
112        Equal      =>  21,  LeftBrace  =>  34,  RightBrace =>  35,  Backslash =>  51,  SemiColon  =>  47,
113        Apostrophe =>  48,  Grave      =>  49,  Comma      =>  59,  Dot       =>  60,  Slash      =>  61,
114        CapsLock   =>  66,  ScrollLock =>  78,  Pause      => 127,  Print     => 107,  Insert     => 118,
115        Delete     => 119,  Home       => 110,  End        => 115,  PageUp    => 112,  PageDown   => 117,
116
117        F1 => 67,  F2 => 68,  F3 => 69,  F4 =>  70,  F5 =>  71,  F6 =>  72,
118        F7 => 73,  F8 => 74,  F9 => 75,  F10 => 76,  F11 => 95,  F12 => 96,
119    }
120    Mods {
121        Shift => 1,  Ctrl => 4,  Alt => 8,  Super => 64,
122    }
123    Mouse {
124        Left => 1,  Middle => 2,  Right => 3,
125    }
126    Scroll {
127        Left => 6,  Down => 4,  Up => 5,  Right => 7,
128    }
129}
130
131impl_platform! {
132    to_macos, from_macos,
133
134    Key {
135        A =>  0,  B => 11,  C =>  8,  D =>  2,  E => 14,  F =>  3,  G =>  5,  H =>  4,  I => 34,  J => 38,  K => 40,  L => 37,  M => 46,
136        N => 45,  O => 31,  P => 35,  Q => 12,  R => 15,  S =>  1,  T => 17,  U => 32,  V =>  9,  W => 13,  X =>  7,  Y => 16,  Z =>  6,
137
138        Num1 => 18,  Num2 => 19,  Num3 => 20,  Num4 => 21,  Num5 => 23,
139        Num6 => 22,  Num7 => 26,  Num8 => 28,  Num9 => 25,  Num0 => 29,
140
141        Left       => 123,  Up         => 126,  Right      => 124,  Down      => 125,  Enter      =>  36,
142        Esc        =>  53,  Backspace  =>  51,  Tab        =>  48,  Space     =>  49,  Minus      =>  27,
143        Equal      =>  24,  LeftBrace  =>  33,  RightBrace =>  30,  Backslash =>  42,  SemiColon  =>  41,
144        Apostrophe =>  39,  Grave      =>  50,  Comma      =>  43,  Dot       =>  47,  Slash      =>  44,
145        CapsLock   =>  57,  ScrollLock => 107,  Pause      => 113,  Print     => 105,  Insert     => 114,
146        Delete     => 117,  Home       => 115,  End        => 119,  PageUp    => 116,  PageDown   => 121,
147
148        F1 => 122,  F2 => 120,  F3 =>  99,  F4 => 118,  F5 =>  96,  F6 =>  97,
149        F7 =>  98,  F8 => 100,  F9 => 101,  F10 => 109,  F11 => 103,  F12 => 111,
150    }
151    Mods {
152        Shift => 0x2_0000,  Ctrl => 0x4_0000,  Alt => 0x8_0000,  Super => 0x10_0000,
153    }
154    Mouse {
155        Left => 1,  Middle => 2,  Right => 3,
156    }
157    Scroll {
158        Left => 1,  Down => 2,  Up => 3,  Right => 4,
159    }
160}