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.

action.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #include "host.h"
  2. #include "timer.h"
  3. #include "keymap.h"
  4. #include "keycode.h"
  5. #include "keyboard.h"
  6. #include "mousekey.h"
  7. #include "command.h"
  8. #include "util.h"
  9. #include "debug.h"
  10. #include "action.h"
  11. static void process(keyrecord_t *record);
  12. // TODO
  13. /* layer */
  14. uint8_t default_layer = 0;
  15. uint8_t current_layer = 0;
  16. /* tap term(ms) */
  17. #define TAP_TERM 200
  18. /* This counts up when tap occurs */
  19. uint8_t tap_count = 0;
  20. keyevent_t tapping_event = {};
  21. keyrecord_t tapping_key = {};
  22. /* TAPPING: This indicates that whether tap or not is not decided yet. */
  23. // NOTE: keyevent_t.time 0 means no event.
  24. #define IS_TAPPING() (tapping_key.event.time != 0)
  25. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  26. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  27. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  28. #define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM)
  29. /* waiting keys buffer */
  30. #define WAITING_BUFFER_SIZE 8
  31. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  32. /* point to empty cell to enq */
  33. static uint8_t waiting_buffer_head = 0;
  34. /* point to the oldest data cell to deq */
  35. static uint8_t waiting_buffer_tail = 0;
  36. static bool waiting_buffer_enq(keyrecord_t record)
  37. {
  38. if (IS_NOEVENT(record.event)) {
  39. return true;
  40. }
  41. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  42. debug("waiting_buffer_enq: Over flow.\n");
  43. return false;
  44. }
  45. debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = ");
  46. debug_hex16(record.event.key.raw); debug("\n");
  47. waiting_buffer[waiting_buffer_head] = record;
  48. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  49. return true;
  50. }
  51. static keyrecord_t waiting_buffer_deq(void)
  52. {
  53. if (waiting_buffer_head == waiting_buffer_tail) {
  54. return (keyrecord_t){};
  55. }
  56. uint8_t last_tail = waiting_buffer_tail;
  57. waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
  58. return waiting_buffer[last_tail];
  59. }
  60. static bool waiting_buffer_is_empty(void)
  61. {
  62. return (waiting_buffer_head == waiting_buffer_tail);
  63. }
  64. static void waiting_buffer_clear(void)
  65. {
  66. waiting_buffer_head = 0;
  67. waiting_buffer_tail = 0;
  68. }
  69. static bool waiting_buffer_typed(keyevent_t event)
  70. {
  71. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  72. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. static bool waiting_buffer_has_anykey_pressed(void)
  79. {
  80. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  81. if (waiting_buffer[i].event.pressed) return true;
  82. }
  83. return false;
  84. }
  85. static void waiting_buffer_process(void)
  86. {
  87. }
  88. /*
  89. * Rule to judge tap:
  90. * Tap key is typed(pressed and released) within TAP_TERM
  91. * without interfaring by typing other key.
  92. */
  93. /* return true when key event is processed. */
  94. static bool process_tap(keyrecord_t *keyp)
  95. {
  96. keyevent_t event = keyp->event;
  97. // if tapping
  98. if (IS_TAPPING_PRESSED()) {
  99. if (WITHIN_TAP_TERM(event)) {
  100. if (tapping_key.tap_count == 0) {
  101. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  102. // first tap!
  103. debug("Tapping: First tap.\n");
  104. tapping_key.tap_count = 1;
  105. process(&tapping_key);
  106. // enqueue
  107. keyp->tap_count = tapping_key.tap_count;
  108. return false;
  109. } else if (!event.pressed && waiting_buffer_typed(event)) {
  110. // other key typed. not tap.
  111. debug("Tapping: End(No tap. Interfered by typing key).\n");
  112. process(&tapping_key);
  113. tapping_key = (keyrecord_t){};
  114. // enqueue
  115. return false;
  116. } else {
  117. // other key events shall be stored till tapping state settles.
  118. return false;
  119. }
  120. } else {
  121. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  122. keyp->tap_count = tapping_key.tap_count;
  123. debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
  124. tapping_key = *keyp;
  125. return false;
  126. }
  127. else if (is_tap_key(keyp->event.key) && event.pressed) {
  128. debug("Tapping: Start with forcing to release last tap.\n");
  129. process(&(keyrecord_t){
  130. .tap_count = tapping_key.tap_count,
  131. .event.key = tapping_key.event.key,
  132. .event.time = event.time,
  133. .event.pressed = false
  134. });
  135. tapping_key = *keyp;
  136. return false;
  137. }
  138. else {
  139. if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
  140. process(keyp);
  141. return true;
  142. }
  143. }
  144. }
  145. // not within TAP_TERM
  146. else {
  147. if (tapping_key.tap_count == 0) {
  148. // timeout. not tap.
  149. debug("Tapping: End. Not tap(time out).\n");
  150. process(&tapping_key);
  151. tapping_key = (keyrecord_t){};
  152. return false;
  153. } else {
  154. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  155. debug("Tapping: End. tap release.");
  156. keyp->tap_count = tapping_key.tap_count;
  157. process(keyp);
  158. tapping_key = (keyrecord_t){};
  159. return true;
  160. } else {
  161. // other key after tap time out.
  162. process(keyp);
  163. return true;
  164. }
  165. }
  166. }
  167. } else if (IS_TAPPING_RELEASED()) {
  168. if (WITHIN_TAP_TERM(event)) {
  169. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  170. // sequential tap.
  171. keyp->tap_count = tapping_key.tap_count + 1;
  172. debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
  173. process(keyp);
  174. tapping_key = *keyp;
  175. return true;
  176. } else if (event.pressed && is_tap_key(event.key)) {
  177. // Sequential tap can be interfered with other tap key.
  178. debug("Tapping: Start with interfering other tap.\n");
  179. tapping_key = *keyp;
  180. return true;
  181. } else {
  182. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  183. process(keyp);
  184. return true;
  185. }
  186. } else {
  187. // timeout. no sequential tap.
  188. debug("Tapping: End(Time out after releasing last tap).\n");
  189. tapping_key = (keyrecord_t){};
  190. process(keyp);
  191. return true;
  192. }
  193. } else {
  194. if (event.pressed && is_tap_key(event.key)) {
  195. debug("Tapping: Start(Press tap key).\n");
  196. tapping_key = *keyp;
  197. return true;
  198. } else {
  199. process(keyp);
  200. return true;
  201. }
  202. }
  203. }
  204. void action_exec(keyevent_t event)
  205. {
  206. if (!IS_NOEVENT(event)) {
  207. debug("event: ");
  208. debug_hex16(event.time); debug(": ");
  209. debug_hex16(event.key.raw);
  210. debug("[");
  211. if (event.pressed) debug("down"); else debug("up");
  212. debug("]\n");
  213. }
  214. keyrecord_t record = { .event = event };
  215. // pre-process on tapping
  216. if (process_tap(&record)) {
  217. if (!IS_NOEVENT(record.event)) debug("processed.\n");
  218. } else {
  219. if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
  220. if (!waiting_buffer_enq(record)) {
  221. // clear all in case of overflow.
  222. clear_keyboard();
  223. waiting_buffer_clear();
  224. tapping_key = (keyrecord_t){};
  225. }
  226. }
  227. // TODO: need to process every time?
  228. // process waiting_buffer
  229. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  230. if (process_tap(&waiting_buffer[waiting_buffer_tail])) {
  231. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  232. debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
  233. } else {
  234. break;
  235. }
  236. }
  237. }
  238. static void process(keyrecord_t *record)
  239. {
  240. // TODO: use record
  241. keyevent_t event = record->event;
  242. uint8_t tap_count = record->tap_count;
  243. if (IS_NOEVENT(event)) { return; }
  244. action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
  245. debug("action: "); debug_hex16(action.code);
  246. if (event.pressed) debug("[down]\n"); else debug("[up]\n");
  247. switch (action.kind.id) {
  248. /* Key and Mods */
  249. case ACT_LMODS:
  250. // |pressed |released
  251. // --------------+---------------------------------+------------
  252. // key |down(key) |up(key)
  253. // mods |add(mods) |del(mods)
  254. // key with mods |add(mods), down(key), unset(mods)|up(key)
  255. if (event.pressed) {
  256. uint8_t tmp_mods = host_get_mods();
  257. if (action.key.mods) {
  258. host_add_mods(action.key.mods);
  259. host_send_keyboard_report();
  260. }
  261. register_code(action.key.code);
  262. if (action.key.mods && action.key.code) {
  263. host_set_mods(tmp_mods);
  264. host_send_keyboard_report();
  265. }
  266. } else {
  267. if (action.key.mods && !action.key.code) {
  268. host_del_mods(action.key.mods);
  269. host_send_keyboard_report();
  270. }
  271. unregister_code(action.key.code);
  272. }
  273. break;
  274. case ACT_RMODS:
  275. // |pressed |released
  276. // --------------+---------------------------------+------------
  277. // key |down(key) |up(key)
  278. // mods |add(mods) |del(mods)
  279. // key with mods |add(mods), down(key), unset(mods)|up(key)
  280. if (event.pressed) {
  281. uint8_t tmp_mods = host_get_mods();
  282. if (action.key.mods) {
  283. host_add_mods(action.key.mods<<4);
  284. host_send_keyboard_report();
  285. }
  286. register_code(action.key.code);
  287. if (action.key.mods && action.key.code) {
  288. host_set_mods(tmp_mods);
  289. host_send_keyboard_report();
  290. }
  291. } else {
  292. if (action.key.mods && !action.key.code) {
  293. host_del_mods(action.key.mods<<4);
  294. host_send_keyboard_report();
  295. }
  296. unregister_code(action.key.code);
  297. }
  298. break;
  299. case ACT_LMODS_TAP:
  300. case ACT_RMODS_TAP:
  301. {
  302. uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  303. action.key.mods<<4;
  304. if (event.pressed) {
  305. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  306. if (waiting_buffer_has_anykey_pressed()) {
  307. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  308. // ad hoc: set 0 to cancel tap
  309. record->tap_count = 0;
  310. add_mods(tmp_mods);
  311. } else {
  312. debug("MODS_TAP: Tap: register_code\n");
  313. register_code(action.key.code);
  314. }
  315. } else {
  316. debug("MODS_TAP: No tap: add_mods\n");
  317. add_mods(tmp_mods);
  318. }
  319. } else {
  320. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  321. debug("MODS_TAP: Tap: unregister_code\n");
  322. unregister_code(action.key.code);
  323. } else {
  324. debug("MODS_TAP: No tap: add_mods\n");
  325. del_mods(tmp_mods);
  326. }
  327. }
  328. }
  329. break;
  330. /* other HID usage */
  331. case ACT_USAGE:
  332. #ifdef EXTRAKEY_ENABLE
  333. switch (action.usage.page) {
  334. case ACTION_USAGE_PAGE_SYSTEM:
  335. if (event.pressed) {
  336. host_system_send(action.usage.code);
  337. } else {
  338. host_system_send(0);
  339. }
  340. break;
  341. case ACTION_USAGE_PAGE_CONSUMER:
  342. if (event.pressed) {
  343. host_consumer_send(action.usage.code);
  344. } else {
  345. host_consumer_send(0);
  346. }
  347. break;
  348. }
  349. #endif
  350. break;
  351. /* Mouse key */
  352. case ACT_MOUSEKEY:
  353. #ifdef MOUSEKEY_ENABLE
  354. if (event.pressed) {
  355. mousekey_on(action.key.code);
  356. mousekey_send();
  357. } else {
  358. mousekey_off(action.key.code);
  359. mousekey_send();
  360. }
  361. #endif
  362. break;
  363. /* Layer key */
  364. case ACT_LAYER_PRESSED:
  365. // layer action when pressed
  366. switch (action.layer.code) {
  367. case 0x00:
  368. if (event.pressed) {
  369. layer_switch(action.layer.opt);
  370. }
  371. break;
  372. case 0xF0:
  373. // TODO: tap toggle
  374. break;
  375. case 0xFF:
  376. if (event.pressed) {
  377. default_layer = action.layer.opt;
  378. layer_switch(default_layer);
  379. }
  380. break;
  381. default:
  382. // with tap key
  383. if (event.pressed) {
  384. if (IS_TAPPING_KEY(event.key)) {
  385. if (tap_count > 0) {
  386. debug("LAYER_PRESSED: Tap: register_code\n");
  387. register_code(action.layer.code);
  388. } else {
  389. debug("LAYER_PRESSED: No tap: layer_switch\n");
  390. layer_switch(action.layer.opt);
  391. }
  392. } else {
  393. // TODO: while other key tapping
  394. debug("LAYER_PRESSED: No tap: layer_switch\n");
  395. layer_switch(action.layer.opt);
  396. }
  397. /*
  398. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  399. debug("LAYER_PRESSED: Tap: register_code\n");
  400. register_code(action.layer.code);
  401. } else {
  402. debug("LAYER_PRESSED: No tap: layer_switch\n");
  403. layer_switch(action.layer.opt);
  404. }
  405. */
  406. } else {
  407. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  408. debug("LAYER_PRESSED: Tap: unregister_code\n");
  409. unregister_code(action.layer.code);
  410. } else {
  411. debug("LAYER_PRESSED: No tap: NO ACTION\n");
  412. }
  413. }
  414. break;
  415. }
  416. break;
  417. case ACT_LAYER_RELEASED:
  418. switch (action.layer.code) {
  419. case 0x00:
  420. if (!event.pressed) {
  421. layer_switch(action.layer.opt);
  422. }
  423. break;
  424. case 0xF0:
  425. // Ignored. LAYER_RELEASED with tap toggle is invalid action.
  426. break;
  427. case 0xFF:
  428. if (!event.pressed) {
  429. default_layer = action.layer.opt;
  430. layer_switch(default_layer);
  431. }
  432. break;
  433. default:
  434. // Ignored. LAYER_RELEASED with tap key is invalid action.
  435. break;
  436. }
  437. break;
  438. case ACT_LAYER_BIT:
  439. switch (action.layer.code) {
  440. case 0x00:
  441. if (event.pressed) {
  442. layer_switch(current_layer | action.layer.opt);
  443. } else {
  444. layer_switch(current_layer & ~action.layer.opt);
  445. }
  446. break;
  447. case 0xF0:
  448. // TODO: tap toggle
  449. break;
  450. case 0xFF:
  451. // change default layer
  452. if (event.pressed) {
  453. default_layer = current_layer | action.layer.opt;
  454. layer_switch(default_layer);
  455. } else {
  456. default_layer = current_layer & ~action.layer.opt;
  457. layer_switch(default_layer);
  458. }
  459. break;
  460. default:
  461. // with tap key
  462. if (event.pressed) {
  463. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  464. debug("LAYER_BIT: Tap: register_code\n");
  465. register_code(action.layer.code);
  466. } else {
  467. debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
  468. layer_switch(current_layer | action.layer.opt);
  469. }
  470. } else {
  471. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  472. debug("LAYER_BIT: Tap: unregister_code\n");
  473. unregister_code(action.layer.code);
  474. } else {
  475. debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
  476. layer_switch(current_layer & ~action.layer.opt);
  477. }
  478. }
  479. break;
  480. }
  481. case ACT_LAYER_EXT:
  482. switch (action.layer.opt) {
  483. case 0x00:
  484. // set default layer when pressed
  485. switch (action.layer.code) {
  486. case 0x00:
  487. if (event.pressed) {
  488. layer_switch(default_layer);
  489. }
  490. break;
  491. case 0xF0:
  492. // TODO: tap toggle
  493. break;
  494. case 0xFF:
  495. if (event.pressed) {
  496. default_layer = current_layer;
  497. layer_switch(default_layer);
  498. }
  499. break;
  500. default:
  501. // TODO: tap key
  502. break;
  503. }
  504. break;
  505. case 0x01:
  506. // set default layer when released
  507. switch (action.layer.code) {
  508. case 0x00:
  509. if (!event.pressed) {
  510. layer_switch(default_layer);
  511. }
  512. break;
  513. case 0xFF:
  514. if (!event.pressed) {
  515. default_layer = current_layer;
  516. layer_switch(default_layer);
  517. }
  518. break;
  519. case 0xF0:
  520. default:
  521. // Ignore tap.
  522. if (!event.pressed) {
  523. layer_switch(default_layer);
  524. }
  525. break;
  526. }
  527. break;
  528. }
  529. break;
  530. /* Extentions */
  531. case ACT_MACRO:
  532. break;
  533. case ACT_COMMAND:
  534. break;
  535. case ACT_FUNCTION:
  536. action_call_function(event, action.func.id);
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. /*
  543. * Utilities for actions.
  544. */
  545. void register_code(uint8_t code)
  546. {
  547. if (code == KC_NO) {
  548. return;
  549. }
  550. else if IS_KEY(code) {
  551. // TODO: should push command_proc out of this block?
  552. if (!command_proc(code)) {
  553. host_add_key(code);
  554. host_send_keyboard_report();
  555. }
  556. }
  557. else if IS_MOD(code) {
  558. host_add_mods(MOD_BIT(code));
  559. host_send_keyboard_report();
  560. }
  561. }
  562. void unregister_code(uint8_t code)
  563. {
  564. if IS_KEY(code) {
  565. host_del_key(code);
  566. host_send_keyboard_report();
  567. }
  568. else if IS_MOD(code) {
  569. host_del_mods(MOD_BIT(code));
  570. host_send_keyboard_report();
  571. }
  572. }
  573. void add_mods(uint8_t mods)
  574. {
  575. if (mods) {
  576. host_add_mods(mods);
  577. host_send_keyboard_report();
  578. }
  579. }
  580. void del_mods(uint8_t mods)
  581. {
  582. if (mods) {
  583. host_del_mods(mods);
  584. host_send_keyboard_report();
  585. }
  586. }
  587. void set_mods(uint8_t mods)
  588. {
  589. host_set_mods(mods);
  590. host_send_keyboard_report();
  591. }
  592. void clear_keyboard(void)
  593. {
  594. host_clear_mods();
  595. clear_keyboard_but_mods();
  596. }
  597. void clear_keyboard_but_mods(void)
  598. {
  599. host_clear_keys();
  600. host_send_keyboard_report();
  601. #ifdef MOUSEKEY_ENABLE
  602. mousekey_clear();
  603. mousekey_send();
  604. #endif
  605. #ifdef EXTRAKEY_ENABLE
  606. host_system_send(0);
  607. host_consumer_send(0);
  608. #endif
  609. }
  610. bool sending_anykey(void)
  611. {
  612. return (host_has_anykey() || host_mouse_in_use() ||
  613. host_last_sysytem_report() || host_last_consumer_report());
  614. }
  615. void layer_switch(uint8_t new_layer)
  616. {
  617. if (current_layer != new_layer) {
  618. debug("Layer Switch: "); debug_hex(current_layer);
  619. debug(" -> "); debug_hex(new_layer); debug("\n");
  620. current_layer = new_layer;
  621. clear_keyboard_but_mods(); // To avoid stuck keys
  622. // TODO: update mods with full scan of matrix? if modifier changes between layers
  623. }
  624. }
  625. bool is_tap_key(key_t key)
  626. {
  627. action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
  628. switch (action.kind.id) {
  629. case ACT_LMODS_TAP:
  630. case ACT_RMODS_TAP:
  631. return true;
  632. case ACT_LAYER_PRESSED:
  633. case ACT_LAYER_BIT:
  634. switch (action.layer.code) {
  635. case 0x00:
  636. case 0xF1 ... 0xFF:
  637. return false;
  638. case 0xF0:
  639. default:
  640. return true;
  641. }
  642. return false;
  643. case ACT_FUNCTION:
  644. if (action.func.opt & 0x1) {
  645. return true;
  646. }
  647. return false;
  648. }
  649. return false;
  650. }