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

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