action.c 35 KB

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