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
use core::char;
#[derive(Copy, Clone)]
pub enum EventOption {
Mouse(MouseEvent),
Key(KeyEvent),
Unknown(Event),
None,
}
#[derive(Copy, Clone)]
#[repr(packed)]
pub struct Event {
pub code: char,
pub a: isize,
pub b: isize,
pub c: isize,
}
impl Event {
pub fn new() -> Event {
Event {
code: '\0',
a: 0,
b: 0,
c: 0,
}
}
pub fn to_option(self) -> EventOption {
match self.code {
'm' => EventOption::Mouse(MouseEvent::from_event(self)),
'k' => EventOption::Key(KeyEvent::from_event(self)),
'\0' => EventOption::None,
_ => EventOption::Unknown(self),
}
}
}
#[derive(Copy, Clone)]
pub struct MouseEvent {
pub x: isize,
pub y: isize,
pub left_button: bool,
pub right_button: bool,
pub middle_button: bool,
}
impl MouseEvent {
pub fn to_event(&self) -> Event {
Event {
code: 'm',
a: self.x,
b: self.y,
c: self.left_button as isize | (self.right_button as isize) << 1 | (self.right_button as isize) << 2,
}
}
pub fn from_event(event: Event) -> MouseEvent {
MouseEvent {
x: event.a,
y: event.b,
left_button: event.c & 1 == 1,
middle_button: event.c & 4 == 4,
right_button: event.c & 2 == 2,
}
}
}
pub const K_ESC: u8 = 0x01;
pub const K_BKSP: u8 = 0x0E;
pub const K_TAB: u8 = 0x0F;
pub const K_CTRL: u8 = 0x1D;
pub const K_ALT: u8 = 0x38;
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;
pub const K_LEFT_SHIFT: u8 = 0x2A;
pub const K_RIGHT_SHIFT: u8 = 0x36;
#[derive(Copy, Clone)]
pub struct KeyEvent {
pub character: char,
pub scancode: u8,
pub pressed: bool,
}
impl KeyEvent {
pub fn to_event(&self) -> Event {
Event {
code: 'k',
a: self.character as isize,
b: self.scancode as isize,
c: self.pressed as isize,
}
}
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,
}
}
}