Keyboard firmwares for Atmel AVR and Cortex-M
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

action.c 34KB

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