action.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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_LEVEL:
  488. backlight_level(action.backlight.level);
  489. break;
  490. }
  491. }
  492. break;
  493. #endif
  494. case ACT_COMMAND:
  495. break;
  496. #ifdef ONEHAND_ENABLE
  497. case ACT_SWAP_HANDS:
  498. switch (action.swap.code) {
  499. case OP_SH_TOGGLE:
  500. if (event.pressed) {
  501. swap_hands = !swap_hands;
  502. }
  503. break;
  504. case OP_SH_ON_OFF:
  505. swap_hands = event.pressed;
  506. break;
  507. case OP_SH_OFF_ON:
  508. swap_hands = !event.pressed;
  509. break;
  510. case OP_SH_ON:
  511. if (!event.pressed) {
  512. swap_hands = true;
  513. }
  514. break;
  515. case OP_SH_OFF:
  516. if (!event.pressed) {
  517. swap_hands = false;
  518. }
  519. break;
  520. #ifndef NO_ACTION_TAPPING
  521. case OP_SH_TAP_TOGGLE:
  522. /* tap toggle */
  523. if (tap_count > 0) {
  524. if (!event.pressed) {
  525. swap_hands = !swap_hands;
  526. }
  527. } else {
  528. swap_hands = event.pressed;
  529. }
  530. break;
  531. default:
  532. if (tap_count > 0) {
  533. if (event.pressed) {
  534. register_code(action.swap.code);
  535. } else {
  536. unregister_code(action.swap.code);
  537. }
  538. } else {
  539. swap_hands = event.pressed;
  540. }
  541. #endif
  542. }
  543. #endif
  544. #ifndef NO_ACTION_FUNCTION
  545. case ACT_FUNCTION:
  546. action_function(record, action.func.id, action.func.opt);
  547. break;
  548. #endif
  549. default:
  550. break;
  551. }
  552. #ifndef NO_ACTION_LAYER
  553. // if this event is a layer action, update the leds
  554. switch (action.kind.id) {
  555. case ACT_LAYER:
  556. #ifndef NO_ACTION_TAPPING
  557. case ACT_LAYER_TAP:
  558. case ACT_LAYER_TAP_EXT:
  559. #endif
  560. led_set(host_keyboard_leds());
  561. break;
  562. default:
  563. break;
  564. }
  565. #endif
  566. #ifndef NO_ACTION_TAPPING
  567. #ifdef RETRO_TAPPING
  568. if (!is_tap_key(record->event.key)) {
  569. retro_tapping_counter = 0;
  570. } else {
  571. if (event.pressed) {
  572. if (tap_count > 0) {
  573. retro_tapping_counter = 0;
  574. } else {
  575. }
  576. } else {
  577. if (tap_count > 0) {
  578. retro_tapping_counter = 0;
  579. } else {
  580. if (retro_tapping_counter == 2) {
  581. register_code(action.layer_tap.code);
  582. unregister_code(action.layer_tap.code);
  583. }
  584. retro_tapping_counter = 0;
  585. }
  586. }
  587. }
  588. #endif
  589. #endif
  590. #ifndef NO_ACTION_ONESHOT
  591. /* Because we switch layers after a oneshot event, we need to release the
  592. * key before we leave the layer or no key up event will be generated.
  593. */
  594. if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
  595. record->event.pressed = false;
  596. layer_on(get_oneshot_layer());
  597. process_record(record);
  598. layer_off(get_oneshot_layer());
  599. }
  600. #endif
  601. }
  602. /*
  603. * Utilities for actions.
  604. */
  605. void register_code(uint8_t code)
  606. {
  607. if (code == KC_NO) {
  608. return;
  609. }
  610. #ifdef LOCKING_SUPPORT_ENABLE
  611. else if (KC_LOCKING_CAPS == code) {
  612. #ifdef LOCKING_RESYNC_ENABLE
  613. // Resync: ignore if caps lock already is on
  614. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  615. #endif
  616. add_key(KC_CAPSLOCK);
  617. send_keyboard_report();
  618. wait_ms(100);
  619. del_key(KC_CAPSLOCK);
  620. send_keyboard_report();
  621. }
  622. else if (KC_LOCKING_NUM == code) {
  623. #ifdef LOCKING_RESYNC_ENABLE
  624. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  625. #endif
  626. add_key(KC_NUMLOCK);
  627. send_keyboard_report();
  628. wait_ms(100);
  629. del_key(KC_NUMLOCK);
  630. send_keyboard_report();
  631. }
  632. else if (KC_LOCKING_SCROLL == code) {
  633. #ifdef LOCKING_RESYNC_ENABLE
  634. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  635. #endif
  636. add_key(KC_SCROLLLOCK);
  637. send_keyboard_report();
  638. wait_ms(100);
  639. del_key(KC_SCROLLLOCK);
  640. send_keyboard_report();
  641. }
  642. #endif
  643. else if IS_KEY(code) {
  644. // TODO: should push command_proc out of this block?
  645. if (command_proc(code)) return;
  646. #ifndef NO_ACTION_ONESHOT
  647. /* TODO: remove
  648. if (oneshot_state.mods && !oneshot_state.disabled) {
  649. uint8_t tmp_mods = get_mods();
  650. add_mods(oneshot_state.mods);
  651. add_key(code);
  652. send_keyboard_report();
  653. set_mods(tmp_mods);
  654. send_keyboard_report();
  655. oneshot_cancel();
  656. } else
  657. */
  658. #endif
  659. {
  660. add_key(code);
  661. send_keyboard_report();
  662. }
  663. }
  664. else if IS_MOD(code) {
  665. add_mods(MOD_BIT(code));
  666. send_keyboard_report();
  667. }
  668. else if IS_SYSTEM(code) {
  669. host_system_send(KEYCODE2SYSTEM(code));
  670. }
  671. else if IS_CONSUMER(code) {
  672. host_consumer_send(KEYCODE2CONSUMER(code));
  673. }
  674. }
  675. void unregister_code(uint8_t code)
  676. {
  677. if (code == KC_NO) {
  678. return;
  679. }
  680. #ifdef LOCKING_SUPPORT_ENABLE
  681. else if (KC_LOCKING_CAPS == code) {
  682. #ifdef LOCKING_RESYNC_ENABLE
  683. // Resync: ignore if caps lock already is off
  684. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  685. #endif
  686. add_key(KC_CAPSLOCK);
  687. send_keyboard_report();
  688. del_key(KC_CAPSLOCK);
  689. send_keyboard_report();
  690. }
  691. else if (KC_LOCKING_NUM == code) {
  692. #ifdef LOCKING_RESYNC_ENABLE
  693. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  694. #endif
  695. add_key(KC_NUMLOCK);
  696. send_keyboard_report();
  697. del_key(KC_NUMLOCK);
  698. send_keyboard_report();
  699. }
  700. else if (KC_LOCKING_SCROLL == code) {
  701. #ifdef LOCKING_RESYNC_ENABLE
  702. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  703. #endif
  704. add_key(KC_SCROLLLOCK);
  705. send_keyboard_report();
  706. del_key(KC_SCROLLLOCK);
  707. send_keyboard_report();
  708. }
  709. #endif
  710. else if IS_KEY(code) {
  711. del_key(code);
  712. send_keyboard_report();
  713. }
  714. else if IS_MOD(code) {
  715. del_mods(MOD_BIT(code));
  716. send_keyboard_report();
  717. }
  718. else if IS_SYSTEM(code) {
  719. host_system_send(0);
  720. }
  721. else if IS_CONSUMER(code) {
  722. host_consumer_send(0);
  723. }
  724. }
  725. void register_mods(uint8_t mods)
  726. {
  727. if (mods) {
  728. add_mods(mods);
  729. send_keyboard_report();
  730. }
  731. }
  732. void unregister_mods(uint8_t mods)
  733. {
  734. if (mods) {
  735. del_mods(mods);
  736. send_keyboard_report();
  737. }
  738. }
  739. void clear_keyboard(void)
  740. {
  741. clear_mods();
  742. clear_keyboard_but_mods();
  743. }
  744. void clear_keyboard_but_mods(void)
  745. {
  746. clear_weak_mods();
  747. clear_macro_mods();
  748. clear_keys();
  749. send_keyboard_report();
  750. #ifdef MOUSEKEY_ENABLE
  751. mousekey_clear();
  752. mousekey_send();
  753. #endif
  754. #ifdef EXTRAKEY_ENABLE
  755. host_system_send(0);
  756. host_consumer_send(0);
  757. #endif
  758. }
  759. bool is_tap_key(keypos_t key)
  760. {
  761. action_t action = layer_switch_get_action(key);
  762. switch (action.kind.id) {
  763. case ACT_LMODS_TAP:
  764. case ACT_RMODS_TAP:
  765. case ACT_LAYER_TAP:
  766. case ACT_LAYER_TAP_EXT:
  767. switch (action.layer_tap.code) {
  768. case 0x00 ... 0xdf:
  769. case OP_TAP_TOGGLE:
  770. case OP_ONESHOT:
  771. return true;
  772. }
  773. return false;
  774. case ACT_SWAP_HANDS:
  775. switch (action.swap.code) {
  776. case 0x00 ... 0xdf:
  777. case OP_SH_TAP_TOGGLE:
  778. return true;
  779. }
  780. return false;
  781. case ACT_MACRO:
  782. case ACT_FUNCTION:
  783. if (action.func.opt & FUNC_TAP) { return true; }
  784. return false;
  785. }
  786. return false;
  787. }
  788. /*
  789. * debug print
  790. */
  791. void debug_event(keyevent_t event)
  792. {
  793. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  794. }
  795. void debug_record(keyrecord_t record)
  796. {
  797. debug_event(record.event);
  798. #ifndef NO_ACTION_TAPPING
  799. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  800. #endif
  801. }
  802. void debug_action(action_t action)
  803. {
  804. switch (action.kind.id) {
  805. case ACT_LMODS: dprint("ACT_LMODS"); break;
  806. case ACT_RMODS: dprint("ACT_RMODS"); break;
  807. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  808. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  809. case ACT_USAGE: dprint("ACT_USAGE"); break;
  810. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  811. case ACT_LAYER: dprint("ACT_LAYER"); break;
  812. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  813. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  814. case ACT_MACRO: dprint("ACT_MACRO"); break;
  815. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  816. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  817. case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
  818. default: dprint("UNKNOWN"); break;
  819. }
  820. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  821. }