action.c 34 KB

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