action.c 33 KB

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