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

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