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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. Copyright 2012,2013 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 "host.h"
  15. #include "timer.h"
  16. #include "keymap.h"
  17. #include "keycode.h"
  18. #include "keyboard.h"
  19. #include "mousekey.h"
  20. #include "command.h"
  21. #include "util.h"
  22. #include "debug.h"
  23. #include "action.h"
  24. static bool process_tapping(keyrecord_t *record);
  25. static void process_action(keyrecord_t *record);
  26. /*
  27. * Tapping
  28. */
  29. /* period of tapping(ms) */
  30. #ifndef TAPPING_TERM
  31. #define TAPPING_TERM 200
  32. #endif
  33. /* tap count needed for toggling a feature */
  34. #ifndef TAPPING_TOGGLE
  35. #define TAPPING_TOGGLE 5
  36. #endif
  37. /* stores a key event of current tap. */
  38. static keyrecord_t tapping_key = {};
  39. #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  40. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  41. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  42. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  43. #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  44. /*
  45. * Waiting buffer
  46. *
  47. * stores key events waiting for settling current tap.
  48. */
  49. #define WAITING_BUFFER_SIZE 8
  50. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  51. /* point to empty cell to enq */
  52. static uint8_t waiting_buffer_head = 0;
  53. /* point to the oldest data cell to deq */
  54. static uint8_t waiting_buffer_tail = 0;
  55. static bool waiting_buffer_enq(keyrecord_t record)
  56. {
  57. if (IS_NOEVENT(record.event)) {
  58. return true;
  59. }
  60. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  61. debug("waiting_buffer_enq: Over flow.\n");
  62. return false;
  63. }
  64. debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = ");
  65. debug_hex16(record.event.key.raw); debug("\n");
  66. waiting_buffer[waiting_buffer_head] = record;
  67. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  68. return true;
  69. }
  70. /*
  71. static keyrecord_t waiting_buffer_deq(void)
  72. {
  73. if (waiting_buffer_head == waiting_buffer_tail) {
  74. return (keyrecord_t){};
  75. }
  76. uint8_t last_tail = waiting_buffer_tail;
  77. waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
  78. return waiting_buffer[last_tail];
  79. }
  80. static bool waiting_buffer_is_empty(void)
  81. {
  82. return (waiting_buffer_head == waiting_buffer_tail);
  83. }
  84. */
  85. static void waiting_buffer_clear(void)
  86. {
  87. waiting_buffer_head = 0;
  88. waiting_buffer_tail = 0;
  89. }
  90. static bool waiting_buffer_typed(keyevent_t event)
  91. {
  92. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  93. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. static bool waiting_buffer_has_anykey_pressed(void)
  100. {
  101. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  102. if (waiting_buffer[i].event.pressed) return true;
  103. }
  104. return false;
  105. }
  106. /* Oneshot modifier
  107. *
  108. * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
  109. * Solution: Oneshot modifier have its effect on only one key coming next.
  110. * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
  111. *
  112. * Hold: works as normal modifier.
  113. * Tap: one shot modifier.
  114. * 2 Tap: cancel one shot modifier.
  115. * 5-Tap: toggles enable/disable oneshot feature.
  116. */
  117. static struct {
  118. uint8_t mods;
  119. uint8_t time;
  120. bool ready;
  121. bool disabled;
  122. } oneshot_state;
  123. static void oneshot_start(uint8_t mods, uint16_t time)
  124. {
  125. oneshot_state.mods = mods;
  126. oneshot_state.time = time;
  127. oneshot_state.ready = true;
  128. }
  129. static void oneshot_cancel(void)
  130. {
  131. oneshot_state.mods = 0;
  132. oneshot_state.time = 0;
  133. oneshot_state.ready = false;
  134. }
  135. static void oneshot_toggle(void)
  136. {
  137. oneshot_state.disabled = !oneshot_state.disabled;
  138. }
  139. void action_exec(keyevent_t event)
  140. {
  141. if (!IS_NOEVENT(event)) {
  142. debug("event: ");
  143. debug_hex16(event.time); debug(": ");
  144. debug_hex16(event.key.raw);
  145. debug("[");
  146. if (event.pressed) debug("down"); else debug("up");
  147. debug("]\n");
  148. }
  149. keyrecord_t record = { .event = event };
  150. // pre-process on tapping
  151. if (process_tapping(&record)) {
  152. if (!IS_NOEVENT(record.event)) debug("processed.\n");
  153. } else {
  154. if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
  155. // enqueue
  156. if (!waiting_buffer_enq(record)) {
  157. // clear all in case of overflow.
  158. clear_keyboard();
  159. waiting_buffer_clear();
  160. tapping_key = (keyrecord_t){};
  161. }
  162. }
  163. // process waiting_buffer
  164. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  165. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  166. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  167. debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
  168. } else {
  169. break;
  170. }
  171. }
  172. }
  173. static void process_action(keyrecord_t *record)
  174. {
  175. keyevent_t event = record->event;
  176. uint8_t tap_count = record->tap_count;
  177. if (IS_NOEVENT(event)) { return; }
  178. action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
  179. debug("action: "); debug_hex16(action.code);
  180. if (event.pressed) debug("[down]\n"); else debug("[up]\n");
  181. switch (action.kind.id) {
  182. /* Key and Mods */
  183. case ACT_LMODS:
  184. case ACT_RMODS:
  185. {
  186. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  187. action.key.mods<<4;
  188. if (event.pressed) {
  189. uint8_t tmp_mods = host_get_mods();
  190. if (mods) {
  191. host_add_mods(mods);
  192. host_send_keyboard_report();
  193. }
  194. register_code(action.key.code);
  195. if (mods && action.key.code) {
  196. host_set_mods(tmp_mods);
  197. host_send_keyboard_report();
  198. }
  199. } else {
  200. if (mods && !action.key.code) {
  201. host_del_mods(mods);
  202. host_send_keyboard_report();
  203. }
  204. unregister_code(action.key.code);
  205. }
  206. }
  207. break;
  208. case ACT_LMODS_TAP:
  209. case ACT_RMODS_TAP:
  210. {
  211. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  212. action.key.mods<<4;
  213. switch (action.layer.code) {
  214. case 0x00:
  215. // Oneshot modifier
  216. if (event.pressed) {
  217. if (tap_count == 0) {
  218. debug("MODS_TAP: Oneshot: add_mods\n");
  219. add_mods(mods);
  220. }
  221. else if (tap_count == 1) {
  222. debug("MODS_TAP: Oneshot: start\n");
  223. oneshot_start(mods, event.time);
  224. }
  225. else if (tap_count == 5) {
  226. debug("MODS_TAP: Oneshot: toggle\n");
  227. oneshot_toggle();
  228. }
  229. else {
  230. debug("MODS_TAP: Oneshot: cancel&add_mods\n");
  231. // double tap cancels oneshot and works as normal modifier.
  232. oneshot_cancel();
  233. add_mods(mods);
  234. }
  235. } else {
  236. if (tap_count == 0) {
  237. debug("MODS_TAP: Oneshot: cancel/del_mods\n");
  238. // cancel oneshot by holding.
  239. oneshot_cancel();
  240. del_mods(mods);
  241. }
  242. else if (tap_count == 1) {
  243. debug("MODS_TAP: Oneshot: del_mods\n");
  244. // retain Oneshot
  245. del_mods(mods);
  246. }
  247. else {
  248. debug("MODS_TAP: Oneshot: del_mods\n");
  249. // cancel Mods
  250. del_mods(mods);
  251. }
  252. }
  253. break;
  254. default:
  255. if (event.pressed) {
  256. if (tap_count > 0) {
  257. if (waiting_buffer_has_anykey_pressed()) {
  258. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  259. // ad hoc: set 0 to cancel tap
  260. record->tap_count = 0;
  261. add_mods(mods);
  262. } else {
  263. debug("MODS_TAP: Tap: register_code\n");
  264. register_code(action.key.code);
  265. }
  266. } else {
  267. debug("MODS_TAP: No tap: add_mods\n");
  268. add_mods(mods);
  269. }
  270. } else {
  271. if (tap_count > 0) {
  272. debug("MODS_TAP: Tap: unregister_code\n");
  273. unregister_code(action.key.code);
  274. } else {
  275. debug("MODS_TAP: No tap: add_mods\n");
  276. del_mods(mods);
  277. }
  278. }
  279. break;
  280. }
  281. }
  282. break;
  283. /* other HID usage */
  284. case ACT_USAGE:
  285. #ifdef EXTRAKEY_ENABLE
  286. switch (action.usage.page) {
  287. case ACTION_USAGE_PAGE_SYSTEM:
  288. if (event.pressed) {
  289. host_system_send(action.usage.code);
  290. } else {
  291. host_system_send(0);
  292. }
  293. break;
  294. case ACTION_USAGE_PAGE_CONSUMER:
  295. if (event.pressed) {
  296. host_consumer_send(action.usage.code);
  297. } else {
  298. host_consumer_send(0);
  299. }
  300. break;
  301. }
  302. #endif
  303. break;
  304. /* Mouse key */
  305. case ACT_MOUSEKEY:
  306. #ifdef MOUSEKEY_ENABLE
  307. if (event.pressed) {
  308. mousekey_on(action.key.code);
  309. mousekey_send();
  310. } else {
  311. mousekey_off(action.key.code);
  312. mousekey_send();
  313. }
  314. #endif
  315. break;
  316. /* Layer key */
  317. case ACT_LAYER_PRESSED:
  318. // layer action when pressed
  319. switch (action.layer.code) {
  320. case 0x00:
  321. if (event.pressed) {
  322. layer_switch(action.layer.opt);
  323. }
  324. break;
  325. case 0xF0:
  326. // tap toggle
  327. if (event.pressed) {
  328. if (tap_count < TAPPING_TOGGLE) {
  329. layer_switch(action.layer.opt);
  330. }
  331. } else {
  332. if (tap_count >= TAPPING_TOGGLE) {
  333. debug("LAYER_PRESSED: tap toggle.\n");
  334. layer_switch(action.layer.opt);
  335. }
  336. }
  337. break;
  338. case 0xFF:
  339. // change default layer
  340. if (event.pressed) {
  341. default_layer = action.layer.opt;
  342. layer_switch(default_layer);
  343. }
  344. break;
  345. default:
  346. // with tap key
  347. if (event.pressed) {
  348. if (tap_count > 0) {
  349. debug("LAYER_PRESSED: Tap: register_code\n");
  350. register_code(action.layer.code);
  351. } else {
  352. debug("LAYER_PRESSED: No tap: layer_switch\n");
  353. layer_switch(action.layer.opt);
  354. }
  355. } else {
  356. if (tap_count > 0) {
  357. debug("LAYER_PRESSED: Tap: unregister_code\n");
  358. unregister_code(action.layer.code);
  359. } else {
  360. debug("LAYER_PRESSED: No tap: NO ACTION\n");
  361. }
  362. }
  363. break;
  364. }
  365. break;
  366. case ACT_LAYER_RELEASED:
  367. switch (action.layer.code) {
  368. case 0x00:
  369. if (!event.pressed) {
  370. layer_switch(action.layer.opt);
  371. }
  372. break;
  373. case 0xF0:
  374. // tap toggle
  375. if (event.pressed) {
  376. if (tap_count >= TAPPING_TOGGLE) {
  377. debug("LAYER_RELEASED: tap toggle.\n");
  378. layer_switch(action.layer.opt);
  379. }
  380. } else {
  381. if (tap_count < TAPPING_TOGGLE) {
  382. layer_switch(action.layer.opt);
  383. }
  384. }
  385. break;
  386. case 0xFF:
  387. // change default layer
  388. if (!event.pressed) {
  389. default_layer = action.layer.opt;
  390. layer_switch(default_layer);
  391. }
  392. break;
  393. default:
  394. // with tap key
  395. if (event.pressed) {
  396. if (tap_count > 0) {
  397. debug("LAYER_RELEASED: Tap: register_code\n");
  398. register_code(action.layer.code);
  399. } else {
  400. debug("LAYER_RELEASED: No tap: NO ACTION\n");
  401. }
  402. } else {
  403. if (tap_count > 0) {
  404. debug("LAYER_RELEASED: Tap: unregister_code\n");
  405. unregister_code(action.layer.code);
  406. } else {
  407. debug("LAYER_RELEASED: No tap: layer_switch\n");
  408. layer_switch(action.layer.opt);
  409. }
  410. }
  411. break;
  412. }
  413. break;
  414. case ACT_LAYER_BIT:
  415. switch (action.layer.code) {
  416. case 0x00:
  417. if (event.pressed) {
  418. layer_switch(current_layer | action.layer.opt);
  419. } else {
  420. layer_switch(current_layer & ~action.layer.opt);
  421. }
  422. break;
  423. case 0xF0:
  424. // tap toggle
  425. if (event.pressed) {
  426. if (tap_count < TAPPING_TOGGLE) {
  427. debug("LAYER_BIT: tap toggle(press).\n");
  428. layer_switch(current_layer | action.layer.opt);
  429. }
  430. } else {
  431. if (tap_count < TAPPING_TOGGLE) {
  432. debug("LAYER_BIT: tap toggle(release).\n");
  433. layer_switch(current_layer & ~action.layer.opt);
  434. } else {
  435. debug("LAYER_BIT: tap toggle.\n");
  436. layer_switch(current_layer | action.layer.opt);
  437. }
  438. }
  439. break;
  440. case 0xFF:
  441. // change default layer
  442. if (event.pressed) {
  443. default_layer = current_layer | action.layer.opt;
  444. layer_switch(default_layer);
  445. } else {
  446. default_layer = current_layer & ~action.layer.opt;
  447. layer_switch(default_layer);
  448. }
  449. break;
  450. default:
  451. // with tap key
  452. if (event.pressed) {
  453. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  454. debug("LAYER_BIT: Tap: register_code\n");
  455. register_code(action.layer.code);
  456. } else {
  457. debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
  458. layer_switch(current_layer | action.layer.opt);
  459. }
  460. } else {
  461. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  462. debug("LAYER_BIT: Tap: unregister_code\n");
  463. unregister_code(action.layer.code);
  464. } else {
  465. debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
  466. layer_switch(current_layer & ~action.layer.opt);
  467. }
  468. }
  469. break;
  470. }
  471. break;
  472. case ACT_LAYER_EXT:
  473. switch (action.layer.opt) {
  474. case 0x00:
  475. // set default layer when pressed
  476. switch (action.layer.code) {
  477. case 0x00:
  478. if (event.pressed) {
  479. layer_switch(default_layer);
  480. }
  481. break;
  482. case 0xF0:
  483. // tap toggle
  484. if (event.pressed) {
  485. if (tap_count < TAPPING_TOGGLE) {
  486. layer_switch(default_layer);
  487. }
  488. } else {
  489. if (tap_count >= TAPPING_TOGGLE) {
  490. debug("LAYER_EXT_PRESSED: tap toggle.\n");
  491. layer_switch(default_layer);
  492. }
  493. }
  494. break;
  495. case 0xFF:
  496. // change default layer
  497. if (event.pressed) {
  498. default_layer = current_layer;
  499. layer_switch(default_layer);
  500. }
  501. break;
  502. default:
  503. // with tap key
  504. if (event.pressed) {
  505. if (tap_count > 0) {
  506. debug("LAYER_EXT_PRESSED: Tap: register_code\n");
  507. register_code(action.layer.code);
  508. } else {
  509. debug("LAYER_EXT_PRESSED: No tap: layer_switch\n");
  510. layer_switch(default_layer);
  511. }
  512. } else {
  513. if (tap_count > 0) {
  514. debug("LAYER_EXT_PRESSED: Tap: unregister_code\n");
  515. unregister_code(action.layer.code);
  516. } else {
  517. debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n");
  518. }
  519. }
  520. break;
  521. }
  522. break;
  523. case 0x01:
  524. // set default layer when released
  525. switch (action.layer.code) {
  526. case 0x00:
  527. if (!event.pressed) {
  528. layer_switch(default_layer);
  529. }
  530. break;
  531. case 0xF0:
  532. // tap toggle
  533. if (event.pressed) {
  534. if (tap_count >= TAPPING_TOGGLE) {
  535. debug("LAYER_EXT_RELEASED: tap toggle.\n");
  536. layer_switch(default_layer);
  537. }
  538. } else {
  539. if (tap_count < TAPPING_TOGGLE) {
  540. layer_switch(default_layer);
  541. }
  542. }
  543. break;
  544. case 0xFF:
  545. // change default layer
  546. if (!event.pressed) {
  547. default_layer = current_layer;
  548. layer_switch(default_layer);
  549. }
  550. break;
  551. default:
  552. // with tap key
  553. if (event.pressed) {
  554. if (tap_count > 0) {
  555. debug("LAYER_EXT_RELEASED: Tap: register_code\n");
  556. register_code(action.layer.code);
  557. } else {
  558. debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n");
  559. }
  560. } else {
  561. if (tap_count > 0) {
  562. debug("LAYER_EXT_RELEASED: Tap: unregister_code\n");
  563. unregister_code(action.layer.code);
  564. } else {
  565. debug("LAYER_EXT_RELEASED: No tap: layer_switch\n");
  566. layer_switch(default_layer);
  567. }
  568. }
  569. break;
  570. }
  571. break;
  572. }
  573. break;
  574. /* Extentions */
  575. case ACT_MACRO:
  576. break;
  577. case ACT_COMMAND:
  578. break;
  579. case ACT_FUNCTION:
  580. // TODO
  581. action_call_function(event, action.func.id);
  582. break;
  583. default:
  584. break;
  585. }
  586. }
  587. /* Tapping
  588. *
  589. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM
  590. * without interfaring by typing other key.
  591. */
  592. /* return true when key event is processed. */
  593. static bool process_tapping(keyrecord_t *keyp)
  594. {
  595. keyevent_t event = keyp->event;
  596. // if tapping
  597. if (IS_TAPPING_PRESSED()) {
  598. if (WITHIN_TAPPING_TERM(event)) {
  599. if (tapping_key.tap_count == 0) {
  600. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  601. // first tap!
  602. debug("Tapping: First tap.\n");
  603. tapping_key.tap_count = 1;
  604. process_action(&tapping_key);
  605. // enqueue
  606. keyp->tap_count = tapping_key.tap_count;
  607. return false;
  608. } else if (!event.pressed && waiting_buffer_typed(event)) {
  609. // other key typed. not tap.
  610. debug("Tapping: End(No tap. Interfered by typing key).\n");
  611. process_action(&tapping_key);
  612. tapping_key = (keyrecord_t){};
  613. // enqueue
  614. return false;
  615. } else {
  616. // other key events shall be stored till tapping state settles.
  617. return false;
  618. }
  619. } else {
  620. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  621. keyp->tap_count = tapping_key.tap_count;
  622. debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
  623. tapping_key = *keyp;
  624. return false;
  625. }
  626. else if (is_tap_key(keyp->event.key) && event.pressed) {
  627. debug("Tapping: Start with forcing to release last tap.\n");
  628. process_action(&(keyrecord_t){
  629. .tap_count = tapping_key.tap_count,
  630. .event.key = tapping_key.event.key,
  631. .event.time = event.time,
  632. .event.pressed = false
  633. });
  634. tapping_key = *keyp;
  635. return false;
  636. }
  637. else {
  638. if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
  639. process_action(keyp);
  640. return true;
  641. }
  642. }
  643. }
  644. // not within TAPPING_TERM
  645. else {
  646. if (tapping_key.tap_count == 0) {
  647. // timeout. not tap.
  648. debug("Tapping: End. Not tap(time out).\n");
  649. process_action(&tapping_key);
  650. tapping_key = (keyrecord_t){};
  651. return false;
  652. } else {
  653. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  654. debug("Tapping: End. tap release.");
  655. keyp->tap_count = tapping_key.tap_count;
  656. process_action(keyp);
  657. tapping_key = (keyrecord_t){};
  658. return true;
  659. } else {
  660. // other key after tap time out.
  661. process_action(keyp);
  662. return true;
  663. }
  664. }
  665. }
  666. } else if (IS_TAPPING_RELEASED()) {
  667. if (WITHIN_TAPPING_TERM(event)) {
  668. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  669. // sequential tap.
  670. keyp->tap_count = tapping_key.tap_count + 1;
  671. debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
  672. process_action(keyp);
  673. tapping_key = *keyp;
  674. return true;
  675. } else if (event.pressed && is_tap_key(event.key)) {
  676. // Sequential tap can be interfered with other tap key.
  677. debug("Tapping: Start with interfering other tap.\n");
  678. tapping_key = *keyp;
  679. return true;
  680. } else {
  681. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  682. process_action(keyp);
  683. return true;
  684. }
  685. } else {
  686. // timeout. no sequential tap.
  687. debug("Tapping: End(Time out after releasing last tap).\n");
  688. tapping_key = (keyrecord_t){};
  689. process_action(keyp);
  690. return true;
  691. }
  692. } else {
  693. if (event.pressed && is_tap_key(event.key)) {
  694. debug("Tapping: Start(Press tap key).\n");
  695. tapping_key = *keyp;
  696. return true;
  697. } else {
  698. process_action(keyp);
  699. return true;
  700. }
  701. }
  702. }
  703. /*
  704. * Utilities for actions.
  705. */
  706. void register_code(uint8_t code)
  707. {
  708. if (code == KC_NO) {
  709. return;
  710. }
  711. else if IS_KEY(code) {
  712. // TODO: should push command_proc out of this block?
  713. if (command_proc(code)) return;
  714. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  715. uint8_t tmp_mods = host_get_mods();
  716. host_add_mods(oneshot_state.mods);
  717. host_add_key(code);
  718. host_send_keyboard_report();
  719. host_set_mods(tmp_mods);
  720. oneshot_state.ready = false;
  721. } else {
  722. host_add_key(code);
  723. host_send_keyboard_report();
  724. }
  725. }
  726. else if IS_MOD(code) {
  727. host_add_mods(MOD_BIT(code));
  728. host_send_keyboard_report();
  729. }
  730. }
  731. void unregister_code(uint8_t code)
  732. {
  733. if IS_KEY(code) {
  734. host_del_key(code);
  735. host_send_keyboard_report();
  736. }
  737. else if IS_MOD(code) {
  738. host_del_mods(MOD_BIT(code));
  739. host_send_keyboard_report();
  740. }
  741. }
  742. void add_mods(uint8_t mods)
  743. {
  744. if (mods) {
  745. host_add_mods(mods);
  746. host_send_keyboard_report();
  747. }
  748. }
  749. void del_mods(uint8_t mods)
  750. {
  751. if (mods) {
  752. host_del_mods(mods);
  753. host_send_keyboard_report();
  754. }
  755. }
  756. void set_mods(uint8_t mods)
  757. {
  758. host_set_mods(mods);
  759. host_send_keyboard_report();
  760. }
  761. void clear_keyboard(void)
  762. {
  763. host_clear_mods();
  764. clear_keyboard_but_mods();
  765. }
  766. void clear_keyboard_but_mods(void)
  767. {
  768. host_clear_keys();
  769. host_send_keyboard_report();
  770. #ifdef MOUSEKEY_ENABLE
  771. mousekey_clear();
  772. mousekey_send();
  773. #endif
  774. #ifdef EXTRAKEY_ENABLE
  775. host_system_send(0);
  776. host_consumer_send(0);
  777. #endif
  778. }
  779. bool sending_anykey(void)
  780. {
  781. return (host_has_anykey() || host_mouse_in_use() ||
  782. host_last_sysytem_report() || host_last_consumer_report());
  783. }
  784. void layer_switch(uint8_t new_layer)
  785. {
  786. if (current_layer != new_layer) {
  787. debug("Layer Switch: "); debug_hex(current_layer);
  788. debug(" -> "); debug_hex(new_layer); debug("\n");
  789. current_layer = new_layer;
  790. clear_keyboard_but_mods(); // To avoid stuck keys
  791. // NOTE: update mods with full scan of matrix? if modifier changes between layers
  792. }
  793. }
  794. bool is_tap_key(key_t key)
  795. {
  796. action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
  797. switch (action.kind.id) {
  798. case ACT_LMODS_TAP:
  799. case ACT_RMODS_TAP:
  800. return true;
  801. case ACT_LAYER_PRESSED:
  802. case ACT_LAYER_BIT:
  803. switch (action.layer.code) {
  804. case 0x00:
  805. case 0xF1 ... 0xFF:
  806. return false;
  807. case 0xF0:
  808. default:
  809. return true;
  810. }
  811. return false;
  812. case ACT_FUNCTION:
  813. if (action.func.opt & 0x1) {
  814. return true;
  815. }
  816. return false;
  817. }
  818. return false;
  819. }