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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. /* Oneshot modifier
  89. *
  90. * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
  91. * Solution: Oneshot modifier have its effect on only one key coming next.
  92. * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
  93. *
  94. * Hold: works as normal modifier.
  95. * Tap: one shot modifier.
  96. * 2 Tap: cancel one shot modifier.
  97. * 5-Tap: toggles enable/disable oneshot feature.
  98. */
  99. static struct {
  100. uint8_t mods;
  101. uint8_t time;
  102. bool ready;
  103. bool disabled;
  104. } oneshot_state;
  105. static void oneshot_start(uint8_t mods, uint16_t time)
  106. {
  107. oneshot_state.mods = mods;
  108. oneshot_state.time = time;
  109. oneshot_state.ready = true;
  110. }
  111. static void oneshot_cancel(void)
  112. {
  113. oneshot_state.mods = 0;
  114. oneshot_state.time = 0;
  115. oneshot_state.ready = false;
  116. }
  117. static void oneshot_toggle(void)
  118. {
  119. oneshot_state.disabled = !oneshot_state.disabled;
  120. }
  121. /*
  122. * Rule to judge tap:
  123. * Tap key is typed(pressed and released) within TAP_TERM
  124. * without interfaring by typing other key.
  125. */
  126. /* return true when key event is processed. */
  127. static bool process_tap(keyrecord_t *keyp)
  128. {
  129. keyevent_t event = keyp->event;
  130. // if tapping
  131. if (IS_TAPPING_PRESSED()) {
  132. if (WITHIN_TAP_TERM(event)) {
  133. if (tapping_key.tap_count == 0) {
  134. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  135. // first tap!
  136. debug("Tapping: First tap.\n");
  137. tapping_key.tap_count = 1;
  138. process(&tapping_key);
  139. // enqueue
  140. keyp->tap_count = tapping_key.tap_count;
  141. return false;
  142. } else if (!event.pressed && waiting_buffer_typed(event)) {
  143. // other key typed. not tap.
  144. debug("Tapping: End(No tap. Interfered by typing key).\n");
  145. process(&tapping_key);
  146. tapping_key = (keyrecord_t){};
  147. // enqueue
  148. return false;
  149. } else {
  150. // other key events shall be stored till tapping state settles.
  151. return false;
  152. }
  153. } else {
  154. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  155. keyp->tap_count = tapping_key.tap_count;
  156. debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
  157. tapping_key = *keyp;
  158. return false;
  159. }
  160. else if (is_tap_key(keyp->event.key) && event.pressed) {
  161. debug("Tapping: Start with forcing to release last tap.\n");
  162. process(&(keyrecord_t){
  163. .tap_count = tapping_key.tap_count,
  164. .event.key = tapping_key.event.key,
  165. .event.time = event.time,
  166. .event.pressed = false
  167. });
  168. tapping_key = *keyp;
  169. return false;
  170. }
  171. else {
  172. if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
  173. process(keyp);
  174. return true;
  175. }
  176. }
  177. }
  178. // not within TAP_TERM
  179. else {
  180. if (tapping_key.tap_count == 0) {
  181. // timeout. not tap.
  182. debug("Tapping: End. Not tap(time out).\n");
  183. process(&tapping_key);
  184. tapping_key = (keyrecord_t){};
  185. return false;
  186. } else {
  187. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  188. debug("Tapping: End. tap release.");
  189. keyp->tap_count = tapping_key.tap_count;
  190. process(keyp);
  191. tapping_key = (keyrecord_t){};
  192. return true;
  193. } else {
  194. // other key after tap time out.
  195. process(keyp);
  196. return true;
  197. }
  198. }
  199. }
  200. } else if (IS_TAPPING_RELEASED()) {
  201. if (WITHIN_TAP_TERM(event)) {
  202. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  203. // sequential tap.
  204. keyp->tap_count = tapping_key.tap_count + 1;
  205. debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
  206. process(keyp);
  207. tapping_key = *keyp;
  208. return true;
  209. } else if (event.pressed && is_tap_key(event.key)) {
  210. // Sequential tap can be interfered with other tap key.
  211. debug("Tapping: Start with interfering other tap.\n");
  212. tapping_key = *keyp;
  213. return true;
  214. } else {
  215. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  216. process(keyp);
  217. return true;
  218. }
  219. } else {
  220. // timeout. no sequential tap.
  221. debug("Tapping: End(Time out after releasing last tap).\n");
  222. tapping_key = (keyrecord_t){};
  223. process(keyp);
  224. return true;
  225. }
  226. } else {
  227. if (event.pressed && is_tap_key(event.key)) {
  228. debug("Tapping: Start(Press tap key).\n");
  229. tapping_key = *keyp;
  230. return true;
  231. } else {
  232. process(keyp);
  233. return true;
  234. }
  235. }
  236. }
  237. void action_exec(keyevent_t event)
  238. {
  239. if (!IS_NOEVENT(event)) {
  240. debug("event: ");
  241. debug_hex16(event.time); debug(": ");
  242. debug_hex16(event.key.raw);
  243. debug("[");
  244. if (event.pressed) debug("down"); else debug("up");
  245. debug("]\n");
  246. }
  247. keyrecord_t record = { .event = event };
  248. // pre-process on tapping
  249. if (process_tap(&record)) {
  250. if (!IS_NOEVENT(record.event)) debug("processed.\n");
  251. } else {
  252. if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
  253. if (!waiting_buffer_enq(record)) {
  254. // clear all in case of overflow.
  255. clear_keyboard();
  256. waiting_buffer_clear();
  257. tapping_key = (keyrecord_t){};
  258. }
  259. }
  260. // TODO: need to process every time?
  261. // process waiting_buffer
  262. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  263. if (process_tap(&waiting_buffer[waiting_buffer_tail])) {
  264. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  265. debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
  266. } else {
  267. break;
  268. }
  269. }
  270. }
  271. static void process(keyrecord_t *record)
  272. {
  273. // TODO: use record
  274. keyevent_t event = record->event;
  275. uint8_t tap_count = record->tap_count;
  276. if (IS_NOEVENT(event)) { return; }
  277. action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
  278. debug("action: "); debug_hex16(action.code);
  279. if (event.pressed) debug("[down]\n"); else debug("[up]\n");
  280. switch (action.kind.id) {
  281. /* Key and Mods */
  282. case ACT_LMODS:
  283. case ACT_RMODS:
  284. {
  285. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  286. action.key.mods<<4;
  287. if (event.pressed) {
  288. uint8_t tmp_mods = host_get_mods();
  289. if (mods) {
  290. host_add_mods(mods);
  291. host_send_keyboard_report();
  292. }
  293. register_code(action.key.code);
  294. if (mods && action.key.code) {
  295. host_set_mods(tmp_mods);
  296. host_send_keyboard_report();
  297. }
  298. } else {
  299. if (mods && !action.key.code) {
  300. host_del_mods(mods);
  301. host_send_keyboard_report();
  302. }
  303. unregister_code(action.key.code);
  304. }
  305. }
  306. break;
  307. case ACT_LMODS_TAP:
  308. case ACT_RMODS_TAP:
  309. {
  310. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  311. action.key.mods<<4;
  312. switch (action.layer.code) {
  313. case 0x00:
  314. // Oneshot modifier
  315. if (event.pressed) {
  316. if (tap_count == 0) {
  317. debug("MODS_TAP: Oneshot: add_mods\n");
  318. add_mods(mods);
  319. }
  320. else if (tap_count == 1) {
  321. debug("MODS_TAP: Oneshot: start\n");
  322. oneshot_start(mods, event.time);
  323. }
  324. else if (tap_count == 5) {
  325. debug("MODS_TAP: Oneshot: toggle\n");
  326. oneshot_toggle();
  327. }
  328. else {
  329. debug("MODS_TAP: Oneshot: cancel&add_mods\n");
  330. // double tap cancels oneshot and works as normal modifier.
  331. oneshot_cancel();
  332. add_mods(mods);
  333. }
  334. } else {
  335. if (tap_count == 0) {
  336. debug("MODS_TAP: Oneshot: cancel/del_mods\n");
  337. // cancel oneshot by holding.
  338. oneshot_cancel();
  339. del_mods(mods);
  340. }
  341. else if (tap_count == 1) {
  342. debug("MODS_TAP: Oneshot: del_mods\n");
  343. // retain Oneshot
  344. del_mods(mods);
  345. }
  346. else {
  347. debug("MODS_TAP: Oneshot: del_mods\n");
  348. // cancel Mods
  349. del_mods(mods);
  350. }
  351. }
  352. break;
  353. default:
  354. if (event.pressed) {
  355. if (tap_count > 0) {
  356. if (waiting_buffer_has_anykey_pressed()) {
  357. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  358. // ad hoc: set 0 to cancel tap
  359. record->tap_count = 0;
  360. add_mods(mods);
  361. } else {
  362. debug("MODS_TAP: Tap: register_code\n");
  363. register_code(action.key.code);
  364. }
  365. } else {
  366. debug("MODS_TAP: No tap: add_mods\n");
  367. add_mods(mods);
  368. }
  369. } else {
  370. if (tap_count > 0) {
  371. debug("MODS_TAP: Tap: unregister_code\n");
  372. unregister_code(action.key.code);
  373. } else {
  374. debug("MODS_TAP: No tap: add_mods\n");
  375. del_mods(mods);
  376. }
  377. }
  378. break;
  379. }
  380. }
  381. break;
  382. /* other HID usage */
  383. case ACT_USAGE:
  384. #ifdef EXTRAKEY_ENABLE
  385. switch (action.usage.page) {
  386. case ACTION_USAGE_PAGE_SYSTEM:
  387. if (event.pressed) {
  388. host_system_send(action.usage.code);
  389. } else {
  390. host_system_send(0);
  391. }
  392. break;
  393. case ACTION_USAGE_PAGE_CONSUMER:
  394. if (event.pressed) {
  395. host_consumer_send(action.usage.code);
  396. } else {
  397. host_consumer_send(0);
  398. }
  399. break;
  400. }
  401. #endif
  402. break;
  403. /* Mouse key */
  404. case ACT_MOUSEKEY:
  405. #ifdef MOUSEKEY_ENABLE
  406. if (event.pressed) {
  407. mousekey_on(action.key.code);
  408. mousekey_send();
  409. } else {
  410. mousekey_off(action.key.code);
  411. mousekey_send();
  412. }
  413. #endif
  414. break;
  415. /* Layer key */
  416. case ACT_LAYER_PRESSED:
  417. // layer action when pressed
  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. // TODO: tap toggle
  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. // with tap key
  435. if (event.pressed) {
  436. if (IS_TAPPING_KEY(event.key)) {
  437. if (tap_count > 0) {
  438. debug("LAYER_PRESSED: Tap: register_code\n");
  439. register_code(action.layer.code);
  440. } else {
  441. debug("LAYER_PRESSED: No tap: layer_switch\n");
  442. layer_switch(action.layer.opt);
  443. }
  444. } else {
  445. // TODO: while other key tapping
  446. debug("LAYER_PRESSED: No tap: layer_switch\n");
  447. layer_switch(action.layer.opt);
  448. }
  449. /*
  450. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  451. debug("LAYER_PRESSED: Tap: register_code\n");
  452. register_code(action.layer.code);
  453. } else {
  454. debug("LAYER_PRESSED: No tap: layer_switch\n");
  455. layer_switch(action.layer.opt);
  456. }
  457. */
  458. } else {
  459. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  460. debug("LAYER_PRESSED: Tap: unregister_code\n");
  461. unregister_code(action.layer.code);
  462. } else {
  463. debug("LAYER_PRESSED: No tap: NO ACTION\n");
  464. }
  465. }
  466. break;
  467. }
  468. break;
  469. case ACT_LAYER_RELEASED:
  470. switch (action.layer.code) {
  471. case 0x00:
  472. if (!event.pressed) {
  473. layer_switch(action.layer.opt);
  474. }
  475. break;
  476. case 0xF0:
  477. // Ignored. LAYER_RELEASED with tap toggle is invalid action.
  478. break;
  479. case 0xFF:
  480. if (!event.pressed) {
  481. default_layer = action.layer.opt;
  482. layer_switch(default_layer);
  483. }
  484. break;
  485. default:
  486. // Ignored. LAYER_RELEASED with tap key is invalid action.
  487. break;
  488. }
  489. break;
  490. case ACT_LAYER_BIT:
  491. switch (action.layer.code) {
  492. case 0x00:
  493. if (event.pressed) {
  494. layer_switch(current_layer | action.layer.opt);
  495. } else {
  496. layer_switch(current_layer & ~action.layer.opt);
  497. }
  498. break;
  499. case 0xF0:
  500. // TODO: tap toggle
  501. break;
  502. case 0xFF:
  503. // change default layer
  504. if (event.pressed) {
  505. default_layer = current_layer | action.layer.opt;
  506. layer_switch(default_layer);
  507. } else {
  508. default_layer = current_layer & ~action.layer.opt;
  509. layer_switch(default_layer);
  510. }
  511. break;
  512. default:
  513. // with tap key
  514. if (event.pressed) {
  515. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  516. debug("LAYER_BIT: Tap: register_code\n");
  517. register_code(action.layer.code);
  518. } else {
  519. debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
  520. layer_switch(current_layer | action.layer.opt);
  521. }
  522. } else {
  523. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  524. debug("LAYER_BIT: Tap: unregister_code\n");
  525. unregister_code(action.layer.code);
  526. } else {
  527. debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
  528. layer_switch(current_layer & ~action.layer.opt);
  529. }
  530. }
  531. break;
  532. }
  533. case ACT_LAYER_EXT:
  534. switch (action.layer.opt) {
  535. case 0x00:
  536. // set default layer when pressed
  537. switch (action.layer.code) {
  538. case 0x00:
  539. if (event.pressed) {
  540. layer_switch(default_layer);
  541. }
  542. break;
  543. case 0xF0:
  544. // TODO: tap toggle
  545. break;
  546. case 0xFF:
  547. if (event.pressed) {
  548. default_layer = current_layer;
  549. layer_switch(default_layer);
  550. }
  551. break;
  552. default:
  553. // TODO: tap key
  554. break;
  555. }
  556. break;
  557. case 0x01:
  558. // set default layer when released
  559. switch (action.layer.code) {
  560. case 0x00:
  561. if (!event.pressed) {
  562. layer_switch(default_layer);
  563. }
  564. break;
  565. case 0xFF:
  566. if (!event.pressed) {
  567. default_layer = current_layer;
  568. layer_switch(default_layer);
  569. }
  570. break;
  571. case 0xF0:
  572. default:
  573. // Ignore tap.
  574. if (!event.pressed) {
  575. layer_switch(default_layer);
  576. }
  577. break;
  578. }
  579. break;
  580. }
  581. break;
  582. /* Extentions */
  583. case ACT_MACRO:
  584. break;
  585. case ACT_COMMAND:
  586. break;
  587. case ACT_FUNCTION:
  588. action_call_function(event, action.func.id);
  589. break;
  590. default:
  591. break;
  592. }
  593. }
  594. /*
  595. * Utilities for actions.
  596. */
  597. void register_code(uint8_t code)
  598. {
  599. if (code == KC_NO) {
  600. return;
  601. }
  602. else if IS_KEY(code) {
  603. // TODO: should push command_proc out of this block?
  604. if (command_proc(code)) return;
  605. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  606. uint8_t tmp_mods = host_get_mods();
  607. host_add_mods(oneshot_state.mods);
  608. host_add_key(code);
  609. host_send_keyboard_report();
  610. host_set_mods(tmp_mods);
  611. oneshot_state.ready = false;
  612. } else {
  613. host_add_key(code);
  614. host_send_keyboard_report();
  615. }
  616. }
  617. else if IS_MOD(code) {
  618. host_add_mods(MOD_BIT(code));
  619. host_send_keyboard_report();
  620. }
  621. }
  622. void unregister_code(uint8_t code)
  623. {
  624. if IS_KEY(code) {
  625. host_del_key(code);
  626. host_send_keyboard_report();
  627. }
  628. else if IS_MOD(code) {
  629. host_del_mods(MOD_BIT(code));
  630. host_send_keyboard_report();
  631. }
  632. }
  633. void add_mods(uint8_t mods)
  634. {
  635. if (mods) {
  636. host_add_mods(mods);
  637. host_send_keyboard_report();
  638. }
  639. }
  640. void del_mods(uint8_t mods)
  641. {
  642. if (mods) {
  643. host_del_mods(mods);
  644. host_send_keyboard_report();
  645. }
  646. }
  647. void set_mods(uint8_t mods)
  648. {
  649. host_set_mods(mods);
  650. host_send_keyboard_report();
  651. }
  652. void clear_keyboard(void)
  653. {
  654. host_clear_mods();
  655. clear_keyboard_but_mods();
  656. }
  657. void clear_keyboard_but_mods(void)
  658. {
  659. host_clear_keys();
  660. host_send_keyboard_report();
  661. #ifdef MOUSEKEY_ENABLE
  662. mousekey_clear();
  663. mousekey_send();
  664. #endif
  665. #ifdef EXTRAKEY_ENABLE
  666. host_system_send(0);
  667. host_consumer_send(0);
  668. #endif
  669. }
  670. bool sending_anykey(void)
  671. {
  672. return (host_has_anykey() || host_mouse_in_use() ||
  673. host_last_sysytem_report() || host_last_consumer_report());
  674. }
  675. void layer_switch(uint8_t new_layer)
  676. {
  677. if (current_layer != new_layer) {
  678. debug("Layer Switch: "); debug_hex(current_layer);
  679. debug(" -> "); debug_hex(new_layer); debug("\n");
  680. current_layer = new_layer;
  681. clear_keyboard_but_mods(); // To avoid stuck keys
  682. // TODO: update mods with full scan of matrix? if modifier changes between layers
  683. }
  684. }
  685. bool is_tap_key(key_t key)
  686. {
  687. action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
  688. switch (action.kind.id) {
  689. case ACT_LMODS_TAP:
  690. case ACT_RMODS_TAP:
  691. return true;
  692. case ACT_LAYER_PRESSED:
  693. case ACT_LAYER_BIT:
  694. switch (action.layer.code) {
  695. case 0x00:
  696. case 0xF1 ... 0xFF:
  697. return false;
  698. case 0xF0:
  699. default:
  700. return true;
  701. }
  702. return false;
  703. case ACT_FUNCTION:
  704. if (action.func.opt & 0x1) {
  705. return true;
  706. }
  707. return false;
  708. }
  709. return false;
  710. }