Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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