action.c 41 KB

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