action.c 32 KB

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