action.c 33 KB

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