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

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