action.c 26 KB

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