action.c 27 KB

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