Keyboard firmwares for Atmel AVR and Cortex-M
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. overlay_clear();
  347. keymap_clear();
  348. default_layer_set(action.layer.val);
  349. }
  350. break;
  351. case (OP_RESET | ON_RELEASE):
  352. if (!event.pressed) {
  353. overlay_clear();
  354. keymap_clear();
  355. default_layer_set(action.layer.val);
  356. }
  357. break;
  358. case (OP_RESET | ON_BOTH):
  359. overlay_clear();
  360. keymap_clear();
  361. default_layer_set(action.layer.val);
  362. break;
  363. /* Keymap Bit invert */
  364. case OP_INV:
  365. /* with tap toggle */
  366. if (event.pressed) {
  367. if (tap_count < TAPPING_TOGGLE) {
  368. debug("KEYMAP_INV: tap toggle(press).\n");
  369. keymap_invert(action.layer.val);
  370. }
  371. } else {
  372. if (tap_count <= TAPPING_TOGGLE) {
  373. debug("KEYMAP_INV: tap toggle(release).\n");
  374. keymap_invert(action.layer.val);
  375. }
  376. }
  377. break;
  378. case (OP_INV | ON_PRESS):
  379. if (event.pressed) {
  380. keymap_invert(action.layer.val);
  381. }
  382. break;
  383. case (OP_INV | ON_RELEASE):
  384. if (!event.pressed) {
  385. keymap_invert(action.layer.val);
  386. }
  387. break;
  388. case (OP_INV | ON_BOTH):
  389. keymap_invert(action.layer.val);
  390. break;
  391. /* Keymap Bit on */
  392. case OP_ON:
  393. if (event.pressed) {
  394. keymap_on(action.layer.val);
  395. } else {
  396. keymap_off(action.layer.val);
  397. }
  398. break;
  399. case (OP_ON | ON_PRESS):
  400. if (event.pressed) {
  401. keymap_on(action.layer.val);
  402. }
  403. break;
  404. case (OP_ON | ON_RELEASE):
  405. if (!event.pressed) {
  406. keymap_on(action.layer.val);
  407. }
  408. break;
  409. case (OP_ON | ON_BOTH):
  410. keymap_on(action.layer.val);
  411. break;
  412. /* Keymap Bit off */
  413. case OP_OFF:
  414. if (event.pressed) {
  415. keymap_off(action.layer.val);
  416. } else {
  417. keymap_on(action.layer.val);
  418. }
  419. break;
  420. case (OP_OFF | ON_PRESS):
  421. if (event.pressed) {
  422. keymap_off(action.layer.val);
  423. }
  424. break;
  425. case (OP_OFF | ON_RELEASE):
  426. if (!event.pressed) {
  427. keymap_off(action.layer.val);
  428. }
  429. break;
  430. case (OP_OFF | ON_BOTH):
  431. keymap_off(action.layer.val);
  432. break;
  433. /* Keymap Bit set */
  434. case OP_SET:
  435. if (event.pressed) {
  436. keymap_set(action.layer.val);
  437. } else {
  438. keymap_clear();
  439. }
  440. break;
  441. case (OP_SET | ON_PRESS):
  442. if (event.pressed) {
  443. keymap_set(action.layer.val);
  444. }
  445. break;
  446. case (OP_SET | ON_RELEASE):
  447. if (!event.pressed) {
  448. keymap_set(action.layer.val);
  449. }
  450. break;
  451. case (OP_SET | ON_BOTH):
  452. keymap_set(action.layer.val);
  453. break;
  454. /* Keymap Bit invert with tap key */
  455. default:
  456. if (event.pressed) {
  457. if (tap_count > 0) {
  458. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  459. register_code(action.layer.code);
  460. } else {
  461. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  462. keymap_on(action.layer.val);
  463. }
  464. } else {
  465. if (tap_count > 0) {
  466. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  467. unregister_code(action.layer.code);
  468. } else {
  469. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  470. keymap_off(action.layer.val);
  471. }
  472. }
  473. break;
  474. }
  475. break;
  476. case ACT_OVERLAY:
  477. switch (action.layer.code) {
  478. // Overlay Invert bit4
  479. case OP_INV4 | 0:
  480. if (action.layer.val == 0) {
  481. overlay_clear();
  482. } else {
  483. overlay_set(overlay_stat ^ action.layer.val);
  484. }
  485. break;
  486. case OP_INV4 | 1:
  487. if (action.layer.val == 0) {
  488. if (event.pressed) overlay_clear();
  489. } else {
  490. overlay_set(overlay_stat ^ action.layer.val<<4);
  491. }
  492. break;
  493. case OP_INV4 | 2:
  494. if (action.layer.val == 0) {
  495. if (!event.pressed) overlay_clear();
  496. } else {
  497. overlay_set(overlay_stat ^ action.layer.val<<8);
  498. }
  499. break;
  500. case OP_INV4 | 3:
  501. if (action.layer.val == 0) {
  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. debug_tapping_key();
  651. process_action(&tapping_key);
  652. // enqueue
  653. keyp->tap_count = tapping_key.tap_count;
  654. return false;
  655. }
  656. #if TAPPING_TERM >= 500
  657. /* This can prevent from typing some tap keys in a row at a time. */
  658. else if (!event.pressed && waiting_buffer_typed(event)) {
  659. // other key typed. not tap.
  660. debug("Tapping: End. No tap. Interfered by typing key\n");
  661. process_action(&tapping_key);
  662. tapping_key = (keyrecord_t){};
  663. debug_tapping_key();
  664. // enqueue
  665. return false;
  666. }
  667. #endif
  668. else {
  669. // other key events shall be enq'd till tapping state settles.
  670. return false;
  671. }
  672. }
  673. // tap_count > 0
  674. else {
  675. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  676. debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
  677. keyp->tap_count = tapping_key.tap_count;
  678. process_action(keyp);
  679. tapping_key = *keyp;
  680. debug_tapping_key();
  681. return true;
  682. }
  683. else if (is_tap_key(keyp->event.key) && event.pressed) {
  684. if (tapping_key.tap_count > 1) {
  685. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  686. // unregister key
  687. process_action(&(keyrecord_t){
  688. .tap_count = tapping_key.tap_count,
  689. .event.key = tapping_key.event.key,
  690. .event.time = event.time,
  691. .event.pressed = false
  692. });
  693. } else {
  694. debug("Tapping: Start while last tap(1).\n");
  695. }
  696. tapping_key = *keyp;
  697. waiting_buffer_scan_tap();
  698. debug_tapping_key();
  699. return true;
  700. }
  701. else {
  702. if (!IS_NOEVENT(keyp->event)) {
  703. debug("Tapping: key event while last tap(>0).\n");
  704. }
  705. process_action(keyp);
  706. return true;
  707. }
  708. }
  709. }
  710. // after TAPPING_TERM
  711. else {
  712. if (tapping_key.tap_count == 0) {
  713. debug("Tapping: End. Timeout. Not tap(0): ");
  714. debug_event(event); debug("\n");
  715. process_action(&tapping_key);
  716. tapping_key = (keyrecord_t){};
  717. debug_tapping_key();
  718. return false;
  719. } else {
  720. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  721. debug("Tapping: End. last timeout tap release(>0).");
  722. keyp->tap_count = tapping_key.tap_count;
  723. process_action(keyp);
  724. tapping_key = (keyrecord_t){};
  725. return true;
  726. }
  727. else if (is_tap_key(keyp->event.key) && event.pressed) {
  728. if (tapping_key.tap_count > 1) {
  729. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  730. // unregister key
  731. process_action(&(keyrecord_t){
  732. .tap_count = tapping_key.tap_count,
  733. .event.key = tapping_key.event.key,
  734. .event.time = event.time,
  735. .event.pressed = false
  736. });
  737. } else {
  738. debug("Tapping: Start while last timeout tap(1).\n");
  739. }
  740. tapping_key = *keyp;
  741. waiting_buffer_scan_tap();
  742. debug_tapping_key();
  743. return true;
  744. }
  745. else {
  746. if (!IS_NOEVENT(keyp->event)) {
  747. debug("Tapping: key event while last timeout tap(>0).\n");
  748. }
  749. process_action(keyp);
  750. return true;
  751. }
  752. }
  753. }
  754. } else if (IS_TAPPING_RELEASED()) {
  755. if (WITHIN_TAPPING_TERM(event)) {
  756. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  757. // sequential tap.
  758. keyp->tap_count = tapping_key.tap_count + 1;
  759. debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
  760. process_action(keyp);
  761. tapping_key = *keyp;
  762. debug_tapping_key();
  763. return true;
  764. } else if (event.pressed && is_tap_key(event.key)) {
  765. // Sequential tap can be interfered with other tap key.
  766. debug("Tapping: Start with interfering other tap.\n");
  767. tapping_key = *keyp;
  768. waiting_buffer_scan_tap();
  769. debug_tapping_key();
  770. return true;
  771. } else {
  772. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  773. process_action(keyp);
  774. return true;
  775. }
  776. } else {
  777. // timeout. no sequential tap.
  778. debug("Tapping: End(Timeout after releasing last tap): ");
  779. debug_event(event); debug("\n");
  780. tapping_key = (keyrecord_t){};
  781. debug_tapping_key();
  782. return false;
  783. }
  784. }
  785. // not tapping satate
  786. else {
  787. if (event.pressed && is_tap_key(event.key)) {
  788. debug("Tapping: Start(Press tap key).\n");
  789. tapping_key = *keyp;
  790. waiting_buffer_scan_tap();
  791. debug_tapping_key();
  792. return true;
  793. } else {
  794. process_action(keyp);
  795. return true;
  796. }
  797. }
  798. }
  799. /* scan buffer for tapping */
  800. static void waiting_buffer_scan_tap(void)
  801. {
  802. // tapping already is settled
  803. if (tapping_key.tap_count > 0) return;
  804. // invalid state: tapping_key released && tap_count == 0
  805. if (!tapping_key.event.pressed) return;
  806. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  807. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  808. !waiting_buffer[i].event.pressed &&
  809. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  810. tapping_key.tap_count = 1;
  811. waiting_buffer[i].tap_count = 1;
  812. process_action(&tapping_key);
  813. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  814. debug_waiting_buffer();
  815. return;
  816. }
  817. }
  818. }
  819. /*
  820. * Utilities for actions.
  821. */
  822. void register_code(uint8_t code)
  823. {
  824. if (code == KC_NO) {
  825. return;
  826. }
  827. else if IS_KEY(code) {
  828. // TODO: should push command_proc out of this block?
  829. if (command_proc(code)) return;
  830. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  831. uint8_t tmp_mods = host_get_mods();
  832. host_add_mods(oneshot_state.mods);
  833. host_add_key(code);
  834. host_send_keyboard_report();
  835. host_set_mods(tmp_mods);
  836. oneshot_state.ready = false;
  837. } else {
  838. host_add_key(code);
  839. host_send_keyboard_report();
  840. }
  841. }
  842. else if IS_MOD(code) {
  843. host_add_mods(MOD_BIT(code));
  844. host_send_keyboard_report();
  845. }
  846. }
  847. void unregister_code(uint8_t code)
  848. {
  849. if IS_KEY(code) {
  850. host_del_key(code);
  851. host_send_keyboard_report();
  852. }
  853. else if IS_MOD(code) {
  854. host_del_mods(MOD_BIT(code));
  855. host_send_keyboard_report();
  856. }
  857. }
  858. void add_mods(uint8_t mods)
  859. {
  860. if (mods) {
  861. host_add_mods(mods);
  862. host_send_keyboard_report();
  863. }
  864. }
  865. void del_mods(uint8_t mods)
  866. {
  867. if (mods) {
  868. host_del_mods(mods);
  869. host_send_keyboard_report();
  870. }
  871. }
  872. void set_mods(uint8_t mods)
  873. {
  874. host_set_mods(mods);
  875. host_send_keyboard_report();
  876. }
  877. void clear_keyboard(void)
  878. {
  879. host_clear_mods();
  880. clear_keyboard_but_mods();
  881. }
  882. void clear_keyboard_but_mods(void)
  883. {
  884. host_clear_keys();
  885. host_send_keyboard_report();
  886. #ifdef MOUSEKEY_ENABLE
  887. mousekey_clear();
  888. mousekey_send();
  889. #endif
  890. #ifdef EXTRAKEY_ENABLE
  891. host_system_send(0);
  892. host_consumer_send(0);
  893. #endif
  894. }
  895. bool sending_anykey(void)
  896. {
  897. return (host_has_anykey() || host_mouse_in_use() ||
  898. host_last_sysytem_report() || host_last_consumer_report());
  899. }
  900. bool is_tap_key(key_t key)
  901. {
  902. action_t action = layer_switch_get_action(key);
  903. switch (action.kind.id) {
  904. case ACT_LMODS_TAP:
  905. case ACT_RMODS_TAP:
  906. return true;
  907. case ACT_KEYMAP:
  908. case ACT_OVERLAY:
  909. switch (action.layer.code) {
  910. case 0x04 ... 0xEF: /* tap key */
  911. case OP_INV:
  912. return true;
  913. default:
  914. return false;
  915. }
  916. case ACT_FUNCTION:
  917. if (action.func.opt & FUNC_TAP) { return true; }
  918. return false;
  919. }
  920. return false;
  921. }
  922. /*
  923. * debug print
  924. */
  925. static void debug_event(keyevent_t event)
  926. {
  927. debug_hex16((event.key.row<<8) | event.key.col);
  928. if (event.pressed) debug("d("); else debug("u(");
  929. debug_dec(event.time); debug(")");
  930. }
  931. static void debug_record(keyrecord_t record)
  932. {
  933. debug_event(record.event); debug(":"); debug_dec(record.tap_count);
  934. }
  935. static void debug_action(action_t action)
  936. {
  937. switch (action.kind.id) {
  938. case ACT_LMODS: debug("ACT_LMODS"); break;
  939. case ACT_RMODS: debug("ACT_RMODS"); break;
  940. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  941. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  942. case ACT_USAGE: debug("ACT_USAGE"); break;
  943. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  944. case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
  945. case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
  946. case ACT_MACRO: debug("ACT_MACRO"); break;
  947. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  948. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  949. default: debug("UNKNOWN"); break;
  950. }
  951. debug("[");
  952. debug_hex4(action.kind.param>>8);
  953. debug(":");
  954. debug_hex8(action.kind.param & 0xff);
  955. debug("]");
  956. }
  957. static void debug_tapping_key(void)
  958. {
  959. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  960. }
  961. static void debug_waiting_buffer(void)
  962. {
  963. debug("{ ");
  964. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  965. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  966. }
  967. debug("}\n");
  968. }