action.c 36 KB

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