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
//! Scan Code Set 2 support

use crate::{
    DecodeState, Error, KeyCode, KeyEvent, KeyState, ScancodeSet, EXTENDED2_KEY_CODE,
    EXTENDED_KEY_CODE, KEY_RELEASE_CODE,
};

/// Contains the implementation of Scancode Set 2.
///
/// See the OS dev wiki: <https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_2>
/// Additional reference: <https://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html>
pub struct ScancodeSet2 {
    state: DecodeState,
}

impl ScancodeSet2 {
    /// Construct a new [`ScancodeSet2`] decoder.
    pub const fn new() -> ScancodeSet2 {
        ScancodeSet2 {
            state: DecodeState::Start,
        }
    }

    /// Implements the single byte codes for Set 2.
    fn map_scancode(code: u8) -> Result<KeyCode, Error> {
        match code {
            0x00 => Ok(KeyCode::TooManyKeys),
            0x01 => Ok(KeyCode::F9),
            // 0x02
            0x03 => Ok(KeyCode::F5),
            0x04 => Ok(KeyCode::F3),
            0x05 => Ok(KeyCode::F1),
            0x06 => Ok(KeyCode::F2),
            0x07 => Ok(KeyCode::F12),
            0x09 => Ok(KeyCode::F10),
            0x0A => Ok(KeyCode::F8),
            0x0B => Ok(KeyCode::F6),
            0x0C => Ok(KeyCode::F4),
            0x0D => Ok(KeyCode::Tab),
            0x0E => Ok(KeyCode::Oem8),
            0x11 => Ok(KeyCode::LAlt),
            0x12 => Ok(KeyCode::LShift),
            0x13 => Ok(KeyCode::Oem11),
            0x14 => Ok(KeyCode::LControl),
            0x15 => Ok(KeyCode::Q),
            0x16 => Ok(KeyCode::Key1),
            0x1A => Ok(KeyCode::Z),
            0x1B => Ok(KeyCode::S),
            0x1C => Ok(KeyCode::A),
            0x1D => Ok(KeyCode::W),
            0x1E => Ok(KeyCode::Key2),
            0x21 => Ok(KeyCode::C),
            0x22 => Ok(KeyCode::X),
            0x23 => Ok(KeyCode::D),
            0x24 => Ok(KeyCode::E),
            0x25 => Ok(KeyCode::Key4),
            0x26 => Ok(KeyCode::Key3),
            0x29 => Ok(KeyCode::Spacebar),
            0x2A => Ok(KeyCode::V),
            0x2B => Ok(KeyCode::F),
            0x2C => Ok(KeyCode::T),
            0x2D => Ok(KeyCode::R),
            0x2E => Ok(KeyCode::Key5),
            0x31 => Ok(KeyCode::N),
            0x32 => Ok(KeyCode::B),
            0x33 => Ok(KeyCode::H),
            0x34 => Ok(KeyCode::G),
            0x35 => Ok(KeyCode::Y),
            0x36 => Ok(KeyCode::Key6),
            0x3A => Ok(KeyCode::M),
            0x3B => Ok(KeyCode::J),
            0x3C => Ok(KeyCode::U),
            0x3D => Ok(KeyCode::Key7),
            0x3E => Ok(KeyCode::Key8),
            0x41 => Ok(KeyCode::OemComma),
            0x42 => Ok(KeyCode::K),
            0x43 => Ok(KeyCode::I),
            0x44 => Ok(KeyCode::O),
            0x45 => Ok(KeyCode::Key0),
            0x46 => Ok(KeyCode::Key9),
            0x49 => Ok(KeyCode::OemPeriod),
            0x4A => Ok(KeyCode::Oem2),
            0x4B => Ok(KeyCode::L),
            0x4C => Ok(KeyCode::Oem1),
            0x4D => Ok(KeyCode::P),
            0x4E => Ok(KeyCode::OemMinus),
            0x51 => Ok(KeyCode::Oem12),
            0x52 => Ok(KeyCode::Oem3),
            0x54 => Ok(KeyCode::Oem4),
            0x55 => Ok(KeyCode::OemPlus),
            0x58 => Ok(KeyCode::CapsLock),
            0x59 => Ok(KeyCode::RShift),
            0x5A => Ok(KeyCode::Return),
            0x5B => Ok(KeyCode::Oem6),
            0x5D => Ok(KeyCode::Oem7),
            0x61 => Ok(KeyCode::Oem5),
            0x64 => Ok(KeyCode::Oem10),
            0x66 => Ok(KeyCode::Backspace),
            0x67 => Ok(KeyCode::Oem9),
            0x69 => Ok(KeyCode::Numpad1),
            0x6A => Ok(KeyCode::Oem13),
            0x6B => Ok(KeyCode::Numpad4),
            0x6C => Ok(KeyCode::Numpad7),
            0x70 => Ok(KeyCode::Numpad0),
            0x71 => Ok(KeyCode::NumpadPeriod),
            0x72 => Ok(KeyCode::Numpad2),
            0x73 => Ok(KeyCode::Numpad5),
            0x74 => Ok(KeyCode::Numpad6),
            0x75 => Ok(KeyCode::Numpad8),
            0x76 => Ok(KeyCode::Escape),
            0x77 => Ok(KeyCode::NumpadLock),
            0x78 => Ok(KeyCode::F11),
            0x79 => Ok(KeyCode::NumpadAdd),
            0x7A => Ok(KeyCode::Numpad3),
            0x7B => Ok(KeyCode::NumpadSubtract),
            0x7C => Ok(KeyCode::NumpadMultiply),
            0x7D => Ok(KeyCode::Numpad9),
            0x7E => Ok(KeyCode::ScrollLock),
            0x7F => Ok(KeyCode::SysRq),
            0x83 => Ok(KeyCode::F7),
            0xAA => Ok(KeyCode::PowerOnTestOk),
            _ => Err(Error::UnknownKeyCode),
        }
    }

    /// Implements the extended byte codes for set 2 (prefixed with E0)
    fn map_extended_scancode(code: u8) -> Result<KeyCode, Error> {
        match code {
            0x11 => Ok(KeyCode::RAltGr),
            0x12 => Ok(KeyCode::RAlt2),
            0x14 => Ok(KeyCode::RControl),
            0x15 => Ok(KeyCode::PrevTrack),
            0x1F => Ok(KeyCode::LWin),
            0x21 => Ok(KeyCode::VolumeDown),
            0x23 => Ok(KeyCode::Mute),
            0x27 => Ok(KeyCode::RWin),
            0x2B => Ok(KeyCode::Calculator),
            0x2F => Ok(KeyCode::Apps),
            0x32 => Ok(KeyCode::VolumeUp),
            0x34 => Ok(KeyCode::Play),
            0x3A => Ok(KeyCode::WWWHome),
            0x3B => Ok(KeyCode::Stop),
            0x4A => Ok(KeyCode::NumpadDivide),
            0x4D => Ok(KeyCode::NextTrack),
            0x5A => Ok(KeyCode::NumpadEnter),
            0x69 => Ok(KeyCode::End),
            0x6B => Ok(KeyCode::ArrowLeft),
            0x6C => Ok(KeyCode::Home),
            0x70 => Ok(KeyCode::Insert),
            0x71 => Ok(KeyCode::Delete),
            0x72 => Ok(KeyCode::ArrowDown),
            0x74 => Ok(KeyCode::ArrowRight),
            0x75 => Ok(KeyCode::ArrowUp),
            0x7A => Ok(KeyCode::PageDown),
            0x7C => Ok(KeyCode::PrintScreen),
            0x7D => Ok(KeyCode::PageUp),
            _ => Err(Error::UnknownKeyCode),
        }
    }

    /// Implements the alternate extended byte codes for set 2 (prefixed with E1)
    fn map_extended2_scancode(code: u8) -> Result<KeyCode, Error> {
        match code {
            0x14 => Ok(KeyCode::RControl2),
            _ => Err(Error::UnknownKeyCode),
        }
    }
}

impl ScancodeSet for ScancodeSet2 {
    /// Implements state logic for scancode set 2
    ///
    /// ## Start:
    /// * F0 => Goto Release
    /// * E0 => Goto Extended
    /// * E1 => Goto Extended2
    /// * xx => Key Down Event
    ///
    /// ## Release:
    /// * xxx => Key Up Event
    ///
    /// ## Extended:
    /// * F0 => Goto Release-Extended
    /// * xx => Extended Key Down Event
    ///
    /// ## Release-Extended:
    /// * xxx => Extended Key Up Event
    ///
    /// ## Extended2:
    /// * F0 => Goto Release-Extended2
    /// * xx => Extended2 Key Down Event
    ///
    /// ## Release-Extended2:
    /// * xxx => Extended2 Key Up Event
    fn advance_state(&mut self, code: u8) -> Result<Option<KeyEvent>, Error> {
        match self.state {
            DecodeState::Start => match code {
                EXTENDED_KEY_CODE => {
                    self.state = DecodeState::Extended;
                    Ok(None)
                }
                EXTENDED2_KEY_CODE => {
                    self.state = DecodeState::Extended2;
                    Ok(None)
                }
                KEY_RELEASE_CODE => {
                    self.state = DecodeState::Release;
                    Ok(None)
                }
                _ => {
                    let keycode = Self::map_scancode(code)?;
                    if keycode == KeyCode::TooManyKeys || keycode == KeyCode::PowerOnTestOk {
                        Ok(Some(KeyEvent::new(keycode, KeyState::SingleShot)))
                    } else {
                        Ok(Some(KeyEvent::new(
                            Self::map_scancode(code)?,
                            KeyState::Down,
                        )))
                    }
                }
            },
            DecodeState::Release => {
                self.state = DecodeState::Start;
                Ok(Some(KeyEvent::new(Self::map_scancode(code)?, KeyState::Up)))
            }
            DecodeState::Extended => match code {
                KEY_RELEASE_CODE => {
                    self.state = DecodeState::ExtendedRelease;
                    Ok(None)
                }
                _ => {
                    self.state = DecodeState::Start;

                    let keycode = Self::map_extended_scancode(code)?;
                    Ok(Some(KeyEvent::new(keycode, KeyState::Down)))
                }
            },
            DecodeState::ExtendedRelease => {
                self.state = DecodeState::Start;
                Ok(Some(KeyEvent::new(
                    Self::map_extended_scancode(code)?,
                    KeyState::Up,
                )))
            }
            DecodeState::Extended2 => match code {
                KEY_RELEASE_CODE => {
                    self.state = DecodeState::Extended2Release;
                    Ok(None)
                }
                _ => {
                    self.state = DecodeState::Start;
                    Ok(Some(KeyEvent::new(
                        Self::map_extended2_scancode(code)?,
                        KeyState::Down,
                    )))
                }
            },
            DecodeState::Extended2Release => {
                self.state = DecodeState::Start;
                Ok(Some(KeyEvent::new(
                    Self::map_extended2_scancode(code)?,
                    KeyState::Up,
                )))
            }
        }
    }
}

impl Default for ScancodeSet2 {
    fn default() -> Self {
        ScancodeSet2::new()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn validate_scancodes() {
        let mut codes = Vec::new();
        let mut errs = Vec::new();
        for code in 0x00..=0xFF {
            let r = ScancodeSet2::map_scancode(code);
            match r {
                Ok(c) => codes.push(c),
                Err(_) => errs.push(code),
            }
        }
        codes.sort();
        println!("{:?}", codes);
        assert_eq!(codes.len(), 94);
        assert_eq!(errs.len(), 162);
    }
}