Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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