action.c 35 KB

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