action.c 36 KB

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