action.c 28 KB

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