action.c 38 KB

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