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
use crate::key_map::KeyMap;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::HashSet;
#[allow(clippy::cognitive_complexity)]
pub fn generate(key_maps: HashSet<KeyMap>) -> TokenStream {
let (usbs, usb_pages, evdevs, xkbs, wins, macs, codes, code_matches, ids) =
key_maps.iter().fold(
(
vec![],
vec![],
vec![],
vec![],
vec![],
vec![],
vec![],
vec![],
vec![],
),
|(
mut usbs,
mut usb_pages,
mut evdevs,
mut xkbs,
mut wins,
mut macs,
mut codes,
mut code_matches,
mut ids,
),
key_map| {
ids.push(format_ident!("{}", key_map.variant));
if let Some(code) = &key_map.dom_code {
let code_ident = format_ident!("{}", code);
codes.push(code_ident.clone());
code_matches.push(quote! {
Some(KeyMappingCode::#code_ident)
});
} else {
code_matches.push(quote! {
None
});
}
usbs.push(key_map.usb_code);
usb_pages.push(key_map.usb_page_code);
evdevs.push(key_map.evdev_code);
xkbs.push(key_map.xkb_code);
wins.push(key_map.win_code);
macs.push(key_map.mac_code);
(
usbs,
usb_pages,
evdevs,
xkbs,
wins,
macs,
codes,
code_matches,
ids,
)
},
);
quote! {
use bitflags::bitflags;
use core::convert::TryFrom;
bitflags! {
/// Bitmask for key modifiers based on the USB HID standard
///
/// See the stardard here:
///
/// <https://www.usb.org/sites/default/files/documents/hid1_11.pdf>
///
/// Go to page 56, "8.3 Report Format for Array Items"
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyModifiers: u8 {
/// Control left key bitmask
const ControlLeft = 0b0000_0001;
/// Shift left key bitmask
const ShiftLeft = 0b0000_0010;
/// Alt left key bitmask
const AltLeft = 0b0000_0100;
/// Meta left key bitmask
const MetaLeft = 0b0000_1000;
/// Control right key bitmask
const ControlRight = 0b0001_0000;
/// Shift right key bitmask
const ShiftRight = 0b0010_0000;
/// Alt right key bitmask
const AltRight = 0b0100_0000; // 👎
/// Meta right key bitmask
const MetaRight = 0b1000_0000;
}
}
/// The mapping of values between platforms for a specific key
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyMapping {
/// USB HID value for a specific key
Usb(u16),
/// Linux kernel evdev value for a specific key
Evdev(u16),
/// X11 value for a specific key
Xkb(u16),
/// Windows value for a specific key
Win(u16),
/// Mac value for a specific key
Mac(u16),
/// W3 browser event code for a specific key
Code(Option<KeyMappingCode>),
/// Id for a specific key
Id(KeyMappingId),
}
/// Ergonomic access to a specific key's mapping of values
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyMap {
/// USB HID value for a specific key
pub usb: u16,
/// Linux kernel evdev value for a specific key
pub evdev: u16,
/// X11 value for a specific key
pub xkb: u16,
/// Windows value for a specific key
pub win: u16,
/// Mac value for a specific key
pub mac: u16,
/// W3 browser event code for a specific key
pub code: Option<KeyMappingCode>,
/// Id for a specific key
pub id: KeyMappingId,
/// USB HID bitmask
pub modifier: Option<KeyModifiers>,
}
impl KeyMap {
/// If you don't want to use TryFrom, until it is stabilized
pub fn from_key_mapping(key_mapping: KeyMapping) -> Result<KeyMap, ()> {
get_key_map(key_mapping)
}
/// Get KeyMap from a USB keycode
pub fn from_usb_code(page: u16, code: u16) -> Result<KeyMap, ()> {
get_usb_code(page, code)
}
}
impl TryFrom<KeyMapping> for KeyMap {
type Error = ();
fn try_from(key_mapping: KeyMapping) -> Result<KeyMap, Self::Error> {
get_key_map(key_mapping)
}
}
fn get_usb_code(page: u16, code: u16) -> Result<KeyMap, ()> {
match (page, code) {
#(
(#usb_pages, #usbs) => {
let id = KeyMappingId::#ids;
let keymap = KeyMap {
usb: #usbs,
evdev: #evdevs,
xkb: #xkbs,
win: #wins,
mac: #macs,
code: #code_matches,
modifier: match id {
KeyMappingId::ControlLeft => Some(KeyModifiers::ControlLeft),
KeyMappingId::ShiftLeft => Some(KeyModifiers::ShiftLeft),
KeyMappingId::AltLeft => Some(KeyModifiers::AltLeft),
KeyMappingId::MetaLeft => Some(KeyModifiers::MetaLeft),
KeyMappingId::ControlRight => Some(KeyModifiers::ControlRight),
KeyMappingId::ShiftRight => Some(KeyModifiers::ShiftRight),
KeyMappingId::AltRight => Some(KeyModifiers::AltRight),
KeyMappingId::MetaRight => Some(KeyModifiers::MetaRight),
_ => None,
},
id,
};
Ok(keymap)
}
)*,
_ => Err(())
}
}
fn get_key_map(key_mapping: KeyMapping) -> Result<KeyMap, ()> {
#[allow(unreachable_patterns)]
match key_mapping {
#(
KeyMapping::Usb(#usbs) | KeyMapping::Evdev(#evdevs) | KeyMapping::Xkb(#xkbs) | KeyMapping::Win(#wins) | KeyMapping::Mac(#macs) | KeyMapping::Id(KeyMappingId::#ids) => {
let id = KeyMappingId::#ids;
let keymap = KeyMap {
usb: #usbs,
evdev: #evdevs,
xkb: #xkbs,
win: #wins,
mac: #macs,
code: #code_matches,
modifier: match id {
KeyMappingId::ControlLeft => Some(KeyModifiers::ControlLeft),
KeyMappingId::ShiftLeft => Some(KeyModifiers::ShiftLeft),
KeyMappingId::AltLeft => Some(KeyModifiers::AltLeft),
KeyMappingId::MetaLeft => Some(KeyModifiers::MetaLeft),
KeyMappingId::ControlRight => Some(KeyModifiers::ControlRight),
KeyMappingId::ShiftRight => Some(KeyModifiers::ShiftRight),
KeyMappingId::AltRight => Some(KeyModifiers::AltRight),
KeyMappingId::MetaRight => Some(KeyModifiers::MetaRight),
_ => None,
},
id,
};
Ok(keymap)
},
)*
#(
KeyMapping::Code(#code_matches) => {
let id = KeyMappingId::#ids;
let keymap = KeyMap {
usb: #usbs,
evdev: #evdevs,
xkb: #xkbs,
win: #wins,
mac: #macs,
code: #code_matches,
modifier: match id {
KeyMappingId::ControlLeft => Some(KeyModifiers::ControlLeft),
KeyMappingId::ShiftLeft => Some(KeyModifiers::ShiftLeft),
KeyMappingId::AltLeft => Some(KeyModifiers::AltLeft),
KeyMappingId::MetaLeft => Some(KeyModifiers::MetaLeft),
KeyMappingId::ControlRight => Some(KeyModifiers::ControlRight),
KeyMappingId::ShiftRight => Some(KeyModifiers::ShiftRight),
KeyMappingId::AltRight => Some(KeyModifiers::AltRight),
KeyMappingId::MetaRight => Some(KeyModifiers::MetaRight),
_ => None,
},
id,
};
Ok(keymap)
},
)*
_ => Err(())
}
}
/// W3 browser event code for a specific key
///
/// <https://www.w3.org/TR/uievents-code/>
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyMappingCode {
#(
#[doc = "W3 browser event code for a specific key"]
#codes,
)*
}
impl core::fmt::Display for KeyMappingCode {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match *self {
#(
KeyMappingCode::#codes => write!(f, stringify!(#codes)),
)*
}
}
}
impl FromStr for KeyMappingCode {
type Err = ();
fn from_str(code: &str) -> Result<KeyMappingCode, Self::Err> {
match code {
#(
stringify!(#codes) => Ok(KeyMappingCode::#codes),
)*
_ => {Err(())},
}
}
}
impl From<KeyMappingCode> for KeyMap {
fn from(code: KeyMappingCode) -> KeyMap {
get_key_map(KeyMapping::Code(Some(code))).unwrap()
}
}
/// Id for a specific key
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyMappingId {
#(
#[doc = "Id for a specific key"]
#[allow(non_camel_case_types)]
#ids,
)*
}
impl core::fmt::Display for KeyMappingId {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match *self {
#(
KeyMappingId::#ids => write!(f, stringify!(#ids)),
)*
}
}
}
impl From<KeyMappingId> for KeyMap {
fn from(id: KeyMappingId) -> KeyMap {
get_key_map(KeyMapping::Id(id)).unwrap()
}
}
}
}