action.c 44 KB

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