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 19KB

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