action.c 28 KB

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