action.c 35 KB

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