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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

action.c 35KB

11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
11 lat temu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. overlay_clear();
  323. keymap_clear();
  324. break;
  325. case ON_PRESS:
  326. if (event.pressed) {
  327. overlay_clear();
  328. keymap_clear();
  329. }
  330. break;
  331. case ON_RELEASE:
  332. if (!event.pressed) {
  333. overlay_clear();
  334. keymap_clear();
  335. }
  336. break;
  337. case ON_BOTH:
  338. overlay_clear();
  339. keymap_clear();
  340. break;
  341. }
  342. break;
  343. /* Keymap Reset default layer */
  344. case (OP_RESET | ON_PRESS):
  345. if (event.pressed) {
  346. default_layer_set(action.layer.val);
  347. }
  348. break;
  349. case (OP_RESET | ON_RELEASE):
  350. if (!event.pressed) {
  351. default_layer_set(action.layer.val);
  352. }
  353. break;
  354. case (OP_RESET | ON_BOTH):
  355. default_layer_set(action.layer.val);
  356. break;
  357. /* Keymap Bit invert */
  358. case OP_INV:
  359. /* with tap toggle */
  360. if (event.pressed) {
  361. if (tap_count < TAPPING_TOGGLE) {
  362. debug("KEYMAP_INV: tap toggle(press).\n");
  363. keymap_invert(action.layer.val);
  364. }
  365. } else {
  366. if (tap_count <= TAPPING_TOGGLE) {
  367. debug("KEYMAP_INV: tap toggle(release).\n");
  368. keymap_invert(action.layer.val);
  369. }
  370. }
  371. break;
  372. case (OP_INV | ON_PRESS):
  373. if (event.pressed) {
  374. keymap_invert(action.layer.val);
  375. }
  376. break;
  377. case (OP_INV | ON_RELEASE):
  378. if (!event.pressed) {
  379. keymap_invert(action.layer.val);
  380. }
  381. break;
  382. case (OP_INV | ON_BOTH):
  383. keymap_invert(action.layer.val);
  384. break;
  385. /* Keymap Bit on */
  386. case OP_ON:
  387. if (event.pressed) {
  388. keymap_on(action.layer.val);
  389. } else {
  390. keymap_off(action.layer.val);
  391. }
  392. break;
  393. case (OP_ON | ON_PRESS):
  394. if (event.pressed) {
  395. keymap_on(action.layer.val);
  396. }
  397. break;
  398. case (OP_ON | ON_RELEASE):
  399. if (!event.pressed) {
  400. keymap_on(action.layer.val);
  401. }
  402. break;
  403. case (OP_ON | ON_BOTH):
  404. keymap_on(action.layer.val);
  405. break;
  406. /* Keymap Bit off */
  407. case OP_OFF:
  408. if (event.pressed) {
  409. keymap_off(action.layer.val);
  410. } else {
  411. keymap_on(action.layer.val);
  412. }
  413. break;
  414. case (OP_OFF | ON_PRESS):
  415. if (event.pressed) {
  416. keymap_off(action.layer.val);
  417. }
  418. break;
  419. case (OP_OFF | ON_RELEASE):
  420. if (!event.pressed) {
  421. keymap_off(action.layer.val);
  422. }
  423. break;
  424. case (OP_OFF | ON_BOTH):
  425. keymap_off(action.layer.val);
  426. break;
  427. /* Keymap Bit set */
  428. case OP_SET:
  429. if (event.pressed) {
  430. keymap_set(action.layer.val);
  431. } else {
  432. keymap_clear();
  433. }
  434. break;
  435. case (OP_SET | ON_PRESS):
  436. if (event.pressed) {
  437. keymap_set(action.layer.val);
  438. }
  439. break;
  440. case (OP_SET | ON_RELEASE):
  441. if (!event.pressed) {
  442. keymap_set(action.layer.val);
  443. }
  444. break;
  445. case (OP_SET | ON_BOTH):
  446. keymap_set(action.layer.val);
  447. break;
  448. /* Keymap Bit invert with tap key */
  449. default:
  450. if (event.pressed) {
  451. if (tap_count > 0) {
  452. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  453. register_code(action.layer.code);
  454. } else {
  455. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  456. keymap_on(action.layer.val);
  457. }
  458. } else {
  459. if (tap_count > 0) {
  460. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  461. unregister_code(action.layer.code);
  462. } else {
  463. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  464. keymap_off(action.layer.val);
  465. }
  466. }
  467. break;
  468. }
  469. break;
  470. case ACT_OVERLAY:
  471. switch (action.layer.code) {
  472. // Overlay Invert bit4
  473. case OP_INV4 | 0:
  474. if (action.layer.val == 0) {
  475. overlay_clear();
  476. } else {
  477. overlay_set(overlay_stat ^ action.layer.val);
  478. }
  479. break;
  480. case OP_INV4 | 1:
  481. if (action.layer.val == 0) {
  482. if (event.pressed) overlay_clear();
  483. } else {
  484. overlay_set(overlay_stat ^ action.layer.val<<4);
  485. }
  486. break;
  487. case OP_INV4 | 2:
  488. if (action.layer.val == 0) {
  489. if (!event.pressed) overlay_clear();
  490. } else {
  491. overlay_set(overlay_stat ^ action.layer.val<<8);
  492. }
  493. break;
  494. case OP_INV4 | 3:
  495. if (action.layer.val == 0) {
  496. overlay_clear();
  497. } else {
  498. overlay_set(overlay_stat ^ action.layer.val<<12);
  499. }
  500. break;
  501. /* Overlay Bit invert */
  502. case OP_INV:
  503. /* with tap toggle */
  504. if (event.pressed) {
  505. if (tap_count < TAPPING_TOGGLE) {
  506. debug("OVERLAY_INV: tap toggle(press).\n");
  507. overlay_invert(action.layer.val);
  508. }
  509. } else {
  510. if (tap_count <= TAPPING_TOGGLE) {
  511. debug("OVERLAY_INV: tap toggle(release).\n");
  512. overlay_invert(action.layer.val);
  513. }
  514. }
  515. break;
  516. case (OP_INV | ON_PRESS):
  517. if (event.pressed) {
  518. overlay_invert(action.layer.val);
  519. }
  520. break;
  521. case (OP_INV | ON_RELEASE):
  522. if (!event.pressed) {
  523. overlay_invert(action.layer.val);
  524. }
  525. break;
  526. case (OP_INV | ON_BOTH):
  527. overlay_invert(action.layer.val);
  528. break;
  529. /* Overlay Bit on */
  530. case OP_ON:
  531. if (event.pressed) {
  532. overlay_on(action.layer.val);
  533. } else {
  534. overlay_off(action.layer.val);
  535. }
  536. break;
  537. case (OP_ON | ON_PRESS):
  538. if (event.pressed) {
  539. overlay_on(action.layer.val);
  540. }
  541. break;
  542. case (OP_ON | ON_RELEASE):
  543. if (!event.pressed) {
  544. overlay_on(action.layer.val);
  545. }
  546. break;
  547. case (OP_ON | ON_BOTH):
  548. overlay_on(action.layer.val);
  549. break;
  550. /* Overlay Bit off */
  551. case OP_OFF:
  552. if (event.pressed) {
  553. overlay_off(action.layer.val);
  554. } else {
  555. overlay_on(action.layer.val);
  556. }
  557. break;
  558. case (OP_OFF | ON_PRESS):
  559. if (event.pressed) {
  560. overlay_off(action.layer.val);
  561. }
  562. break;
  563. case (OP_OFF | ON_RELEASE):
  564. if (!event.pressed) {
  565. overlay_off(action.layer.val);
  566. }
  567. break;
  568. case (OP_OFF | ON_BOTH):
  569. overlay_off(action.layer.val);
  570. break;
  571. /* Overlay Bit set */
  572. case OP_SET:
  573. if (event.pressed) {
  574. overlay_move(action.layer.val);
  575. } else {
  576. overlay_clear();
  577. }
  578. break;
  579. case (OP_SET | ON_PRESS):
  580. if (event.pressed) {
  581. overlay_move(action.layer.val);
  582. }
  583. break;
  584. case (OP_SET | ON_RELEASE):
  585. if (!event.pressed) {
  586. overlay_move(action.layer.val);
  587. }
  588. break;
  589. case (OP_SET | ON_BOTH):
  590. overlay_move(action.layer.val);
  591. break;
  592. /* Overlay Bit invert with tap key */
  593. default:
  594. if (event.pressed) {
  595. if (tap_count > 0) {
  596. debug("OVERLAY_TAP_KEY: Tap: register_code\n");
  597. register_code(action.layer.code);
  598. } else {
  599. debug("OVERLAY_TAP_KEY: No tap: On on press\n");
  600. overlay_on(action.layer.val);
  601. }
  602. } else {
  603. if (tap_count > 0) {
  604. debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
  605. unregister_code(action.layer.code);
  606. } else {
  607. debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
  608. overlay_off(action.layer.val);
  609. }
  610. }
  611. break;
  612. }
  613. break;
  614. /* Extentions */
  615. case ACT_MACRO:
  616. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  617. break;
  618. case ACT_COMMAND:
  619. break;
  620. case ACT_FUNCTION:
  621. action_function(record, action.func.id, action.func.opt);
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. /* Tapping
  628. *
  629. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  630. * (without interfering by typing other key)
  631. */
  632. /* return true when key event is processed or consumed. */
  633. static bool process_tapping(keyrecord_t *keyp)
  634. {
  635. keyevent_t event = keyp->event;
  636. // if tapping
  637. if (IS_TAPPING_PRESSED()) {
  638. if (WITHIN_TAPPING_TERM(event)) {
  639. if (tapping_key.tap.count == 0) {
  640. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  641. // first tap!
  642. debug("Tapping: First tap(0->1).\n");
  643. tapping_key.tap.count = 1;
  644. tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
  645. debug_tapping_key();
  646. process_action(&tapping_key);
  647. // enqueue
  648. keyp->tap = tapping_key.tap;
  649. return false;
  650. }
  651. #if TAPPING_TERM >= 500
  652. /* This can prevent from typing some tap keys in a row at a time. */
  653. else if (!event.pressed && waiting_buffer_typed(event)) {
  654. // other key typed. not tap.
  655. debug("Tapping: End. No tap. Interfered by typing key\n");
  656. process_action(&tapping_key);
  657. tapping_key = (keyrecord_t){};
  658. debug_tapping_key();
  659. // enqueue
  660. return false;
  661. }
  662. #endif
  663. else {
  664. // other key events shall be enq'd till tapping state settles.
  665. return false;
  666. }
  667. }
  668. // tap_count > 0
  669. else {
  670. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  671. debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
  672. keyp->tap = tapping_key.tap;
  673. process_action(keyp);
  674. tapping_key = *keyp;
  675. debug_tapping_key();
  676. return true;
  677. }
  678. else if (is_tap_key(keyp->event.key) && event.pressed) {
  679. if (tapping_key.tap.count > 1) {
  680. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  681. // unregister key
  682. process_action(&(keyrecord_t){
  683. .tap = tapping_key.tap,
  684. .event.key = tapping_key.event.key,
  685. .event.time = event.time,
  686. .event.pressed = false
  687. });
  688. } else {
  689. debug("Tapping: Start while last tap(1).\n");
  690. }
  691. tapping_key = *keyp;
  692. waiting_buffer_scan_tap();
  693. debug_tapping_key();
  694. return true;
  695. }
  696. else {
  697. if (!IS_NOEVENT(keyp->event)) {
  698. debug("Tapping: key event while last tap(>0).\n");
  699. }
  700. process_action(keyp);
  701. return true;
  702. }
  703. }
  704. }
  705. // after TAPPING_TERM
  706. else {
  707. if (tapping_key.tap.count == 0) {
  708. debug("Tapping: End. Timeout. Not tap(0): ");
  709. debug_event(event); debug("\n");
  710. process_action(&tapping_key);
  711. tapping_key = (keyrecord_t){};
  712. debug_tapping_key();
  713. return false;
  714. } else {
  715. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  716. debug("Tapping: End. last timeout tap release(>0).");
  717. keyp->tap = tapping_key.tap;
  718. process_action(keyp);
  719. tapping_key = (keyrecord_t){};
  720. return true;
  721. }
  722. else if (is_tap_key(keyp->event.key) && event.pressed) {
  723. if (tapping_key.tap.count > 1) {
  724. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  725. // unregister key
  726. process_action(&(keyrecord_t){
  727. .tap = tapping_key.tap,
  728. .event.key = tapping_key.event.key,
  729. .event.time = event.time,
  730. .event.pressed = false
  731. });
  732. } else {
  733. debug("Tapping: Start while last timeout tap(1).\n");
  734. }
  735. tapping_key = *keyp;
  736. waiting_buffer_scan_tap();
  737. debug_tapping_key();
  738. return true;
  739. }
  740. else {
  741. if (!IS_NOEVENT(keyp->event)) {
  742. debug("Tapping: key event while last timeout tap(>0).\n");
  743. }
  744. process_action(keyp);
  745. return true;
  746. }
  747. }
  748. }
  749. } else if (IS_TAPPING_RELEASED()) {
  750. if (WITHIN_TAPPING_TERM(event)) {
  751. if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  752. // sequential tap.
  753. keyp->tap = tapping_key.tap;
  754. keyp->tap.count += 1;
  755. debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
  756. process_action(keyp);
  757. tapping_key = *keyp;
  758. debug_tapping_key();
  759. return true;
  760. } else if (event.pressed && is_tap_key(event.key)) {
  761. // Sequential tap can be interfered with other tap key.
  762. debug("Tapping: Start with interfering other tap.\n");
  763. tapping_key = *keyp;
  764. waiting_buffer_scan_tap();
  765. debug_tapping_key();
  766. return true;
  767. } else {
  768. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  769. process_action(keyp);
  770. return true;
  771. }
  772. } else {
  773. // timeout. no sequential tap.
  774. debug("Tapping: End(Timeout after releasing last tap): ");
  775. debug_event(event); debug("\n");
  776. tapping_key = (keyrecord_t){};
  777. debug_tapping_key();
  778. return false;
  779. }
  780. }
  781. // not tapping satate
  782. else {
  783. if (event.pressed && is_tap_key(event.key)) {
  784. debug("Tapping: Start(Press tap key).\n");
  785. tapping_key = *keyp;
  786. waiting_buffer_scan_tap();
  787. debug_tapping_key();
  788. return true;
  789. } else {
  790. process_action(keyp);
  791. return true;
  792. }
  793. }
  794. }
  795. /* scan buffer for tapping */
  796. static void waiting_buffer_scan_tap(void)
  797. {
  798. // tapping already is settled
  799. if (tapping_key.tap.count > 0) return;
  800. // invalid state: tapping_key released && tap.count == 0
  801. if (!tapping_key.event.pressed) return;
  802. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  803. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  804. !waiting_buffer[i].event.pressed &&
  805. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  806. tapping_key.tap.count = 1;
  807. waiting_buffer[i].tap.count = 1;
  808. process_action(&tapping_key);
  809. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  810. debug_waiting_buffer();
  811. return;
  812. }
  813. }
  814. }
  815. /*
  816. * Utilities for actions.
  817. */
  818. void register_code(uint8_t code)
  819. {
  820. if (code == KC_NO) {
  821. return;
  822. }
  823. else if IS_KEY(code) {
  824. // TODO: should push command_proc out of this block?
  825. if (command_proc(code)) return;
  826. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  827. uint8_t tmp_mods = host_get_mods();
  828. host_add_mods(oneshot_state.mods);
  829. host_add_key(code);
  830. host_send_keyboard_report();
  831. host_set_mods(tmp_mods);
  832. oneshot_state.ready = false;
  833. } else {
  834. host_add_key(code);
  835. host_send_keyboard_report();
  836. }
  837. }
  838. else if IS_MOD(code) {
  839. host_add_mods(MOD_BIT(code));
  840. host_send_keyboard_report();
  841. }
  842. }
  843. void unregister_code(uint8_t code)
  844. {
  845. if IS_KEY(code) {
  846. host_del_key(code);
  847. host_send_keyboard_report();
  848. }
  849. else if IS_MOD(code) {
  850. host_del_mods(MOD_BIT(code));
  851. host_send_keyboard_report();
  852. }
  853. }
  854. void add_mods(uint8_t mods)
  855. {
  856. if (mods) {
  857. host_add_mods(mods);
  858. host_send_keyboard_report();
  859. }
  860. }
  861. void del_mods(uint8_t mods)
  862. {
  863. if (mods) {
  864. host_del_mods(mods);
  865. host_send_keyboard_report();
  866. }
  867. }
  868. void set_mods(uint8_t mods)
  869. {
  870. host_set_mods(mods);
  871. host_send_keyboard_report();
  872. }
  873. void clear_keyboard(void)
  874. {
  875. host_clear_mods();
  876. clear_keyboard_but_mods();
  877. }
  878. void clear_keyboard_but_mods(void)
  879. {
  880. host_clear_keys();
  881. host_send_keyboard_report();
  882. #ifdef MOUSEKEY_ENABLE
  883. mousekey_clear();
  884. mousekey_send();
  885. #endif
  886. #ifdef EXTRAKEY_ENABLE
  887. host_system_send(0);
  888. host_consumer_send(0);
  889. #endif
  890. }
  891. bool sending_anykey(void)
  892. {
  893. return (host_has_anykey() || host_mouse_in_use() ||
  894. host_last_sysytem_report() || host_last_consumer_report());
  895. }
  896. bool is_tap_key(key_t key)
  897. {
  898. action_t action = layer_switch_get_action(key);
  899. switch (action.kind.id) {
  900. case ACT_LMODS_TAP:
  901. case ACT_RMODS_TAP:
  902. return true;
  903. case ACT_KEYMAP:
  904. case ACT_OVERLAY:
  905. switch (action.layer.code) {
  906. case 0x04 ... 0xEF: /* tap key */
  907. case OP_INV:
  908. return true;
  909. default:
  910. return false;
  911. }
  912. case ACT_MACRO:
  913. case ACT_FUNCTION:
  914. if (action.func.opt & FUNC_TAP) { return true; }
  915. return false;
  916. }
  917. return false;
  918. }
  919. /*
  920. * debug print
  921. */
  922. static void debug_event(keyevent_t event)
  923. {
  924. debug_hex16((event.key.row<<8) | event.key.col);
  925. if (event.pressed) debug("d("); else debug("u(");
  926. debug_dec(event.time); debug(")");
  927. }
  928. static void debug_record(keyrecord_t record)
  929. {
  930. debug_event(record.event); debug(":"); debug_dec(record.tap.count);
  931. if (record.tap.interrupted) debug("-");
  932. }
  933. static void debug_action(action_t action)
  934. {
  935. switch (action.kind.id) {
  936. case ACT_LMODS: debug("ACT_LMODS"); break;
  937. case ACT_RMODS: debug("ACT_RMODS"); break;
  938. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  939. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  940. case ACT_USAGE: debug("ACT_USAGE"); break;
  941. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  942. case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
  943. case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
  944. case ACT_MACRO: debug("ACT_MACRO"); break;
  945. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  946. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  947. default: debug("UNKNOWN"); break;
  948. }
  949. debug("[");
  950. debug_hex4(action.kind.param>>8);
  951. debug(":");
  952. debug_hex8(action.kind.param & 0xff);
  953. debug("]");
  954. }
  955. static void debug_tapping_key(void)
  956. {
  957. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  958. }
  959. static void debug_waiting_buffer(void)
  960. {
  961. debug("{ ");
  962. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  963. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  964. }
  965. debug("}\n");
  966. }