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

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