action.c 26 KB

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