action.c 36 KB

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