action.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. break;
  301. }
  302. mousekey_on(action.key.code);
  303. mousekey_send();
  304. } else {
  305. switch (action.key.code) {
  306. case KC_MS_BTN1:
  307. tp_buttons &= ~(1<<0);
  308. break;
  309. case KC_MS_BTN2:
  310. tp_buttons &= ~(1<<1);
  311. break;
  312. case KC_MS_BTN3:
  313. tp_buttons &= ~(1<<2);
  314. break;
  315. default:
  316. break;
  317. }
  318. mousekey_off(action.key.code);
  319. mousekey_send();
  320. }
  321. break;
  322. #endif
  323. #ifndef NO_ACTION_LAYER
  324. case ACT_LAYER:
  325. if (action.layer_bitop.on == 0) {
  326. /* Default Layer Bitwise Operation */
  327. if (!event.pressed) {
  328. uint8_t shift = action.layer_bitop.part*4;
  329. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  330. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  331. switch (action.layer_bitop.op) {
  332. case OP_BIT_AND: default_layer_and(bits | mask); break;
  333. case OP_BIT_OR: default_layer_or(bits | mask); break;
  334. case OP_BIT_XOR: default_layer_xor(bits | mask); break;
  335. case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
  336. }
  337. }
  338. } else {
  339. /* Layer Bitwise Operation */
  340. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
  341. (action.layer_bitop.on & ON_RELEASE)) {
  342. uint8_t shift = action.layer_bitop.part*4;
  343. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  344. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  345. switch (action.layer_bitop.op) {
  346. case OP_BIT_AND: layer_and(bits | mask); break;
  347. case OP_BIT_OR: layer_or(bits | mask); break;
  348. case OP_BIT_XOR: layer_xor(bits | mask); break;
  349. case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
  350. }
  351. }
  352. }
  353. break;
  354. #ifndef NO_ACTION_TAPPING
  355. case ACT_LAYER_TAP:
  356. case ACT_LAYER_TAP_EXT:
  357. switch (action.layer_tap.code) {
  358. case 0xe0 ... 0xef:
  359. /* layer On/Off with modifiers(left only) */
  360. if (event.pressed) {
  361. layer_on(action.layer_tap.val);
  362. register_mods(action.layer_tap.code & 0x0f);
  363. } else {
  364. layer_off(action.layer_tap.val);
  365. unregister_mods(action.layer_tap.code & 0x0f);
  366. }
  367. break;
  368. case OP_TAP_TOGGLE:
  369. /* tap toggle */
  370. if (event.pressed) {
  371. if (tap_count < TAPPING_TOGGLE) {
  372. layer_invert(action.layer_tap.val);
  373. }
  374. } else {
  375. if (tap_count <= TAPPING_TOGGLE) {
  376. layer_invert(action.layer_tap.val);
  377. }
  378. }
  379. break;
  380. case OP_ON_OFF:
  381. event.pressed ? layer_on(action.layer_tap.val) :
  382. layer_off(action.layer_tap.val);
  383. break;
  384. case OP_OFF_ON:
  385. event.pressed ? layer_off(action.layer_tap.val) :
  386. layer_on(action.layer_tap.val);
  387. break;
  388. case OP_SET_CLEAR:
  389. event.pressed ? layer_move(action.layer_tap.val) :
  390. layer_clear();
  391. break;
  392. #ifndef NO_ACTION_ONESHOT
  393. case OP_ONESHOT:
  394. // Oneshot modifier
  395. #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
  396. do_release_oneshot = false;
  397. if (event.pressed) {
  398. del_mods(get_oneshot_locked_mods());
  399. if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
  400. reset_oneshot_layer();
  401. layer_off(action.layer_tap.val);
  402. break;
  403. } else if (tap_count < ONESHOT_TAP_TOGGLE) {
  404. layer_on(action.layer_tap.val);
  405. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  406. }
  407. } else {
  408. add_mods(get_oneshot_locked_mods());
  409. if (tap_count >= ONESHOT_TAP_TOGGLE) {
  410. reset_oneshot_layer();
  411. clear_oneshot_locked_mods();
  412. set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
  413. } else {
  414. clear_oneshot_layer_state(ONESHOT_PRESSED);
  415. }
  416. }
  417. #else
  418. if (event.pressed) {
  419. layer_on(action.layer_tap.val);
  420. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  421. } else {
  422. clear_oneshot_layer_state(ONESHOT_PRESSED);
  423. if (tap_count > 1) {
  424. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  425. }
  426. }
  427. #endif
  428. break;
  429. #endif
  430. default:
  431. /* tap key */
  432. if (event.pressed) {
  433. if (tap_count > 0) {
  434. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  435. register_code(action.layer_tap.code);
  436. } else {
  437. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  438. layer_on(action.layer_tap.val);
  439. }
  440. } else {
  441. if (tap_count > 0) {
  442. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  443. if (action.layer_tap.code == KC_CAPS) {
  444. wait_ms(80);
  445. }
  446. unregister_code(action.layer_tap.code);
  447. } else {
  448. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  449. layer_off(action.layer_tap.val);
  450. }
  451. }
  452. break;
  453. }
  454. break;
  455. #endif
  456. #endif
  457. /* Extentions */
  458. #ifndef NO_ACTION_MACRO
  459. case ACT_MACRO:
  460. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  461. break;
  462. #endif
  463. #ifdef BACKLIGHT_ENABLE
  464. case ACT_BACKLIGHT:
  465. if (!event.pressed) {
  466. switch (action.backlight.opt) {
  467. case BACKLIGHT_INCREASE:
  468. backlight_increase();
  469. break;
  470. case BACKLIGHT_DECREASE:
  471. backlight_decrease();
  472. break;
  473. case BACKLIGHT_TOGGLE:
  474. backlight_toggle();
  475. break;
  476. case BACKLIGHT_STEP:
  477. backlight_step();
  478. break;
  479. case BACKLIGHT_LEVEL:
  480. backlight_level(action.backlight.level);
  481. break;
  482. }
  483. }
  484. break;
  485. #endif
  486. case ACT_COMMAND:
  487. break;
  488. #ifdef ONEHAND_ENABLE
  489. case ACT_SWAP_HANDS:
  490. switch (action.swap.code) {
  491. case OP_SH_TOGGLE:
  492. if (event.pressed) {
  493. swap_hands = !swap_hands;
  494. }
  495. break;
  496. case OP_SH_ON_OFF:
  497. swap_hands = event.pressed;
  498. break;
  499. case OP_SH_OFF_ON:
  500. swap_hands = !event.pressed;
  501. break;
  502. case OP_SH_ON:
  503. if (!event.pressed) {
  504. swap_hands = true;
  505. }
  506. break;
  507. case OP_SH_OFF:
  508. if (!event.pressed) {
  509. swap_hands = false;
  510. }
  511. break;
  512. #ifndef NO_ACTION_TAPPING
  513. case OP_SH_TAP_TOGGLE:
  514. /* tap toggle */
  515. if (tap_count > 0) {
  516. if (!event.pressed) {
  517. swap_hands = !swap_hands;
  518. }
  519. } else {
  520. swap_hands = event.pressed;
  521. }
  522. break;
  523. default:
  524. if (tap_count > 0) {
  525. if (event.pressed) {
  526. register_code(action.swap.code);
  527. } else {
  528. unregister_code(action.swap.code);
  529. }
  530. } else {
  531. swap_hands = event.pressed;
  532. }
  533. #endif
  534. }
  535. #endif
  536. #ifndef NO_ACTION_FUNCTION
  537. case ACT_FUNCTION:
  538. action_function(record, action.func.id, action.func.opt);
  539. break;
  540. #endif
  541. default:
  542. break;
  543. }
  544. #ifndef NO_ACTION_LAYER
  545. // if this event is a layer action, update the leds
  546. switch (action.kind.id) {
  547. case ACT_LAYER:
  548. #ifndef NO_ACTION_TAPPING
  549. case ACT_LAYER_TAP:
  550. case ACT_LAYER_TAP_EXT:
  551. #endif
  552. led_set(host_keyboard_leds());
  553. break;
  554. default:
  555. break;
  556. }
  557. #endif
  558. #ifndef NO_ACTION_ONESHOT
  559. /* Because we switch layers after a oneshot event, we need to release the
  560. * key before we leave the layer or no key up event will be generated.
  561. */
  562. if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
  563. record->event.pressed = false;
  564. layer_on(get_oneshot_layer());
  565. process_record(record);
  566. layer_off(get_oneshot_layer());
  567. }
  568. #endif
  569. }
  570. /*
  571. * Utilities for actions.
  572. */
  573. void register_code(uint8_t code)
  574. {
  575. if (code == KC_NO) {
  576. return;
  577. }
  578. #ifdef LOCKING_SUPPORT_ENABLE
  579. else if (KC_LOCKING_CAPS == code) {
  580. #ifdef LOCKING_RESYNC_ENABLE
  581. // Resync: ignore if caps lock already is on
  582. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  583. #endif
  584. add_key(KC_CAPSLOCK);
  585. send_keyboard_report();
  586. del_key(KC_CAPSLOCK);
  587. send_keyboard_report();
  588. }
  589. else if (KC_LOCKING_NUM == code) {
  590. #ifdef LOCKING_RESYNC_ENABLE
  591. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  592. #endif
  593. add_key(KC_NUMLOCK);
  594. send_keyboard_report();
  595. del_key(KC_NUMLOCK);
  596. send_keyboard_report();
  597. }
  598. else if (KC_LOCKING_SCROLL == code) {
  599. #ifdef LOCKING_RESYNC_ENABLE
  600. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  601. #endif
  602. add_key(KC_SCROLLLOCK);
  603. send_keyboard_report();
  604. del_key(KC_SCROLLLOCK);
  605. send_keyboard_report();
  606. }
  607. #endif
  608. else if IS_KEY(code) {
  609. // TODO: should push command_proc out of this block?
  610. if (command_proc(code)) return;
  611. #ifndef NO_ACTION_ONESHOT
  612. /* TODO: remove
  613. if (oneshot_state.mods && !oneshot_state.disabled) {
  614. uint8_t tmp_mods = get_mods();
  615. add_mods(oneshot_state.mods);
  616. add_key(code);
  617. send_keyboard_report();
  618. set_mods(tmp_mods);
  619. send_keyboard_report();
  620. oneshot_cancel();
  621. } else
  622. */
  623. #endif
  624. {
  625. add_key(code);
  626. send_keyboard_report();
  627. }
  628. }
  629. else if IS_MOD(code) {
  630. add_mods(MOD_BIT(code));
  631. send_keyboard_report();
  632. }
  633. else if IS_SYSTEM(code) {
  634. host_system_send(KEYCODE2SYSTEM(code));
  635. }
  636. else if IS_CONSUMER(code) {
  637. host_consumer_send(KEYCODE2CONSUMER(code));
  638. }
  639. }
  640. void unregister_code(uint8_t code)
  641. {
  642. if (code == KC_NO) {
  643. return;
  644. }
  645. #ifdef LOCKING_SUPPORT_ENABLE
  646. else if (KC_LOCKING_CAPS == code) {
  647. #ifdef LOCKING_RESYNC_ENABLE
  648. // Resync: ignore if caps lock already is off
  649. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  650. #endif
  651. add_key(KC_CAPSLOCK);
  652. send_keyboard_report();
  653. del_key(KC_CAPSLOCK);
  654. send_keyboard_report();
  655. }
  656. else if (KC_LOCKING_NUM == code) {
  657. #ifdef LOCKING_RESYNC_ENABLE
  658. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  659. #endif
  660. add_key(KC_NUMLOCK);
  661. send_keyboard_report();
  662. del_key(KC_NUMLOCK);
  663. send_keyboard_report();
  664. }
  665. else if (KC_LOCKING_SCROLL == code) {
  666. #ifdef LOCKING_RESYNC_ENABLE
  667. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  668. #endif
  669. add_key(KC_SCROLLLOCK);
  670. send_keyboard_report();
  671. del_key(KC_SCROLLLOCK);
  672. send_keyboard_report();
  673. }
  674. #endif
  675. else if IS_KEY(code) {
  676. del_key(code);
  677. send_keyboard_report();
  678. }
  679. else if IS_MOD(code) {
  680. del_mods(MOD_BIT(code));
  681. send_keyboard_report();
  682. }
  683. else if IS_SYSTEM(code) {
  684. host_system_send(0);
  685. }
  686. else if IS_CONSUMER(code) {
  687. host_consumer_send(0);
  688. }
  689. }
  690. void register_mods(uint8_t mods)
  691. {
  692. if (mods) {
  693. add_mods(mods);
  694. send_keyboard_report();
  695. }
  696. }
  697. void unregister_mods(uint8_t mods)
  698. {
  699. if (mods) {
  700. del_mods(mods);
  701. send_keyboard_report();
  702. }
  703. }
  704. void clear_keyboard(void)
  705. {
  706. clear_mods();
  707. clear_keyboard_but_mods();
  708. }
  709. void clear_keyboard_but_mods(void)
  710. {
  711. clear_weak_mods();
  712. clear_macro_mods();
  713. clear_keys();
  714. send_keyboard_report();
  715. #ifdef MOUSEKEY_ENABLE
  716. mousekey_clear();
  717. mousekey_send();
  718. #endif
  719. #ifdef EXTRAKEY_ENABLE
  720. host_system_send(0);
  721. host_consumer_send(0);
  722. #endif
  723. }
  724. bool is_tap_key(keypos_t key)
  725. {
  726. action_t action = layer_switch_get_action(key);
  727. switch (action.kind.id) {
  728. case ACT_LMODS_TAP:
  729. case ACT_RMODS_TAP:
  730. case ACT_LAYER_TAP:
  731. case ACT_LAYER_TAP_EXT:
  732. switch (action.layer_tap.code) {
  733. case 0x00 ... 0xdf:
  734. case OP_TAP_TOGGLE:
  735. case OP_ONESHOT:
  736. return true;
  737. }
  738. return false;
  739. case ACT_SWAP_HANDS:
  740. switch (action.swap.code) {
  741. case 0x00 ... 0xdf:
  742. case OP_SH_TAP_TOGGLE:
  743. return true;
  744. }
  745. return false;
  746. case ACT_MACRO:
  747. case ACT_FUNCTION:
  748. if (action.func.opt & FUNC_TAP) { return true; }
  749. return false;
  750. }
  751. return false;
  752. }
  753. /*
  754. * debug print
  755. */
  756. void debug_event(keyevent_t event)
  757. {
  758. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  759. }
  760. void debug_record(keyrecord_t record)
  761. {
  762. debug_event(record.event);
  763. #ifndef NO_ACTION_TAPPING
  764. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  765. #endif
  766. }
  767. void debug_action(action_t action)
  768. {
  769. switch (action.kind.id) {
  770. case ACT_LMODS: dprint("ACT_LMODS"); break;
  771. case ACT_RMODS: dprint("ACT_RMODS"); break;
  772. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  773. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  774. case ACT_USAGE: dprint("ACT_USAGE"); break;
  775. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  776. case ACT_LAYER: dprint("ACT_LAYER"); break;
  777. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  778. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  779. case ACT_MACRO: dprint("ACT_MACRO"); break;
  780. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  781. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  782. case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
  783. default: dprint("UNKNOWN"); break;
  784. }
  785. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  786. }