action.c 32 KB

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