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

action.c 33KB

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