action.c 34 KB

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