action.c 38 KB

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