keyboard_parser.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. use crate::driver::tty::kthread::send_to_tty_refresh_thread;
  2. #[allow(dead_code)]
  3. pub const NUM_SCAN_CODES: u8 = 0x80;
  4. #[allow(dead_code)]
  5. pub const TYPE1_KEYCODE_MAP_TABLE_COLS: u8 = 2;
  6. #[allow(dead_code)]
  7. pub const TYPE1_KEYCODE_FLAG_BREAK: u8 = 0x80; // 用于判断按键是否被按下
  8. /// 标志状态
  9. #[repr(u8)]
  10. #[derive(Debug, PartialEq, Eq)]
  11. #[allow(dead_code)]
  12. pub enum KeyFlag {
  13. NoneFlag = 0_u8,
  14. PauseBreak = 1_u8,
  15. PrintScreenPress = 2_u8,
  16. PrintScreenRelease = 4_u8,
  17. OtherKey = 8_u8, // 除了上面两个按键以外的功能按键(不包括下面的第三类按键)
  18. }
  19. /// @brief A FSM to parse type one keyboard scan code
  20. #[derive(Debug)]
  21. #[allow(dead_code)]
  22. pub struct TypeOneFSM {
  23. status: ScanCodeStatus,
  24. current_state: TypeOneFSMState,
  25. }
  26. impl TypeOneFSM {
  27. #[allow(dead_code)]
  28. pub fn new() -> Self {
  29. Self {
  30. status: ScanCodeStatus::new(),
  31. current_state: TypeOneFSMState::Start,
  32. }
  33. }
  34. /// @brief 解析扫描码
  35. #[allow(dead_code)]
  36. pub fn parse(&mut self, scancode: u8) -> TypeOneFSMState {
  37. self.current_state = self.current_state.parse(scancode, &mut self.status);
  38. self.current_state
  39. }
  40. }
  41. /// @brief 第一类扫描码状态机的状态
  42. #[derive(Debug, Copy, Clone)]
  43. pub enum TypeOneFSMState {
  44. /// 起始状态
  45. Start,
  46. /// PauseBreak 第n个扫描码
  47. PauseBreak(u8),
  48. /// 多扫描码功能键起始状态
  49. Func0,
  50. /// 第三类扫描码或字符
  51. Type3,
  52. PrtscPress(u8),
  53. PrtscRelease(u8),
  54. }
  55. impl TypeOneFSMState {
  56. /// @brief 状态机总控程序
  57. fn parse(&self, scancode: u8, scancode_status: &mut ScanCodeStatus) -> TypeOneFSMState {
  58. // debug!("the code is {:#x}\n", scancode);
  59. match self {
  60. TypeOneFSMState::Start => {
  61. return self.handle_start(scancode, scancode_status);
  62. }
  63. TypeOneFSMState::PauseBreak(n) => {
  64. return self.handle_pause_break(*n, scancode_status);
  65. }
  66. TypeOneFSMState::Func0 => {
  67. return self.handle_func0(scancode, scancode_status);
  68. }
  69. TypeOneFSMState::Type3 => {
  70. return self.handle_type3(scancode, scancode_status);
  71. }
  72. TypeOneFSMState::PrtscPress(n) => return self.handle_prtsc_press(*n, scancode_status),
  73. TypeOneFSMState::PrtscRelease(n) => {
  74. return self.handle_prtsc_release(*n, scancode_status)
  75. }
  76. }
  77. }
  78. /// @brief 处理起始状态
  79. fn handle_start(&self, scancode: u8, scancode_status: &mut ScanCodeStatus) -> TypeOneFSMState {
  80. //debug!("in handle_start the code is {:#x}\n",scancode);
  81. match scancode {
  82. 0xe1 => {
  83. return TypeOneFSMState::PauseBreak(1);
  84. }
  85. 0xe0 => {
  86. return TypeOneFSMState::Func0;
  87. }
  88. _ => {
  89. //debug!("in _d the code is {:#x}\n",scancode);
  90. return TypeOneFSMState::Type3.handle_type3(scancode, scancode_status);
  91. }
  92. }
  93. }
  94. /// @brief 处理PauseBreak状态
  95. fn handle_pause_break(
  96. &self,
  97. scancode: u8,
  98. scancode_status: &mut ScanCodeStatus,
  99. ) -> TypeOneFSMState {
  100. static PAUSE_BREAK_SCAN_CODE: [u8; 6] = [0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5];
  101. let i = match self {
  102. TypeOneFSMState::PauseBreak(i) => *i,
  103. _ => {
  104. return self.handle_type3(scancode, scancode_status);
  105. }
  106. };
  107. if scancode != PAUSE_BREAK_SCAN_CODE[i as usize] {
  108. return self.handle_type3(scancode, scancode_status);
  109. } else if i == 5 {
  110. // 所有Pause Break扫描码都被清除
  111. return TypeOneFSMState::Start;
  112. } else {
  113. return TypeOneFSMState::PauseBreak(i + 1);
  114. }
  115. }
  116. fn handle_func0(&self, scancode: u8, scancode_status: &mut ScanCodeStatus) -> TypeOneFSMState {
  117. //0xE0
  118. match scancode {
  119. 0x2a => {
  120. return TypeOneFSMState::PrtscPress(2);
  121. }
  122. 0xb7 => {
  123. return TypeOneFSMState::PrtscRelease(2);
  124. }
  125. 0x1d => {
  126. // 按下右边的ctrl
  127. scancode_status.ctrl_r = true;
  128. }
  129. 0x9d => {
  130. // 松开右边的ctrl
  131. scancode_status.ctrl_r = false;
  132. }
  133. 0x38 => {
  134. // 按下右边的alt
  135. scancode_status.alt_r = true;
  136. }
  137. 0xb8 => {
  138. // 松开右边的alt
  139. scancode_status.alt_r = false;
  140. }
  141. 0x5b => {
  142. scancode_status.gui_l = true;
  143. }
  144. 0xdb => {
  145. scancode_status.gui_l = false;
  146. }
  147. 0x5c => {
  148. scancode_status.gui_r = true;
  149. }
  150. 0xdc => {
  151. scancode_status.gui_r = false;
  152. }
  153. 0x5d => {
  154. scancode_status.apps = true;
  155. }
  156. 0xdd => {
  157. scancode_status.apps = false;
  158. }
  159. 0x52 => {
  160. scancode_status.insert = true;
  161. }
  162. 0xd2 => {
  163. scancode_status.insert = false;
  164. }
  165. 0x47 => {
  166. scancode_status.home = true;
  167. Self::emit(0x1b);
  168. Self::emit(0x5b);
  169. Self::emit(0x48);
  170. }
  171. 0xc7 => {
  172. scancode_status.home = false;
  173. }
  174. 0x49 => {
  175. scancode_status.pgup = true;
  176. }
  177. 0xc9 => {
  178. scancode_status.pgup = false;
  179. }
  180. 0x53 => {
  181. scancode_status.del = true;
  182. Self::emit(0x1b);
  183. Self::emit(0x5b);
  184. Self::emit(0x33);
  185. Self::emit(0x7e);
  186. }
  187. 0xd3 => {
  188. scancode_status.del = false;
  189. }
  190. 0x4f => {
  191. scancode_status.end = true;
  192. Self::emit(0x1b);
  193. Self::emit(0x5b);
  194. Self::emit(0x46);
  195. }
  196. 0xcf => {
  197. scancode_status.end = false;
  198. }
  199. 0x51 => {
  200. scancode_status.pgdn = true;
  201. }
  202. 0xd1 => {
  203. scancode_status.pgdn = false;
  204. }
  205. 0x48 => {
  206. scancode_status.arrow_u = true;
  207. Self::emit(0x1b);
  208. Self::emit(0x5b);
  209. Self::emit(0x41);
  210. }
  211. 0xc8 => {
  212. scancode_status.arrow_u = false;
  213. }
  214. 0x4b => {
  215. scancode_status.arrow_l = true;
  216. Self::emit(0x1b);
  217. Self::emit(0x5b);
  218. Self::emit(0x44);
  219. }
  220. 0xcb => {
  221. scancode_status.arrow_l = false;
  222. }
  223. 0x50 => {
  224. scancode_status.arrow_d = true;
  225. Self::emit(0x1b);
  226. Self::emit(0x5b);
  227. Self::emit(0x42);
  228. }
  229. 0xd0 => {
  230. scancode_status.arrow_d = false;
  231. }
  232. 0x4d => {
  233. scancode_status.arrow_r = true;
  234. Self::emit(0x1b);
  235. Self::emit(0x5b);
  236. Self::emit(0x43);
  237. }
  238. 0xcd => {
  239. scancode_status.arrow_r = false;
  240. }
  241. 0x35 => {
  242. // 数字小键盘的 / 符号
  243. scancode_status.kp_forward_slash = true;
  244. let ch = b'/';
  245. Self::emit(ch);
  246. }
  247. 0xb5 => {
  248. scancode_status.kp_forward_slash = false;
  249. }
  250. 0x1c => {
  251. scancode_status.kp_enter = true;
  252. Self::emit(b'\n');
  253. }
  254. 0x9c => {
  255. scancode_status.kp_enter = false;
  256. }
  257. _ => {
  258. return TypeOneFSMState::Start;
  259. }
  260. }
  261. return TypeOneFSMState::Start;
  262. }
  263. fn handle_type3(&self, scancode: u8, scancode_status: &mut ScanCodeStatus) -> TypeOneFSMState {
  264. // 判断按键是被按下还是抬起
  265. let flag_make = if (scancode & (TYPE1_KEYCODE_FLAG_BREAK)) > 0 {
  266. false //up
  267. } else {
  268. true //down
  269. };
  270. // 计算扫描码位于码表的第几行
  271. let mut col: bool = false;
  272. let index = scancode & 0x7f;
  273. //debug!("in type3 ch is {:#x}\n",ch);
  274. let mut key = KeyFlag::OtherKey; // 可视字符
  275. match index {
  276. 0x2a => {
  277. scancode_status.shift_l = flag_make;
  278. key = KeyFlag::NoneFlag;
  279. }
  280. 0x36 => {
  281. scancode_status.shift_r = flag_make;
  282. key = KeyFlag::NoneFlag;
  283. }
  284. 0x1d => {
  285. scancode_status.ctrl_l = flag_make;
  286. key = KeyFlag::NoneFlag;
  287. }
  288. 0x38 => {
  289. scancode_status.alt_r = flag_make;
  290. key = KeyFlag::NoneFlag;
  291. }
  292. 0x3A => {
  293. if scancode_status.caps_lock {
  294. scancode_status.caps_lock = !flag_make;
  295. }
  296. //if caps_lock: true, flag_make: true => cap_lock: false
  297. else {
  298. scancode_status.caps_lock = flag_make;
  299. } //else false => cap_lock: true
  300. key = KeyFlag::NoneFlag;
  301. }
  302. _ => {
  303. if !flag_make {
  304. // debug!("in type3 ch is {:#x}\n",ch);
  305. key = KeyFlag::NoneFlag;
  306. }
  307. }
  308. }
  309. // shift被按下
  310. let shift = scancode_status.shift_l || scancode_status.shift_r;
  311. if shift {
  312. col = true;
  313. }
  314. if scancode_status.caps_lock
  315. && ((0x10..=0x19).contains(&index)
  316. || (0x1e..=0x26).contains(&index)
  317. || (0x2c..=0x32).contains(&index))
  318. {
  319. col = !col;
  320. }
  321. let mut ch = TYPE1_KEY_CODE_MAPTABLE[col as usize + 2 * index as usize];
  322. if key != KeyFlag::NoneFlag {
  323. if scancode_status.ctrl_l || scancode_status.ctrl_r {
  324. ch = Self::to_ctrl(ch, shift);
  325. }
  326. Self::emit(ch);
  327. }
  328. return TypeOneFSMState::Start;
  329. }
  330. #[inline]
  331. fn to_ctrl(ch: u8, shift: bool) -> u8 {
  332. return match ch as char {
  333. 'a'..='z' => ch - 0x60,
  334. 'A'..='Z' => {
  335. if shift {
  336. ch
  337. } else {
  338. ch - 0x40
  339. }
  340. }
  341. '@'..='_' => ch - 0x40,
  342. _ => ch,
  343. };
  344. }
  345. #[inline(always)]
  346. fn emit(ch: u8) {
  347. // 发送到tty
  348. send_to_tty_refresh_thread(&[ch]);
  349. }
  350. /// @brief 处理Prtsc按下事件
  351. fn handle_prtsc_press(
  352. &self,
  353. scancode: u8,
  354. scancode_status: &mut ScanCodeStatus,
  355. ) -> TypeOneFSMState {
  356. static PRTSC_SCAN_CODE: [u8; 4] = [0xe0, 0x2a, 0xe0, 0x37];
  357. let i = match self {
  358. TypeOneFSMState::PrtscPress(i) => *i,
  359. _ => return TypeOneFSMState::Start, // 解析错误,返回起始状态
  360. };
  361. if i > 3 {
  362. // 解析错误,返回起始状态
  363. return TypeOneFSMState::Start;
  364. }
  365. if scancode != PRTSC_SCAN_CODE[i as usize] {
  366. return self.handle_type3(scancode, scancode_status);
  367. } else if i == 3 {
  368. // 成功解析出PrtscPress
  369. return TypeOneFSMState::Start;
  370. } else {
  371. // 继续解析
  372. return TypeOneFSMState::PrtscPress(i + 1);
  373. }
  374. }
  375. fn handle_prtsc_release(
  376. &self,
  377. scancode: u8,
  378. scancode_status: &mut ScanCodeStatus,
  379. ) -> TypeOneFSMState {
  380. static PRTSC_SCAN_CODE: [u8; 4] = [0xe0, 0xb7, 0xe0, 0xaa];
  381. let i = match self {
  382. TypeOneFSMState::PrtscRelease(i) => *i,
  383. _ => return TypeOneFSMState::Start, // 解析错误,返回起始状态
  384. };
  385. if i > 3 {
  386. // 解析错误,返回起始状态
  387. return TypeOneFSMState::Start;
  388. }
  389. if scancode != PRTSC_SCAN_CODE[i as usize] {
  390. return self.handle_type3(scancode, scancode_status);
  391. } else if i == 3 {
  392. // 成功解析出PrtscRelease
  393. return TypeOneFSMState::Start;
  394. } else {
  395. // 继续解析
  396. return TypeOneFSMState::PrtscRelease(i + 1);
  397. }
  398. }
  399. }
  400. /// 按键状态
  401. #[derive(Debug)]
  402. #[allow(dead_code)]
  403. pub struct ScanCodeStatus {
  404. // Shift 按键
  405. shift_l: bool,
  406. shift_r: bool,
  407. // Ctrl 按键
  408. ctrl_l: bool,
  409. ctrl_r: bool,
  410. // Alt 按键
  411. alt_l: bool,
  412. alt_r: bool,
  413. //
  414. gui_l: bool,
  415. gui_r: bool,
  416. //
  417. apps: bool,
  418. insert: bool,
  419. // page up/down
  420. pgup: bool,
  421. pgdn: bool,
  422. del: bool,
  423. home: bool,
  424. end: bool,
  425. arrow_u: bool,
  426. arrow_l: bool,
  427. arrow_d: bool,
  428. arrow_r: bool,
  429. // 斜杠
  430. kp_forward_slash: bool,
  431. // 回车
  432. kp_enter: bool,
  433. caps_lock: bool,
  434. }
  435. impl ScanCodeStatus {
  436. fn new() -> Self {
  437. ScanCodeStatus {
  438. shift_l: false,
  439. shift_r: false,
  440. ctrl_l: false,
  441. ctrl_r: false,
  442. alt_l: false,
  443. alt_r: false,
  444. gui_l: false,
  445. gui_r: false,
  446. apps: false,
  447. insert: false,
  448. pgup: false,
  449. pgdn: false,
  450. del: false,
  451. home: false,
  452. end: false,
  453. arrow_u: false,
  454. arrow_l: false,
  455. arrow_d: false,
  456. arrow_r: false,
  457. kp_forward_slash: false,
  458. kp_enter: false,
  459. caps_lock: false,
  460. }
  461. }
  462. }
  463. const TYPE1_KEY_CODE_MAPTABLE: [u8; 256] = [
  464. /*0x00*/ 0, 0, /*0x01*/ 0, 0, // ESC
  465. /*0x02*/ b'1', b'!', /*0x03*/ b'2', b'@', /*0x04*/ b'3', b'#',
  466. /*0x05*/ b'4', b'$', /*0x06*/ b'5', b'%', /*0x07*/ b'6', b'^',
  467. /*0x08*/ b'7', b'&', /*0x09*/ b'8', b'*', /*0x0a*/ b'9', b'(',
  468. /*0x0b*/ b'0', b')', /*0x0c*/ b'-', b'_', /*0x0d*/ b'=', b'+',
  469. /*0x0e \b */ 0x7f, 0x7f, // BACKSPACE
  470. /*0x0f*/ b'\t', b'\t', // TAB
  471. ////////////////////////character///////////////////////////
  472. /*0x10*/ b'q', b'Q',
  473. /*0x11*/ b'w', b'W', /*0x12*/ b'e', b'E', /*0x13*/ b'r', b'R',
  474. /*0x14*/ b't', b'T', /*0x15*/ b'y', b'Y', /*0x16*/ b'u', b'U',
  475. /*0x17*/ b'i', b'I', /*0x18*/ b'o', b'O', /*0x19*/ b'p', b'P',
  476. ////////////////////////character///////////////////////////
  477. /*0x1a*/ b'[', b'{',
  478. /*0x1b*/ b']', b'}', /*0x1c*/ b'\n', b'\n', // ENTER
  479. /*0x1d*/ 0x1d, 0x1d, // CTRL Left
  480. ////////////////////////character///////////////////////////
  481. /*0x1e*/ b'a', b'A',
  482. /*0x1f*/ b's', b'S', /*0x20*/ b'd', b'D', /*0x21*/ b'f', b'F',
  483. /*0x22*/ b'g', b'G', /*0x23*/ b'h', b'H', /*0x24*/ b'j', b'J',
  484. /*0x25*/ b'k', b'K', /*0x26*/ b'l', b'L',
  485. ////////////////////////character///////////////////////////
  486. /*0x27*/ b';', b':',
  487. /*0x28*/ b'\'', b'"', /*0x29*/ b'`', b'~', /*0x2a*/ 0x2a,
  488. 0x2a, // SHIFT Left
  489. /*0x2b*/ b'\\', b'|',
  490. ////////////////////////character///////////////////////////
  491. /*0x2c*/ b'z', b'Z',
  492. /*0x2d*/ b'x', b'X', /*0x2e*/ b'c', b'C', /*0x2f*/ b'v', b'V',
  493. /*0x30*/ b'b', b'B', /*0x31*/ b'n', b'N', /*0x32*/ b'm', b'M',
  494. ////////////////////////character///////////////////////////
  495. /*0x33*/ b',', b'<',
  496. /*0x34*/ b'.', b'>', /*0x35*/ b'/', b'?', /*0x36*/ 0x36,
  497. 0x36, // SHIFT Right
  498. /*0x37*/ b'*', b'*', /*0x38*/ 0x38, 0x38, // ALT Left
  499. /*0x39*/ b' ', b' ', /*0x3a*/ 0, 0, // CAPS LOCK
  500. /*0x3b*/ 0, 0, // F1
  501. /*0x3c*/ 0, 0, // F2
  502. /*0x3d*/ 0, 0, // F3
  503. /*0x3e*/ 0, 0, // F4
  504. /*0x3f*/ 0, 0, // F5
  505. /*0x40*/ 0, 0, // F6
  506. /*0x41*/ 0, 0, // F7
  507. /*0x42*/ 0, 0, // F8
  508. /*0x43*/ 0, 0, // F9
  509. /*0x44*/ 0, 0, // F10
  510. /*0x45*/ 0, 0, // NUM LOCK
  511. /*0x46*/ 0, 0, // SCROLL LOCK
  512. /*0x47*/ b'7', 0, /*PAD HONE*/
  513. /*0x48*/ b'8', 0, /*PAD UP*/
  514. /*0x49*/ b'9', 0, /*PAD PAGEUP*/
  515. /*0x4a*/ b'-', 0, /*PAD MINUS*/
  516. /*0x4b*/ b'4', 0, /*PAD LEFT*/
  517. /*0x4c*/ b'5', 0, /*PAD MID*/
  518. /*0x4d*/ b'6', 0, /*PAD RIGHT*/
  519. /*0x4e*/ b'+', 0, /*PAD PLUS*/
  520. /*0x4f*/ b'1', 0, /*PAD END*/
  521. /*0x50*/ b'2', 0, /*PAD DOWN*/
  522. /*0x51*/ b'3', 0, /*PAD PAGEDOWN*/
  523. /*0x52*/ b'0', 0, /*PAD INS*/
  524. /*0x53*/ b'.', 0, /*PAD DOT*/
  525. /*0x54*/ 0, 0, /*0x55*/ 0, 0, /*0x56*/ 0, 0, /*0x57*/ 0, 0, // F11
  526. /*0x58*/ 0, 0, // F12
  527. /*0x59*/ 0, 0, /*0x5a*/ 0, 0, /*0x5b*/ 0, 0, /*0x5c*/ 0, 0,
  528. /*0x5d*/ 0, 0, /*0x5e*/ 0, 0, /*0x5f*/ 0, 0, /*0x60*/ 0, 0,
  529. /*0x61*/ 0, 0, /*0x62*/ 0, 0, /*0x63*/ 0, 0, /*0x64*/ 0, 0,
  530. /*0x65*/ 0, 0, /*0x66*/ 0, 0, /*0x67*/ 0, 0, /*0x68*/ 0, 0,
  531. /*0x69*/ 0, 0, /*0x6a*/ 0, 0, /*0x6b*/ 0, 0, /*0x6c*/ 0, 0,
  532. /*0x6d*/ 0, 0, /*0x6e*/ 0, 0, /*0x6f*/ 0, 0, /*0x70*/ 0, 0,
  533. /*0x71*/ 0, 0, /*0x72*/ 0, 0, /*0x73*/ 0, 0, /*0x74*/ 0, 0,
  534. /*0x75*/ 0, 0, /*0x76*/ 0, 0, /*0x77*/ 0, 0, /*0x78*/ 0, 0,
  535. /*0x79*/ 0, 0, /*0x7a*/ 0, 0, /*0x7b*/ 0, 0, /*0x7c*/ 0, 0,
  536. /*0x7d*/ 0, 0, /*0x7e*/ 0, 0, /*0x7f*/ 0, 0,
  537. ];