action.c 38 KB

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