action.c 40 KB

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