action.c 39 KB

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