action.c 28 KB

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