action.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 "keymap.h"
  19. #include "mousekey.h"
  20. #include "programmable_button.h"
  21. #include "command.h"
  22. #include "led.h"
  23. #include "action_layer.h"
  24. #include "action_tapping.h"
  25. #include "action_util.h"
  26. #include "action.h"
  27. #include "wait.h"
  28. #include "keycode_config.h"
  29. #ifdef BACKLIGHT_ENABLE
  30. # include "backlight.h"
  31. #endif
  32. #ifdef POINTING_DEVICE_ENABLE
  33. # include "pointing_device.h"
  34. #endif
  35. int tp_buttons;
  36. #if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) || (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT))
  37. int retro_tapping_counter = 0;
  38. #endif
  39. #if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT) && !defined(NO_ACTION_TAPPING)
  40. # include "process_auto_shift.h"
  41. #endif
  42. #ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  43. __attribute__((weak)) bool get_hold_on_other_key_press(uint16_t keycode, keyrecord_t *record) {
  44. return false;
  45. }
  46. #endif
  47. #ifdef RETRO_TAPPING_PER_KEY
  48. __attribute__((weak)) bool get_retro_tapping(uint16_t keycode, keyrecord_t *record) {
  49. return false;
  50. }
  51. #endif
  52. __attribute__((weak)) bool pre_process_record_quantum(keyrecord_t *record) {
  53. return true;
  54. }
  55. /** \brief Called to execute an action.
  56. *
  57. * FIXME: Needs documentation.
  58. */
  59. void action_exec(keyevent_t event) {
  60. if (IS_EVENT(event)) {
  61. ac_dprintf("\n---- action_exec: start -----\n");
  62. ac_dprintf("EVENT: ");
  63. debug_event(event);
  64. ac_dprintf("\n");
  65. #if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) || (defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT))
  66. retro_tapping_counter++;
  67. #endif
  68. }
  69. if (event.pressed) {
  70. // clear the potential weak mods left by previously pressed keys
  71. clear_weak_mods();
  72. }
  73. #ifdef SWAP_HANDS_ENABLE
  74. // Swap hands handles both keys and encoders, if ENCODER_MAP_ENABLE is defined.
  75. if (IS_EVENT(event)) {
  76. process_hand_swap(&event);
  77. }
  78. #endif
  79. keyrecord_t record = {.event = event};
  80. #ifndef NO_ACTION_ONESHOT
  81. if (keymap_config.oneshot_enable) {
  82. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  83. if (has_oneshot_layer_timed_out()) {
  84. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  85. }
  86. if (has_oneshot_mods_timed_out()) {
  87. clear_oneshot_mods();
  88. }
  89. # ifdef SWAP_HANDS_ENABLE
  90. if (has_oneshot_swaphands_timed_out()) {
  91. clear_oneshot_swaphands();
  92. }
  93. # endif
  94. # endif
  95. }
  96. #endif
  97. #ifndef NO_ACTION_TAPPING
  98. # if defined(AUTO_SHIFT_ENABLE) && defined(RETRO_SHIFT)
  99. if (event.pressed) {
  100. retroshift_poll_time(&event);
  101. }
  102. # endif
  103. if (IS_NOEVENT(record.event) || pre_process_record_quantum(&record)) {
  104. action_tapping_process(record);
  105. }
  106. #else
  107. if (IS_NOEVENT(record.event) || pre_process_record_quantum(&record)) {
  108. process_record(&record);
  109. }
  110. if (IS_EVENT(record.event)) {
  111. ac_dprintf("processed: ");
  112. debug_record(record);
  113. dprintln();
  114. }
  115. #endif
  116. }
  117. #ifdef SWAP_HANDS_ENABLE
  118. extern const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS];
  119. # ifdef ENCODER_MAP_ENABLE
  120. extern const uint8_t PROGMEM encoder_hand_swap_config[NUM_ENCODERS];
  121. # endif // ENCODER_MAP_ENABLE
  122. bool swap_hands = false;
  123. bool swap_held = false;
  124. bool should_swap_hands(size_t index, uint8_t *swap_state, bool pressed) {
  125. size_t array_index = index / (CHAR_BIT);
  126. size_t bit_index = index % (CHAR_BIT);
  127. uint8_t bit_val = 1 << bit_index;
  128. bool do_swap = pressed ? swap_hands : swap_state[array_index] & bit_val;
  129. return do_swap;
  130. }
  131. void set_swap_hands_state(size_t index, uint8_t *swap_state, bool on) {
  132. size_t array_index = index / (CHAR_BIT);
  133. size_t bit_index = index % (CHAR_BIT);
  134. uint8_t bit_val = 1 << bit_index;
  135. if (on) {
  136. swap_state[array_index] |= bit_val;
  137. } else {
  138. swap_state[array_index] &= ~bit_val;
  139. }
  140. }
  141. /** \brief Process Hand Swap
  142. *
  143. * FIXME: Needs documentation.
  144. */
  145. void process_hand_swap(keyevent_t *event) {
  146. keypos_t pos = event->key;
  147. if (pos.row < MATRIX_ROWS && pos.col < MATRIX_COLS) {
  148. static uint8_t matrix_swap_state[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)];
  149. size_t index = (size_t)(pos.row * MATRIX_COLS) + pos.col;
  150. bool do_swap = should_swap_hands(index, matrix_swap_state, event->pressed);
  151. if (do_swap) {
  152. event->key.row = pgm_read_byte(&hand_swap_config[pos.row][pos.col].row);
  153. event->key.col = pgm_read_byte(&hand_swap_config[pos.row][pos.col].col);
  154. set_swap_hands_state(index, matrix_swap_state, true);
  155. } else {
  156. set_swap_hands_state(index, matrix_swap_state, false);
  157. }
  158. }
  159. # ifdef ENCODER_MAP_ENABLE
  160. else if (pos.row == KEYLOC_ENCODER_CW || pos.row == KEYLOC_ENCODER_CCW) {
  161. static uint8_t encoder_swap_state[((NUM_ENCODERS) + (CHAR_BIT)-1) / (CHAR_BIT)];
  162. size_t index = pos.col;
  163. bool do_swap = should_swap_hands(index, encoder_swap_state, event->pressed);
  164. if (do_swap) {
  165. event->key.row = pos.row;
  166. event->key.col = pgm_read_byte(&encoder_hand_swap_config[pos.col]);
  167. set_swap_hands_state(index, encoder_swap_state, true);
  168. } else {
  169. set_swap_hands_state(index, encoder_swap_state, false);
  170. }
  171. }
  172. # endif // ENCODER_MAP_ENABLE
  173. }
  174. #endif
  175. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  176. bool disable_action_cache = false;
  177. void process_record_nocache(keyrecord_t *record) {
  178. disable_action_cache = true;
  179. process_record(record);
  180. disable_action_cache = false;
  181. }
  182. #else
  183. void process_record_nocache(keyrecord_t *record) {
  184. process_record(record);
  185. }
  186. #endif
  187. __attribute__((weak)) bool process_record_quantum(keyrecord_t *record) {
  188. return true;
  189. }
  190. __attribute__((weak)) void post_process_record_quantum(keyrecord_t *record) {}
  191. #ifndef NO_ACTION_TAPPING
  192. /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
  193. *
  194. * FIXME: Needs documentation.
  195. */
  196. void process_record_tap_hint(keyrecord_t *record) {
  197. action_t action = layer_switch_get_action(record->event.key);
  198. switch (action.kind.id) {
  199. # ifdef SWAP_HANDS_ENABLE
  200. case ACT_SWAP_HANDS:
  201. switch (action.swap.code) {
  202. case OP_SH_ONESHOT:
  203. break;
  204. case OP_SH_TAP_TOGGLE:
  205. default:
  206. swap_hands = !swap_hands;
  207. swap_held = true;
  208. }
  209. break;
  210. # endif
  211. }
  212. }
  213. #endif
  214. /** \brief Take a key event (key press or key release) and processes it.
  215. *
  216. * FIXME: Needs documentation.
  217. */
  218. void process_record(keyrecord_t *record) {
  219. if (IS_NOEVENT(record->event)) {
  220. return;
  221. }
  222. if (!process_record_quantum(record)) {
  223. #ifndef NO_ACTION_ONESHOT
  224. if (is_oneshot_layer_active() && record->event.pressed && keymap_config.oneshot_enable) {
  225. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  226. }
  227. #endif
  228. return;
  229. }
  230. process_record_handler(record);
  231. post_process_record_quantum(record);
  232. }
  233. void process_record_handler(keyrecord_t *record) {
  234. #ifdef COMBO_ENABLE
  235. action_t action;
  236. if (record->keycode) {
  237. action = action_for_keycode(record->keycode);
  238. } else {
  239. action = store_or_get_action(record->event.pressed, record->event.key);
  240. }
  241. #else
  242. action_t action = store_or_get_action(record->event.pressed, record->event.key);
  243. #endif
  244. ac_dprintf("ACTION: ");
  245. debug_action(action);
  246. #ifndef NO_ACTION_LAYER
  247. ac_dprintf(" layer_state: ");
  248. layer_debug();
  249. ac_dprintf(" default_layer_state: ");
  250. default_layer_debug();
  251. #endif
  252. ac_dprintf("\n");
  253. process_action(record, action);
  254. }
  255. /**
  256. * @brief handles all the messy mouse stuff
  257. *
  258. * Handles all the edgecases and special stuff that is needed for coexistense
  259. * of the multiple mouse subsystems.
  260. *
  261. * @param mouse_keycode[in] uint8_t mouse keycode
  262. * @param pressed[in] bool
  263. */
  264. void register_mouse(uint8_t mouse_keycode, bool pressed) {
  265. #ifdef MOUSEKEY_ENABLE
  266. // if mousekeys is enabled, let it do the brunt of the work
  267. if (pressed) {
  268. mousekey_on(mouse_keycode);
  269. } else {
  270. mousekey_off(mouse_keycode);
  271. }
  272. // should mousekeys send report, or does something else handle this?
  273. switch (mouse_keycode) {
  274. # if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
  275. case KC_MS_BTN1 ... KC_MS_BTN8:
  276. // let pointing device handle the buttons
  277. // expand if/when it handles more of the code
  278. # if defined(POINTING_DEVICE_ENABLE)
  279. pointing_device_keycode_handler(mouse_keycode, pressed);
  280. # endif
  281. break;
  282. # endif
  283. default:
  284. mousekey_send();
  285. break;
  286. }
  287. #elif defined(POINTING_DEVICE_ENABLE)
  288. // if mousekeys isn't enabled, and pointing device is enabled, then
  289. // let pointing device do all the heavy lifting, then
  290. if IS_MOUSEKEY (mouse_keycode) {
  291. pointing_device_keycode_handler(mouse_keycode, pressed);
  292. }
  293. #endif
  294. #ifdef PS2_MOUSE_ENABLE
  295. // make sure that ps2 mouse has button report synced
  296. if (KC_MS_BTN1 <= mouse_keycode && mouse_keycode <= KC_MS_BTN3) {
  297. uint8_t tmp_button_msk = MOUSE_BTN_MASK(mouse_keycode - KC_MS_BTN1);
  298. tp_buttons = pressed ? tp_buttons | tmp_button_msk : tp_buttons & ~tmp_button_msk;
  299. }
  300. #endif
  301. }
  302. /** \brief Take an action and processes it.
  303. *
  304. * FIXME: Needs documentation.
  305. */
  306. void process_action(keyrecord_t *record, action_t action) {
  307. keyevent_t event = record->event;
  308. #ifndef NO_ACTION_TAPPING
  309. uint8_t tap_count = record->tap.count;
  310. #endif
  311. #ifndef NO_ACTION_ONESHOT
  312. bool do_release_oneshot = false;
  313. // notice we only clear the one shot layer if the pressed key is not a modifier.
  314. if (is_oneshot_layer_active() && event.pressed &&
  315. (action.kind.id == ACT_USAGE || !(IS_MODIFIER_KEYCODE(action.key.code)
  316. # ifndef NO_ACTION_TAPPING
  317. || (tap_count == 0 && (action.kind.id == ACT_LMODS_TAP || action.kind.id == ACT_RMODS_TAP))
  318. # endif
  319. ))
  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_MODIFIER_KEYCODE(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_MODIFIER_KEYCODE(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_MODIFIER_KEYCODE(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_MODIFIER_KEYCODE(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. ac_dprintf("MODS_TAP: Oneshot: 0\n");
  395. register_mods(mods | get_oneshot_mods());
  396. } else if (tap_count == 1) {
  397. ac_dprintf("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. ac_dprintf("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(HOLD_ON_OTHER_KEY_PRESS_PER_KEY)
  447. if (
  448. # ifdef HOLD_ON_OTHER_KEY_PRESS_PER_KEY
  449. get_hold_on_other_key_press(get_event_keycode(record->event, false), record) &&
  450. # endif
  451. record->tap.interrupted) {
  452. ac_dprintf("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. ac_dprintf("MODS_TAP: Tap: register_code\n");
  460. register_code(action.key.code);
  461. }
  462. } else {
  463. ac_dprintf("MODS_TAP: No tap: add_mods\n");
  464. register_mods(mods);
  465. }
  466. } else {
  467. if (tap_count > 0) {
  468. ac_dprintf("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. ac_dprintf("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. ac_dprintf("KEYMAP_TAP_KEY: Tap: register_code\n");
  629. register_code(action.layer_tap.code);
  630. } else {
  631. ac_dprintf("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. ac_dprintf("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. ac_dprintf("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_BASIC_KEYCODE (code) {
  829. // TODO: should push command_proc out of this block?
  830. if (command_proc(code)) return;
  831. // Force a new key press if the key is already pressed
  832. // without this, keys with the same keycode, but different
  833. // modifiers will be reported incorrectly, see issue #1708
  834. if (is_key_pressed(keyboard_report, code)) {
  835. del_key(code);
  836. send_keyboard_report();
  837. }
  838. add_key(code);
  839. send_keyboard_report();
  840. } else if IS_MODIFIER_KEYCODE (code) {
  841. add_mods(MOD_BIT(code));
  842. send_keyboard_report();
  843. #ifdef EXTRAKEY_ENABLE
  844. } else if IS_SYSTEM (code) {
  845. host_system_send(KEYCODE2SYSTEM(code));
  846. } else if IS_CONSUMER (code) {
  847. host_consumer_send(KEYCODE2CONSUMER(code));
  848. #endif
  849. } else if IS_MOUSEKEY (code) {
  850. register_mouse(code, true);
  851. }
  852. }
  853. /** \brief Utilities for actions. (FIXME: Needs better description)
  854. *
  855. * FIXME: Needs documentation.
  856. */
  857. __attribute__((weak)) void unregister_code(uint8_t code) {
  858. if (code == KC_NO) {
  859. return;
  860. #ifdef LOCKING_SUPPORT_ENABLE
  861. } else if (KC_LOCKING_CAPS_LOCK == code) {
  862. # ifdef LOCKING_RESYNC_ENABLE
  863. // Resync: ignore if caps lock already is off
  864. if (!(host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK))) return;
  865. # endif
  866. add_key(KC_CAPS_LOCK);
  867. send_keyboard_report();
  868. del_key(KC_CAPS_LOCK);
  869. send_keyboard_report();
  870. } else if (KC_LOCKING_NUM_LOCK == code) {
  871. # ifdef LOCKING_RESYNC_ENABLE
  872. if (!(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK))) return;
  873. # endif
  874. add_key(KC_NUM_LOCK);
  875. send_keyboard_report();
  876. del_key(KC_NUM_LOCK);
  877. send_keyboard_report();
  878. } else if (KC_LOCKING_SCROLL_LOCK == code) {
  879. # ifdef LOCKING_RESYNC_ENABLE
  880. if (!(host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK))) return;
  881. # endif
  882. add_key(KC_SCROLL_LOCK);
  883. send_keyboard_report();
  884. del_key(KC_SCROLL_LOCK);
  885. send_keyboard_report();
  886. #endif
  887. } else if IS_BASIC_KEYCODE (code) {
  888. del_key(code);
  889. send_keyboard_report();
  890. } else if IS_MODIFIER_KEYCODE (code) {
  891. del_mods(MOD_BIT(code));
  892. send_keyboard_report();
  893. #ifdef EXTRAKEY_ENABLE
  894. } else if IS_SYSTEM (code) {
  895. host_system_send(0);
  896. } else if IS_CONSUMER (code) {
  897. host_consumer_send(0);
  898. #endif
  899. } else if IS_MOUSEKEY (code) {
  900. register_mouse(code, false);
  901. }
  902. }
  903. /** \brief Tap a keycode with a delay.
  904. *
  905. * \param code The basic keycode to tap.
  906. * \param delay The amount of time in milliseconds to leave the keycode registered, before unregistering it.
  907. */
  908. __attribute__((weak)) void tap_code_delay(uint8_t code, uint16_t delay) {
  909. register_code(code);
  910. for (uint16_t i = delay; i > 0; i--) {
  911. wait_ms(1);
  912. }
  913. unregister_code(code);
  914. }
  915. /** \brief Tap a keycode with the default delay.
  916. *
  917. * \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.
  918. */
  919. __attribute__((weak)) void tap_code(uint8_t code) {
  920. tap_code_delay(code, code == KC_CAPS_LOCK ? TAP_HOLD_CAPS_DELAY : TAP_CODE_DELAY);
  921. }
  922. /** \brief Adds the given physically pressed modifiers and sends a keyboard report immediately.
  923. *
  924. * \param mods A bitfield of modifiers to register.
  925. */
  926. __attribute__((weak)) void register_mods(uint8_t mods) {
  927. if (mods) {
  928. add_mods(mods);
  929. send_keyboard_report();
  930. }
  931. }
  932. /** \brief Removes the given physically pressed modifiers and sends a keyboard report immediately.
  933. *
  934. * \param mods A bitfield of modifiers to unregister.
  935. */
  936. __attribute__((weak)) void unregister_mods(uint8_t mods) {
  937. if (mods) {
  938. del_mods(mods);
  939. send_keyboard_report();
  940. }
  941. }
  942. /** \brief Adds the given weak modifiers and sends a keyboard report immediately.
  943. *
  944. * \param mods A bitfield of modifiers to register.
  945. */
  946. __attribute__((weak)) void register_weak_mods(uint8_t mods) {
  947. if (mods) {
  948. add_weak_mods(mods);
  949. send_keyboard_report();
  950. }
  951. }
  952. /** \brief Removes the given weak modifiers and sends a keyboard report immediately.
  953. *
  954. * \param mods A bitfield of modifiers to unregister.
  955. */
  956. __attribute__((weak)) void unregister_weak_mods(uint8_t mods) {
  957. if (mods) {
  958. del_weak_mods(mods);
  959. send_keyboard_report();
  960. }
  961. }
  962. /** \brief Utilities for actions. (FIXME: Needs better description)
  963. *
  964. * FIXME: Needs documentation.
  965. */
  966. void clear_keyboard(void) {
  967. clear_mods();
  968. clear_keyboard_but_mods();
  969. }
  970. /** \brief Utilities for actions. (FIXME: Needs better description)
  971. *
  972. * FIXME: Needs documentation.
  973. */
  974. void clear_keyboard_but_mods(void) {
  975. clear_keys();
  976. clear_keyboard_but_mods_and_keys();
  977. }
  978. /** \brief Utilities for actions. (FIXME: Needs better description)
  979. *
  980. * FIXME: Needs documentation.
  981. */
  982. void clear_keyboard_but_mods_and_keys(void) {
  983. #ifdef EXTRAKEY_ENABLE
  984. host_system_send(0);
  985. host_consumer_send(0);
  986. #endif
  987. clear_weak_mods();
  988. send_keyboard_report();
  989. #ifdef MOUSEKEY_ENABLE
  990. mousekey_clear();
  991. mousekey_send();
  992. #endif
  993. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  994. programmable_button_clear();
  995. #endif
  996. }
  997. /** \brief Utilities for actions. (FIXME: Needs better description)
  998. *
  999. * FIXME: Needs documentation.
  1000. */
  1001. bool is_tap_record(keyrecord_t *record) {
  1002. if (IS_NOEVENT(record->event)) {
  1003. return false;
  1004. }
  1005. #ifdef COMBO_ENABLE
  1006. action_t action;
  1007. if (record->keycode) {
  1008. action = action_for_keycode(record->keycode);
  1009. } else {
  1010. action = layer_switch_get_action(record->event.key);
  1011. }
  1012. #else
  1013. action_t action = layer_switch_get_action(record->event.key);
  1014. #endif
  1015. return is_tap_action(action);
  1016. }
  1017. /** \brief Utilities for actions. (FIXME: Needs better description)
  1018. *
  1019. * FIXME: Needs documentation.
  1020. */
  1021. bool is_tap_action(action_t action) {
  1022. switch (action.kind.id) {
  1023. case ACT_LMODS_TAP:
  1024. case ACT_RMODS_TAP:
  1025. case ACT_LAYER_TAP:
  1026. case ACT_LAYER_TAP_EXT:
  1027. switch (action.layer_tap.code) {
  1028. case KC_NO ... KC_RIGHT_GUI:
  1029. case OP_TAP_TOGGLE:
  1030. case OP_ONESHOT:
  1031. return true;
  1032. }
  1033. return false;
  1034. case ACT_SWAP_HANDS:
  1035. switch (action.swap.code) {
  1036. case KC_NO ... KC_RIGHT_GUI:
  1037. case OP_SH_TAP_TOGGLE:
  1038. return true;
  1039. }
  1040. return false;
  1041. }
  1042. return false;
  1043. }
  1044. /** \brief Debug print (FIXME: Needs better description)
  1045. *
  1046. * FIXME: Needs documentation.
  1047. */
  1048. void debug_event(keyevent_t event) {
  1049. ac_dprintf("%04X%c(%u)", (event.key.row << 8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  1050. }
  1051. /** \brief Debug print (FIXME: Needs better description)
  1052. *
  1053. * FIXME: Needs documentation.
  1054. */
  1055. void debug_record(keyrecord_t record) {
  1056. debug_event(record.event);
  1057. #ifndef NO_ACTION_TAPPING
  1058. ac_dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  1059. #endif
  1060. }
  1061. /** \brief Debug print (FIXME: Needs better description)
  1062. *
  1063. * FIXME: Needs documentation.
  1064. */
  1065. void debug_action(action_t action) {
  1066. switch (action.kind.id) {
  1067. case ACT_LMODS:
  1068. ac_dprintf("ACT_LMODS");
  1069. break;
  1070. case ACT_RMODS:
  1071. ac_dprintf("ACT_RMODS");
  1072. break;
  1073. case ACT_LMODS_TAP:
  1074. ac_dprintf("ACT_LMODS_TAP");
  1075. break;
  1076. case ACT_RMODS_TAP:
  1077. ac_dprintf("ACT_RMODS_TAP");
  1078. break;
  1079. case ACT_USAGE:
  1080. ac_dprintf("ACT_USAGE");
  1081. break;
  1082. case ACT_MOUSEKEY:
  1083. ac_dprintf("ACT_MOUSEKEY");
  1084. break;
  1085. case ACT_LAYER:
  1086. ac_dprintf("ACT_LAYER");
  1087. break;
  1088. case ACT_LAYER_MODS:
  1089. ac_dprintf("ACT_LAYER_MODS");
  1090. break;
  1091. case ACT_LAYER_TAP:
  1092. ac_dprintf("ACT_LAYER_TAP");
  1093. break;
  1094. case ACT_LAYER_TAP_EXT:
  1095. ac_dprintf("ACT_LAYER_TAP_EXT");
  1096. break;
  1097. case ACT_SWAP_HANDS:
  1098. ac_dprintf("ACT_SWAP_HANDS");
  1099. break;
  1100. default:
  1101. ac_dprintf("UNKNOWN");
  1102. break;
  1103. }
  1104. ac_dprintf("[%X:%02X]", action.kind.param >> 8, action.kind.param & 0xff);
  1105. }