Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keyboard.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keyboard.h"
  15. #include "matrix.h"
  16. #include "keymap.h"
  17. #include "host.h"
  18. #include "led.h"
  19. #include "keycode.h"
  20. #include "timer.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "command.h"
  24. #include "util.h"
  25. #ifdef MOUSEKEY_ENABLE
  26. #include "mousekey.h"
  27. #endif
  28. #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
  29. #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
  30. #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
  31. #define LAYER_DELAY 250
  32. typedef enum keykind {
  33. NONE,
  34. FN_DOWN, FN_UP,
  35. FNK_DOWN, FNK_UP,
  36. KEY_DOWN, KEY_UP,
  37. MOD_DOWN, MOD_UP,
  38. } keykind_t;
  39. typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
  40. #ifdef KEYMAP_DEFAULT_LAYER
  41. uint8_t default_layer = KEYMAP_DEFAULT_LAYER;
  42. uint8_t current_layer = KEYMAP_DEFAULT_LAYER;
  43. #else
  44. uint8_t default_layer = 0;
  45. uint8_t current_layer = 0;
  46. #endif
  47. /* keyboard internal states */
  48. static kbdstate_t kbdstate = IDLE;
  49. static uint8_t fn_state_bits = 0;
  50. static keyrecord_t delayed_fn;
  51. static keyrecord_t waiting_key;
  52. static const char *state_str(kbdstate_t state)
  53. {
  54. if (state == IDLE) return PSTR("IDLE");
  55. if (state == DELAYING) return PSTR("DELAYING");
  56. if (state == WAITING) return PSTR("WAITING");
  57. if (state == PRESSING) return PSTR("PRESSING");
  58. return PSTR("UNKNOWN");
  59. }
  60. static inline keykind_t get_keykind(uint8_t code, bool pressed)
  61. {
  62. if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP);
  63. if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP);
  64. if IS_FN(code) {
  65. if (keymap_fn_keycode(FN_INDEX(code)))
  66. return (pressed ? FNK_DOWN : FNK_UP);
  67. else
  68. return (pressed ? FN_DOWN : FN_UP);
  69. }
  70. if IS_MOUSEKEY(code) return (pressed ? KEY_DOWN : KEY_UP);
  71. if IS_SYSTEM(code) return (pressed ? KEY_DOWN : KEY_UP);
  72. if IS_CONSUMER(code) return (pressed ? KEY_DOWN : KEY_UP);
  73. return NONE;
  74. }
  75. static void clear_keyboard(void)
  76. {
  77. host_clear_keys();
  78. host_clear_mods();
  79. host_send_keyboard_report();
  80. host_system_send(0);
  81. host_consumer_send(0);
  82. #ifdef MOUSEKEY_ENABLE
  83. mousekey_clear();
  84. mousekey_send();
  85. #endif
  86. }
  87. static void clear_keyboard_but_mods(void)
  88. {
  89. host_clear_keys();
  90. host_send_keyboard_report();
  91. host_system_send(0);
  92. host_consumer_send(0);
  93. #ifdef MOUSEKEY_ENABLE
  94. mousekey_clear();
  95. mousekey_send();
  96. #endif
  97. }
  98. static bool anykey_sent_to_host(void)
  99. {
  100. return (host_has_anykey() || host_mouse_in_use() ||
  101. host_last_sysytem_report() || host_last_consumer_report());
  102. }
  103. static void layer_switch_on(uint8_t code)
  104. {
  105. if (!IS_FN(code)) return;
  106. fn_state_bits |= FN_BIT(code);
  107. uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer);
  108. if (current_layer != new_layer) {
  109. Kdebug("Layer Switch(on): "); Kdebug_hex(current_layer);
  110. Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
  111. clear_keyboard_but_mods();
  112. current_layer = new_layer;
  113. }
  114. }
  115. static bool layer_switch_off(uint8_t code)
  116. {
  117. if (!IS_FN(code)) return false;
  118. fn_state_bits &= ~FN_BIT(code);
  119. uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer);
  120. if (current_layer != new_layer) {
  121. Kdebug("Layer Switch(off): "); Kdebug_hex(current_layer);
  122. Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
  123. clear_keyboard_but_mods();
  124. current_layer = new_layer;
  125. return true;
  126. }
  127. return false;
  128. }
  129. static void register_code(uint8_t code)
  130. {
  131. if IS_KEY(code) {
  132. if (!command_proc(code)) {
  133. host_add_key(code);
  134. host_send_keyboard_report();
  135. }
  136. }
  137. else if IS_MOD(code) {
  138. host_add_mod_bit(MOD_BIT(code));
  139. host_send_keyboard_report();
  140. }
  141. else if IS_FN(code) {
  142. if (!command_proc(keymap_fn_keycode(FN_INDEX(code)))) {
  143. host_add_key(keymap_fn_keycode(FN_INDEX(code)));
  144. host_send_keyboard_report();
  145. }
  146. }
  147. else if IS_MOUSEKEY(code) {
  148. #ifdef MOUSEKEY_ENABLE
  149. mousekey_on(code);
  150. mousekey_send();
  151. #endif
  152. }
  153. else if IS_CONSUMER(code) {
  154. uint16_t usage = 0;
  155. switch (code) {
  156. case KC_AUDIO_MUTE:
  157. usage = AUDIO_MUTE;
  158. break;
  159. case KC_AUDIO_VOL_UP:
  160. usage = AUDIO_VOL_UP;
  161. break;
  162. case KC_AUDIO_VOL_DOWN:
  163. usage = AUDIO_VOL_DOWN;
  164. break;
  165. case KC_MEDIA_NEXT_TRACK:
  166. usage = TRANSPORT_NEXT_TRACK;
  167. break;
  168. case KC_MEDIA_PREV_TRACK:
  169. usage = TRANSPORT_PREV_TRACK;
  170. break;
  171. case KC_MEDIA_STOP:
  172. usage = TRANSPORT_STOP;
  173. break;
  174. case KC_MEDIA_PLAY_PAUSE:
  175. usage = TRANSPORT_PLAY_PAUSE;
  176. break;
  177. case KC_MEDIA_SELECT:
  178. usage = AL_CC_CONFIG;
  179. break;
  180. case KC_MAIL:
  181. usage = AL_EMAIL;
  182. break;
  183. case KC_CALCULATOR:
  184. usage = AL_CALCULATOR;
  185. break;
  186. case KC_MY_COMPUTER:
  187. usage = AL_LOCAL_BROWSER;
  188. break;
  189. case KC_WWW_SEARCH:
  190. usage = AC_SEARCH;
  191. break;
  192. case KC_WWW_HOME:
  193. usage = AC_HOME;
  194. break;
  195. case KC_WWW_BACK:
  196. usage = AC_BACK;
  197. break;
  198. case KC_WWW_FORWARD:
  199. usage = AC_FORWARD;
  200. break;
  201. case KC_WWW_STOP:
  202. usage = AC_STOP;
  203. break;
  204. case KC_WWW_REFRESH:
  205. usage = AC_REFRESH;
  206. break;
  207. case KC_WWW_FAVORITES:
  208. usage = AC_BOOKMARKS;
  209. break;
  210. }
  211. host_consumer_send(usage);
  212. }
  213. else if IS_SYSTEM(code) {
  214. uint16_t usage = 0;
  215. switch (code) {
  216. case KC_SYSTEM_POWER:
  217. usage = SYSTEM_POWER_DOWN;
  218. break;
  219. case KC_SYSTEM_SLEEP:
  220. usage = SYSTEM_SLEEP;
  221. break;
  222. case KC_SYSTEM_WAKE:
  223. usage = SYSTEM_WAKE_UP;
  224. break;
  225. }
  226. host_system_send(usage);
  227. }
  228. }
  229. static void unregister_code(uint8_t code)
  230. {
  231. if IS_KEY(code) {
  232. host_del_key(code);
  233. host_send_keyboard_report();
  234. }
  235. else if IS_MOD(code) {
  236. host_del_mod_bit(MOD_BIT(code));
  237. host_send_keyboard_report();
  238. }
  239. else if IS_FN(code) {
  240. host_del_key(keymap_fn_keycode(FN_INDEX(code)));
  241. host_send_keyboard_report();
  242. }
  243. else if IS_MOUSEKEY(code) {
  244. #ifdef MOUSEKEY_ENABLE
  245. mousekey_off(code);
  246. mousekey_send();
  247. #endif
  248. }
  249. else if IS_CONSUMER(code) {
  250. host_consumer_send(0x0000);
  251. }
  252. else if IS_SYSTEM(code) {
  253. host_system_send(0x0000);
  254. }
  255. }
  256. /*
  257. *
  258. * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
  259. * -----------+------------------------------------------------------------------
  260. * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
  261. * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
  262. * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
  263. * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
  264. * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
  265. * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
  266. * |
  267. * Delay |- - IDLE(L+) IDLE(L+,Ps)
  268. * Magic Key |COMMAND*5
  269. *
  270. * *1: ignore Fn if other key is down.
  271. * *2: register Fnk if any key is pressing
  272. * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
  273. * *4: if no keys registered to host
  274. * *5: unregister all keys
  275. * *6: only if no keys down
  276. * *7: ignore Fn because Fnk key and stored key are down.
  277. * *8: move to IDLE if layer switch(off) occurs, else stay at current state
  278. * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
  279. * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
  280. *
  281. * States:
  282. * IDLE: No key is down except modifiers
  283. * DELAYING: delay layer switch after pressing Fn with alt keycode
  284. * WAITING: key is pressed during DELAYING
  285. *
  286. * Events:
  287. * Fn: Fn key without alternative keycode
  288. * Fnk: Fn key with alternative keycode
  289. * -: ignore
  290. * Delay: layer switch delay term is elapsed
  291. *
  292. * Actions:
  293. * Rk: register key
  294. * Uk: unregister key
  295. * Rf: register Fn(alt keycode)
  296. * Uf: unregister Fn(alt keycode)
  297. * Rs: register stored key
  298. * Us: unregister stored key
  299. * Sk: Store key(waiting Key)
  300. * Sf: Store Fn(delayed Fn)
  301. * Ps: Process stored key
  302. * Ps: Process key
  303. * Is: Interpret stored keys in current layer
  304. * L+: Switch to new layer(*unregister* all keys but modifiers)
  305. * L-: Switch back to last layer(*unregister* all keys but modifiers)
  306. * Ld: Switch back to default layer(*unregister* all keys but modifiers)
  307. */
  308. #define NEXT(state) do { \
  309. Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
  310. kbdstate = state; \
  311. Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
  312. } while (0)
  313. static inline void process_key(keyevent_t event)
  314. {
  315. uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col);
  316. keykind_t kind = get_keykind(code, event.pressed);
  317. uint8_t tmp_mods;
  318. Kdebug("state: "); Kdebug_P(state_str(kbdstate));
  319. Kdebug(" kind: "); Kdebug_hex(kind);
  320. Kdebug(" code: "); Kdebug_hex(code);
  321. if (event.pressed) { Kdebug("d"); } else { Kdebug("u"); }
  322. Kdebug("\n");
  323. switch (kbdstate) {
  324. case IDLE:
  325. switch (kind) {
  326. case FN_DOWN:
  327. layer_switch_on(code);
  328. break;
  329. case FN_UP:
  330. layer_switch_off(code);
  331. break;
  332. case FNK_DOWN:
  333. // repeat Fn alt key when press Fn key down, up then down again quickly
  334. if (KEYEQ(delayed_fn.event.key, event.key) &&
  335. timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
  336. register_code(code);
  337. NEXT(PRESSING);
  338. } else {
  339. delayed_fn = (keyrecord_t) {
  340. .event = event,
  341. .code = code,
  342. .mods = keyboard_report->mods,
  343. .time = timer_read()
  344. };
  345. NEXT(DELAYING);
  346. }
  347. break;
  348. case FNK_UP:
  349. layer_switch_off(code);
  350. break;
  351. case KEY_DOWN:
  352. register_code(code);
  353. NEXT(PRESSING);
  354. break;
  355. case MOD_DOWN:
  356. register_code(code);
  357. break;
  358. case KEY_UP:
  359. case MOD_UP:
  360. unregister_code(code);
  361. break;
  362. default:
  363. break;
  364. }
  365. break;
  366. case PRESSING:
  367. switch (kind) {
  368. case FN_DOWN:
  369. // ignored when any key is pressed
  370. break;
  371. case FN_UP:
  372. if (layer_switch_off(code))
  373. NEXT(IDLE);
  374. break;
  375. case FNK_DOWN:
  376. register_code(code);
  377. break;
  378. case FNK_UP:
  379. if (layer_switch_off(code)) {
  380. NEXT(IDLE);
  381. } else {
  382. unregister_code(code);
  383. if (!anykey_sent_to_host())
  384. NEXT(IDLE);
  385. }
  386. break;
  387. case KEY_DOWN:
  388. case MOD_DOWN:
  389. register_code(code);
  390. break;
  391. case KEY_UP:
  392. case MOD_UP:
  393. unregister_code(code);
  394. if (!anykey_sent_to_host())
  395. NEXT(IDLE);
  396. break;
  397. default:
  398. break;
  399. }
  400. break;
  401. case DELAYING:
  402. switch (kind) {
  403. case FN_DOWN:
  404. case FNK_DOWN:
  405. case KEY_DOWN:
  406. waiting_key = (keyrecord_t) {
  407. .event = event,
  408. .code = code,
  409. .mods = keyboard_report->mods,
  410. .time = timer_read()
  411. };
  412. NEXT(WAITING);
  413. break;
  414. case MOD_DOWN:
  415. register_code(code);
  416. break;
  417. case FN_UP:
  418. if (layer_switch_off(code))
  419. NEXT(IDLE);
  420. break;
  421. case FNK_UP:
  422. if (code == delayed_fn.code) {
  423. // type Fn with alt keycode
  424. // restore the mod status at the time of pressing Fn key
  425. tmp_mods = keyboard_report->mods;
  426. host_set_mods(delayed_fn.mods);
  427. register_code(delayed_fn.code);
  428. unregister_code(delayed_fn.code);
  429. host_set_mods(tmp_mods);
  430. NEXT(IDLE);
  431. } else {
  432. if (layer_switch_off(code))
  433. NEXT(IDLE);
  434. }
  435. break;
  436. case KEY_UP:
  437. case MOD_UP:
  438. unregister_code(code);
  439. break;
  440. default:
  441. break;
  442. }
  443. break;
  444. case WAITING:
  445. switch (kind) {
  446. case FN_DOWN:
  447. case FNK_DOWN:
  448. case KEY_DOWN:
  449. tmp_mods = keyboard_report->mods;
  450. host_set_mods(delayed_fn.mods);
  451. register_code(delayed_fn.code);
  452. host_set_mods(waiting_key.mods);
  453. register_code(waiting_key.code);
  454. host_set_mods(tmp_mods);
  455. if (kind == FN_DOWN) {
  456. // ignore Fn
  457. } else if (kind == FNK_DOWN) {
  458. register_code(code);
  459. } else if (kind == KEY_DOWN) {
  460. register_code(code);
  461. }
  462. NEXT(IDLE);
  463. break;
  464. case MOD_DOWN:
  465. register_code(code);
  466. break;
  467. case FN_UP:
  468. if (layer_switch_off(code))
  469. NEXT(IDLE);
  470. break;
  471. case FNK_UP:
  472. if (code == delayed_fn.code) {
  473. // alt down, key down, alt up
  474. tmp_mods = keyboard_report->mods;
  475. host_set_mods(delayed_fn.mods);
  476. register_code(delayed_fn.code);
  477. host_set_mods(waiting_key.mods);
  478. register_code(waiting_key.code);
  479. unregister_code(delayed_fn.code);
  480. host_set_mods(tmp_mods);
  481. NEXT(IDLE);
  482. } else {
  483. if (layer_switch_off(code))
  484. NEXT(IDLE);
  485. }
  486. break;
  487. case KEY_UP:
  488. if (code == waiting_key.code) {
  489. layer_switch_on(delayed_fn.code);
  490. NEXT(IDLE);
  491. // process waiting_key
  492. tmp_mods = keyboard_report->mods;
  493. host_set_mods(waiting_key.mods);
  494. process_key(waiting_key.event);
  495. host_set_mods(tmp_mods);
  496. process_key(event);
  497. } else {
  498. unregister_code(code);
  499. }
  500. break;
  501. case MOD_UP:
  502. unregister_code(code);
  503. break;
  504. default:
  505. break;
  506. }
  507. break;
  508. }
  509. }
  510. void keyboard_init(void)
  511. {
  512. debug_keyboard = true;
  513. timer_init();
  514. matrix_init();
  515. #ifdef PS2_MOUSE_ENABLE
  516. ps2_mouse_init();
  517. #endif
  518. }
  519. void keyboard_task(void)
  520. {
  521. static matrix_row_t matrix_prev[MATRIX_ROWS];
  522. static uint8_t led_status = 0;
  523. matrix_row_t matrix_row = 0;
  524. matrix_row_t matrix_change = 0;
  525. matrix_scan();
  526. for (int r = 0; r < MATRIX_ROWS; r++) {
  527. matrix_row = matrix_get_row(r);
  528. matrix_change = matrix_row ^ matrix_prev[r];
  529. if (matrix_change) {
  530. if (debug_matrix) matrix_print();
  531. for (int c = 0; c < MATRIX_COLS; c++) {
  532. if (matrix_change & (1<<c)) {
  533. process_key((keyevent_t){
  534. .key = (key_t){ .row = r, .col = c },
  535. .pressed = (matrix_row & (1<<c))
  536. });
  537. // record a processed key
  538. matrix_prev[r] ^= (1<<c);
  539. // process a key per task call
  540. goto MATRIX_LOOP_END;
  541. }
  542. }
  543. }
  544. }
  545. MATRIX_LOOP_END:
  546. // layer switch when delay term elapses
  547. if (kbdstate == DELAYING || kbdstate == WAITING) {
  548. if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) {
  549. if (kbdstate == DELAYING) {
  550. layer_switch_on(delayed_fn.code);
  551. NEXT(IDLE);
  552. }
  553. if (kbdstate == WAITING) {
  554. layer_switch_on(delayed_fn.code);
  555. NEXT(IDLE);
  556. uint8_t tmp_mods = keyboard_report->mods;
  557. host_set_mods(waiting_key.mods);
  558. process_key(waiting_key.event);
  559. host_set_mods(tmp_mods);
  560. }
  561. }
  562. }
  563. #ifdef MOUSEKEY_ENABLE
  564. // mousekey repeat & acceleration
  565. mousekey_task();
  566. #endif
  567. // FAIL SAFE: clear all key if no key down
  568. if (matrix_change) {
  569. matrix_row_t is_matrix_on = 0;
  570. for (int r = 0; r < MATRIX_ROWS; r++) {
  571. is_matrix_on |= matrix_get_row(r);
  572. }
  573. if (!is_matrix_on) {
  574. Kdebug("FAIL SAFE: clear all keys(default layer).\n");
  575. clear_keyboard();
  576. current_layer = default_layer;
  577. }
  578. }
  579. // update LED
  580. if (led_status != host_keyboard_leds()) {
  581. led_status = host_keyboard_leds();
  582. keyboard_set_leds(led_status);
  583. }
  584. return;
  585. }
  586. void keyboard_set_leds(uint8_t leds)
  587. {
  588. led_set(leds);
  589. }