action.c 30 KB

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