pc_keyboard/scancodes/
set1.rs

1//! Scan Code Set 1 support
2
3use crate::{
4    DecodeState, Error, KeyCode, KeyEvent, KeyState, ScancodeSet, EXTENDED2_KEY_CODE,
5    EXTENDED_KEY_CODE,
6};
7
8/// Contains the implementation of Scancode Set 1.
9///
10/// See the OS dev wiki: <https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1>
11pub struct ScancodeSet1 {
12    state: DecodeState,
13}
14
15impl ScancodeSet1 {
16    /// Construct a new [`ScancodeSet1`] decoder.
17    pub const fn new() -> ScancodeSet1 {
18        ScancodeSet1 {
19            state: DecodeState::Start,
20        }
21    }
22
23    /// Implements the single byte codes for Set 1.
24    fn map_scancode(code: u8) -> Result<KeyCode, Error> {
25        match code {
26            0x01 => Ok(KeyCode::Escape),
27            0x02 => Ok(KeyCode::Key1),
28            0x03 => Ok(KeyCode::Key2),
29            0x04 => Ok(KeyCode::Key3),
30            0x05 => Ok(KeyCode::Key4),
31            0x06 => Ok(KeyCode::Key5),
32            0x07 => Ok(KeyCode::Key6),
33            0x08 => Ok(KeyCode::Key7),
34            0x09 => Ok(KeyCode::Key8),
35            0x0A => Ok(KeyCode::Key9),
36            0x0B => Ok(KeyCode::Key0),
37            0x0C => Ok(KeyCode::OemMinus),
38            0x0D => Ok(KeyCode::OemPlus),
39            0x0E => Ok(KeyCode::Backspace),
40            0x0F => Ok(KeyCode::Tab),
41            0x10 => Ok(KeyCode::Q),
42            0x11 => Ok(KeyCode::W),
43            0x12 => Ok(KeyCode::E),
44            0x13 => Ok(KeyCode::R),
45            0x14 => Ok(KeyCode::T),
46            0x15 => Ok(KeyCode::Y),
47            0x16 => Ok(KeyCode::U),
48            0x17 => Ok(KeyCode::I),
49            0x18 => Ok(KeyCode::O),
50            0x19 => Ok(KeyCode::P),
51            0x1A => Ok(KeyCode::Oem4),
52            0x1B => Ok(KeyCode::Oem6),
53            0x1C => Ok(KeyCode::Return),
54            0x1D => Ok(KeyCode::LControl),
55            0x1E => Ok(KeyCode::A),
56            0x1F => Ok(KeyCode::S),
57            0x20 => Ok(KeyCode::D),
58            0x21 => Ok(KeyCode::F),
59            0x22 => Ok(KeyCode::G),
60            0x23 => Ok(KeyCode::H),
61            0x24 => Ok(KeyCode::J),
62            0x25 => Ok(KeyCode::K),
63            0x26 => Ok(KeyCode::L),
64            0x27 => Ok(KeyCode::Oem1),
65            0x28 => Ok(KeyCode::Oem3),
66            0x29 => Ok(KeyCode::Oem8),
67            0x2A => Ok(KeyCode::LShift),
68            0x2B => Ok(KeyCode::Oem7),
69            0x2C => Ok(KeyCode::Z),
70            0x2D => Ok(KeyCode::X),
71            0x2E => Ok(KeyCode::C),
72            0x2F => Ok(KeyCode::V),
73            0x30 => Ok(KeyCode::B),
74            0x31 => Ok(KeyCode::N),
75            0x32 => Ok(KeyCode::M),
76            0x33 => Ok(KeyCode::OemComma),
77            0x34 => Ok(KeyCode::OemPeriod),
78            0x35 => Ok(KeyCode::Oem2),
79            0x36 => Ok(KeyCode::RShift),
80            0x37 => Ok(KeyCode::NumpadMultiply),
81            0x38 => Ok(KeyCode::LAlt),
82            0x39 => Ok(KeyCode::Spacebar),
83            0x3A => Ok(KeyCode::CapsLock),
84            0x3B => Ok(KeyCode::F1),
85            0x3C => Ok(KeyCode::F2),
86            0x3D => Ok(KeyCode::F3),
87            0x3E => Ok(KeyCode::F4),
88            0x3F => Ok(KeyCode::F5),
89            0x40 => Ok(KeyCode::F6),
90            0x41 => Ok(KeyCode::F7),
91            0x42 => Ok(KeyCode::F8),
92            0x43 => Ok(KeyCode::F9),
93            0x44 => Ok(KeyCode::F10),
94            0x45 => Ok(KeyCode::NumpadLock),
95            0x46 => Ok(KeyCode::ScrollLock),
96            0x47 => Ok(KeyCode::Numpad7),
97            0x48 => Ok(KeyCode::Numpad8),
98            0x49 => Ok(KeyCode::Numpad9),
99            0x4A => Ok(KeyCode::NumpadSubtract),
100            0x4B => Ok(KeyCode::Numpad4),
101            0x4C => Ok(KeyCode::Numpad5),
102            0x4D => Ok(KeyCode::Numpad6),
103            0x4E => Ok(KeyCode::NumpadAdd),
104            0x4F => Ok(KeyCode::Numpad1),
105            0x50 => Ok(KeyCode::Numpad2),
106            0x51 => Ok(KeyCode::Numpad3),
107            0x52 => Ok(KeyCode::Numpad0),
108            0x53 => Ok(KeyCode::NumpadPeriod),
109            0x54 => Ok(KeyCode::SysRq),
110            // 0x55 is unused?
111            0x56 => Ok(KeyCode::Oem5),
112            0x57 => Ok(KeyCode::F11),
113            0x58 => Ok(KeyCode::F12),
114            _ => Err(Error::UnknownKeyCode),
115        }
116    }
117
118    /// Implements the extended byte codes for set 1 (prefixed with E0)
119    fn map_extended_scancode(code: u8) -> Result<KeyCode, Error> {
120        match code {
121            0x10 => Ok(KeyCode::PrevTrack),
122            //0x11
123            //0x12
124            //0x13
125            //0x14
126            //0x15
127            //0x16
128            //0x17
129            //0x18
130            0x19 => Ok(KeyCode::NextTrack),
131            //0x1A
132            //0x1B
133            0x1C => Ok(KeyCode::NumpadEnter),
134            0x1D => Ok(KeyCode::RControl),
135            //0x1E
136            //0x1F
137            0x20 => Ok(KeyCode::Mute),
138            0x21 => Ok(KeyCode::Calculator),
139            0x22 => Ok(KeyCode::Play),
140            //0x23
141            0x24 => Ok(KeyCode::Stop),
142            //0x25
143            //0x26
144            //0x27
145            //0x28
146            //0x29
147            0x2A => Ok(KeyCode::RAlt2),
148            //0x2B
149            //0x2C
150            //0x2D
151            0x2E => Ok(KeyCode::VolumeDown),
152            //0x2F
153            0x30 => Ok(KeyCode::VolumeUp),
154            //0x31
155            0x32 => Ok(KeyCode::WWWHome),
156            //0x33
157            //0x34
158            0x35 => Ok(KeyCode::NumpadDivide),
159            //0x36
160            0x37 => Ok(KeyCode::PrintScreen),
161            0x38 => Ok(KeyCode::RAltGr),
162            //0x39
163            //0x3A
164            //0x3B
165            //0x3C
166            //0x3D
167            //0x3E
168            //0x3F
169            //0x40
170            //0x41
171            //0x42
172            //0x43
173            //0x44
174            //0x45
175            //0x46
176            0x47 => Ok(KeyCode::Home),
177            0x48 => Ok(KeyCode::ArrowUp),
178            0x49 => Ok(KeyCode::PageUp),
179            //0x4A
180            0x4B => Ok(KeyCode::ArrowLeft),
181            //0x4C
182            0x4D => Ok(KeyCode::ArrowRight),
183            //0x4E
184            0x4F => Ok(KeyCode::End),
185            0x50 => Ok(KeyCode::ArrowDown),
186            0x51 => Ok(KeyCode::PageDown),
187            0x52 => Ok(KeyCode::Insert),
188            0x53 => Ok(KeyCode::Delete),
189            0x5B => Ok(KeyCode::LWin),
190            0x5C => Ok(KeyCode::RWin),
191            0x5D => Ok(KeyCode::Apps),
192            // 0x5E ACPI Power
193            // 0x5F ACPI Sleep
194            // 0x60
195            // 0x61
196            // 0x62
197            // 0x63 ACPI Wake
198            // 0x64
199            // 0x65 WWW Search
200            // 0x66 WWW Favourites
201            // 0x67 WWW Refresh
202            // 0x68 WWW Stop
203            // 0x69 WWW Forward
204            // 0x6A WWW Back
205            // 0x6B My Computer
206            // 0x6C Email
207            // 0x6D Media Select
208            0x70 => Ok(KeyCode::Oem11),
209            0x73 => Ok(KeyCode::Oem12),
210            0x79 => Ok(KeyCode::Oem10),
211            0x7B => Ok(KeyCode::Oem9),
212            0x7D => Ok(KeyCode::Oem13),
213            _ => Err(Error::UnknownKeyCode),
214        }
215    }
216
217    /// Implements the extended byte codes for set 1 (prefixed with E1)
218    fn map_extended2_scancode(code: u8) -> Result<KeyCode, Error> {
219        match code {
220            0x1D => Ok(KeyCode::RControl2),
221            _ => Err(Error::UnknownKeyCode),
222        }
223    }
224}
225
226impl ScancodeSet for ScancodeSet1 {
227    /// Implements state logic for scancode set 1
228    ///
229    /// ## Start:
230    /// * `E0` => Goto Extended
231    /// * `E1` => Goto Extended 2
232    /// * `< 0x80` => Key Down
233    /// * `>= 0x80` => Key Up
234    ///
235    /// ## Extended:
236    /// * `< 0x80` => Extended Key Down
237    /// * `>= 0x80` => Extended Key Up
238    ///
239    /// ## Extended 2:
240    /// * `< 0x80` => Extended 2 Key Down
241    /// * `>= 0x80` => Extended 2 Key Up
242    fn advance_state(&mut self, code: u8) -> Result<Option<KeyEvent>, Error> {
243        match self.state {
244            DecodeState::Start => {
245                match code {
246                    EXTENDED_KEY_CODE => {
247                        self.state = DecodeState::Extended;
248                        Ok(None)
249                    }
250                    EXTENDED2_KEY_CODE => {
251                        self.state = DecodeState::Extended2;
252                        Ok(None)
253                    }
254                    0x80..=0xFF => {
255                        // Break codes
256                        Ok(Some(KeyEvent::new(
257                            Self::map_scancode(code - 0x80)?,
258                            KeyState::Up,
259                        )))
260                    }
261                    _ => {
262                        // Make codes
263                        Ok(Some(KeyEvent::new(
264                            Self::map_scancode(code)?,
265                            KeyState::Down,
266                        )))
267                    }
268                }
269            }
270            DecodeState::Extended => {
271                self.state = DecodeState::Start;
272                match code {
273                    0x80..=0xFF => {
274                        // Extended break codes
275                        Ok(Some(KeyEvent::new(
276                            Self::map_extended_scancode(code - 0x80)?,
277                            KeyState::Up,
278                        )))
279                    }
280                    _ => {
281                        // Extended make codes
282                        Ok(Some(KeyEvent::new(
283                            Self::map_extended_scancode(code)?,
284                            KeyState::Down,
285                        )))
286                    }
287                }
288            }
289            DecodeState::Extended2 => {
290                self.state = DecodeState::Start;
291                match code {
292                    0x80..=0xFF => {
293                        // Extended 2 break codes
294                        Ok(Some(KeyEvent::new(
295                            Self::map_extended2_scancode(code - 0x80)?,
296                            KeyState::Up,
297                        )))
298                    }
299                    _ => {
300                        // Extended 2 make codes
301                        Ok(Some(KeyEvent::new(
302                            Self::map_extended2_scancode(code)?,
303                            KeyState::Down,
304                        )))
305                    }
306                }
307            }
308            _ => {
309                // Can't get in to this state
310                unimplemented!();
311            }
312        }
313    }
314}
315
316impl Default for ScancodeSet1 {
317    fn default() -> Self {
318        ScancodeSet1::new()
319    }
320}
321
322#[cfg(test)]
323mod test {
324    use super::*;
325
326    #[test]
327    fn validate_scancodes() {
328        let mut codes = Vec::new();
329        let mut errs = Vec::new();
330        for code in 0x00..=0x7F {
331            let r = ScancodeSet1::map_scancode(code);
332            match r {
333                Ok(c) => codes.push(c),
334                Err(_) => errs.push(code),
335            }
336        }
337        codes.sort();
338        println!("{:?}", codes);
339        assert_eq!(codes.len(), 87);
340        assert_eq!(errs.len(), 41);
341    }
342}