Skip to content

Commit 9b5f8c6

Browse files
committed
Insert key code/name mapping entries without accidental overwriting
1 parent 3670519 commit 9b5f8c6

2 files changed

Lines changed: 62 additions & 53 deletions

File tree

src/buttons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn handle_button_presses(
1919
buttons_to_key_code_names: HashMap<Button, KeyName>,
2020
event_sender: EventSender,
2121
) -> Result<()> {
22-
let key_code_name_mapping = KeyCodeNameMapping::new();
22+
let key_code_name_mapping = KeyCodeNameMapping::new()?;
2323

2424
let key_codes_to_buttons =
2525
KeyCodeToButtonMapping::new(key_code_name_mapping, buttons_to_key_code_names)?;

src/keycodenames.rs

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* License: MIT
44
*/
55

6+
use anyhow::{Result, anyhow};
67
use bimap::BiMap;
78
use evdev::KeyCode;
89

@@ -13,75 +14,83 @@ pub(crate) struct KeyCodeNameMapping {
1314
}
1415

1516
impl KeyCodeNameMapping {
16-
pub(crate) fn new() -> Self {
17+
pub(crate) fn new() -> Result<Self> {
1718
let mut names_to_codes: BiMap<KeyName, KeyCode> = BiMap::new();
1819

19-
let mut insert = |name: &str, code: KeyCode| {
20-
names_to_codes.insert(name.to_owned(), code);
20+
let mut insert = |name: &str, code: KeyCode| -> Result<()> {
21+
names_to_codes
22+
.insert_no_overwrite(name.to_owned(), code)
23+
.map_err(|(name, code)| {
24+
anyhow!(
25+
"Inserting ({:?}, {:?}) without overwrite failed",
26+
name,
27+
code
28+
)
29+
})
2130
};
2231

2332
// See file `/usr/include/linux/input-event-codes.h` (or
2433
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
2534
// on the web) for details.
2635

2736
// mouse
28-
insert("left", KeyCode::BTN_LEFT);
29-
insert("right", KeyCode::BTN_RIGHT);
30-
insert("middle", KeyCode::BTN_MIDDLE);
31-
insert("side", KeyCode::BTN_SIDE);
32-
insert("extra", KeyCode::BTN_EXTRA);
33-
insert("forward", KeyCode::BTN_FORWARD);
34-
insert("back", KeyCode::BTN_BACK);
35-
insert("task", KeyCode::BTN_TASK);
37+
insert("left", KeyCode::BTN_LEFT)?;
38+
insert("right", KeyCode::BTN_RIGHT)?;
39+
insert("middle", KeyCode::BTN_MIDDLE)?;
40+
insert("side", KeyCode::BTN_SIDE)?;
41+
insert("extra", KeyCode::BTN_EXTRA)?;
42+
insert("forward", KeyCode::BTN_FORWARD)?;
43+
insert("back", KeyCode::BTN_BACK)?;
44+
insert("task", KeyCode::BTN_TASK)?;
3645

3746
// joystick
38-
insert("trigger", KeyCode::BTN_TRIGGER);
39-
insert("thumb", KeyCode::BTN_THUMB);
40-
insert("thumb2", KeyCode::BTN_THUMB2);
41-
insert("top", KeyCode::BTN_TOP);
42-
insert("top2", KeyCode::BTN_TOP2);
43-
insert("pinkie", KeyCode::BTN_PINKIE);
44-
insert("base", KeyCode::BTN_BASE);
45-
insert("base2", KeyCode::BTN_BASE2);
46-
insert("base3", KeyCode::BTN_BASE3);
47-
insert("base4", KeyCode::BTN_BASE4);
48-
insert("base5", KeyCode::BTN_BASE5);
49-
insert("base6", KeyCode::BTN_BASE6);
50-
insert("dead", KeyCode::BTN_DEAD);
47+
insert("trigger", KeyCode::BTN_TRIGGER)?;
48+
insert("thumb", KeyCode::BTN_THUMB)?;
49+
insert("thumb2", KeyCode::BTN_THUMB2)?;
50+
insert("top", KeyCode::BTN_TOP)?;
51+
insert("top2", KeyCode::BTN_TOP2)?;
52+
insert("pinkie", KeyCode::BTN_PINKIE)?;
53+
insert("base", KeyCode::BTN_BASE)?;
54+
insert("base2", KeyCode::BTN_BASE2)?;
55+
insert("base3", KeyCode::BTN_BASE3)?;
56+
insert("base4", KeyCode::BTN_BASE4)?;
57+
insert("base5", KeyCode::BTN_BASE5)?;
58+
insert("base6", KeyCode::BTN_BASE6)?;
59+
insert("dead", KeyCode::BTN_DEAD)?;
5160

5261
// gamepad
53-
insert("a", KeyCode::BTN_SOUTH);
54-
insert("b", KeyCode::BTN_EAST);
55-
insert("c", KeyCode::BTN_C);
56-
insert("x", KeyCode::BTN_NORTH);
57-
insert("y", KeyCode::BTN_WEST);
58-
insert("z", KeyCode::BTN_Z);
59-
insert("tl", KeyCode::BTN_TL);
60-
insert("tr", KeyCode::BTN_TR);
61-
insert("tl2", KeyCode::BTN_TL2);
62-
insert("tr2", KeyCode::BTN_TR2);
63-
insert("select", KeyCode::BTN_SELECT);
64-
insert("start", KeyCode::BTN_START);
65-
insert("mode", KeyCode::BTN_MODE);
66-
insert("thumbl", KeyCode::BTN_THUMBL);
67-
insert("thumbr", KeyCode::BTN_THUMBR);
62+
insert("a", KeyCode::BTN_SOUTH)?;
63+
insert("b", KeyCode::BTN_EAST)?;
64+
insert("c", KeyCode::BTN_C)?;
65+
insert("x", KeyCode::BTN_NORTH)?;
66+
insert("y", KeyCode::BTN_WEST)?;
67+
insert("z", KeyCode::BTN_Z)?;
68+
insert("tl", KeyCode::BTN_TL)?;
69+
insert("tr", KeyCode::BTN_TR)?;
70+
insert("tl2", KeyCode::BTN_TL2)?;
71+
insert("tr2", KeyCode::BTN_TR2)?;
72+
insert("select", KeyCode::BTN_SELECT)?;
73+
insert("start", KeyCode::BTN_START)?;
74+
insert("mode", KeyCode::BTN_MODE)?;
75+
insert("thumbl", KeyCode::BTN_THUMBL)?;
76+
insert("thumbr", KeyCode::BTN_THUMBR)?;
6877

6978
// directional pad
70-
insert("dpad_up", KeyCode::BTN_DPAD_UP);
71-
insert("dpad_down", KeyCode::BTN_DPAD_DOWN);
72-
insert("dpad_left", KeyCode::BTN_DPAD_LEFT);
73-
insert("dpad_right", KeyCode::BTN_DPAD_RIGHT);
79+
insert("dpad_up", KeyCode::BTN_DPAD_UP)?;
80+
insert("dpad_down", KeyCode::BTN_DPAD_DOWN)?;
81+
insert("dpad_left", KeyCode::BTN_DPAD_LEFT)?;
82+
insert("dpad_right", KeyCode::BTN_DPAD_RIGHT)?;
7483

75-
insert("trigger_happy1", KeyCode::BTN_TRIGGER_HAPPY1);
76-
insert("trigger_happy2", KeyCode::BTN_TRIGGER_HAPPY2);
77-
insert("trigger_happy3", KeyCode::BTN_TRIGGER_HAPPY3);
78-
insert("trigger_happy4", KeyCode::BTN_TRIGGER_HAPPY4);
79-
insert("trigger_happy5", KeyCode::BTN_TRIGGER_HAPPY5);
80-
insert("trigger_happy6", KeyCode::BTN_TRIGGER_HAPPY6);
81-
insert("trigger_happy7", KeyCode::BTN_TRIGGER_HAPPY7);
82-
insert("trigger_happy8", KeyCode::BTN_TRIGGER_HAPPY8);
84+
insert("trigger_happy1", KeyCode::BTN_TRIGGER_HAPPY1)?;
85+
insert("trigger_happy2", KeyCode::BTN_TRIGGER_HAPPY2)?;
86+
insert("trigger_happy3", KeyCode::BTN_TRIGGER_HAPPY3)?;
87+
insert("trigger_happy4", KeyCode::BTN_TRIGGER_HAPPY4)?;
88+
insert("trigger_happy5", KeyCode::BTN_TRIGGER_HAPPY5)?;
89+
insert("trigger_happy6", KeyCode::BTN_TRIGGER_HAPPY6)?;
90+
insert("trigger_happy7", KeyCode::BTN_TRIGGER_HAPPY7)?;
91+
insert("trigger_happy8", KeyCode::BTN_TRIGGER_HAPPY8)?;
8392

84-
Self { names_to_codes }
93+
Ok(Self { names_to_codes })
8594
}
8695

8796
pub(crate) fn find_code_for_name(&self, name: KeyName) -> Option<&KeyCode> {

0 commit comments

Comments
 (0)