action.c 41 KB

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