Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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