Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

action.c 35KB

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