action.c 37 KB

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