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.

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