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
use core::{char, mem, slice};
use core::ops::{Deref, DerefMut};
pub const EVENT_NONE: i64 = 0;
pub const EVENT_MOUSE: i64 = 1;
pub const EVENT_KEY: i64 = 2;
pub const EVENT_QUIT: i64 = 3;
pub const EVENT_FOCUS: i64 = 4;
#[derive(Copy, Clone, Debug)]
pub enum EventOption {
Mouse(MouseEvent),
Key(KeyEvent),
Quit(QuitEvent),
Focus(FocusEvent),
Unknown(Event),
None,
}
#[derive(Copy, Clone, Debug)]
#[repr(packed)]
pub struct Event {
pub code: i64,
pub a: i64,
pub b: i64,
pub c: i64,
}
impl Event {
pub fn new() -> Event {
Event {
code: 0,
a: 0,
b: 0,
c: 0,
}
}
pub fn to_option(self) -> EventOption {
match self.code {
EVENT_NONE => EventOption::None,
EVENT_MOUSE => EventOption::Mouse(MouseEvent::from_event(self)),
EVENT_KEY => EventOption::Key(KeyEvent::from_event(self)),
EVENT_QUIT => EventOption::Quit(QuitEvent::from_event(self)),
EVENT_FOCUS => EventOption::Focus(FocusEvent::from_event(self)),
_ => EventOption::Unknown(self),
}
}
}
impl Deref for Event {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>()) as &[u8]
}
}
}
impl DerefMut for Event {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>()) as &mut [u8]
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct MouseEvent {
pub x: i32,
pub y: i32,
pub left_button: bool,
pub middle_button: bool,
pub right_button: bool,
}
impl MouseEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_MOUSE,
a: self.x as i64,
b: self.y as i64,
c: self.left_button as i64 | (self.middle_button as i64) << 1 |
(self.right_button as i64) << 2,
}
}
pub fn from_event(event: Event) -> MouseEvent {
MouseEvent {
x: event.a as i32,
y: event.b as i32,
left_button: event.c & 1 == 1,
middle_button: event.c & 2 == 2,
right_button: event.c & 4 == 4,
}
}
}
pub const K_A: u8 = 0x1E;
pub const K_B: u8 = 0x30;
pub const K_C: u8 = 0x2E;
pub const K_D: u8 = 0x20;
pub const K_E: u8 = 0x12;
pub const K_F: u8 = 0x21;
pub const K_G: u8 = 0x22;
pub const K_H: u8 = 0x23;
pub const K_I: u8 = 0x17;
pub const K_J: u8 = 0x24;
pub const K_K: u8 = 0x25;
pub const K_L: u8 = 0x26;
pub const K_M: u8 = 0x32;
pub const K_N: u8 = 0x31;
pub const K_O: u8 = 0x18;
pub const K_P: u8 = 0x19;
pub const K_Q: u8 = 0x10;
pub const K_R: u8 = 0x13;
pub const K_S: u8 = 0x1F;
pub const K_T: u8 = 0x14;
pub const K_U: u8 = 0x16;
pub const K_V: u8 = 0x2F;
pub const K_W: u8 = 0x11;
pub const K_X: u8 = 0x2D;
pub const K_Y: u8 = 0x15;
pub const K_Z: u8 = 0x2C;
pub const K_0: u8 = 0x0B;
pub const K_1: u8 = 0x02;
pub const K_2: u8 = 0x03;
pub const K_3: u8 = 0x04;
pub const K_4: u8 = 0x05;
pub const K_5: u8 = 0x06;
pub const K_6: u8 = 0x07;
pub const K_7: u8 = 0x08;
pub const K_8: u8 = 0x09;
pub const K_9: u8 = 0x0A;
pub const K_TICK: u8 = 0x29;
pub const K_MINUS: u8 = 0x0C;
pub const K_EQUALS: u8 = 0x0D;
pub const K_BACKSLASH: u8 = 0x2B;
pub const K_BRACE_OPEN: u8 = 0x1A;
pub const K_BRACE_CLOSE: u8 = 0x1B;
pub const K_SEMICOLON: u8 = 0x27;
pub const K_QUOTE: u8 = 0x28;
pub const K_COMMA: u8 = 0x33;
pub const K_PERIOD: u8 = 0x34;
pub const K_SLASH: u8 = 0x35;
pub const K_BKSP: u8 = 0x0E;
pub const K_SPACE: u8 = 0x39;
pub const K_TAB: u8 = 0x0F;
pub const K_CAPS: u8 = 0x3A;
pub const K_LEFT_SHIFT: u8 = 0x2A;
pub const K_RIGHT_SHIFT: u8 = 0x36;
pub const K_CTRL: u8 = 0x1D;
pub const K_ALT: u8 = 0x38;
pub const K_ENTER: u8 = 0x1C;
pub const K_ESC: u8 = 0x01;
pub const K_F1: u8 = 0x3B;
pub const K_F2: u8 = 0x3C;
pub const K_F3: u8 = 0x3D;
pub const K_F4: u8 = 0x3E;
pub const K_F5: u8 = 0x3F;
pub const K_F6: u8 = 0x40;
pub const K_F7: u8 = 0x41;
pub const K_F8: u8 = 0x42;
pub const K_F9: u8 = 0x43;
pub const K_F10: u8 = 0x44;
pub const K_HOME: u8 = 0x47;
pub const K_UP: u8 = 0x48;
pub const K_PGUP: u8 = 0x49;
pub const K_LEFT: u8 = 0x4B;
pub const K_RIGHT: u8 = 0x4D;
pub const K_END: u8 = 0x4F;
pub const K_DOWN: u8 = 0x50;
pub const K_PGDN: u8 = 0x51;
pub const K_DEL: u8 = 0x53;
pub const K_F11: u8 = 0x57;
pub const K_F12: u8 = 0x58;
#[derive(Copy, Clone, Debug)]
pub struct KeyEvent {
pub character: char,
pub scancode: u8,
pub pressed: bool,
}
impl KeyEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_KEY,
a: self.character as i64,
b: self.scancode as i64,
c: self.pressed as i64,
}
}
pub fn from_event(event: Event) -> KeyEvent {
KeyEvent {
character: char::from_u32(event.a as u32).unwrap_or('\0'),
scancode: event.b as u8,
pressed: event.c > 0,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct QuitEvent;
impl QuitEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_QUIT,
a: 0,
b: 0,
c: 0,
}
}
pub fn from_event(_: Event) -> QuitEvent {
QuitEvent
}
}
#[derive(Copy, Clone, Debug)]
pub struct FocusEvent {
pub focused: bool
}
impl FocusEvent {
pub fn to_event(&self) -> Event {
Event {
code: EVENT_FOCUS,
a: self.focused as i64,
b: 0,
c: 0,
}
}
pub fn from_event(event: Event) -> FocusEvent {
FocusEvent {
focused: event.a > 0
}
}
}