action.c 36 KB

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