action.c 33 KB

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