action.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
  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 "keycode.h"
  16. #include "keyboard.h"
  17. #include "mousekey.h"
  18. #include "command.h"
  19. #include "led.h"
  20. #include "backlight.h"
  21. #include "action_layer.h"
  22. #include "action_tapping.h"
  23. #include "action_macro.h"
  24. #include "action_util.h"
  25. #include "action.h"
  26. #include "wait.h"
  27. #ifdef DEBUG_ACTION
  28. #include "debug.h"
  29. #else
  30. #include "nodebug.h"
  31. #endif
  32. int tp_buttons;
  33. #ifdef RETRO_TAPPING
  34. int retro_tapping_counter = 0;
  35. #endif
  36. #ifdef FAUXCLICKY_ENABLE
  37. #include <fauxclicky.h>
  38. #endif
  39. /** \brief Called to execute an action.
  40. *
  41. * FIXME: Needs documentation.
  42. */
  43. void action_exec(keyevent_t event)
  44. {
  45. if (!IS_NOEVENT(event)) {
  46. dprint("\n---- action_exec: start -----\n");
  47. dprint("EVENT: "); debug_event(event); dprintln();
  48. #ifdef RETRO_TAPPING
  49. retro_tapping_counter++;
  50. #endif
  51. }
  52. #ifdef FAUXCLICKY_ENABLE
  53. if (IS_PRESSED(event)) {
  54. FAUXCLICKY_ACTION_PRESS;
  55. }
  56. if (IS_RELEASED(event)) {
  57. FAUXCLICKY_ACTION_RELEASE;
  58. }
  59. fauxclicky_check();
  60. #endif
  61. #ifdef SWAP_HANDS_ENABLE
  62. if (!IS_NOEVENT(event)) {
  63. process_hand_swap(&event);
  64. }
  65. #endif
  66. keyrecord_t record = { .event = event };
  67. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  68. if (has_oneshot_layer_timed_out()) {
  69. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  70. }
  71. if (has_oneshot_mods_timed_out()) {
  72. clear_oneshot_mods();
  73. }
  74. #endif
  75. #ifndef NO_ACTION_TAPPING
  76. action_tapping_process(record);
  77. #else
  78. process_record(&record);
  79. if (!IS_NOEVENT(record.event)) {
  80. dprint("processed: "); debug_record(record); dprintln();
  81. }
  82. #endif
  83. }
  84. #ifdef SWAP_HANDS_ENABLE
  85. bool swap_hands = false;
  86. bool swap_held = false;
  87. /** \brief Process Hand Swap
  88. *
  89. * FIXME: Needs documentation.
  90. */
  91. void process_hand_swap(keyevent_t *event) {
  92. static swap_state_row_t swap_state[MATRIX_ROWS];
  93. // TODO: Properly support multimatrices, currenty this only works for single-matrix keyboards
  94. keypos_t pos = event->key.pos;
  95. swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
  96. bool do_swap = event->pressed ? swap_hands :
  97. swap_state[pos.row] & (col_bit);
  98. if (do_swap) {
  99. event->key.pos = hand_swap_config[pos.row][pos.col];
  100. swap_state[pos.row] |= col_bit;
  101. } else {
  102. swap_state[pos.row] &= ~(col_bit);
  103. }
  104. }
  105. #endif
  106. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  107. bool disable_action_cache = false;
  108. void process_record_nocache(keyrecord_t *record)
  109. {
  110. disable_action_cache = true;
  111. process_record(record);
  112. disable_action_cache = false;
  113. }
  114. #else
  115. void process_record_nocache(keyrecord_t *record)
  116. {
  117. process_record(record);
  118. }
  119. #endif
  120. __attribute__ ((weak))
  121. bool process_record_quantum(keyrecord_t *record) {
  122. return true;
  123. }
  124. #ifndef NO_ACTION_TAPPING
  125. /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
  126. *
  127. * FIXME: Needs documentation.
  128. */
  129. void process_record_tap_hint(keyrecord_t *record)
  130. {
  131. action_t action = layer_switch_get_action(record->event.key);
  132. switch (action.kind.id) {
  133. #ifdef SWAP_HANDS_ENABLE
  134. case ACT_SWAP_HANDS:
  135. switch (action.swap.code) {
  136. case OP_SH_TAP_TOGGLE:
  137. default:
  138. swap_hands = !swap_hands;
  139. swap_held = true;
  140. }
  141. break;
  142. #endif
  143. }
  144. }
  145. #endif
  146. /** \brief Take a key event (key press or key release) and processes it.
  147. *
  148. * FIXME: Needs documentation.
  149. */
  150. void process_record(keyrecord_t *record)
  151. {
  152. if (IS_NOEVENT(record->event)) { return; }
  153. if(!process_record_quantum(record))
  154. return;
  155. action_t action = store_or_get_action(record->event.pressed, record->event.key);
  156. dprint("ACTION: "); debug_action(action);
  157. #ifndef NO_ACTION_LAYER
  158. dprint(" layer_state: "); layer_debug();
  159. dprint(" default_layer_state: "); default_layer_debug();
  160. #endif
  161. dprintln();
  162. process_action(record, action);
  163. }
  164. /** \brief Take an action and processes it.
  165. *
  166. * FIXME: Needs documentation.
  167. */
  168. void process_action(keyrecord_t *record, action_t action)
  169. {
  170. keyevent_t event = record->event;
  171. #ifndef NO_ACTION_TAPPING
  172. uint8_t tap_count = record->tap.count;
  173. #endif
  174. if (event.pressed) {
  175. // clear the potential weak mods left by previously pressed keys
  176. clear_weak_mods();
  177. }
  178. #ifndef NO_ACTION_ONESHOT
  179. bool do_release_oneshot = false;
  180. // notice we only clear the one shot layer if the pressed key is not a modifier.
  181. if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
  182. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  183. do_release_oneshot = !is_oneshot_layer_active();
  184. }
  185. #endif
  186. switch (action.kind.id) {
  187. /* Key and Mods */
  188. case ACT_LMODS:
  189. case ACT_RMODS:
  190. {
  191. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  192. action.key.mods<<4;
  193. if (event.pressed) {
  194. if (mods) {
  195. if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
  196. // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
  197. // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
  198. // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
  199. add_mods(mods);
  200. } else {
  201. add_weak_mods(mods);
  202. }
  203. send_keyboard_report();
  204. }
  205. register_code(action.key.code);
  206. } else {
  207. unregister_code(action.key.code);
  208. if (mods) {
  209. if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
  210. del_mods(mods);
  211. } else {
  212. del_weak_mods(mods);
  213. }
  214. send_keyboard_report();
  215. }
  216. }
  217. }
  218. break;
  219. #ifndef NO_ACTION_TAPPING
  220. case ACT_LMODS_TAP:
  221. case ACT_RMODS_TAP:
  222. {
  223. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  224. action.key.mods<<4;
  225. switch (action.layer_tap.code) {
  226. #ifndef NO_ACTION_ONESHOT
  227. case MODS_ONESHOT:
  228. // Oneshot modifier
  229. if (event.pressed) {
  230. if (tap_count == 0) {
  231. dprint("MODS_TAP: Oneshot: 0\n");
  232. register_mods(mods | get_oneshot_mods());
  233. } else if (tap_count == 1) {
  234. dprint("MODS_TAP: Oneshot: start\n");
  235. set_oneshot_mods(mods | get_oneshot_mods());
  236. #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
  237. } else if (tap_count == ONESHOT_TAP_TOGGLE) {
  238. dprint("MODS_TAP: Toggling oneshot");
  239. clear_oneshot_mods();
  240. set_oneshot_locked_mods(mods);
  241. register_mods(mods);
  242. #endif
  243. } else {
  244. register_mods(mods | get_oneshot_mods());
  245. }
  246. } else {
  247. if (tap_count == 0) {
  248. clear_oneshot_mods();
  249. unregister_mods(mods);
  250. } else if (tap_count == 1) {
  251. // Retain Oneshot mods
  252. #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
  253. if (mods & get_mods()) {
  254. clear_oneshot_locked_mods();
  255. clear_oneshot_mods();
  256. unregister_mods(mods);
  257. }
  258. } else if (tap_count == ONESHOT_TAP_TOGGLE) {
  259. // Toggle Oneshot Layer
  260. #endif
  261. } else {
  262. clear_oneshot_mods();
  263. unregister_mods(mods);
  264. }
  265. }
  266. break;
  267. #endif
  268. case MODS_TAP_TOGGLE:
  269. if (event.pressed) {
  270. if (tap_count <= TAPPING_TOGGLE) {
  271. register_mods(mods);
  272. }
  273. } else {
  274. if (tap_count < TAPPING_TOGGLE) {
  275. unregister_mods(mods);
  276. }
  277. }
  278. break;
  279. default:
  280. if (event.pressed) {
  281. if (tap_count > 0) {
  282. #ifndef IGNORE_MOD_TAP_INTERRUPT
  283. if (record->tap.interrupted) {
  284. dprint("mods_tap: tap: cancel: add_mods\n");
  285. // ad hoc: set 0 to cancel tap
  286. record->tap.count = 0;
  287. register_mods(mods);
  288. } else
  289. #endif
  290. {
  291. dprint("MODS_TAP: Tap: register_code\n");
  292. register_code(action.key.code);
  293. }
  294. } else {
  295. dprint("MODS_TAP: No tap: add_mods\n");
  296. register_mods(mods);
  297. }
  298. } else {
  299. if (tap_count > 0) {
  300. dprint("MODS_TAP: Tap: unregister_code\n");
  301. unregister_code(action.key.code);
  302. } else {
  303. dprint("MODS_TAP: No tap: add_mods\n");
  304. unregister_mods(mods);
  305. }
  306. }
  307. break;
  308. }
  309. }
  310. break;
  311. #endif
  312. #ifdef EXTRAKEY_ENABLE
  313. /* other HID usage */
  314. case ACT_USAGE:
  315. switch (action.usage.page) {
  316. case PAGE_SYSTEM:
  317. if (event.pressed) {
  318. host_system_send(action.usage.code);
  319. } else {
  320. host_system_send(0);
  321. }
  322. break;
  323. case PAGE_CONSUMER:
  324. if (event.pressed) {
  325. host_consumer_send(action.usage.code);
  326. } else {
  327. host_consumer_send(0);
  328. }
  329. break;
  330. }
  331. break;
  332. #endif
  333. #ifdef MOUSEKEY_ENABLE
  334. /* Mouse key */
  335. case ACT_MOUSEKEY:
  336. if (event.pressed) {
  337. switch (action.key.code) {
  338. case KC_MS_BTN1:
  339. tp_buttons |= (1<<0);
  340. break;
  341. case KC_MS_BTN2:
  342. tp_buttons |= (1<<1);
  343. break;
  344. case KC_MS_BTN3:
  345. tp_buttons |= (1<<2);
  346. break;
  347. default:
  348. break;
  349. }
  350. mousekey_on(action.key.code);
  351. mousekey_send();
  352. } else {
  353. switch (action.key.code) {
  354. case KC_MS_BTN1:
  355. tp_buttons &= ~(1<<0);
  356. break;
  357. case KC_MS_BTN2:
  358. tp_buttons &= ~(1<<1);
  359. break;
  360. case KC_MS_BTN3:
  361. tp_buttons &= ~(1<<2);
  362. break;
  363. default:
  364. break;
  365. }
  366. mousekey_off(action.key.code);
  367. mousekey_send();
  368. }
  369. break;
  370. #endif
  371. #ifndef NO_ACTION_LAYER
  372. case ACT_LAYER:
  373. if (action.layer_bitop.on == 0) {
  374. /* Default Layer Bitwise Operation */
  375. if (!event.pressed) {
  376. uint8_t shift = action.layer_bitop.part*4;
  377. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  378. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  379. switch (action.layer_bitop.op) {
  380. case OP_BIT_AND: default_layer_and(bits | mask); break;
  381. case OP_BIT_OR: default_layer_or(bits | mask); break;
  382. case OP_BIT_XOR: default_layer_xor(bits | mask); break;
  383. case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
  384. }
  385. }
  386. } else {
  387. /* Layer Bitwise Operation */
  388. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
  389. (action.layer_bitop.on & ON_RELEASE)) {
  390. uint8_t shift = action.layer_bitop.part*4;
  391. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  392. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  393. switch (action.layer_bitop.op) {
  394. case OP_BIT_AND: layer_and(bits | mask); break;
  395. case OP_BIT_OR: layer_or(bits | mask); break;
  396. case OP_BIT_XOR: layer_xor(bits | mask); break;
  397. case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
  398. }
  399. }
  400. }
  401. break;
  402. #ifndef NO_ACTION_TAPPING
  403. case ACT_LAYER_TAP:
  404. case ACT_LAYER_TAP_EXT:
  405. switch (action.layer_tap.code) {
  406. case 0xe0 ... 0xef:
  407. /* layer On/Off with modifiers(left only) */
  408. if (event.pressed) {
  409. layer_on(action.layer_tap.val);
  410. register_mods(action.layer_tap.code & 0x0f);
  411. } else {
  412. layer_off(action.layer_tap.val);
  413. unregister_mods(action.layer_tap.code & 0x0f);
  414. }
  415. break;
  416. case OP_TAP_TOGGLE:
  417. /* tap toggle */
  418. if (event.pressed) {
  419. if (tap_count < TAPPING_TOGGLE) {
  420. layer_invert(action.layer_tap.val);
  421. }
  422. } else {
  423. if (tap_count <= TAPPING_TOGGLE) {
  424. layer_invert(action.layer_tap.val);
  425. }
  426. }
  427. break;
  428. case OP_ON_OFF:
  429. event.pressed ? layer_on(action.layer_tap.val) :
  430. layer_off(action.layer_tap.val);
  431. break;
  432. case OP_OFF_ON:
  433. event.pressed ? layer_off(action.layer_tap.val) :
  434. layer_on(action.layer_tap.val);
  435. break;
  436. case OP_SET_CLEAR:
  437. event.pressed ? layer_move(action.layer_tap.val) :
  438. layer_clear();
  439. break;
  440. #ifndef NO_ACTION_ONESHOT
  441. case OP_ONESHOT:
  442. // Oneshot modifier
  443. #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
  444. do_release_oneshot = false;
  445. if (event.pressed) {
  446. del_mods(get_oneshot_locked_mods());
  447. if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
  448. reset_oneshot_layer();
  449. layer_off(action.layer_tap.val);
  450. break;
  451. } else if (tap_count < ONESHOT_TAP_TOGGLE) {
  452. layer_on(action.layer_tap.val);
  453. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  454. }
  455. } else {
  456. add_mods(get_oneshot_locked_mods());
  457. if (tap_count >= ONESHOT_TAP_TOGGLE) {
  458. reset_oneshot_layer();
  459. clear_oneshot_locked_mods();
  460. set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
  461. } else {
  462. clear_oneshot_layer_state(ONESHOT_PRESSED);
  463. }
  464. }
  465. #else
  466. if (event.pressed) {
  467. layer_on(action.layer_tap.val);
  468. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  469. } else {
  470. clear_oneshot_layer_state(ONESHOT_PRESSED);
  471. if (tap_count > 1) {
  472. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  473. }
  474. }
  475. #endif
  476. break;
  477. #endif
  478. default:
  479. /* tap key */
  480. if (event.pressed) {
  481. if (tap_count > 0) {
  482. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  483. register_code(action.layer_tap.code);
  484. } else {
  485. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  486. layer_on(action.layer_tap.val);
  487. }
  488. } else {
  489. if (tap_count > 0) {
  490. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  491. if (action.layer_tap.code == KC_CAPS) {
  492. wait_ms(80);
  493. }
  494. unregister_code(action.layer_tap.code);
  495. } else {
  496. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  497. layer_off(action.layer_tap.val);
  498. }
  499. }
  500. break;
  501. }
  502. break;
  503. #endif
  504. #endif
  505. /* Extentions */
  506. #ifndef NO_ACTION_MACRO
  507. case ACT_MACRO:
  508. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  509. break;
  510. #endif
  511. #ifdef BACKLIGHT_ENABLE
  512. case ACT_BACKLIGHT:
  513. if (!event.pressed) {
  514. switch (action.backlight.opt) {
  515. case BACKLIGHT_INCREASE:
  516. backlight_increase();
  517. break;
  518. case BACKLIGHT_DECREASE:
  519. backlight_decrease();
  520. break;
  521. case BACKLIGHT_TOGGLE:
  522. backlight_toggle();
  523. break;
  524. case BACKLIGHT_STEP:
  525. backlight_step();
  526. break;
  527. case BACKLIGHT_ON:
  528. backlight_level(BACKLIGHT_LEVELS);
  529. break;
  530. case BACKLIGHT_OFF:
  531. backlight_level(0);
  532. break;
  533. }
  534. }
  535. break;
  536. #endif
  537. case ACT_COMMAND:
  538. break;
  539. #ifdef SWAP_HANDS_ENABLE
  540. case ACT_SWAP_HANDS:
  541. switch (action.swap.code) {
  542. case OP_SH_TOGGLE:
  543. if (event.pressed) {
  544. swap_hands = !swap_hands;
  545. }
  546. break;
  547. case OP_SH_ON_OFF:
  548. swap_hands = event.pressed;
  549. break;
  550. case OP_SH_OFF_ON:
  551. swap_hands = !event.pressed;
  552. break;
  553. case OP_SH_ON:
  554. if (!event.pressed) {
  555. swap_hands = true;
  556. }
  557. break;
  558. case OP_SH_OFF:
  559. if (!event.pressed) {
  560. swap_hands = false;
  561. }
  562. break;
  563. #ifndef NO_ACTION_TAPPING
  564. case OP_SH_TAP_TOGGLE:
  565. /* tap toggle */
  566. if (event.pressed) {
  567. if (swap_held) {
  568. swap_held = false;
  569. } else {
  570. swap_hands = !swap_hands;
  571. }
  572. } else {
  573. if (tap_count < TAPPING_TOGGLE) {
  574. swap_hands = !swap_hands;
  575. }
  576. }
  577. break;
  578. default:
  579. /* tap key */
  580. if (tap_count > 0) {
  581. if (swap_held) {
  582. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  583. swap_held = false;
  584. }
  585. if (event.pressed) {
  586. register_code(action.swap.code);
  587. } else {
  588. unregister_code(action.swap.code);
  589. *record = (keyrecord_t){}; // hack: reset tap mode
  590. }
  591. } else {
  592. if (swap_held && !event.pressed) {
  593. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  594. swap_held = false;
  595. }
  596. }
  597. #endif
  598. }
  599. #endif
  600. #ifndef NO_ACTION_FUNCTION
  601. case ACT_FUNCTION:
  602. action_function(record, action.func.id, action.func.opt);
  603. break;
  604. #endif
  605. default:
  606. break;
  607. }
  608. #ifndef NO_ACTION_LAYER
  609. // if this event is a layer action, update the leds
  610. switch (action.kind.id) {
  611. case ACT_LAYER:
  612. #ifndef NO_ACTION_TAPPING
  613. case ACT_LAYER_TAP:
  614. case ACT_LAYER_TAP_EXT:
  615. #endif
  616. led_set(host_keyboard_leds());
  617. break;
  618. default:
  619. break;
  620. }
  621. #endif
  622. #ifndef NO_ACTION_TAPPING
  623. #ifdef RETRO_TAPPING
  624. if (!is_tap_key(record->event.key)) {
  625. retro_tapping_counter = 0;
  626. } else {
  627. if (event.pressed) {
  628. if (tap_count > 0) {
  629. retro_tapping_counter = 0;
  630. } else {
  631. }
  632. } else {
  633. if (tap_count > 0) {
  634. retro_tapping_counter = 0;
  635. } else {
  636. if (retro_tapping_counter == 2) {
  637. register_code(action.layer_tap.code);
  638. unregister_code(action.layer_tap.code);
  639. }
  640. retro_tapping_counter = 0;
  641. }
  642. }
  643. }
  644. #endif
  645. #endif
  646. #ifndef NO_ACTION_ONESHOT
  647. /* Because we switch layers after a oneshot event, we need to release the
  648. * key before we leave the layer or no key up event will be generated.
  649. */
  650. if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
  651. record->event.pressed = false;
  652. layer_on(get_oneshot_layer());
  653. process_record(record);
  654. layer_off(get_oneshot_layer());
  655. }
  656. #endif
  657. }
  658. /** \brief Utilities for actions. (FIXME: Needs better description)
  659. *
  660. * FIXME: Needs documentation.
  661. */
  662. void register_code(uint8_t code)
  663. {
  664. if (code == KC_NO) {
  665. return;
  666. }
  667. #ifdef LOCKING_SUPPORT_ENABLE
  668. else if (KC_LOCKING_CAPS == code) {
  669. #ifdef LOCKING_RESYNC_ENABLE
  670. // Resync: ignore if caps lock already is on
  671. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  672. #endif
  673. add_key(KC_CAPSLOCK);
  674. send_keyboard_report();
  675. wait_ms(100);
  676. del_key(KC_CAPSLOCK);
  677. send_keyboard_report();
  678. }
  679. else if (KC_LOCKING_NUM == code) {
  680. #ifdef LOCKING_RESYNC_ENABLE
  681. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  682. #endif
  683. add_key(KC_NUMLOCK);
  684. send_keyboard_report();
  685. wait_ms(100);
  686. del_key(KC_NUMLOCK);
  687. send_keyboard_report();
  688. }
  689. else if (KC_LOCKING_SCROLL == code) {
  690. #ifdef LOCKING_RESYNC_ENABLE
  691. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  692. #endif
  693. add_key(KC_SCROLLLOCK);
  694. send_keyboard_report();
  695. wait_ms(100);
  696. del_key(KC_SCROLLLOCK);
  697. send_keyboard_report();
  698. }
  699. #endif
  700. else if IS_KEY(code) {
  701. // TODO: should push command_proc out of this block?
  702. if (command_proc(code)) return;
  703. #ifndef NO_ACTION_ONESHOT
  704. /* TODO: remove
  705. if (oneshot_state.mods && !oneshot_state.disabled) {
  706. uint8_t tmp_mods = get_mods();
  707. add_mods(oneshot_state.mods);
  708. add_key(code);
  709. send_keyboard_report();
  710. set_mods(tmp_mods);
  711. send_keyboard_report();
  712. oneshot_cancel();
  713. } else
  714. */
  715. #endif
  716. {
  717. add_key(code);
  718. send_keyboard_report();
  719. }
  720. }
  721. else if IS_MOD(code) {
  722. add_mods(MOD_BIT(code));
  723. send_keyboard_report();
  724. }
  725. else if IS_SYSTEM(code) {
  726. host_system_send(KEYCODE2SYSTEM(code));
  727. }
  728. else if IS_CONSUMER(code) {
  729. host_consumer_send(KEYCODE2CONSUMER(code));
  730. }
  731. }
  732. /** \brief Utilities for actions. (FIXME: Needs better description)
  733. *
  734. * FIXME: Needs documentation.
  735. */
  736. void unregister_code(uint8_t code)
  737. {
  738. if (code == KC_NO) {
  739. return;
  740. }
  741. #ifdef LOCKING_SUPPORT_ENABLE
  742. else if (KC_LOCKING_CAPS == code) {
  743. #ifdef LOCKING_RESYNC_ENABLE
  744. // Resync: ignore if caps lock already is off
  745. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  746. #endif
  747. add_key(KC_CAPSLOCK);
  748. send_keyboard_report();
  749. del_key(KC_CAPSLOCK);
  750. send_keyboard_report();
  751. }
  752. else if (KC_LOCKING_NUM == code) {
  753. #ifdef LOCKING_RESYNC_ENABLE
  754. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  755. #endif
  756. add_key(KC_NUMLOCK);
  757. send_keyboard_report();
  758. del_key(KC_NUMLOCK);
  759. send_keyboard_report();
  760. }
  761. else if (KC_LOCKING_SCROLL == code) {
  762. #ifdef LOCKING_RESYNC_ENABLE
  763. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  764. #endif
  765. add_key(KC_SCROLLLOCK);
  766. send_keyboard_report();
  767. del_key(KC_SCROLLLOCK);
  768. send_keyboard_report();
  769. }
  770. #endif
  771. else if IS_KEY(code) {
  772. del_key(code);
  773. send_keyboard_report();
  774. }
  775. else if IS_MOD(code) {
  776. del_mods(MOD_BIT(code));
  777. send_keyboard_report();
  778. }
  779. else if IS_SYSTEM(code) {
  780. host_system_send(0);
  781. }
  782. else if IS_CONSUMER(code) {
  783. host_consumer_send(0);
  784. }
  785. }
  786. /** \brief Utilities for actions. (FIXME: Needs better description)
  787. *
  788. * FIXME: Needs documentation.
  789. */
  790. void register_mods(uint8_t mods)
  791. {
  792. if (mods) {
  793. add_mods(mods);
  794. send_keyboard_report();
  795. }
  796. }
  797. /** \brief Utilities for actions. (FIXME: Needs better description)
  798. *
  799. * FIXME: Needs documentation.
  800. */
  801. void unregister_mods(uint8_t mods)
  802. {
  803. if (mods) {
  804. del_mods(mods);
  805. send_keyboard_report();
  806. }
  807. }
  808. /** \brief Utilities for actions. (FIXME: Needs better description)
  809. *
  810. * FIXME: Needs documentation.
  811. */
  812. void clear_keyboard(void)
  813. {
  814. clear_mods();
  815. clear_keyboard_but_mods();
  816. }
  817. /** \brief Utilities for actions. (FIXME: Needs better description)
  818. *
  819. * FIXME: Needs documentation.
  820. */
  821. void clear_keyboard_but_mods(void)
  822. {
  823. clear_weak_mods();
  824. clear_macro_mods();
  825. clear_keys();
  826. send_keyboard_report();
  827. #ifdef MOUSEKEY_ENABLE
  828. mousekey_clear();
  829. mousekey_send();
  830. #endif
  831. #ifdef EXTRAKEY_ENABLE
  832. host_system_send(0);
  833. host_consumer_send(0);
  834. #endif
  835. }
  836. /** \brief Utilities for actions. (FIXME: Needs better description)
  837. *
  838. * FIXME: Needs documentation.
  839. */
  840. bool is_tap_key(keymatrix_t key)
  841. {
  842. action_t action = layer_switch_get_action(key);
  843. switch (action.kind.id) {
  844. case ACT_LMODS_TAP:
  845. case ACT_RMODS_TAP:
  846. case ACT_LAYER_TAP:
  847. case ACT_LAYER_TAP_EXT:
  848. switch (action.layer_tap.code) {
  849. case 0x00 ... 0xdf:
  850. case OP_TAP_TOGGLE:
  851. case OP_ONESHOT:
  852. return true;
  853. }
  854. return false;
  855. case ACT_SWAP_HANDS:
  856. switch (action.swap.code) {
  857. case 0x00 ... 0xdf:
  858. case OP_SH_TAP_TOGGLE:
  859. return true;
  860. }
  861. return false;
  862. case ACT_MACRO:
  863. case ACT_FUNCTION:
  864. if (action.func.opt & FUNC_TAP) { return true; }
  865. return false;
  866. }
  867. return false;
  868. }
  869. /** \brief Debug print (FIXME: Needs better description)
  870. *
  871. * FIXME: Needs documentation.
  872. */
  873. void debug_event(keyevent_t event)
  874. {
  875. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  876. }
  877. /** \brief Debug print (FIXME: Needs better description)
  878. *
  879. * FIXME: Needs documentation.
  880. */
  881. void debug_record(keyrecord_t record)
  882. {
  883. debug_event(record.event);
  884. #ifndef NO_ACTION_TAPPING
  885. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  886. #endif
  887. }
  888. /** \brief Debug print (FIXME: Needs better description)
  889. *
  890. * FIXME: Needs documentation.
  891. */
  892. void debug_action(action_t action)
  893. {
  894. switch (action.kind.id) {
  895. case ACT_LMODS: dprint("ACT_LMODS"); break;
  896. case ACT_RMODS: dprint("ACT_RMODS"); break;
  897. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  898. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  899. case ACT_USAGE: dprint("ACT_USAGE"); break;
  900. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  901. case ACT_LAYER: dprint("ACT_LAYER"); break;
  902. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  903. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  904. case ACT_MACRO: dprint("ACT_MACRO"); break;
  905. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  906. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  907. case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
  908. default: dprint("UNKNOWN"); break;
  909. }
  910. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  911. }