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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 "led.h"
  24. #include "layer_switch.h"
  25. #include "action_macro.h"
  26. #include "action.h"
  27. static void process_action(keyrecord_t *record);
  28. #ifndef NO_ACTION_TAPPING
  29. static bool process_tapping(keyrecord_t *record);
  30. static void waiting_buffer_scan_tap(void);
  31. #endif
  32. static void debug_event(keyevent_t event);
  33. static void debug_record(keyrecord_t record);
  34. static void debug_action(action_t action);
  35. #ifndef NO_ACTION_TAPPING
  36. static void debug_tapping_key(void);
  37. static void debug_waiting_buffer(void);
  38. #endif
  39. /*
  40. * Tapping
  41. */
  42. /* period of tapping(ms) */
  43. #ifndef TAPPING_TERM
  44. #define TAPPING_TERM 200
  45. #endif
  46. /* tap count needed for toggling a feature */
  47. #ifndef TAPPING_TOGGLE
  48. #define TAPPING_TOGGLE 5
  49. #endif
  50. #ifndef NO_ACTION_TAPPING
  51. /* stores a key event of current tap. */
  52. static keyrecord_t tapping_key = {};
  53. #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
  54. #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
  55. #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
  56. #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
  57. #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
  58. /*
  59. * Waiting buffer
  60. *
  61. * stores key events waiting for settling current tap.
  62. */
  63. #define WAITING_BUFFER_SIZE 8
  64. static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
  65. /* point to empty cell to enq */
  66. static uint8_t waiting_buffer_head = 0;
  67. /* point to the oldest data cell to deq */
  68. static uint8_t waiting_buffer_tail = 0;
  69. static bool waiting_buffer_enq(keyrecord_t record)
  70. {
  71. if (IS_NOEVENT(record.event)) {
  72. return true;
  73. }
  74. if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
  75. debug("waiting_buffer_enq: Over flow.\n");
  76. return false;
  77. }
  78. waiting_buffer[waiting_buffer_head] = record;
  79. waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
  80. debug("waiting_buffer_enq: "); debug_waiting_buffer();
  81. return true;
  82. }
  83. static void waiting_buffer_clear(void)
  84. {
  85. waiting_buffer_head = 0;
  86. waiting_buffer_tail = 0;
  87. }
  88. #if TAPPING_TERM >= 500
  89. static bool waiting_buffer_typed(keyevent_t event)
  90. {
  91. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  92. if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. #endif
  99. 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. #endif
  140. void action_exec(keyevent_t event)
  141. {
  142. if (!IS_NOEVENT(event)) {
  143. debug("\n---- action_exec: start -----\n");
  144. debug("EVENT: "); debug_event(event); debug("\n");
  145. }
  146. keyrecord_t record = { .event = event };
  147. #ifndef NO_ACTION_TAPPING
  148. // pre-process on tapping
  149. if (process_tapping(&record)) {
  150. if (!IS_NOEVENT(record.event)) {
  151. debug("processed: "); debug_record(record); debug("\n");
  152. }
  153. } else {
  154. // enqueue
  155. if (!waiting_buffer_enq(record)) {
  156. // clear all in case of overflow.
  157. debug("OVERFLOW: CLEAR ALL STATES\n");
  158. clear_keyboard();
  159. waiting_buffer_clear();
  160. tapping_key = (keyrecord_t){};
  161. }
  162. }
  163. // process waiting_buffer
  164. if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
  165. debug("---- action_exec: process waiting_buffer -----\n");
  166. }
  167. for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
  168. if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
  169. debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
  170. debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
  171. } else {
  172. break;
  173. }
  174. }
  175. if (!IS_NOEVENT(event)) {
  176. debug("\n");
  177. }
  178. #else
  179. process_action(&record);
  180. if (!IS_NOEVENT(record.event)) {
  181. debug("processed: "); debug_record(record); debug("\n");
  182. }
  183. #endif
  184. }
  185. static void process_action(keyrecord_t *record)
  186. {
  187. keyevent_t event = record->event;
  188. uint8_t tap_count = record->tap.count;
  189. if (IS_NOEVENT(event)) { return; }
  190. action_t action = layer_switch_get_action(event.key);
  191. debug("ACTION: "); debug_action(action);
  192. debug(" overlays: "); overlay_debug();
  193. debug(" keymaps: "); keymap_debug();
  194. debug(" default_layer: "); debug_dec(default_layer); debug("\n");
  195. switch (action.kind.id) {
  196. /* Key and Mods */
  197. case ACT_LMODS:
  198. case ACT_RMODS:
  199. {
  200. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  201. action.key.mods<<4;
  202. if (event.pressed) {
  203. uint8_t tmp_mods = host_get_mods();
  204. if (mods) {
  205. host_add_mods(mods);
  206. host_send_keyboard_report();
  207. }
  208. register_code(action.key.code);
  209. if (mods && action.key.code) {
  210. host_set_mods(tmp_mods);
  211. host_send_keyboard_report();
  212. }
  213. } else {
  214. if (mods && !action.key.code) {
  215. host_del_mods(mods);
  216. host_send_keyboard_report();
  217. }
  218. unregister_code(action.key.code);
  219. }
  220. }
  221. break;
  222. #ifndef NO_ACTION_TAPPING
  223. case ACT_LMODS_TAP:
  224. case ACT_RMODS_TAP:
  225. {
  226. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  227. action.key.mods<<4;
  228. switch (action.layer.code) {
  229. case 0x00:
  230. // Oneshot modifier
  231. if (event.pressed) {
  232. if (tap_count == 0) {
  233. debug("MODS_TAP: Oneshot: add_mods\n");
  234. add_mods(mods);
  235. }
  236. else if (tap_count == 1) {
  237. debug("MODS_TAP: Oneshot: start\n");
  238. oneshot_start(mods, event.time);
  239. }
  240. else if (tap_count == TAPPING_TOGGLE) {
  241. debug("MODS_TAP: Oneshot: toggle\n");
  242. oneshot_toggle();
  243. }
  244. else {
  245. debug("MODS_TAP: Oneshot: cancel&add_mods\n");
  246. // double tap cancels oneshot and works as normal modifier.
  247. oneshot_cancel();
  248. add_mods(mods);
  249. }
  250. } else {
  251. if (tap_count == 0) {
  252. debug("MODS_TAP: Oneshot: cancel/del_mods\n");
  253. // cancel oneshot on hold
  254. oneshot_cancel();
  255. del_mods(mods);
  256. }
  257. else if (tap_count == 1) {
  258. debug("MODS_TAP: Oneshot: del_mods\n");
  259. // retain Oneshot
  260. del_mods(mods);
  261. }
  262. else {
  263. debug("MODS_TAP: Oneshot: del_mods\n");
  264. // cancel Mods
  265. del_mods(mods);
  266. }
  267. }
  268. break;
  269. default:
  270. if (event.pressed) {
  271. if (tap_count > 0) {
  272. if (waiting_buffer_has_anykey_pressed()) {
  273. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  274. // ad hoc: set 0 to cancel tap
  275. record->tap.count = 0;
  276. add_mods(mods);
  277. } else {
  278. debug("MODS_TAP: Tap: register_code\n");
  279. register_code(action.key.code);
  280. }
  281. } else {
  282. debug("MODS_TAP: No tap: add_mods\n");
  283. add_mods(mods);
  284. }
  285. } else {
  286. if (tap_count > 0) {
  287. debug("MODS_TAP: Tap: unregister_code\n");
  288. unregister_code(action.key.code);
  289. } else {
  290. debug("MODS_TAP: No tap: add_mods\n");
  291. del_mods(mods);
  292. }
  293. }
  294. break;
  295. }
  296. }
  297. break;
  298. #endif
  299. #ifdef EXTRAKEY_ENABLE
  300. /* other HID usage */
  301. case ACT_USAGE:
  302. switch (action.usage.page) {
  303. case PAGE_SYSTEM:
  304. if (event.pressed) {
  305. host_system_send(action.usage.code);
  306. } else {
  307. host_system_send(0);
  308. }
  309. break;
  310. case PAGE_CONSUMER:
  311. if (event.pressed) {
  312. host_consumer_send(action.usage.code);
  313. } else {
  314. host_consumer_send(0);
  315. }
  316. break;
  317. }
  318. break;
  319. #endif
  320. #ifdef MOUSEKEY_ENABLE
  321. /* Mouse key */
  322. case ACT_MOUSEKEY:
  323. if (event.pressed) {
  324. mousekey_on(action.key.code);
  325. mousekey_send();
  326. } else {
  327. mousekey_off(action.key.code);
  328. mousekey_send();
  329. }
  330. break;
  331. #endif
  332. case ACT_KEYMAP:
  333. switch (action.layer.code) {
  334. /* Keymap clear */
  335. case OP_RESET:
  336. switch (action.layer.val & 0x03) {
  337. case 0:
  338. // NOTE: reserved
  339. overlay_clear();
  340. keymap_clear();
  341. break;
  342. case ON_PRESS:
  343. if (event.pressed) {
  344. overlay_clear();
  345. keymap_clear();
  346. }
  347. break;
  348. case ON_RELEASE:
  349. if (!event.pressed) {
  350. overlay_clear();
  351. keymap_clear();
  352. }
  353. break;
  354. case ON_BOTH:
  355. overlay_clear();
  356. keymap_clear();
  357. break;
  358. /* NOTE: 4-7 rserved */
  359. }
  360. break;
  361. /* Keymap Reset default layer */
  362. case (OP_RESET | ON_PRESS):
  363. if (event.pressed) {
  364. default_layer_set(action.layer.val);
  365. }
  366. break;
  367. case (OP_RESET | ON_RELEASE):
  368. if (!event.pressed) {
  369. default_layer_set(action.layer.val);
  370. }
  371. break;
  372. case (OP_RESET | ON_BOTH):
  373. default_layer_set(action.layer.val);
  374. break;
  375. /* Keymap Bit invert */
  376. case OP_INV:
  377. /* with tap toggle */
  378. if (event.pressed) {
  379. if (tap_count < TAPPING_TOGGLE) {
  380. debug("KEYMAP_INV: tap toggle(press).\n");
  381. keymap_invert(action.layer.val);
  382. }
  383. } else {
  384. if (tap_count <= TAPPING_TOGGLE) {
  385. debug("KEYMAP_INV: tap toggle(release).\n");
  386. keymap_invert(action.layer.val);
  387. }
  388. }
  389. break;
  390. case (OP_INV | ON_PRESS):
  391. if (event.pressed) {
  392. keymap_invert(action.layer.val);
  393. }
  394. break;
  395. case (OP_INV | ON_RELEASE):
  396. if (!event.pressed) {
  397. keymap_invert(action.layer.val);
  398. }
  399. break;
  400. case (OP_INV | ON_BOTH):
  401. keymap_invert(action.layer.val);
  402. break;
  403. /* Keymap Bit on */
  404. case OP_ON:
  405. if (event.pressed) {
  406. keymap_on(action.layer.val);
  407. } else {
  408. keymap_off(action.layer.val);
  409. }
  410. break;
  411. case (OP_ON | ON_PRESS):
  412. if (event.pressed) {
  413. keymap_on(action.layer.val);
  414. }
  415. break;
  416. case (OP_ON | ON_RELEASE):
  417. if (!event.pressed) {
  418. keymap_on(action.layer.val);
  419. }
  420. break;
  421. case (OP_ON | ON_BOTH):
  422. keymap_on(action.layer.val);
  423. break;
  424. /* Keymap Bit off */
  425. case OP_OFF:
  426. if (event.pressed) {
  427. keymap_off(action.layer.val);
  428. } else {
  429. keymap_on(action.layer.val);
  430. }
  431. break;
  432. case (OP_OFF | ON_PRESS):
  433. if (event.pressed) {
  434. keymap_off(action.layer.val);
  435. }
  436. break;
  437. case (OP_OFF | ON_RELEASE):
  438. if (!event.pressed) {
  439. keymap_off(action.layer.val);
  440. }
  441. break;
  442. case (OP_OFF | ON_BOTH):
  443. keymap_off(action.layer.val);
  444. break;
  445. /* Keymap Bit set */
  446. case OP_SET:
  447. if (event.pressed) {
  448. keymap_set(action.layer.val);
  449. } else {
  450. keymap_clear();
  451. }
  452. break;
  453. case (OP_SET | ON_PRESS):
  454. if (event.pressed) {
  455. keymap_set(action.layer.val);
  456. }
  457. break;
  458. case (OP_SET | ON_RELEASE):
  459. if (!event.pressed) {
  460. keymap_set(action.layer.val);
  461. }
  462. break;
  463. case (OP_SET | ON_BOTH):
  464. keymap_set(action.layer.val);
  465. break;
  466. /* Keymap Bit invert with tap key */
  467. default:
  468. if (event.pressed) {
  469. if (tap_count > 0) {
  470. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  471. register_code(action.layer.code);
  472. } else {
  473. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  474. keymap_on(action.layer.val);
  475. }
  476. } else {
  477. if (tap_count > 0) {
  478. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  479. unregister_code(action.layer.code);
  480. } else {
  481. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  482. keymap_off(action.layer.val);
  483. }
  484. }
  485. break;
  486. }
  487. break;
  488. #ifndef NO_ACTION_OVERLAY
  489. case ACT_OVERLAY:
  490. switch (action.layer.code) {
  491. // Overlay Invert bit4
  492. case OP_INV4 | 0:
  493. if (action.layer.val == 0) {
  494. // NOTE: reserved for future use
  495. overlay_clear();
  496. } else {
  497. overlay_set(overlay_stat ^ action.layer.val);
  498. }
  499. break;
  500. case OP_INV4 | 1:
  501. if (action.layer.val == 0) {
  502. // on pressed
  503. if (event.pressed) overlay_clear();
  504. } else {
  505. overlay_set(overlay_stat ^ action.layer.val<<4);
  506. }
  507. break;
  508. case OP_INV4 | 2:
  509. if (action.layer.val == 0) {
  510. // on released
  511. if (!event.pressed) overlay_clear();
  512. } else {
  513. overlay_set(overlay_stat ^ action.layer.val<<8);
  514. }
  515. break;
  516. case OP_INV4 | 3:
  517. if (action.layer.val == 0) {
  518. // on both
  519. overlay_clear();
  520. } else {
  521. overlay_set(overlay_stat ^ action.layer.val<<12);
  522. }
  523. break;
  524. /* Overlay Bit invert */
  525. case OP_INV:
  526. /* with tap toggle */
  527. if (event.pressed) {
  528. if (tap_count < TAPPING_TOGGLE) {
  529. debug("OVERLAY_INV: tap toggle(press).\n");
  530. overlay_invert(action.layer.val);
  531. }
  532. } else {
  533. if (tap_count <= TAPPING_TOGGLE) {
  534. debug("OVERLAY_INV: tap toggle(release).\n");
  535. overlay_invert(action.layer.val);
  536. }
  537. }
  538. break;
  539. case (OP_INV | ON_PRESS):
  540. if (event.pressed) {
  541. overlay_invert(action.layer.val);
  542. }
  543. break;
  544. case (OP_INV | ON_RELEASE):
  545. if (!event.pressed) {
  546. overlay_invert(action.layer.val);
  547. }
  548. break;
  549. case (OP_INV | ON_BOTH):
  550. overlay_invert(action.layer.val);
  551. break;
  552. /* Overlay Bit on */
  553. case OP_ON:
  554. if (event.pressed) {
  555. overlay_on(action.layer.val);
  556. } else {
  557. overlay_off(action.layer.val);
  558. }
  559. break;
  560. case (OP_ON | ON_PRESS):
  561. if (event.pressed) {
  562. overlay_on(action.layer.val);
  563. }
  564. break;
  565. case (OP_ON | ON_RELEASE):
  566. if (!event.pressed) {
  567. overlay_on(action.layer.val);
  568. }
  569. break;
  570. case (OP_ON | ON_BOTH):
  571. overlay_on(action.layer.val);
  572. break;
  573. /* Overlay Bit off */
  574. case OP_OFF:
  575. if (event.pressed) {
  576. overlay_off(action.layer.val);
  577. } else {
  578. overlay_on(action.layer.val);
  579. }
  580. break;
  581. case (OP_OFF | ON_PRESS):
  582. if (event.pressed) {
  583. overlay_off(action.layer.val);
  584. }
  585. break;
  586. case (OP_OFF | ON_RELEASE):
  587. if (!event.pressed) {
  588. overlay_off(action.layer.val);
  589. }
  590. break;
  591. case (OP_OFF | ON_BOTH):
  592. overlay_off(action.layer.val);
  593. break;
  594. /* Overlay Bit set */
  595. case OP_SET:
  596. if (event.pressed) {
  597. overlay_move(action.layer.val);
  598. } else {
  599. overlay_clear();
  600. }
  601. break;
  602. case (OP_SET | ON_PRESS):
  603. if (event.pressed) {
  604. overlay_move(action.layer.val);
  605. }
  606. break;
  607. case (OP_SET | ON_RELEASE):
  608. if (!event.pressed) {
  609. overlay_move(action.layer.val);
  610. }
  611. break;
  612. case (OP_SET | ON_BOTH):
  613. overlay_move(action.layer.val);
  614. break;
  615. /* Overlay Bit invert with tap key */
  616. default:
  617. if (event.pressed) {
  618. if (tap_count > 0) {
  619. debug("OVERLAY_TAP_KEY: Tap: register_code\n");
  620. register_code(action.layer.code);
  621. } else {
  622. debug("OVERLAY_TAP_KEY: No tap: On on press\n");
  623. overlay_on(action.layer.val);
  624. }
  625. } else {
  626. if (tap_count > 0) {
  627. debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
  628. unregister_code(action.layer.code);
  629. } else {
  630. debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
  631. overlay_off(action.layer.val);
  632. }
  633. }
  634. break;
  635. }
  636. break;
  637. #endif
  638. /* Extentions */
  639. case ACT_MACRO:
  640. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  641. break;
  642. case ACT_COMMAND:
  643. break;
  644. case ACT_FUNCTION:
  645. action_function(record, action.func.id, action.func.opt);
  646. break;
  647. default:
  648. break;
  649. }
  650. }
  651. #ifndef NO_ACTION_TAPPING
  652. /* Tapping
  653. *
  654. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  655. * (without interfering by typing other key)
  656. */
  657. /* return true when key event is processed or consumed. */
  658. static bool process_tapping(keyrecord_t *keyp)
  659. {
  660. keyevent_t event = keyp->event;
  661. // if tapping
  662. if (IS_TAPPING_PRESSED()) {
  663. if (WITHIN_TAPPING_TERM(event)) {
  664. if (tapping_key.tap.count == 0) {
  665. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  666. // first tap!
  667. debug("Tapping: First tap(0->1).\n");
  668. tapping_key.tap.count = 1;
  669. tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
  670. debug_tapping_key();
  671. process_action(&tapping_key);
  672. // enqueue
  673. keyp->tap = tapping_key.tap;
  674. return false;
  675. }
  676. #if TAPPING_TERM >= 500
  677. /* This can prevent from typing some tap keys in a row at a time. */
  678. else if (!event.pressed && waiting_buffer_typed(event)) {
  679. // other key typed. not tap.
  680. debug("Tapping: End. No tap. Interfered by typing key\n");
  681. process_action(&tapping_key);
  682. tapping_key = (keyrecord_t){};
  683. debug_tapping_key();
  684. // enqueue
  685. return false;
  686. }
  687. #endif
  688. else {
  689. // other key events shall be enq'd till tapping state settles.
  690. return false;
  691. }
  692. }
  693. // tap_count > 0
  694. else {
  695. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  696. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  697. keyp->tap = tapping_key.tap;
  698. process_action(keyp);
  699. tapping_key = *keyp;
  700. debug_tapping_key();
  701. return true;
  702. }
  703. else if (is_tap_key(keyp->event.key) && event.pressed) {
  704. if (tapping_key.tap.count > 1) {
  705. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  706. // unregister key
  707. process_action(&(keyrecord_t){
  708. .tap = tapping_key.tap,
  709. .event.key = tapping_key.event.key,
  710. .event.time = event.time,
  711. .event.pressed = false
  712. });
  713. } else {
  714. debug("Tapping: Start while last tap(1).\n");
  715. }
  716. tapping_key = *keyp;
  717. waiting_buffer_scan_tap();
  718. debug_tapping_key();
  719. return true;
  720. }
  721. else {
  722. if (!IS_NOEVENT(keyp->event)) {
  723. debug("Tapping: key event while last tap(>0).\n");
  724. }
  725. process_action(keyp);
  726. return true;
  727. }
  728. }
  729. }
  730. // after TAPPING_TERM
  731. else {
  732. if (tapping_key.tap.count == 0) {
  733. debug("Tapping: End. Timeout. Not tap(0): ");
  734. debug_event(event); debug("\n");
  735. process_action(&tapping_key);
  736. tapping_key = (keyrecord_t){};
  737. debug_tapping_key();
  738. return false;
  739. } else {
  740. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  741. debug("Tapping: End. last timeout tap release(>0).");
  742. keyp->tap = tapping_key.tap;
  743. process_action(keyp);
  744. tapping_key = (keyrecord_t){};
  745. return true;
  746. }
  747. else if (is_tap_key(keyp->event.key) && event.pressed) {
  748. if (tapping_key.tap.count > 1) {
  749. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  750. // unregister key
  751. process_action(&(keyrecord_t){
  752. .tap = tapping_key.tap,
  753. .event.key = tapping_key.event.key,
  754. .event.time = event.time,
  755. .event.pressed = false
  756. });
  757. } else {
  758. debug("Tapping: Start while last timeout tap(1).\n");
  759. }
  760. tapping_key = *keyp;
  761. waiting_buffer_scan_tap();
  762. debug_tapping_key();
  763. return true;
  764. }
  765. else {
  766. if (!IS_NOEVENT(keyp->event)) {
  767. debug("Tapping: key event while last timeout tap(>0).\n");
  768. }
  769. process_action(keyp);
  770. return true;
  771. }
  772. }
  773. }
  774. } else if (IS_TAPPING_RELEASED()) {
  775. if (WITHIN_TAPPING_TERM(event)) {
  776. if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  777. // sequential tap.
  778. keyp->tap = tapping_key.tap;
  779. keyp->tap.count += 1;
  780. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  781. process_action(keyp);
  782. tapping_key = *keyp;
  783. debug_tapping_key();
  784. return true;
  785. } else if (event.pressed && is_tap_key(event.key)) {
  786. // Sequential tap can be interfered with other tap key.
  787. debug("Tapping: Start with interfering other tap.\n");
  788. tapping_key = *keyp;
  789. waiting_buffer_scan_tap();
  790. debug_tapping_key();
  791. return true;
  792. } else {
  793. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  794. process_action(keyp);
  795. return true;
  796. }
  797. } else {
  798. // timeout. no sequential tap.
  799. debug("Tapping: End(Timeout after releasing last tap): ");
  800. debug_event(event); debug("\n");
  801. tapping_key = (keyrecord_t){};
  802. debug_tapping_key();
  803. return false;
  804. }
  805. }
  806. // not tapping satate
  807. else {
  808. if (event.pressed && is_tap_key(event.key)) {
  809. debug("Tapping: Start(Press tap key).\n");
  810. tapping_key = *keyp;
  811. waiting_buffer_scan_tap();
  812. debug_tapping_key();
  813. return true;
  814. } else {
  815. process_action(keyp);
  816. return true;
  817. }
  818. }
  819. }
  820. /* scan buffer for tapping */
  821. static void waiting_buffer_scan_tap(void)
  822. {
  823. // tapping already is settled
  824. if (tapping_key.tap.count > 0) return;
  825. // invalid state: tapping_key released && tap.count == 0
  826. if (!tapping_key.event.pressed) return;
  827. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  828. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  829. !waiting_buffer[i].event.pressed &&
  830. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  831. tapping_key.tap.count = 1;
  832. waiting_buffer[i].tap.count = 1;
  833. process_action(&tapping_key);
  834. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  835. debug_waiting_buffer();
  836. return;
  837. }
  838. }
  839. }
  840. #endif
  841. /*
  842. * Utilities for actions.
  843. */
  844. void register_code(uint8_t code)
  845. {
  846. if (code == KC_NO) {
  847. return;
  848. }
  849. #ifdef CAPSLOCK_LOCKING_ENABLE
  850. else if (KC_LOCKING_CAPS == code) {
  851. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  852. // Resync: ignore if caps lock already is on
  853. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  854. #endif
  855. host_add_key(KC_CAPSLOCK);
  856. host_send_keyboard_report();
  857. host_del_key(KC_CAPSLOCK);
  858. host_send_keyboard_report();
  859. }
  860. #endif
  861. else if IS_KEY(code) {
  862. // TODO: should push command_proc out of this block?
  863. if (command_proc(code)) return;
  864. #ifndef NO_ACTION_TAPPING
  865. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  866. uint8_t tmp_mods = host_get_mods();
  867. host_add_mods(oneshot_state.mods);
  868. host_add_key(code);
  869. host_send_keyboard_report();
  870. host_set_mods(tmp_mods);
  871. oneshot_state.ready = false;
  872. } else
  873. #endif
  874. {
  875. host_add_key(code);
  876. host_send_keyboard_report();
  877. }
  878. }
  879. else if IS_MOD(code) {
  880. host_add_mods(MOD_BIT(code));
  881. host_send_keyboard_report();
  882. }
  883. }
  884. void unregister_code(uint8_t code)
  885. {
  886. if (code == KC_NO) {
  887. return;
  888. }
  889. #ifdef CAPSLOCK_LOCKING_ENABLE
  890. else if (KC_LOCKING_CAPS == code) {
  891. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  892. // Resync: ignore if caps lock already is off
  893. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  894. #endif
  895. host_add_key(KC_CAPSLOCK);
  896. host_send_keyboard_report();
  897. host_del_key(KC_CAPSLOCK);
  898. host_send_keyboard_report();
  899. }
  900. #endif
  901. else if IS_KEY(code) {
  902. host_del_key(code);
  903. host_send_keyboard_report();
  904. }
  905. else if IS_MOD(code) {
  906. host_del_mods(MOD_BIT(code));
  907. host_send_keyboard_report();
  908. }
  909. }
  910. void add_mods(uint8_t mods)
  911. {
  912. if (mods) {
  913. host_add_mods(mods);
  914. host_send_keyboard_report();
  915. }
  916. }
  917. void del_mods(uint8_t mods)
  918. {
  919. if (mods) {
  920. host_del_mods(mods);
  921. host_send_keyboard_report();
  922. }
  923. }
  924. void set_mods(uint8_t mods)
  925. {
  926. host_set_mods(mods);
  927. host_send_keyboard_report();
  928. }
  929. void clear_keyboard(void)
  930. {
  931. host_clear_mods();
  932. clear_keyboard_but_mods();
  933. }
  934. void clear_keyboard_but_mods(void)
  935. {
  936. host_clear_keys();
  937. host_send_keyboard_report();
  938. #ifdef MOUSEKEY_ENABLE
  939. mousekey_clear();
  940. mousekey_send();
  941. #endif
  942. #ifdef EXTRAKEY_ENABLE
  943. host_system_send(0);
  944. host_consumer_send(0);
  945. #endif
  946. }
  947. bool sending_anykey(void)
  948. {
  949. return (host_has_anykey() || host_mouse_in_use() ||
  950. host_last_sysytem_report() || host_last_consumer_report());
  951. }
  952. bool is_tap_key(key_t key)
  953. {
  954. action_t action = layer_switch_get_action(key);
  955. switch (action.kind.id) {
  956. case ACT_LMODS_TAP:
  957. case ACT_RMODS_TAP:
  958. return true;
  959. case ACT_KEYMAP:
  960. case ACT_OVERLAY:
  961. switch (action.layer.code) {
  962. case 0x04 ... 0xEF: /* tap key */
  963. case OP_INV:
  964. return true;
  965. default:
  966. return false;
  967. }
  968. case ACT_MACRO:
  969. case ACT_FUNCTION:
  970. if (action.func.opt & FUNC_TAP) { return true; }
  971. return false;
  972. }
  973. return false;
  974. }
  975. /*
  976. * debug print
  977. */
  978. static void debug_event(keyevent_t event)
  979. {
  980. debug_hex16((event.key.row<<8) | event.key.col);
  981. if (event.pressed) debug("d("); else debug("u(");
  982. debug_dec(event.time); debug(")");
  983. }
  984. static void debug_record(keyrecord_t record)
  985. {
  986. debug_event(record.event); debug(":"); debug_dec(record.tap.count);
  987. if (record.tap.interrupted) debug("-");
  988. }
  989. static void debug_action(action_t action)
  990. {
  991. switch (action.kind.id) {
  992. case ACT_LMODS: debug("ACT_LMODS"); break;
  993. case ACT_RMODS: debug("ACT_RMODS"); break;
  994. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  995. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  996. case ACT_USAGE: debug("ACT_USAGE"); break;
  997. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  998. case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
  999. case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
  1000. case ACT_MACRO: debug("ACT_MACRO"); break;
  1001. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  1002. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  1003. default: debug("UNKNOWN"); break;
  1004. }
  1005. debug("[");
  1006. debug_hex4(action.kind.param>>8);
  1007. debug(":");
  1008. debug_hex8(action.kind.param & 0xff);
  1009. debug("]");
  1010. }
  1011. #ifndef NO_ACTION_TAPPING
  1012. static void debug_tapping_key(void)
  1013. {
  1014. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  1015. }
  1016. static void debug_waiting_buffer(void)
  1017. {
  1018. debug("{ ");
  1019. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  1020. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  1021. }
  1022. debug("}\n");
  1023. }
  1024. #endif