action.c 34 KB

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