action.c 40 KB

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