action.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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) { 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)) &&
  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. case OP_SH_ONESHOT:
  574. if (event.pressed) {
  575. set_oneshot_swaphands();
  576. } else {
  577. release_oneshot_swaphands();
  578. }
  579. break;
  580. # ifndef NO_ACTION_TAPPING
  581. case OP_SH_TAP_TOGGLE:
  582. /* tap toggle */
  583. if (event.pressed) {
  584. if (swap_held) {
  585. swap_held = false;
  586. } else {
  587. swap_hands = !swap_hands;
  588. }
  589. } else {
  590. if (tap_count < TAPPING_TOGGLE) {
  591. swap_hands = !swap_hands;
  592. }
  593. }
  594. break;
  595. default:
  596. /* tap key */
  597. if (tap_count > 0) {
  598. if (swap_held) {
  599. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  600. swap_held = false;
  601. }
  602. if (event.pressed) {
  603. register_code(action.swap.code);
  604. } else {
  605. wait_ms(TAP_CODE_DELAY);
  606. unregister_code(action.swap.code);
  607. *record = (keyrecord_t){}; // hack: reset tap mode
  608. }
  609. } else {
  610. if (swap_held && !event.pressed) {
  611. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  612. swap_held = false;
  613. }
  614. }
  615. # endif
  616. }
  617. #endif
  618. #ifndef NO_ACTION_FUNCTION
  619. case ACT_FUNCTION:
  620. action_function(record, action.func.id, action.func.opt);
  621. break;
  622. #endif
  623. default:
  624. break;
  625. }
  626. #ifndef NO_ACTION_LAYER
  627. // if this event is a layer action, update the leds
  628. switch (action.kind.id) {
  629. case ACT_LAYER:
  630. case ACT_LAYER_MODS:
  631. # ifndef NO_ACTION_TAPPING
  632. case ACT_LAYER_TAP:
  633. case ACT_LAYER_TAP_EXT:
  634. # endif
  635. led_set(host_keyboard_leds());
  636. break;
  637. default:
  638. break;
  639. }
  640. #endif
  641. #ifndef NO_ACTION_TAPPING
  642. # ifdef RETRO_TAPPING
  643. if (!is_tap_action(action)) {
  644. retro_tapping_counter = 0;
  645. } else {
  646. if (event.pressed) {
  647. if (tap_count > 0) {
  648. retro_tapping_counter = 0;
  649. } else {
  650. }
  651. } else {
  652. if (tap_count > 0) {
  653. retro_tapping_counter = 0;
  654. } else {
  655. if (retro_tapping_counter == 2) {
  656. tap_code(action.layer_tap.code);
  657. }
  658. retro_tapping_counter = 0;
  659. }
  660. }
  661. }
  662. # endif
  663. #endif
  664. #ifdef SWAP_HANDS_ENABLE
  665. if (event.pressed && !(action.kind.id == ACT_SWAP_HANDS && action.swap.code == OP_SH_ONESHOT)) {
  666. use_oneshot_swaphands();
  667. }
  668. #endif
  669. #ifndef NO_ACTION_ONESHOT
  670. /* Because we switch layers after a oneshot event, we need to release the
  671. * key before we leave the layer or no key up event will be generated.
  672. */
  673. if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED)) {
  674. record->event.pressed = false;
  675. layer_on(get_oneshot_layer());
  676. process_record(record);
  677. layer_off(get_oneshot_layer());
  678. }
  679. #endif
  680. }
  681. /** \brief Utilities for actions. (FIXME: Needs better description)
  682. *
  683. * FIXME: Needs documentation.
  684. */
  685. void register_code(uint8_t code) {
  686. if (code == KC_NO) {
  687. return;
  688. }
  689. #ifdef LOCKING_SUPPORT_ENABLE
  690. else if (KC_LOCKING_CAPS == code) {
  691. # ifdef LOCKING_RESYNC_ENABLE
  692. // Resync: ignore if caps lock already is on
  693. if (host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK)) return;
  694. # endif
  695. add_key(KC_CAPSLOCK);
  696. send_keyboard_report();
  697. wait_ms(100);
  698. del_key(KC_CAPSLOCK);
  699. send_keyboard_report();
  700. }
  701. else if (KC_LOCKING_NUM == code) {
  702. # ifdef LOCKING_RESYNC_ENABLE
  703. if (host_keyboard_leds() & (1 << USB_LED_NUM_LOCK)) return;
  704. # endif
  705. add_key(KC_NUMLOCK);
  706. send_keyboard_report();
  707. wait_ms(100);
  708. del_key(KC_NUMLOCK);
  709. send_keyboard_report();
  710. }
  711. else if (KC_LOCKING_SCROLL == code) {
  712. # ifdef LOCKING_RESYNC_ENABLE
  713. if (host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK)) return;
  714. # endif
  715. add_key(KC_SCROLLLOCK);
  716. send_keyboard_report();
  717. wait_ms(100);
  718. del_key(KC_SCROLLLOCK);
  719. send_keyboard_report();
  720. }
  721. #endif
  722. else if
  723. IS_KEY(code) {
  724. // TODO: should push command_proc out of this block?
  725. if (command_proc(code)) return;
  726. #ifndef NO_ACTION_ONESHOT
  727. /* TODO: remove
  728. if (oneshot_state.mods && !oneshot_state.disabled) {
  729. uint8_t tmp_mods = get_mods();
  730. add_mods(oneshot_state.mods);
  731. add_key(code);
  732. send_keyboard_report();
  733. set_mods(tmp_mods);
  734. send_keyboard_report();
  735. oneshot_cancel();
  736. } else
  737. */
  738. #endif
  739. {
  740. // Force a new key press if the key is already pressed
  741. // without this, keys with the same keycode, but different
  742. // modifiers will be reported incorrectly, see issue #1708
  743. if (is_key_pressed(keyboard_report, code)) {
  744. del_key(code);
  745. send_keyboard_report();
  746. }
  747. add_key(code);
  748. send_keyboard_report();
  749. }
  750. }
  751. else if
  752. IS_MOD(code) {
  753. add_mods(MOD_BIT(code));
  754. send_keyboard_report();
  755. }
  756. #ifdef EXTRAKEY_ENABLE
  757. else if
  758. IS_SYSTEM(code) { host_system_send(KEYCODE2SYSTEM(code)); }
  759. else if
  760. IS_CONSUMER(code) { host_consumer_send(KEYCODE2CONSUMER(code)); }
  761. #endif
  762. #ifdef MOUSEKEY_ENABLE
  763. else if
  764. IS_MOUSEKEY(code) {
  765. mousekey_on(code);
  766. mousekey_send();
  767. }
  768. #endif
  769. }
  770. /** \brief Utilities for actions. (FIXME: Needs better description)
  771. *
  772. * FIXME: Needs documentation.
  773. */
  774. void unregister_code(uint8_t code) {
  775. if (code == KC_NO) {
  776. return;
  777. }
  778. #ifdef LOCKING_SUPPORT_ENABLE
  779. else if (KC_LOCKING_CAPS == code) {
  780. # ifdef LOCKING_RESYNC_ENABLE
  781. // Resync: ignore if caps lock already is off
  782. if (!(host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK))) return;
  783. # endif
  784. add_key(KC_CAPSLOCK);
  785. send_keyboard_report();
  786. del_key(KC_CAPSLOCK);
  787. send_keyboard_report();
  788. }
  789. else if (KC_LOCKING_NUM == code) {
  790. # ifdef LOCKING_RESYNC_ENABLE
  791. if (!(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK))) return;
  792. # endif
  793. add_key(KC_NUMLOCK);
  794. send_keyboard_report();
  795. del_key(KC_NUMLOCK);
  796. send_keyboard_report();
  797. }
  798. else if (KC_LOCKING_SCROLL == code) {
  799. # ifdef LOCKING_RESYNC_ENABLE
  800. if (!(host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK))) return;
  801. # endif
  802. add_key(KC_SCROLLLOCK);
  803. send_keyboard_report();
  804. del_key(KC_SCROLLLOCK);
  805. send_keyboard_report();
  806. }
  807. #endif
  808. else if
  809. IS_KEY(code) {
  810. del_key(code);
  811. send_keyboard_report();
  812. }
  813. else if
  814. IS_MOD(code) {
  815. del_mods(MOD_BIT(code));
  816. send_keyboard_report();
  817. }
  818. else if
  819. IS_SYSTEM(code) { host_system_send(0); }
  820. else if
  821. IS_CONSUMER(code) { host_consumer_send(0); }
  822. #ifdef MOUSEKEY_ENABLE
  823. else if
  824. IS_MOUSEKEY(code) {
  825. mousekey_off(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 tap_code(uint8_t code) {
  835. register_code(code);
  836. if (code == KC_CAPS) {
  837. wait_ms(TAP_HOLD_CAPS_DELAY);
  838. } else {
  839. wait_ms(TAP_CODE_DELAY);
  840. }
  841. unregister_code(code);
  842. }
  843. /** \brief Adds the given physically pressed modifiers and sends a keyboard report immediately.
  844. *
  845. * \param mods A bitfield of modifiers to register.
  846. */
  847. void register_mods(uint8_t mods) {
  848. if (mods) {
  849. add_mods(mods);
  850. send_keyboard_report();
  851. }
  852. }
  853. /** \brief Removes the given physically pressed modifiers and sends a keyboard report immediately.
  854. *
  855. * \param mods A bitfield of modifiers to unregister.
  856. */
  857. void unregister_mods(uint8_t mods) {
  858. if (mods) {
  859. del_mods(mods);
  860. send_keyboard_report();
  861. }
  862. }
  863. /** \brief Adds the given weak modifiers and sends a keyboard report immediately.
  864. *
  865. * \param mods A bitfield of modifiers to register.
  866. */
  867. void register_weak_mods(uint8_t mods) {
  868. if (mods) {
  869. add_weak_mods(mods);
  870. send_keyboard_report();
  871. }
  872. }
  873. /** \brief Removes the given weak modifiers and sends a keyboard report immediately.
  874. *
  875. * \param mods A bitfield of modifiers to unregister.
  876. */
  877. void unregister_weak_mods(uint8_t mods) {
  878. if (mods) {
  879. del_weak_mods(mods);
  880. send_keyboard_report();
  881. }
  882. }
  883. /** \brief Utilities for actions. (FIXME: Needs better description)
  884. *
  885. * FIXME: Needs documentation.
  886. */
  887. void clear_keyboard(void) {
  888. clear_mods();
  889. clear_keyboard_but_mods();
  890. }
  891. /** \brief Utilities for actions. (FIXME: Needs better description)
  892. *
  893. * FIXME: Needs documentation.
  894. */
  895. void clear_keyboard_but_mods(void) {
  896. clear_keys();
  897. clear_keyboard_but_mods_and_keys();
  898. }
  899. /** \brief Utilities for actions. (FIXME: Needs better description)
  900. *
  901. * FIXME: Needs documentation.
  902. */
  903. void clear_keyboard_but_mods_and_keys() {
  904. clear_weak_mods();
  905. clear_macro_mods();
  906. send_keyboard_report();
  907. #ifdef MOUSEKEY_ENABLE
  908. mousekey_clear();
  909. mousekey_send();
  910. #endif
  911. #ifdef EXTRAKEY_ENABLE
  912. host_system_send(0);
  913. host_consumer_send(0);
  914. #endif
  915. }
  916. /** \brief Utilities for actions. (FIXME: Needs better description)
  917. *
  918. * FIXME: Needs documentation.
  919. */
  920. bool is_tap_key(keypos_t key) {
  921. action_t action = layer_switch_get_action(key);
  922. return is_tap_action(action);
  923. }
  924. /** \brief Utilities for actions. (FIXME: Needs better description)
  925. *
  926. * FIXME: Needs documentation.
  927. */
  928. bool is_tap_action(action_t action) {
  929. switch (action.kind.id) {
  930. case ACT_LMODS_TAP:
  931. case ACT_RMODS_TAP:
  932. case ACT_LAYER_TAP:
  933. case ACT_LAYER_TAP_EXT:
  934. switch (action.layer_tap.code) {
  935. case KC_NO ... KC_RGUI:
  936. case OP_TAP_TOGGLE:
  937. case OP_ONESHOT:
  938. return true;
  939. }
  940. return false;
  941. case ACT_SWAP_HANDS:
  942. switch (action.swap.code) {
  943. case KC_NO ... KC_RGUI:
  944. case OP_SH_TAP_TOGGLE:
  945. return true;
  946. }
  947. return false;
  948. case ACT_MACRO:
  949. case ACT_FUNCTION:
  950. if (action.func.opt & FUNC_TAP) {
  951. return true;
  952. }
  953. return false;
  954. }
  955. return false;
  956. }
  957. /** \brief Debug print (FIXME: Needs better description)
  958. *
  959. * FIXME: Needs documentation.
  960. */
  961. void debug_event(keyevent_t event) { dprintf("%04X%c(%u)", (event.key.row << 8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time); }
  962. /** \brief Debug print (FIXME: Needs better description)
  963. *
  964. * FIXME: Needs documentation.
  965. */
  966. void debug_record(keyrecord_t record) {
  967. debug_event(record.event);
  968. #ifndef NO_ACTION_TAPPING
  969. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  970. #endif
  971. }
  972. /** \brief Debug print (FIXME: Needs better description)
  973. *
  974. * FIXME: Needs documentation.
  975. */
  976. void debug_action(action_t action) {
  977. switch (action.kind.id) {
  978. case ACT_LMODS:
  979. dprint("ACT_LMODS");
  980. break;
  981. case ACT_RMODS:
  982. dprint("ACT_RMODS");
  983. break;
  984. case ACT_LMODS_TAP:
  985. dprint("ACT_LMODS_TAP");
  986. break;
  987. case ACT_RMODS_TAP:
  988. dprint("ACT_RMODS_TAP");
  989. break;
  990. case ACT_USAGE:
  991. dprint("ACT_USAGE");
  992. break;
  993. case ACT_MOUSEKEY:
  994. dprint("ACT_MOUSEKEY");
  995. break;
  996. case ACT_LAYER:
  997. dprint("ACT_LAYER");
  998. break;
  999. case ACT_LAYER_MODS:
  1000. dprint("ACT_LAYER_MODS");
  1001. break;
  1002. case ACT_LAYER_TAP:
  1003. dprint("ACT_LAYER_TAP");
  1004. break;
  1005. case ACT_LAYER_TAP_EXT:
  1006. dprint("ACT_LAYER_TAP_EXT");
  1007. break;
  1008. case ACT_MACRO:
  1009. dprint("ACT_MACRO");
  1010. break;
  1011. case ACT_FUNCTION:
  1012. dprint("ACT_FUNCTION");
  1013. break;
  1014. case ACT_SWAP_HANDS:
  1015. dprint("ACT_SWAP_HANDS");
  1016. break;
  1017. default:
  1018. dprint("UNKNOWN");
  1019. break;
  1020. }
  1021. dprintf("[%X:%02X]", action.kind.param >> 8, action.kind.param & 0xff);
  1022. }