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.

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