action.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. # endif
  410. default:
  411. mousekey_send();
  412. break;
  413. }
  414. } else {
  415. mousekey_off(action.key.code);
  416. switch (action.key.code) {
  417. # if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
  418. case KC_MS_BTN1:
  419. register_button(false, MOUSE_BTN1);
  420. break;
  421. case KC_MS_BTN2:
  422. register_button(false, MOUSE_BTN2);
  423. break;
  424. case KC_MS_BTN3:
  425. register_button(false, MOUSE_BTN3);
  426. break;
  427. # endif
  428. # ifdef POINTING_DEVICE_ENABLE
  429. case KC_MS_BTN4:
  430. register_button(false, MOUSE_BTN4);
  431. break;
  432. case KC_MS_BTN5:
  433. register_button(false, MOUSE_BTN5);
  434. break;
  435. # endif
  436. default:
  437. mousekey_send();
  438. break;
  439. }
  440. }
  441. break;
  442. #endif
  443. #ifndef NO_ACTION_LAYER
  444. case ACT_LAYER:
  445. if (action.layer_bitop.on == 0) {
  446. /* Default Layer Bitwise Operation */
  447. if (!event.pressed) {
  448. uint8_t shift = action.layer_bitop.part * 4;
  449. layer_state_t bits = ((layer_state_t)action.layer_bitop.bits) << shift;
  450. layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf) << shift) : 0;
  451. switch (action.layer_bitop.op) {
  452. case OP_BIT_AND:
  453. default_layer_and(bits | mask);
  454. break;
  455. case OP_BIT_OR:
  456. default_layer_or(bits | mask);
  457. break;
  458. case OP_BIT_XOR:
  459. default_layer_xor(bits | mask);
  460. break;
  461. case OP_BIT_SET:
  462. default_layer_set(bits | mask);
  463. break;
  464. }
  465. }
  466. } else {
  467. /* Layer Bitwise Operation */
  468. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) : (action.layer_bitop.on & ON_RELEASE)) {
  469. uint8_t shift = action.layer_bitop.part * 4;
  470. layer_state_t bits = ((layer_state_t)action.layer_bitop.bits) << shift;
  471. layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf) << shift) : 0;
  472. switch (action.layer_bitop.op) {
  473. case OP_BIT_AND:
  474. layer_and(bits | mask);
  475. break;
  476. case OP_BIT_OR:
  477. layer_or(bits | mask);
  478. break;
  479. case OP_BIT_XOR:
  480. layer_xor(bits | mask);
  481. break;
  482. case OP_BIT_SET:
  483. layer_state_set(bits | mask);
  484. break;
  485. }
  486. }
  487. }
  488. break;
  489. case ACT_LAYER_MODS:
  490. if (event.pressed) {
  491. layer_on(action.layer_mods.layer);
  492. register_mods(action.layer_mods.mods);
  493. } else {
  494. unregister_mods(action.layer_mods.mods);
  495. layer_off(action.layer_mods.layer);
  496. }
  497. break;
  498. # ifndef NO_ACTION_TAPPING
  499. case ACT_LAYER_TAP:
  500. case ACT_LAYER_TAP_EXT:
  501. switch (action.layer_tap.code) {
  502. case OP_TAP_TOGGLE:
  503. /* tap toggle */
  504. if (event.pressed) {
  505. if (tap_count < TAPPING_TOGGLE) {
  506. layer_invert(action.layer_tap.val);
  507. }
  508. } else {
  509. if (tap_count <= TAPPING_TOGGLE) {
  510. layer_invert(action.layer_tap.val);
  511. }
  512. }
  513. break;
  514. case OP_ON_OFF:
  515. event.pressed ? layer_on(action.layer_tap.val) : layer_off(action.layer_tap.val);
  516. break;
  517. case OP_OFF_ON:
  518. event.pressed ? layer_off(action.layer_tap.val) : layer_on(action.layer_tap.val);
  519. break;
  520. case OP_SET_CLEAR:
  521. event.pressed ? layer_move(action.layer_tap.val) : layer_clear();
  522. break;
  523. # ifndef NO_ACTION_ONESHOT
  524. case OP_ONESHOT:
  525. // Oneshot modifier
  526. # if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
  527. do_release_oneshot = false;
  528. if (event.pressed) {
  529. del_mods(get_oneshot_locked_mods());
  530. if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
  531. reset_oneshot_layer();
  532. layer_off(action.layer_tap.val);
  533. break;
  534. } else if (tap_count < ONESHOT_TAP_TOGGLE) {
  535. layer_on(action.layer_tap.val);
  536. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  537. }
  538. } else {
  539. add_mods(get_oneshot_locked_mods());
  540. if (tap_count >= ONESHOT_TAP_TOGGLE) {
  541. reset_oneshot_layer();
  542. clear_oneshot_locked_mods();
  543. set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
  544. } else {
  545. clear_oneshot_layer_state(ONESHOT_PRESSED);
  546. }
  547. }
  548. # else
  549. if (event.pressed) {
  550. layer_on(action.layer_tap.val);
  551. set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
  552. } else {
  553. clear_oneshot_layer_state(ONESHOT_PRESSED);
  554. if (tap_count > 1) {
  555. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  556. }
  557. }
  558. # endif
  559. break;
  560. # endif
  561. default:
  562. /* tap key */
  563. if (event.pressed) {
  564. if (tap_count > 0) {
  565. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  566. register_code(action.layer_tap.code);
  567. } else {
  568. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  569. layer_on(action.layer_tap.val);
  570. }
  571. } else {
  572. if (tap_count > 0) {
  573. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  574. if (action.layer_tap.code == KC_CAPS) {
  575. wait_ms(TAP_HOLD_CAPS_DELAY);
  576. } else {
  577. wait_ms(TAP_CODE_DELAY);
  578. }
  579. unregister_code(action.layer_tap.code);
  580. } else {
  581. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  582. layer_off(action.layer_tap.val);
  583. }
  584. }
  585. break;
  586. }
  587. break;
  588. # endif
  589. #endif
  590. /* Extentions */
  591. #ifndef NO_ACTION_MACRO
  592. case ACT_MACRO:
  593. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  594. break;
  595. #endif
  596. #ifdef SWAP_HANDS_ENABLE
  597. case ACT_SWAP_HANDS:
  598. switch (action.swap.code) {
  599. case OP_SH_TOGGLE:
  600. if (event.pressed) {
  601. swap_hands = !swap_hands;
  602. }
  603. break;
  604. case OP_SH_ON_OFF:
  605. swap_hands = event.pressed;
  606. break;
  607. case OP_SH_OFF_ON:
  608. swap_hands = !event.pressed;
  609. break;
  610. case OP_SH_ON:
  611. if (!event.pressed) {
  612. swap_hands = true;
  613. }
  614. break;
  615. case OP_SH_OFF:
  616. if (!event.pressed) {
  617. swap_hands = false;
  618. }
  619. break;
  620. # ifndef NO_ACTION_ONESHOT
  621. case OP_SH_ONESHOT:
  622. if (event.pressed) {
  623. set_oneshot_swaphands();
  624. } else {
  625. release_oneshot_swaphands();
  626. }
  627. break;
  628. # endif
  629. # ifndef NO_ACTION_TAPPING
  630. case OP_SH_TAP_TOGGLE:
  631. /* tap toggle */
  632. if (event.pressed) {
  633. if (swap_held) {
  634. swap_held = false;
  635. } else {
  636. swap_hands = !swap_hands;
  637. }
  638. } else {
  639. if (tap_count < TAPPING_TOGGLE) {
  640. swap_hands = !swap_hands;
  641. }
  642. }
  643. break;
  644. default:
  645. /* tap key */
  646. if (tap_count > 0) {
  647. if (swap_held) {
  648. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  649. swap_held = false;
  650. }
  651. if (event.pressed) {
  652. register_code(action.swap.code);
  653. } else {
  654. wait_ms(TAP_CODE_DELAY);
  655. unregister_code(action.swap.code);
  656. *record = (keyrecord_t){}; // hack: reset tap mode
  657. }
  658. } else {
  659. if (swap_held && !event.pressed) {
  660. swap_hands = !swap_hands; // undo hold set up in _tap_hint
  661. swap_held = false;
  662. }
  663. }
  664. # endif
  665. }
  666. #endif
  667. #ifndef NO_ACTION_FUNCTION
  668. case ACT_FUNCTION:
  669. action_function(record, action.func.id, action.func.opt);
  670. break;
  671. #endif
  672. default:
  673. break;
  674. }
  675. #ifndef NO_ACTION_LAYER
  676. // if this event is a layer action, update the leds
  677. switch (action.kind.id) {
  678. case ACT_LAYER:
  679. case ACT_LAYER_MODS:
  680. # ifndef NO_ACTION_TAPPING
  681. case ACT_LAYER_TAP:
  682. case ACT_LAYER_TAP_EXT:
  683. # endif
  684. led_set(host_keyboard_leds());
  685. break;
  686. default:
  687. break;
  688. }
  689. #endif
  690. #ifndef NO_ACTION_TAPPING
  691. # if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY)
  692. if (!is_tap_action(action)) {
  693. retro_tapping_counter = 0;
  694. } else {
  695. if (event.pressed) {
  696. if (tap_count > 0) {
  697. retro_tapping_counter = 0;
  698. }
  699. } else {
  700. if (tap_count > 0) {
  701. retro_tapping_counter = 0;
  702. } else {
  703. if (
  704. # ifdef RETRO_TAPPING_PER_KEY
  705. get_retro_tapping(get_event_keycode(record->event, false), record) &&
  706. # endif
  707. retro_tapping_counter == 2) {
  708. tap_code(action.layer_tap.code);
  709. }
  710. retro_tapping_counter = 0;
  711. }
  712. }
  713. }
  714. # endif
  715. #endif
  716. #ifdef SWAP_HANDS_ENABLE
  717. # ifndef NO_ACTION_ONESHOT
  718. if (event.pressed && !(action.kind.id == ACT_SWAP_HANDS && action.swap.code == OP_SH_ONESHOT)) {
  719. use_oneshot_swaphands();
  720. }
  721. # endif
  722. #endif
  723. #ifndef NO_ACTION_ONESHOT
  724. /* Because we switch layers after a oneshot event, we need to release the
  725. * key before we leave the layer or no key up event will be generated.
  726. */
  727. if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED)) {
  728. record->event.pressed = false;
  729. layer_on(get_oneshot_layer());
  730. process_record(record);
  731. layer_off(get_oneshot_layer());
  732. }
  733. #endif
  734. }
  735. /** \brief Utilities for actions. (FIXME: Needs better description)
  736. *
  737. * FIXME: Needs documentation.
  738. */
  739. void register_code(uint8_t code) {
  740. if (code == KC_NO) {
  741. return;
  742. }
  743. #ifdef LOCKING_SUPPORT_ENABLE
  744. else if (KC_LOCKING_CAPS == code) {
  745. # ifdef LOCKING_RESYNC_ENABLE
  746. // Resync: ignore if caps lock already is on
  747. if (host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK)) return;
  748. # endif
  749. add_key(KC_CAPSLOCK);
  750. send_keyboard_report();
  751. wait_ms(100);
  752. del_key(KC_CAPSLOCK);
  753. send_keyboard_report();
  754. }
  755. else if (KC_LOCKING_NUM == code) {
  756. # ifdef LOCKING_RESYNC_ENABLE
  757. if (host_keyboard_leds() & (1 << USB_LED_NUM_LOCK)) return;
  758. # endif
  759. add_key(KC_NUMLOCK);
  760. send_keyboard_report();
  761. wait_ms(100);
  762. del_key(KC_NUMLOCK);
  763. send_keyboard_report();
  764. }
  765. else if (KC_LOCKING_SCROLL == code) {
  766. # ifdef LOCKING_RESYNC_ENABLE
  767. if (host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK)) return;
  768. # endif
  769. add_key(KC_SCROLLLOCK);
  770. send_keyboard_report();
  771. wait_ms(100);
  772. del_key(KC_SCROLLLOCK);
  773. send_keyboard_report();
  774. }
  775. #endif
  776. else if
  777. IS_KEY(code) {
  778. // TODO: should push command_proc out of this block?
  779. if (command_proc(code)) return;
  780. #ifndef NO_ACTION_ONESHOT
  781. /* TODO: remove
  782. if (oneshot_state.mods && !oneshot_state.disabled) {
  783. uint8_t tmp_mods = get_mods();
  784. add_mods(oneshot_state.mods);
  785. add_key(code);
  786. send_keyboard_report();
  787. set_mods(tmp_mods);
  788. send_keyboard_report();
  789. oneshot_cancel();
  790. } else
  791. */
  792. #endif
  793. {
  794. // Force a new key press if the key is already pressed
  795. // without this, keys with the same keycode, but different
  796. // modifiers will be reported incorrectly, see issue #1708
  797. if (is_key_pressed(keyboard_report, code)) {
  798. del_key(code);
  799. send_keyboard_report();
  800. }
  801. add_key(code);
  802. send_keyboard_report();
  803. }
  804. }
  805. else if
  806. IS_MOD(code) {
  807. add_mods(MOD_BIT(code));
  808. send_keyboard_report();
  809. }
  810. #ifdef EXTRAKEY_ENABLE
  811. else if
  812. IS_SYSTEM(code) { host_system_send(KEYCODE2SYSTEM(code)); }
  813. else if
  814. IS_CONSUMER(code) { host_consumer_send(KEYCODE2CONSUMER(code)); }
  815. #endif
  816. #ifdef MOUSEKEY_ENABLE
  817. else if
  818. IS_MOUSEKEY(code) {
  819. mousekey_on(code);
  820. mousekey_send();
  821. }
  822. #endif
  823. }
  824. /** \brief Utilities for actions. (FIXME: Needs better description)
  825. *
  826. * FIXME: Needs documentation.
  827. */
  828. void unregister_code(uint8_t code) {
  829. if (code == KC_NO) {
  830. return;
  831. }
  832. #ifdef LOCKING_SUPPORT_ENABLE
  833. else if (KC_LOCKING_CAPS == code) {
  834. # ifdef LOCKING_RESYNC_ENABLE
  835. // Resync: ignore if caps lock already is off
  836. if (!(host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK))) return;
  837. # endif
  838. add_key(KC_CAPSLOCK);
  839. send_keyboard_report();
  840. del_key(KC_CAPSLOCK);
  841. send_keyboard_report();
  842. }
  843. else if (KC_LOCKING_NUM == code) {
  844. # ifdef LOCKING_RESYNC_ENABLE
  845. if (!(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK))) return;
  846. # endif
  847. add_key(KC_NUMLOCK);
  848. send_keyboard_report();
  849. del_key(KC_NUMLOCK);
  850. send_keyboard_report();
  851. }
  852. else if (KC_LOCKING_SCROLL == code) {
  853. # ifdef LOCKING_RESYNC_ENABLE
  854. if (!(host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK))) return;
  855. # endif
  856. add_key(KC_SCROLLLOCK);
  857. send_keyboard_report();
  858. del_key(KC_SCROLLLOCK);
  859. send_keyboard_report();
  860. }
  861. #endif
  862. else if
  863. IS_KEY(code) {
  864. del_key(code);
  865. send_keyboard_report();
  866. }
  867. else if
  868. IS_MOD(code) {
  869. del_mods(MOD_BIT(code));
  870. send_keyboard_report();
  871. }
  872. else if
  873. IS_SYSTEM(code) { host_system_send(0); }
  874. else if
  875. IS_CONSUMER(code) { host_consumer_send(0); }
  876. #ifdef MOUSEKEY_ENABLE
  877. else if
  878. IS_MOUSEKEY(code) {
  879. mousekey_off(code);
  880. mousekey_send();
  881. }
  882. #endif
  883. }
  884. /** \brief Utilities for actions. (FIXME: Needs better description)
  885. *
  886. * FIXME: Needs documentation.
  887. */
  888. void tap_code(uint8_t code) {
  889. register_code(code);
  890. if (code == KC_CAPS) {
  891. wait_ms(TAP_HOLD_CAPS_DELAY);
  892. } else {
  893. wait_ms(TAP_CODE_DELAY);
  894. }
  895. unregister_code(code);
  896. }
  897. /** \brief Adds the given physically pressed modifiers and sends a keyboard report immediately.
  898. *
  899. * \param mods A bitfield of modifiers to register.
  900. */
  901. void register_mods(uint8_t mods) {
  902. if (mods) {
  903. add_mods(mods);
  904. send_keyboard_report();
  905. }
  906. }
  907. /** \brief Removes the given physically pressed modifiers and sends a keyboard report immediately.
  908. *
  909. * \param mods A bitfield of modifiers to unregister.
  910. */
  911. void unregister_mods(uint8_t mods) {
  912. if (mods) {
  913. del_mods(mods);
  914. send_keyboard_report();
  915. }
  916. }
  917. /** \brief Adds the given weak modifiers and sends a keyboard report immediately.
  918. *
  919. * \param mods A bitfield of modifiers to register.
  920. */
  921. void register_weak_mods(uint8_t mods) {
  922. if (mods) {
  923. add_weak_mods(mods);
  924. send_keyboard_report();
  925. }
  926. }
  927. /** \brief Removes the given weak modifiers and sends a keyboard report immediately.
  928. *
  929. * \param mods A bitfield of modifiers to unregister.
  930. */
  931. void unregister_weak_mods(uint8_t mods) {
  932. if (mods) {
  933. del_weak_mods(mods);
  934. send_keyboard_report();
  935. }
  936. }
  937. /** \brief Utilities for actions. (FIXME: Needs better description)
  938. *
  939. * FIXME: Needs documentation.
  940. */
  941. void clear_keyboard(void) {
  942. clear_mods();
  943. clear_keyboard_but_mods();
  944. }
  945. /** \brief Utilities for actions. (FIXME: Needs better description)
  946. *
  947. * FIXME: Needs documentation.
  948. */
  949. void clear_keyboard_but_mods(void) {
  950. clear_keys();
  951. clear_keyboard_but_mods_and_keys();
  952. }
  953. /** \brief Utilities for actions. (FIXME: Needs better description)
  954. *
  955. * FIXME: Needs documentation.
  956. */
  957. void clear_keyboard_but_mods_and_keys() {
  958. clear_weak_mods();
  959. clear_macro_mods();
  960. send_keyboard_report();
  961. #ifdef MOUSEKEY_ENABLE
  962. mousekey_clear();
  963. mousekey_send();
  964. #endif
  965. #ifdef EXTRAKEY_ENABLE
  966. host_system_send(0);
  967. host_consumer_send(0);
  968. #endif
  969. }
  970. /** \brief Utilities for actions. (FIXME: Needs better description)
  971. *
  972. * FIXME: Needs documentation.
  973. */
  974. bool is_tap_key(keypos_t key) {
  975. action_t action = layer_switch_get_action(key);
  976. return is_tap_action(action);
  977. }
  978. /** \brief Utilities for actions. (FIXME: Needs better description)
  979. *
  980. * FIXME: Needs documentation.
  981. */
  982. bool is_tap_action(action_t action) {
  983. switch (action.kind.id) {
  984. case ACT_LMODS_TAP:
  985. case ACT_RMODS_TAP:
  986. case ACT_LAYER_TAP:
  987. case ACT_LAYER_TAP_EXT:
  988. switch (action.layer_tap.code) {
  989. case KC_NO ... KC_RGUI:
  990. case OP_TAP_TOGGLE:
  991. case OP_ONESHOT:
  992. return true;
  993. }
  994. return false;
  995. case ACT_SWAP_HANDS:
  996. switch (action.swap.code) {
  997. case KC_NO ... KC_RGUI:
  998. case OP_SH_TAP_TOGGLE:
  999. return true;
  1000. }
  1001. return false;
  1002. case ACT_MACRO:
  1003. case ACT_FUNCTION:
  1004. if (action.func.opt & FUNC_TAP) {
  1005. return true;
  1006. }
  1007. return false;
  1008. }
  1009. return false;
  1010. }
  1011. /** \brief Debug print (FIXME: Needs better description)
  1012. *
  1013. * FIXME: Needs documentation.
  1014. */
  1015. void debug_event(keyevent_t event) { dprintf("%04X%c(%u)", (event.key.row << 8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time); }
  1016. /** \brief Debug print (FIXME: Needs better description)
  1017. *
  1018. * FIXME: Needs documentation.
  1019. */
  1020. void debug_record(keyrecord_t record) {
  1021. debug_event(record.event);
  1022. #ifndef NO_ACTION_TAPPING
  1023. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  1024. #endif
  1025. }
  1026. /** \brief Debug print (FIXME: Needs better description)
  1027. *
  1028. * FIXME: Needs documentation.
  1029. */
  1030. void debug_action(action_t action) {
  1031. switch (action.kind.id) {
  1032. case ACT_LMODS:
  1033. dprint("ACT_LMODS");
  1034. break;
  1035. case ACT_RMODS:
  1036. dprint("ACT_RMODS");
  1037. break;
  1038. case ACT_LMODS_TAP:
  1039. dprint("ACT_LMODS_TAP");
  1040. break;
  1041. case ACT_RMODS_TAP:
  1042. dprint("ACT_RMODS_TAP");
  1043. break;
  1044. case ACT_USAGE:
  1045. dprint("ACT_USAGE");
  1046. break;
  1047. case ACT_MOUSEKEY:
  1048. dprint("ACT_MOUSEKEY");
  1049. break;
  1050. case ACT_LAYER:
  1051. dprint("ACT_LAYER");
  1052. break;
  1053. case ACT_LAYER_MODS:
  1054. dprint("ACT_LAYER_MODS");
  1055. break;
  1056. case ACT_LAYER_TAP:
  1057. dprint("ACT_LAYER_TAP");
  1058. break;
  1059. case ACT_LAYER_TAP_EXT:
  1060. dprint("ACT_LAYER_TAP_EXT");
  1061. break;
  1062. case ACT_MACRO:
  1063. dprint("ACT_MACRO");
  1064. break;
  1065. case ACT_FUNCTION:
  1066. dprint("ACT_FUNCTION");
  1067. break;
  1068. case ACT_SWAP_HANDS:
  1069. dprint("ACT_SWAP_HANDS");
  1070. break;
  1071. default:
  1072. dprint("UNKNOWN");
  1073. break;
  1074. }
  1075. dprintf("[%X:%02X]", action.kind.param >> 8, action.kind.param & 0xff);
  1076. }