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

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