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.

1102 lines
34 KiB

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