action.c 28 KB

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