action.c 40 KB

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