Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

action.c 34KB

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