action.c 32 KB

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