action.c 41 KB

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