2
0

action.c 32 KB

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