action.c 38 KB

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