Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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_switch.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_switch */
  179. action = layer_switch_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_SWITCH:
  488. switch (action.layer.code) {
  489. case LAYER_MOMENTARY: /* momentary */
  490. if (event.pressed) {
  491. layer_switch_on(action.layer.val);
  492. } else {
  493. layer_switch_off(action.layer.val);
  494. }
  495. break;
  496. case LAYER_ON_PRESS:
  497. if (event.pressed) {
  498. layer_switch_inv(action.layer.val);
  499. }
  500. break;
  501. case LAYER_ON_RELEASE:
  502. if (!event.pressed) {
  503. layer_switch_inv(action.layer.val);
  504. }
  505. break;
  506. case LAYER_ON_BOTH:
  507. layer_switch_inv(action.layer.val);
  508. break;
  509. case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
  510. if (event.pressed) {
  511. if (tap_count < TAPPING_TOGGLE) {
  512. debug("LAYER_SWITCH: tap toggle(press).\n");
  513. layer_switch_inv(action.layer.val);
  514. }
  515. } else {
  516. if (tap_count <= TAPPING_TOGGLE) {
  517. debug("LAYER_SWITCH: tap toggle(release).\n");
  518. layer_switch_inv(action.layer.val);
  519. }
  520. }
  521. break;
  522. default:
  523. // tap key
  524. if (event.pressed) {
  525. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  526. debug("LAYER_SWITCH: Tap: register_code\n");
  527. register_code(action.layer.code);
  528. } else {
  529. debug("LAYER_SWITCH: No tap: layer_switch(on press)\n");
  530. layer_switch_inv(action.layer.val);
  531. }
  532. } else {
  533. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  534. debug("LAYER_SWITCH: Tap: unregister_code\n");
  535. unregister_code(action.layer.code);
  536. } else {
  537. debug("LAYER_SWITCH: No tap: layer_switch(on release)\n");
  538. layer_switch_inv(action.layer.val);
  539. }
  540. }
  541. break;
  542. }
  543. break;
  544. /* Extentions */
  545. case ACT_MACRO:
  546. // TODO
  547. break;
  548. case ACT_COMMAND:
  549. break;
  550. case ACT_FUNCTION:
  551. action_function(record, action.func.id, action.func.opt);
  552. break;
  553. default:
  554. break;
  555. }
  556. }
  557. /* Tapping
  558. *
  559. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  560. * (without interfering by typing other key)
  561. */
  562. /* return true when key event is processed or consumed. */
  563. static bool process_tapping(keyrecord_t *keyp)
  564. {
  565. keyevent_t event = keyp->event;
  566. // if tapping
  567. if (IS_TAPPING_PRESSED()) {
  568. if (WITHIN_TAPPING_TERM(event)) {
  569. if (tapping_key.tap_count == 0) {
  570. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  571. // first tap!
  572. debug("Tapping: First tap(0->1).\n");
  573. tapping_key.tap_count = 1;
  574. debug_tapping_key();
  575. process_action(&tapping_key);
  576. // enqueue
  577. keyp->tap_count = tapping_key.tap_count;
  578. return false;
  579. }
  580. #if TAPPING_TERM >= 500
  581. /* This can prevent from typing some tap keys in a row at a time. */
  582. else if (!event.pressed && waiting_buffer_typed(event)) {
  583. // other key typed. not tap.
  584. debug("Tapping: End. No tap. Interfered by typing key\n");
  585. process_action(&tapping_key);
  586. tapping_key = (keyrecord_t){};
  587. debug_tapping_key();
  588. // enqueue
  589. return false;
  590. }
  591. #endif
  592. else {
  593. // other key events shall be enq'd till tapping state settles.
  594. return false;
  595. }
  596. }
  597. // tap_count > 0
  598. else {
  599. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  600. debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
  601. keyp->tap_count = tapping_key.tap_count;
  602. process_action(keyp);
  603. tapping_key = *keyp;
  604. debug_tapping_key();
  605. return true;
  606. }
  607. else if (is_tap_key(keyp->event.key) && event.pressed) {
  608. if (tapping_key.tap_count > 1) {
  609. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  610. // unregister key
  611. process_action(&(keyrecord_t){
  612. .tap_count = tapping_key.tap_count,
  613. .event.key = tapping_key.event.key,
  614. .event.time = event.time,
  615. .event.pressed = false
  616. });
  617. } else {
  618. debug("Tapping: Start while last tap(1).\n");
  619. }
  620. tapping_key = *keyp;
  621. waiting_buffer_scan_tap();
  622. debug_tapping_key();
  623. return true;
  624. }
  625. else {
  626. if (!IS_NOEVENT(keyp->event)) {
  627. debug("Tapping: key event while last tap(>0).\n");
  628. }
  629. process_action(keyp);
  630. return true;
  631. }
  632. }
  633. }
  634. // after TAPPING_TERM
  635. else {
  636. if (tapping_key.tap_count == 0) {
  637. debug("Tapping: End. Timeout. Not tap(0): ");
  638. debug_event(event); debug("\n");
  639. process_action(&tapping_key);
  640. tapping_key = (keyrecord_t){};
  641. debug_tapping_key();
  642. return false;
  643. } else {
  644. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  645. debug("Tapping: End. last timeout tap release(>0).");
  646. keyp->tap_count = tapping_key.tap_count;
  647. process_action(keyp);
  648. tapping_key = (keyrecord_t){};
  649. return true;
  650. }
  651. else if (is_tap_key(keyp->event.key) && event.pressed) {
  652. if (tapping_key.tap_count > 1) {
  653. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  654. // unregister key
  655. process_action(&(keyrecord_t){
  656. .tap_count = tapping_key.tap_count,
  657. .event.key = tapping_key.event.key,
  658. .event.time = event.time,
  659. .event.pressed = false
  660. });
  661. } else {
  662. debug("Tapping: Start while last timeout tap(1).\n");
  663. }
  664. tapping_key = *keyp;
  665. waiting_buffer_scan_tap();
  666. debug_tapping_key();
  667. return true;
  668. }
  669. else {
  670. if (!IS_NOEVENT(keyp->event)) {
  671. debug("Tapping: key event while last timeout tap(>0).\n");
  672. }
  673. process_action(keyp);
  674. return true;
  675. }
  676. }
  677. }
  678. } else if (IS_TAPPING_RELEASED()) {
  679. if (WITHIN_TAPPING_TERM(event)) {
  680. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  681. // sequential tap.
  682. keyp->tap_count = tapping_key.tap_count + 1;
  683. debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
  684. process_action(keyp);
  685. tapping_key = *keyp;
  686. debug_tapping_key();
  687. return true;
  688. } else if (event.pressed && is_tap_key(event.key)) {
  689. // Sequential tap can be interfered with other tap key.
  690. debug("Tapping: Start with interfering other tap.\n");
  691. tapping_key = *keyp;
  692. waiting_buffer_scan_tap();
  693. debug_tapping_key();
  694. return true;
  695. } else {
  696. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  697. process_action(keyp);
  698. return true;
  699. }
  700. } else {
  701. // timeout. no sequential tap.
  702. debug("Tapping: End(Timeout after releasing last tap): ");
  703. debug_event(event); debug("\n");
  704. tapping_key = (keyrecord_t){};
  705. debug_tapping_key();
  706. return false;
  707. }
  708. }
  709. // not tapping satate
  710. else {
  711. if (event.pressed && is_tap_key(event.key)) {
  712. debug("Tapping: Start(Press tap key).\n");
  713. tapping_key = *keyp;
  714. waiting_buffer_scan_tap();
  715. debug_tapping_key();
  716. return true;
  717. } else {
  718. process_action(keyp);
  719. return true;
  720. }
  721. }
  722. }
  723. /* scan buffer for tapping */
  724. static void waiting_buffer_scan_tap(void)
  725. {
  726. // tapping already is settled
  727. if (tapping_key.tap_count > 0) return;
  728. // invalid state: tapping_key released && tap_count == 0
  729. if (!tapping_key.event.pressed) return;
  730. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  731. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  732. !waiting_buffer[i].event.pressed &&
  733. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  734. tapping_key.tap_count = 1;
  735. waiting_buffer[i].tap_count = 1;
  736. process_action(&tapping_key);
  737. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  738. debug_waiting_buffer();
  739. return;
  740. }
  741. }
  742. }
  743. /*
  744. * Utilities for actions.
  745. */
  746. void register_code(uint8_t code)
  747. {
  748. if (code == KC_NO) {
  749. return;
  750. }
  751. else if IS_KEY(code) {
  752. // TODO: should push command_proc out of this block?
  753. if (command_proc(code)) return;
  754. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  755. uint8_t tmp_mods = host_get_mods();
  756. host_add_mods(oneshot_state.mods);
  757. host_add_key(code);
  758. host_send_keyboard_report();
  759. host_set_mods(tmp_mods);
  760. oneshot_state.ready = false;
  761. } else {
  762. host_add_key(code);
  763. host_send_keyboard_report();
  764. }
  765. }
  766. else if IS_MOD(code) {
  767. host_add_mods(MOD_BIT(code));
  768. host_send_keyboard_report();
  769. }
  770. }
  771. void unregister_code(uint8_t code)
  772. {
  773. if IS_KEY(code) {
  774. host_del_key(code);
  775. host_send_keyboard_report();
  776. }
  777. else if IS_MOD(code) {
  778. host_del_mods(MOD_BIT(code));
  779. host_send_keyboard_report();
  780. }
  781. }
  782. void add_mods(uint8_t mods)
  783. {
  784. if (mods) {
  785. host_add_mods(mods);
  786. host_send_keyboard_report();
  787. }
  788. }
  789. void del_mods(uint8_t mods)
  790. {
  791. if (mods) {
  792. host_del_mods(mods);
  793. host_send_keyboard_report();
  794. }
  795. }
  796. void set_mods(uint8_t mods)
  797. {
  798. host_set_mods(mods);
  799. host_send_keyboard_report();
  800. }
  801. void clear_keyboard(void)
  802. {
  803. host_clear_mods();
  804. clear_keyboard_but_mods();
  805. }
  806. void clear_keyboard_but_mods(void)
  807. {
  808. host_clear_keys();
  809. host_send_keyboard_report();
  810. #ifdef MOUSEKEY_ENABLE
  811. mousekey_clear();
  812. mousekey_send();
  813. #endif
  814. #ifdef EXTRAKEY_ENABLE
  815. host_system_send(0);
  816. host_consumer_send(0);
  817. #endif
  818. }
  819. bool sending_anykey(void)
  820. {
  821. return (host_has_anykey() || host_mouse_in_use() ||
  822. host_last_sysytem_report() || host_last_consumer_report());
  823. }
  824. // TODO: rename or reinpl with new layer_switch.c
  825. void layer_switch(uint8_t new_layer)
  826. {
  827. if (current_layer != new_layer) {
  828. debug("Layer Switch: "); debug_hex(current_layer);
  829. debug(" -> "); debug_hex(new_layer); debug("\n");
  830. current_layer = new_layer;
  831. clear_keyboard_but_mods(); // To avoid stuck keys
  832. // NOTE: update mods with full scan of matrix? if modifier changes between layers
  833. }
  834. }
  835. bool is_tap_key(key_t key)
  836. {
  837. action_t action = get_action(key);
  838. switch (action.kind.id) {
  839. case ACT_LMODS_TAP:
  840. case ACT_RMODS_TAP:
  841. return true;
  842. case ACT_LAYER:
  843. case ACT_LAYER_BIT:
  844. switch (action.layer.code) {
  845. case LAYER_MOMENTARY:
  846. case LAYER_ON_PRESS:
  847. case LAYER_ON_RELEASE:
  848. case LAYER_ON_BOTH:
  849. case LAYER_SET_DEFAULT_ON_PRESS:
  850. case LAYER_SET_DEFAULT_ON_RELEASE:
  851. case LAYER_SET_DEFAULT_ON_BOTH:
  852. return false;
  853. case LAYER_TAP_TOGGLE:
  854. default: /* tap key */
  855. return true;
  856. }
  857. return false;
  858. case ACT_FUNCTION:
  859. if (action.func.opt & FUNC_TAP) { return true; }
  860. return false;
  861. }
  862. return false;
  863. }
  864. /*
  865. * debug print
  866. */
  867. static void debug_event(keyevent_t event)
  868. {
  869. debug_hex16((event.key.row<<8) | event.key.col);
  870. if (event.pressed) debug("d("); else debug("u(");
  871. debug_dec(event.time); debug(")");
  872. }
  873. static void debug_record(keyrecord_t record)
  874. {
  875. debug_event(record.event); debug(":"); debug_dec(record.tap_count);
  876. }
  877. static void debug_action(action_t action)
  878. {
  879. switch (action.kind.id) {
  880. case ACT_LMODS: debug("ACT_LMODS"); break;
  881. case ACT_RMODS: debug("ACT_RMODS"); break;
  882. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  883. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  884. case ACT_USAGE: debug("ACT_USAGE"); break;
  885. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  886. case ACT_LAYER: debug("ACT_LAYER"); break;
  887. case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
  888. case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break;
  889. case ACT_MACRO: debug("ACT_MACRO"); break;
  890. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  891. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  892. default: debug("UNKNOWN"); break;
  893. }
  894. debug("[");
  895. debug_hex4(action.kind.param>>8);
  896. debug(":");
  897. debug_hex8(action.kind.param & 0xff);
  898. debug("]");
  899. }
  900. static void debug_tapping_key(void)
  901. {
  902. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  903. }
  904. static void debug_waiting_buffer(void)
  905. {
  906. debug("{ ");
  907. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  908. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  909. }
  910. debug("}\n");
  911. }