fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题 (#877)

* fix: 修复键盘码解析器没能正确处理类似ctrl C的控制字符的问题

* fix: 解决ntty潜在的panic问题
This commit is contained in:
LoGin 2024-08-06 19:51:45 +08:00 committed by GitHub
parent 0648a547da
commit a1fc824fcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -388,9 +388,9 @@ impl NTtyData {
continue; continue;
} }
if self.char_map.get(c as usize).unwrap() { if ((c as usize) < self.char_map.size()) && self.char_map.get(c as usize).unwrap() {
// 特殊字符 // 特殊字符
self.receive_special_char(c, tty.clone(), lookahead_done) self.receive_special_char(c, tty.clone(), lookahead_done);
} else { } else {
self.receive_char(c, tty.clone()); self.receive_char(c, tty.clone());
} }

View File

@ -313,7 +313,8 @@ impl TypeOneFSMState {
} }
// shift被按下 // shift被按下
if scancode_status.shift_l || scancode_status.shift_r { let shift = scancode_status.shift_l || scancode_status.shift_r;
if shift {
col = true; col = true;
} }
@ -327,9 +328,8 @@ impl TypeOneFSMState {
let mut ch = TYPE1_KEY_CODE_MAPTABLE[col as usize + 2 * index as usize]; let mut ch = TYPE1_KEY_CODE_MAPTABLE[col as usize + 2 * index as usize];
if key != KeyFlag::NoneFlag { if key != KeyFlag::NoneFlag {
// debug!("EMIT: ch is '{}', keyflag is {:?}\n", ch as char, key);
if scancode_status.ctrl_l || scancode_status.ctrl_r { if scancode_status.ctrl_l || scancode_status.ctrl_r {
ch = Self::to_ctrl(ch); ch = Self::to_ctrl(ch, shift);
} }
Self::emit(ch); Self::emit(ch);
} }
@ -337,10 +337,16 @@ impl TypeOneFSMState {
} }
#[inline] #[inline]
fn to_ctrl(ch: u8) -> u8 { fn to_ctrl(ch: u8, shift: bool) -> u8 {
return match ch as char { return match ch as char {
'a'..='z' => ch - 0x40, 'a'..='z' => ch - 0x60,
'A'..='Z' => ch - 0x40, 'A'..='Z' => {
if shift {
ch
} else {
ch - 0x40
}
}
'@'..='_' => ch - 0x40, '@'..='_' => ch - 0x40,
_ => ch, _ => ch,
}; };