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.

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