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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Scan Code Set 1 support

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

/// Contains the implementation of Scancode Set 1.
///
/// See the OS dev wiki: <https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1>
pub struct ScancodeSet1 {
    state: DecodeState,
}

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

    /// Implements the single byte codes for Set 1.
    fn map_scancode(code: u8) -> Result<KeyCode, Error> {
        match code {
            0x01 => Ok(KeyCode::Escape),
            0x02 => Ok(KeyCode::Key1),
            0x03 => Ok(KeyCode::Key2),
            0x04 => Ok(KeyCode::Key3),
            0x05 => Ok(KeyCode::Key4),
            0x06 => Ok(KeyCode::Key5),
            0x07 => Ok(KeyCode::Key6),
            0x08 => Ok(KeyCode::Key7),
            0x09 => Ok(KeyCode::Key8),
            0x0A => Ok(KeyCode::Key9),
            0x0B => Ok(KeyCode::Key0),
            0x0C => Ok(KeyCode::OemMinus),
            0x0D => Ok(KeyCode::OemPlus),
            0x0E => Ok(KeyCode::Backspace),
            0x0F => Ok(KeyCode::Tab),
            0x10 => Ok(KeyCode::Q),
            0x11 => Ok(KeyCode::W),
            0x12 => Ok(KeyCode::E),
            0x13 => Ok(KeyCode::R),
            0x14 => Ok(KeyCode::T),
            0x15 => Ok(KeyCode::Y),
            0x16 => Ok(KeyCode::U),
            0x17 => Ok(KeyCode::I),
            0x18 => Ok(KeyCode::O),
            0x19 => Ok(KeyCode::P),
            0x1A => Ok(KeyCode::Oem4),
            0x1B => Ok(KeyCode::Oem6),
            0x1C => Ok(KeyCode::Return),
            0x1D => Ok(KeyCode::LControl),
            0x1E => Ok(KeyCode::A),
            0x1F => Ok(KeyCode::S),
            0x20 => Ok(KeyCode::D),
            0x21 => Ok(KeyCode::F),
            0x22 => Ok(KeyCode::G),
            0x23 => Ok(KeyCode::H),
            0x24 => Ok(KeyCode::J),
            0x25 => Ok(KeyCode::K),
            0x26 => Ok(KeyCode::L),
            0x27 => Ok(KeyCode::Oem1),
            0x28 => Ok(KeyCode::Oem3),
            0x29 => Ok(KeyCode::Oem8),
            0x2A => Ok(KeyCode::LShift),
            0x2B => Ok(KeyCode::Oem7),
            0x2C => Ok(KeyCode::Z),
            0x2D => Ok(KeyCode::X),
            0x2E => Ok(KeyCode::C),
            0x2F => Ok(KeyCode::V),
            0x30 => Ok(KeyCode::B),
            0x31 => Ok(KeyCode::N),
            0x32 => Ok(KeyCode::M),
            0x33 => Ok(KeyCode::OemComma),
            0x34 => Ok(KeyCode::OemPeriod),
            0x35 => Ok(KeyCode::Oem2),
            0x36 => Ok(KeyCode::RShift),
            0x37 => Ok(KeyCode::NumpadMultiply),
            0x38 => Ok(KeyCode::LAlt),
            0x39 => Ok(KeyCode::Spacebar),
            0x3A => Ok(KeyCode::CapsLock),
            0x3B => Ok(KeyCode::F1),
            0x3C => Ok(KeyCode::F2),
            0x3D => Ok(KeyCode::F3),
            0x3E => Ok(KeyCode::F4),
            0x3F => Ok(KeyCode::F5),
            0x40 => Ok(KeyCode::F6),
            0x41 => Ok(KeyCode::F7),
            0x42 => Ok(KeyCode::F8),
            0x43 => Ok(KeyCode::F9),
            0x44 => Ok(KeyCode::F10),
            0x45 => Ok(KeyCode::NumpadLock),
            0x46 => Ok(KeyCode::ScrollLock),
            0x47 => Ok(KeyCode::Numpad7),
            0x48 => Ok(KeyCode::Numpad8),
            0x49 => Ok(KeyCode::Numpad9),
            0x4A => Ok(KeyCode::NumpadSubtract),
            0x4B => Ok(KeyCode::Numpad4),
            0x4C => Ok(KeyCode::Numpad5),
            0x4D => Ok(KeyCode::Numpad6),
            0x4E => Ok(KeyCode::NumpadAdd),
            0x4F => Ok(KeyCode::Numpad1),
            0x50 => Ok(KeyCode::Numpad2),
            0x51 => Ok(KeyCode::Numpad3),
            0x52 => Ok(KeyCode::Numpad0),
            0x53 => Ok(KeyCode::NumpadPeriod),
            0x54 => Ok(KeyCode::SysRq),
            // 0x55 is unused?
            0x56 => Ok(KeyCode::Oem5),
            0x57 => Ok(KeyCode::F11),
            0x58 => Ok(KeyCode::F12),
            _ => Err(Error::UnknownKeyCode),
        }
    }

    /// Implements the extended byte codes for set 1 (prefixed with E0)
    fn map_extended_scancode(code: u8) -> Result<KeyCode, Error> {
        match code {
            0x10 => Ok(KeyCode::PrevTrack),
            //0x11
            //0x12
            //0x13
            //0x14
            //0x15
            //0x16
            //0x17
            //0x18
            0x19 => Ok(KeyCode::NextTrack),
            //0x1A
            //0x1B
            0x1C => Ok(KeyCode::NumpadEnter),
            0x1D => Ok(KeyCode::RControl),
            //0x1E
            //0x1F
            0x20 => Ok(KeyCode::Mute),
            0x21 => Ok(KeyCode::Calculator),
            0x22 => Ok(KeyCode::Play),
            //0x23
            0x24 => Ok(KeyCode::Stop),
            //0x25
            //0x26
            //0x27
            //0x28
            //0x29
            0x2A => Ok(KeyCode::RAlt2),
            //0x2B
            //0x2C
            //0x2D
            0x2E => Ok(KeyCode::VolumeDown),
            //0x2F
            0x30 => Ok(KeyCode::VolumeUp),
            //0x31
            0x32 => Ok(KeyCode::WWWHome),
            //0x33
            //0x34
            0x35 => Ok(KeyCode::NumpadDivide),
            //0x36
            0x37 => Ok(KeyCode::PrintScreen),
            0x38 => Ok(KeyCode::RAltGr),
            //0x39
            //0x3A
            //0x3B
            //0x3C
            //0x3D
            //0x3E
            //0x3F
            //0x40
            //0x41
            //0x42
            //0x43
            //0x44
            //0x45
            //0x46
            0x47 => Ok(KeyCode::Home),
            0x48 => Ok(KeyCode::ArrowUp),
            0x49 => Ok(KeyCode::PageUp),
            //0x4A
            0x4B => Ok(KeyCode::ArrowLeft),
            //0x4C
            0x4D => Ok(KeyCode::ArrowRight),
            //0x4E
            0x4F => Ok(KeyCode::End),
            0x50 => Ok(KeyCode::ArrowDown),
            0x51 => Ok(KeyCode::PageDown),
            0x52 => Ok(KeyCode::Insert),
            0x53 => Ok(KeyCode::Delete),
            0x5B => Ok(KeyCode::LWin),
            0x5C => Ok(KeyCode::RWin),
            0x5D => Ok(KeyCode::Apps),
            // 0x5E ACPI Power
            // 0x5F ACPI Sleep
            // 0x60
            // 0x61
            // 0x62
            // 0x63 ACPI Wake
            // 0x64
            // 0x65 WWW Search
            // 0x66 WWW Favourites
            // 0x67 WWW Refresh
            // 0x68 WWW Stop
            // 0x69 WWW Forward
            // 0x6A WWW Back
            // 0x6B My Computer
            // 0x6C Email
            // 0x6D Media Select
            0x70 => Ok(KeyCode::Oem11),
            0x73 => Ok(KeyCode::Oem12),
            0x79 => Ok(KeyCode::Oem10),
            0x7B => Ok(KeyCode::Oem9),
            0x7D => Ok(KeyCode::Oem13),
            _ => Err(Error::UnknownKeyCode),
        }
    }

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

impl ScancodeSet for ScancodeSet1 {
    /// Implements state logic for scancode set 1
    ///
    /// ## Start:
    /// * `E0` => Goto Extended
    /// * `E1` => Goto Extended 2
    /// * `< 0x80` => Key Down
    /// * `>= 0x80` => Key Up
    ///
    /// ## Extended:
    /// * `< 0x80` => Extended Key Down
    /// * `>= 0x80` => Extended Key Up
    ///
    /// ## Extended 2:
    /// * `< 0x80` => Extended 2 Key Down
    /// * `>= 0x80` => Extended 2 Key Up
    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)
                    }
                    0x80..=0xFF => {
                        // Break codes
                        Ok(Some(KeyEvent::new(
                            Self::map_scancode(code - 0x80)?,
                            KeyState::Up,
                        )))
                    }
                    _ => {
                        // Make codes
                        Ok(Some(KeyEvent::new(
                            Self::map_scancode(code)?,
                            KeyState::Down,
                        )))
                    }
                }
            }
            DecodeState::Extended => {
                self.state = DecodeState::Start;
                match code {
                    0x80..=0xFF => {
                        // Extended break codes
                        Ok(Some(KeyEvent::new(
                            Self::map_extended_scancode(code - 0x80)?,
                            KeyState::Up,
                        )))
                    }
                    _ => {
                        // Extended make codes
                        Ok(Some(KeyEvent::new(
                            Self::map_extended_scancode(code)?,
                            KeyState::Down,
                        )))
                    }
                }
            }
            DecodeState::Extended2 => {
                self.state = DecodeState::Start;
                match code {
                    0x80..=0xFF => {
                        // Extended 2 break codes
                        Ok(Some(KeyEvent::new(
                            Self::map_extended2_scancode(code - 0x80)?,
                            KeyState::Up,
                        )))
                    }
                    _ => {
                        // Extended 2 make codes
                        Ok(Some(KeyEvent::new(
                            Self::map_extended2_scancode(code)?,
                            KeyState::Down,
                        )))
                    }
                }
            }
            _ => {
                // Can't get in to this state
                unimplemented!();
            }
        }
    }
}

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

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