action.c 32 KB

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