Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. 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 == TAPPING_TOGGLE) {
  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 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 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:
  314. switch (action.layer.code) {
  315. case LAYER_MOMENTARY: /* momentary */
  316. if (event.pressed) {
  317. layer_switch(action.layer.val);
  318. }
  319. else {
  320. // NOTE: This is needed by legacy keymap support
  321. layer_switch(default_layer);
  322. }
  323. break;
  324. case LAYER_ON_PRESS:
  325. if (event.pressed) {
  326. layer_switch(action.layer.val);
  327. }
  328. break;
  329. case LAYER_ON_RELEASE:
  330. if (!event.pressed) {
  331. layer_switch(action.layer.val);
  332. }
  333. break;
  334. case LAYER_DEFAULT: /* default layer */
  335. switch (action.layer.val) {
  336. case DEFAULT_ON_BOTH:
  337. layer_switch(default_layer);
  338. break;
  339. case DEFAULT_ON_PRESS:
  340. if (event.pressed) {
  341. layer_switch(default_layer);
  342. }
  343. break;
  344. case DEFAULT_ON_RELEASE:
  345. if (!event.pressed) {
  346. layer_switch(default_layer);
  347. }
  348. break;
  349. }
  350. break;
  351. case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
  352. if (event.pressed) {
  353. if (tap_count < TAPPING_TOGGLE) {
  354. layer_switch(action.layer.val);
  355. }
  356. } else {
  357. if (tap_count >= TAPPING_TOGGLE) {
  358. debug("LAYER_PRESSED: tap toggle.\n");
  359. layer_switch(action.layer.val);
  360. }
  361. }
  362. break;
  363. case LAYER_CHANGE_DEFAULT: /* change default layer */
  364. if (event.pressed) {
  365. default_layer = action.layer.val;
  366. layer_switch(default_layer);
  367. }
  368. break;
  369. default: /* switch layer on hold and key on tap*/
  370. if (event.pressed) {
  371. if (tap_count > 0) {
  372. debug("LAYER_PRESSED: Tap: register_code\n");
  373. register_code(action.layer.code);
  374. } else {
  375. debug("LAYER_PRESSED: No tap: layer_switch\n");
  376. layer_switch(action.layer.val);
  377. }
  378. } else {
  379. if (tap_count > 0) {
  380. debug("LAYER_PRESSED: Tap: unregister_code\n");
  381. unregister_code(action.layer.code);
  382. } else {
  383. //debug("LAYER_PRESSED: No tap: NO ACTION\n");
  384. // NOTE: This is needed by legacy keymap support
  385. debug("LAYER_PRESSED: No tap: return to default layer\n");
  386. layer_switch(default_layer);
  387. }
  388. }
  389. break;
  390. }
  391. break;
  392. case ACT_LAYER_BIT:
  393. switch (action.layer.code) {
  394. case LAYER_MOMENTARY: /* momentary */
  395. if (event.pressed) {
  396. layer_switch(current_layer ^ action.layer.val);
  397. } else {
  398. layer_switch(current_layer ^ action.layer.val);
  399. }
  400. break;
  401. case LAYER_ON_PRESS:
  402. if (event.pressed) {
  403. layer_switch(current_layer ^ action.layer.val);
  404. }
  405. break;
  406. case LAYER_ON_RELEASE:
  407. if (!event.pressed) {
  408. layer_switch(current_layer ^ action.layer.val);
  409. }
  410. break;
  411. case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
  412. if (event.pressed) {
  413. if (tap_count < TAPPING_TOGGLE) {
  414. debug("LAYER_BIT: tap toggle(press).\n");
  415. layer_switch(current_layer ^ action.layer.val);
  416. }
  417. } else {
  418. if (tap_count <= TAPPING_TOGGLE) {
  419. debug("LAYER_BIT: tap toggle(release).\n");
  420. layer_switch(current_layer ^ action.layer.val);
  421. }
  422. }
  423. break;
  424. case 0xFF:
  425. // change default layer
  426. if (event.pressed) {
  427. default_layer = current_layer ^ action.layer.val;
  428. layer_switch(default_layer);
  429. } else {
  430. default_layer = current_layer ^ action.layer.val;
  431. layer_switch(default_layer);
  432. }
  433. break;
  434. default:
  435. // with tap key
  436. if (event.pressed) {
  437. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  438. debug("LAYER_BIT: Tap: register_code\n");
  439. register_code(action.layer.code);
  440. } else {
  441. debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
  442. layer_switch(current_layer ^ action.layer.val);
  443. }
  444. } else {
  445. if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
  446. debug("LAYER_BIT: Tap: unregister_code\n");
  447. unregister_code(action.layer.code);
  448. } else {
  449. debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
  450. layer_switch(current_layer ^ action.layer.val);
  451. }
  452. }
  453. break;
  454. }
  455. break;
  456. /* Extentions */
  457. case ACT_MACRO:
  458. break;
  459. case ACT_COMMAND:
  460. break;
  461. case ACT_FUNCTION:
  462. // TODO
  463. keymap_call_function(record, action.func.id, action.func.opt);
  464. break;
  465. default:
  466. break;
  467. }
  468. }
  469. /* Tapping
  470. *
  471. * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
  472. * (without interfering by typing other key)
  473. */
  474. /* return true when key event is processed or consumed. */
  475. static bool process_tapping(keyrecord_t *keyp)
  476. {
  477. keyevent_t event = keyp->event;
  478. // if tapping
  479. if (IS_TAPPING_PRESSED()) {
  480. if (WITHIN_TAPPING_TERM(event)) {
  481. if (tapping_key.tap_count == 0) {
  482. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  483. // first tap!
  484. debug("Tapping: First tap(0->1).\n");
  485. tapping_key.tap_count = 1;
  486. debug_tapping_key();
  487. process_action(&tapping_key);
  488. // enqueue
  489. keyp->tap_count = tapping_key.tap_count;
  490. return false;
  491. }
  492. #if TAPPING_TERM >= 500
  493. /* This can prevent from typing some tap keys in a row at a time. */
  494. else if (!event.pressed && waiting_buffer_typed(event)) {
  495. // other key typed. not tap.
  496. debug("Tapping: End. No tap. Interfered by typing key\n");
  497. process_action(&tapping_key);
  498. tapping_key = (keyrecord_t){};
  499. debug_tapping_key();
  500. // enqueue
  501. return false;
  502. }
  503. #endif
  504. else {
  505. // other key events shall be enq'd till tapping state settles.
  506. return false;
  507. }
  508. }
  509. // tap_count > 0
  510. else {
  511. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  512. debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
  513. keyp->tap_count = tapping_key.tap_count;
  514. process_action(keyp);
  515. tapping_key = *keyp;
  516. debug_tapping_key();
  517. return true;
  518. }
  519. else if (is_tap_key(keyp->event.key) && event.pressed) {
  520. if (tapping_key.tap_count > 1) {
  521. debug("Tapping: Start new tap with releasing last tap(>1).\n");
  522. // unregister key
  523. process_action(&(keyrecord_t){
  524. .tap_count = tapping_key.tap_count,
  525. .event.key = tapping_key.event.key,
  526. .event.time = event.time,
  527. .event.pressed = false
  528. });
  529. } else {
  530. debug("Tapping: Start while last tap(1).\n");
  531. }
  532. tapping_key = *keyp;
  533. waiting_buffer_scan_tap();
  534. debug_tapping_key();
  535. return true;
  536. }
  537. else {
  538. if (!IS_NOEVENT(keyp->event)) {
  539. debug("Tapping: key event while last tap(>0).\n");
  540. }
  541. process_action(keyp);
  542. return true;
  543. }
  544. }
  545. }
  546. // after TAPPING_TERM
  547. else {
  548. if (tapping_key.tap_count == 0) {
  549. debug("Tapping: End. Timeout. Not tap(0): ");
  550. debug_event(event); debug("\n");
  551. process_action(&tapping_key);
  552. tapping_key = (keyrecord_t){};
  553. debug_tapping_key();
  554. return false;
  555. } else {
  556. if (IS_TAPPING_KEY(event.key) && !event.pressed) {
  557. debug("Tapping: End. last timeout tap release(>0).");
  558. keyp->tap_count = tapping_key.tap_count;
  559. process_action(keyp);
  560. tapping_key = (keyrecord_t){};
  561. return true;
  562. }
  563. else if (is_tap_key(keyp->event.key) && event.pressed) {
  564. if (tapping_key.tap_count > 1) {
  565. debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
  566. // unregister key
  567. process_action(&(keyrecord_t){
  568. .tap_count = tapping_key.tap_count,
  569. .event.key = tapping_key.event.key,
  570. .event.time = event.time,
  571. .event.pressed = false
  572. });
  573. } else {
  574. debug("Tapping: Start while last timeout tap(1).\n");
  575. }
  576. tapping_key = *keyp;
  577. waiting_buffer_scan_tap();
  578. debug_tapping_key();
  579. return true;
  580. }
  581. else {
  582. if (!IS_NOEVENT(keyp->event)) {
  583. debug("Tapping: key event while last timeout tap(>0).\n");
  584. }
  585. process_action(keyp);
  586. return true;
  587. }
  588. }
  589. }
  590. } else if (IS_TAPPING_RELEASED()) {
  591. if (WITHIN_TAPPING_TERM(event)) {
  592. if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
  593. // sequential tap.
  594. keyp->tap_count = tapping_key.tap_count + 1;
  595. debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
  596. process_action(keyp);
  597. tapping_key = *keyp;
  598. debug_tapping_key();
  599. return true;
  600. } else if (event.pressed && is_tap_key(event.key)) {
  601. // Sequential tap can be interfered with other tap key.
  602. debug("Tapping: Start with interfering other tap.\n");
  603. tapping_key = *keyp;
  604. waiting_buffer_scan_tap();
  605. debug_tapping_key();
  606. return true;
  607. } else {
  608. if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
  609. process_action(keyp);
  610. return true;
  611. }
  612. } else {
  613. // timeout. no sequential tap.
  614. debug("Tapping: End(Timeout after releasing last tap): ");
  615. debug_event(event); debug("\n");
  616. tapping_key = (keyrecord_t){};
  617. debug_tapping_key();
  618. return false;
  619. }
  620. }
  621. // not tapping satate
  622. else {
  623. if (event.pressed && is_tap_key(event.key)) {
  624. debug("Tapping: Start(Press tap key).\n");
  625. tapping_key = *keyp;
  626. waiting_buffer_scan_tap();
  627. debug_tapping_key();
  628. return true;
  629. } else {
  630. process_action(keyp);
  631. return true;
  632. }
  633. }
  634. }
  635. /* scan buffer for tapping */
  636. static void waiting_buffer_scan_tap(void)
  637. {
  638. // tapping already is settled
  639. if (tapping_key.tap_count > 0) return;
  640. // invalid state: tapping_key released && tap_count == 0
  641. if (!tapping_key.event.pressed) return;
  642. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  643. if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
  644. !waiting_buffer[i].event.pressed &&
  645. WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
  646. tapping_key.tap_count = 1;
  647. waiting_buffer[i].tap_count = 1;
  648. process_action(&tapping_key);
  649. debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
  650. debug_waiting_buffer();
  651. return;
  652. }
  653. }
  654. }
  655. /*
  656. * Utilities for actions.
  657. */
  658. void register_code(uint8_t code)
  659. {
  660. if (code == KC_NO) {
  661. return;
  662. }
  663. else if IS_KEY(code) {
  664. // TODO: should push command_proc out of this block?
  665. if (command_proc(code)) return;
  666. if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
  667. uint8_t tmp_mods = host_get_mods();
  668. host_add_mods(oneshot_state.mods);
  669. host_add_key(code);
  670. host_send_keyboard_report();
  671. host_set_mods(tmp_mods);
  672. oneshot_state.ready = false;
  673. } else {
  674. host_add_key(code);
  675. host_send_keyboard_report();
  676. }
  677. }
  678. else if IS_MOD(code) {
  679. host_add_mods(MOD_BIT(code));
  680. host_send_keyboard_report();
  681. }
  682. }
  683. void unregister_code(uint8_t code)
  684. {
  685. if IS_KEY(code) {
  686. host_del_key(code);
  687. host_send_keyboard_report();
  688. }
  689. else if IS_MOD(code) {
  690. host_del_mods(MOD_BIT(code));
  691. host_send_keyboard_report();
  692. }
  693. }
  694. void add_mods(uint8_t mods)
  695. {
  696. if (mods) {
  697. host_add_mods(mods);
  698. host_send_keyboard_report();
  699. }
  700. }
  701. void del_mods(uint8_t mods)
  702. {
  703. if (mods) {
  704. host_del_mods(mods);
  705. host_send_keyboard_report();
  706. }
  707. }
  708. void set_mods(uint8_t mods)
  709. {
  710. host_set_mods(mods);
  711. host_send_keyboard_report();
  712. }
  713. void clear_keyboard(void)
  714. {
  715. host_clear_mods();
  716. clear_keyboard_but_mods();
  717. }
  718. void clear_keyboard_but_mods(void)
  719. {
  720. host_clear_keys();
  721. host_send_keyboard_report();
  722. #ifdef MOUSEKEY_ENABLE
  723. mousekey_clear();
  724. mousekey_send();
  725. #endif
  726. #ifdef EXTRAKEY_ENABLE
  727. host_system_send(0);
  728. host_consumer_send(0);
  729. #endif
  730. }
  731. bool sending_anykey(void)
  732. {
  733. return (host_has_anykey() || host_mouse_in_use() ||
  734. host_last_sysytem_report() || host_last_consumer_report());
  735. }
  736. void layer_switch(uint8_t new_layer)
  737. {
  738. if (current_layer != new_layer) {
  739. debug("Layer Switch: "); debug_hex(current_layer);
  740. debug(" -> "); debug_hex(new_layer); debug("\n");
  741. current_layer = new_layer;
  742. clear_keyboard_but_mods(); // To avoid stuck keys
  743. // NOTE: update mods with full scan of matrix? if modifier changes between layers
  744. }
  745. }
  746. bool is_tap_key(key_t key)
  747. {
  748. action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
  749. switch (action.kind.id) {
  750. case ACT_LMODS_TAP:
  751. case ACT_RMODS_TAP:
  752. return true;
  753. case ACT_LAYER:
  754. case ACT_LAYER_BIT:
  755. switch (action.layer.code) {
  756. case LAYER_MOMENTARY:
  757. case LAYER_ON_PRESS:
  758. case LAYER_ON_RELEASE:
  759. case LAYER_DEFAULT:
  760. return false;
  761. case LAYER_TAP_TOGGLE:
  762. default: /* tap key */
  763. return true;
  764. }
  765. return false;
  766. case ACT_FUNCTION:
  767. if (action.func.opt & FUNC_TAP) { return true; }
  768. return false;
  769. }
  770. return false;
  771. }
  772. /*
  773. * debug print
  774. */
  775. static void debug_event(keyevent_t event)
  776. {
  777. debug_hex16(event.key.raw);
  778. if (event.pressed) debug("d("); else debug("u(");
  779. debug_dec(event.time); debug(")");
  780. }
  781. static void debug_record(keyrecord_t record)
  782. {
  783. debug_event(record.event); debug(":"); debug_dec(record.tap_count);
  784. }
  785. static void debug_action(action_t action)
  786. {
  787. switch (action.kind.id) {
  788. case ACT_LMODS: debug("ACT_LMODS"); break;
  789. case ACT_RMODS: debug("ACT_RMODS"); break;
  790. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  791. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  792. case ACT_USAGE: debug("ACT_USAGE"); break;
  793. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  794. case ACT_LAYER: debug("ACT_LAYER"); break;
  795. case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
  796. case ACT_MACRO: debug("ACT_MACRO"); break;
  797. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  798. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  799. default: debug("UNKNOWN"); break;
  800. }
  801. debug("[");
  802. debug_hex4(action.kind.param>>8);
  803. debug(":");
  804. debug_hex8(action.kind.param & 0xff);
  805. debug("]");
  806. }
  807. static void debug_tapping_key(void)
  808. {
  809. debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
  810. }
  811. static void debug_waiting_buffer(void)
  812. {
  813. debug("{ ");
  814. for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
  815. debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
  816. }
  817. debug("}\n");
  818. }