action.c 26 KB

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