action.c 33 KB

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