action.c 37 KB

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