keyboard_parser.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. }
  168. 0xc7 => {
  169. scancode_status.home = false;
  170. }
  171. 0x49 => {
  172. scancode_status.pgup = true;
  173. }
  174. 0xc9 => {
  175. scancode_status.pgup = false;
  176. }
  177. 0x53 => {
  178. scancode_status.del = true;
  179. Self::emit(127);
  180. }
  181. 0xd3 => {
  182. scancode_status.del = false;
  183. }
  184. 0x4f => {
  185. scancode_status.end = true;
  186. }
  187. 0xcf => {
  188. scancode_status.end = false;
  189. }
  190. 0x51 => {
  191. scancode_status.pgdn = true;
  192. }
  193. 0xd1 => {
  194. scancode_status.pgdn = false;
  195. }
  196. 0x48 => {
  197. scancode_status.arrow_u = true;
  198. Self::emit(224);
  199. Self::emit(72);
  200. }
  201. 0xc8 => {
  202. scancode_status.arrow_u = false;
  203. }
  204. 0x4b => {
  205. scancode_status.arrow_l = true;
  206. Self::emit(224);
  207. Self::emit(75);
  208. }
  209. 0xcb => {
  210. scancode_status.arrow_l = false;
  211. }
  212. 0x50 => {
  213. scancode_status.arrow_d = true;
  214. Self::emit(224);
  215. Self::emit(80);
  216. }
  217. 0xd0 => {
  218. scancode_status.arrow_d = false;
  219. }
  220. 0x4d => {
  221. scancode_status.arrow_r = true;
  222. Self::emit(224);
  223. Self::emit(77);
  224. }
  225. 0xcd => {
  226. scancode_status.arrow_r = false;
  227. }
  228. 0x35 => {
  229. // 数字小键盘的 / 符号
  230. scancode_status.kp_forward_slash = true;
  231. let ch = b'/';
  232. Self::emit(ch);
  233. }
  234. 0xb5 => {
  235. scancode_status.kp_forward_slash = false;
  236. }
  237. 0x1c => {
  238. scancode_status.kp_enter = true;
  239. Self::emit(b'\n');
  240. }
  241. 0x9c => {
  242. scancode_status.kp_enter = false;
  243. }
  244. _ => {
  245. return TypeOneFSMState::Start;
  246. }
  247. }
  248. return TypeOneFSMState::Start;
  249. }
  250. fn handle_type3(&self, scancode: u8, scancode_status: &mut ScanCodeStatus) -> TypeOneFSMState {
  251. // 判断按键是被按下还是抬起
  252. let flag_make = if (scancode & (TYPE1_KEYCODE_FLAG_BREAK)) > 0 {
  253. false //up
  254. } else {
  255. true //down
  256. };
  257. // 计算扫描码位于码表的第几行
  258. let mut col: bool = false;
  259. let index = scancode & 0x7f;
  260. //debug!("in type3 ch is {:#x}\n",ch);
  261. let mut key = KeyFlag::OtherKey; // 可视字符
  262. match index {
  263. 0x2a => {
  264. scancode_status.shift_l = flag_make;
  265. key = KeyFlag::NoneFlag;
  266. }
  267. 0x36 => {
  268. scancode_status.shift_r = flag_make;
  269. key = KeyFlag::NoneFlag;
  270. }
  271. 0x1d => {
  272. scancode_status.ctrl_l = flag_make;
  273. key = KeyFlag::NoneFlag;
  274. }
  275. 0x38 => {
  276. scancode_status.alt_r = flag_make;
  277. key = KeyFlag::NoneFlag;
  278. }
  279. 0x3A => {
  280. if scancode_status.caps_lock {
  281. scancode_status.caps_lock = !flag_make;
  282. }
  283. //if caps_lock: true, flag_make: true => cap_lock: false
  284. else {
  285. scancode_status.caps_lock = flag_make;
  286. } //else false => cap_lock: true
  287. key = KeyFlag::NoneFlag;
  288. }
  289. _ => {
  290. if !flag_make {
  291. // debug!("in type3 ch is {:#x}\n",ch);
  292. key = KeyFlag::NoneFlag;
  293. }
  294. }
  295. }
  296. // shift被按下
  297. let shift = scancode_status.shift_l || scancode_status.shift_r;
  298. if shift {
  299. col = true;
  300. }
  301. if scancode_status.caps_lock
  302. && ((0x10..=0x19).contains(&index)
  303. || (0x1e..=0x26).contains(&index)
  304. || (0x2c..=0x32).contains(&index))
  305. {
  306. col = !col;
  307. }
  308. let mut ch = TYPE1_KEY_CODE_MAPTABLE[col as usize + 2 * index as usize];
  309. if key != KeyFlag::NoneFlag {
  310. if scancode_status.ctrl_l || scancode_status.ctrl_r {
  311. ch = Self::to_ctrl(ch, shift);
  312. }
  313. Self::emit(ch);
  314. }
  315. return TypeOneFSMState::Start;
  316. }
  317. #[inline]
  318. fn to_ctrl(ch: u8, shift: bool) -> u8 {
  319. return match ch as char {
  320. 'a'..='z' => ch - 0x60,
  321. 'A'..='Z' => {
  322. if shift {
  323. ch
  324. } else {
  325. ch - 0x40
  326. }
  327. }
  328. '@'..='_' => ch - 0x40,
  329. _ => ch,
  330. };
  331. }
  332. #[inline(always)]
  333. fn emit(ch: u8) {
  334. // 发送到tty
  335. send_to_tty_refresh_thread(&[ch]);
  336. }
  337. /// @brief 处理Prtsc按下事件
  338. fn handle_prtsc_press(
  339. &self,
  340. scancode: u8,
  341. scancode_status: &mut ScanCodeStatus,
  342. ) -> TypeOneFSMState {
  343. static PRTSC_SCAN_CODE: [u8; 4] = [0xe0, 0x2a, 0xe0, 0x37];
  344. let i = match self {
  345. TypeOneFSMState::PrtscPress(i) => *i,
  346. _ => return TypeOneFSMState::Start, // 解析错误,返回起始状态
  347. };
  348. if i > 3 {
  349. // 解析错误,返回起始状态
  350. return TypeOneFSMState::Start;
  351. }
  352. if scancode != PRTSC_SCAN_CODE[i as usize] {
  353. return self.handle_type3(scancode, scancode_status);
  354. } else if i == 3 {
  355. // 成功解析出PrtscPress
  356. return TypeOneFSMState::Start;
  357. } else {
  358. // 继续解析
  359. return TypeOneFSMState::PrtscPress(i + 1);
  360. }
  361. }
  362. fn handle_prtsc_release(
  363. &self,
  364. scancode: u8,
  365. scancode_status: &mut ScanCodeStatus,
  366. ) -> TypeOneFSMState {
  367. static PRTSC_SCAN_CODE: [u8; 4] = [0xe0, 0xb7, 0xe0, 0xaa];
  368. let i = match self {
  369. TypeOneFSMState::PrtscRelease(i) => *i,
  370. _ => return TypeOneFSMState::Start, // 解析错误,返回起始状态
  371. };
  372. if i > 3 {
  373. // 解析错误,返回起始状态
  374. return TypeOneFSMState::Start;
  375. }
  376. if scancode != PRTSC_SCAN_CODE[i as usize] {
  377. return self.handle_type3(scancode, scancode_status);
  378. } else if i == 3 {
  379. // 成功解析出PrtscRelease
  380. return TypeOneFSMState::Start;
  381. } else {
  382. // 继续解析
  383. return TypeOneFSMState::PrtscRelease(i + 1);
  384. }
  385. }
  386. }
  387. /// 按键状态
  388. #[derive(Debug)]
  389. #[allow(dead_code)]
  390. pub struct ScanCodeStatus {
  391. // Shift 按键
  392. shift_l: bool,
  393. shift_r: bool,
  394. // Ctrl 按键
  395. ctrl_l: bool,
  396. ctrl_r: bool,
  397. // Alt 按键
  398. alt_l: bool,
  399. alt_r: bool,
  400. //
  401. gui_l: bool,
  402. gui_r: bool,
  403. //
  404. apps: bool,
  405. insert: bool,
  406. // page up/down
  407. pgup: bool,
  408. pgdn: bool,
  409. del: bool,
  410. home: bool,
  411. end: bool,
  412. arrow_u: bool,
  413. arrow_l: bool,
  414. arrow_d: bool,
  415. arrow_r: bool,
  416. // 斜杠
  417. kp_forward_slash: bool,
  418. // 回车
  419. kp_enter: bool,
  420. caps_lock: bool,
  421. }
  422. impl ScanCodeStatus {
  423. fn new() -> Self {
  424. ScanCodeStatus {
  425. shift_l: false,
  426. shift_r: false,
  427. ctrl_l: false,
  428. ctrl_r: false,
  429. alt_l: false,
  430. alt_r: false,
  431. gui_l: false,
  432. gui_r: false,
  433. apps: false,
  434. insert: false,
  435. pgup: false,
  436. pgdn: false,
  437. del: false,
  438. home: false,
  439. end: false,
  440. arrow_u: false,
  441. arrow_l: false,
  442. arrow_d: false,
  443. arrow_r: false,
  444. kp_forward_slash: false,
  445. kp_enter: false,
  446. caps_lock: false,
  447. }
  448. }
  449. }
  450. const TYPE1_KEY_CODE_MAPTABLE: [u8; 256] = [
  451. /*0x00*/ 0, 0, /*0x01*/ 0, 0, // ESC
  452. /*0x02*/ b'1', b'!', /*0x03*/ b'2', b'@', /*0x04*/ b'3', b'#',
  453. /*0x05*/ b'4', b'$', /*0x06*/ b'5', b'%', /*0x07*/ b'6', b'^',
  454. /*0x08*/ b'7', b'&', /*0x09*/ b'8', b'*', /*0x0a*/ b'9', b'(',
  455. /*0x0b*/ b'0', b')', /*0x0c*/ b'-', b'_', /*0x0d*/ b'=', b'+',
  456. /*0x0e \b */ 8, 8, // BACKSPACE
  457. /*0x0f*/ b'\t', b'\t', // TAB
  458. ////////////////////////character///////////////////////////
  459. /*0x10*/ b'q', b'Q',
  460. /*0x11*/ b'w', b'W', /*0x12*/ b'e', b'E', /*0x13*/ b'r', b'R',
  461. /*0x14*/ b't', b'T', /*0x15*/ b'y', b'Y', /*0x16*/ b'u', b'U',
  462. /*0x17*/ b'i', b'I', /*0x18*/ b'o', b'O', /*0x19*/ b'p', b'P',
  463. ////////////////////////character///////////////////////////
  464. /*0x1a*/ b'[', b'{',
  465. /*0x1b*/ b']', b'}', /*0x1c*/ b'\n', b'\n', // ENTER
  466. /*0x1d*/ 0x1d, 0x1d, // CTRL Left
  467. ////////////////////////character///////////////////////////
  468. /*0x1e*/ b'a', b'A',
  469. /*0x1f*/ b's', b'S', /*0x20*/ b'd', b'D', /*0x21*/ b'f', b'F',
  470. /*0x22*/ b'g', b'G', /*0x23*/ b'h', b'H', /*0x24*/ b'j', b'J',
  471. /*0x25*/ b'k', b'K', /*0x26*/ b'l', b'L',
  472. ////////////////////////character///////////////////////////
  473. /*0x27*/ b';', b':',
  474. /*0x28*/ b'\'', b'"', /*0x29*/ b'`', b'~', /*0x2a*/ 0x2a,
  475. 0x2a, // SHIFT Left
  476. /*0x2b*/ b'\\', b'|',
  477. ////////////////////////character///////////////////////////
  478. /*0x2c*/ b'z', b'Z',
  479. /*0x2d*/ b'x', b'X', /*0x2e*/ b'c', b'C', /*0x2f*/ b'v', b'V',
  480. /*0x30*/ b'b', b'B', /*0x31*/ b'n', b'N', /*0x32*/ b'm', b'M',
  481. ////////////////////////character///////////////////////////
  482. /*0x33*/ b',', b'<',
  483. /*0x34*/ b'.', b'>', /*0x35*/ b'/', b'?', /*0x36*/ 0x36,
  484. 0x36, // SHIFT Right
  485. /*0x37*/ b'*', b'*', /*0x38*/ 0x38, 0x38, // ALT Left
  486. /*0x39*/ b' ', b' ', /*0x3a*/ 0, 0, // CAPS LOCK
  487. /*0x3b*/ 0, 0, // F1
  488. /*0x3c*/ 0, 0, // F2
  489. /*0x3d*/ 0, 0, // F3
  490. /*0x3e*/ 0, 0, // F4
  491. /*0x3f*/ 0, 0, // F5
  492. /*0x40*/ 0, 0, // F6
  493. /*0x41*/ 0, 0, // F7
  494. /*0x42*/ 0, 0, // F8
  495. /*0x43*/ 0, 0, // F9
  496. /*0x44*/ 0, 0, // F10
  497. /*0x45*/ 0, 0, // NUM LOCK
  498. /*0x46*/ 0, 0, // SCROLL LOCK
  499. /*0x47*/ b'7', 0, /*PAD HONE*/
  500. /*0x48*/ b'8', 0, /*PAD UP*/
  501. /*0x49*/ b'9', 0, /*PAD PAGEUP*/
  502. /*0x4a*/ b'-', 0, /*PAD MINUS*/
  503. /*0x4b*/ b'4', 0, /*PAD LEFT*/
  504. /*0x4c*/ b'5', 0, /*PAD MID*/
  505. /*0x4d*/ b'6', 0, /*PAD RIGHT*/
  506. /*0x4e*/ b'+', 0, /*PAD PLUS*/
  507. /*0x4f*/ b'1', 0, /*PAD END*/
  508. /*0x50*/ b'2', 0, /*PAD DOWN*/
  509. /*0x51*/ b'3', 0, /*PAD PAGEDOWN*/
  510. /*0x52*/ b'0', 0, /*PAD INS*/
  511. /*0x53*/ b'.', 0, /*PAD DOT*/
  512. /*0x54*/ 0, 0, /*0x55*/ 0, 0, /*0x56*/ 0, 0, /*0x57*/ 0, 0, // F11
  513. /*0x58*/ 0, 0, // F12
  514. /*0x59*/ 0, 0, /*0x5a*/ 0, 0, /*0x5b*/ 0, 0, /*0x5c*/ 0, 0,
  515. /*0x5d*/ 0, 0, /*0x5e*/ 0, 0, /*0x5f*/ 0, 0, /*0x60*/ 0, 0,
  516. /*0x61*/ 0, 0, /*0x62*/ 0, 0, /*0x63*/ 0, 0, /*0x64*/ 0, 0,
  517. /*0x65*/ 0, 0, /*0x66*/ 0, 0, /*0x67*/ 0, 0, /*0x68*/ 0, 0,
  518. /*0x69*/ 0, 0, /*0x6a*/ 0, 0, /*0x6b*/ 0, 0, /*0x6c*/ 0, 0,
  519. /*0x6d*/ 0, 0, /*0x6e*/ 0, 0, /*0x6f*/ 0, 0, /*0x70*/ 0, 0,
  520. /*0x71*/ 0, 0, /*0x72*/ 0, 0, /*0x73*/ 0, 0, /*0x74*/ 0, 0,
  521. /*0x75*/ 0, 0, /*0x76*/ 0, 0, /*0x77*/ 0, 0, /*0x78*/ 0, 0,
  522. /*0x79*/ 0, 0, /*0x7a*/ 0, 0, /*0x7b*/ 0, 0, /*0x7c*/ 0, 0,
  523. /*0x7d*/ 0, 0, /*0x7e*/ 0, 0, /*0x7f*/ 0, 0,
  524. ];