action_util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "host.h"
  15. #include "report.h"
  16. #include "debug.h"
  17. #include "action_util.h"
  18. #include "action_layer.h"
  19. #include "action_tapping.h"
  20. #include "timer.h"
  21. #include "keycode_config.h"
  22. #include <string.h>
  23. extern keymap_config_t keymap_config;
  24. static uint8_t real_mods = 0;
  25. static uint8_t weak_mods = 0;
  26. #ifdef KEY_OVERRIDE_ENABLE
  27. static uint8_t weak_override_mods = 0;
  28. static uint8_t suppressed_mods = 0;
  29. #endif
  30. // TODO: pointer variable is not needed
  31. // report_keyboard_t keyboard_report = {};
  32. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  33. #ifdef NKRO_ENABLE
  34. report_nkro_t *nkro_report = &(report_nkro_t){};
  35. #endif
  36. extern inline void add_key(uint8_t key);
  37. extern inline void del_key(uint8_t key);
  38. extern inline void clear_keys(void);
  39. #ifndef NO_ACTION_ONESHOT
  40. static uint8_t oneshot_mods = 0;
  41. static uint8_t oneshot_locked_mods = 0;
  42. /**
  43. * @brief Retrieve current state of locked oneshot modifiers.
  44. *
  45. * @return Current state of the locked oneshot modifier keys as a bitmask.
  46. */
  47. uint8_t get_oneshot_locked_mods(void) {
  48. return oneshot_locked_mods;
  49. }
  50. /**
  51. * Same as \ref get_oneshot_locked_mods but returns \ref mod_t for convenience.
  52. */
  53. mod_t get_oneshot_locked_mod_state(void) {
  54. return (mod_t)get_oneshot_locked_mods();
  55. }
  56. void add_oneshot_locked_mods(uint8_t mods) {
  57. if ((oneshot_locked_mods & mods) != mods) {
  58. oneshot_locked_mods |= mods;
  59. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  60. }
  61. }
  62. void set_oneshot_locked_mods(uint8_t mods) {
  63. if (mods != oneshot_locked_mods) {
  64. oneshot_locked_mods = mods;
  65. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  66. }
  67. }
  68. void clear_oneshot_locked_mods(void) {
  69. if (oneshot_locked_mods) {
  70. oneshot_locked_mods = 0;
  71. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  72. }
  73. }
  74. void del_oneshot_locked_mods(uint8_t mods) {
  75. if (oneshot_locked_mods & mods) {
  76. oneshot_locked_mods &= ~mods;
  77. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  78. }
  79. }
  80. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  81. static uint16_t oneshot_time = 0;
  82. bool has_oneshot_mods_timed_out(void) {
  83. return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
  84. }
  85. # else
  86. bool has_oneshot_mods_timed_out(void) {
  87. return false;
  88. }
  89. # endif
  90. #endif
  91. /* oneshot layer */
  92. #ifndef NO_ACTION_ONESHOT
  93. /** \brief oneshot_layer_data bits
  94. * LLLL LSSS
  95. * where:
  96. * L => are layer bits
  97. * S => oneshot state bits
  98. */
  99. static uint8_t oneshot_layer_data = 0;
  100. inline uint8_t get_oneshot_layer(void) {
  101. return oneshot_layer_data >> 3;
  102. }
  103. inline uint8_t get_oneshot_layer_state(void) {
  104. return oneshot_layer_data & 0b111;
  105. }
  106. # ifdef SWAP_HANDS_ENABLE
  107. enum {
  108. SHO_OFF,
  109. SHO_ACTIVE, // Swap hands button was pressed, and we didn't send any swapped keys yet
  110. SHO_PRESSED, // Swap hands button is currently pressed
  111. SHO_USED, // Swap hands button is still pressed, and we already sent swapped keys
  112. } swap_hands_oneshot = SHO_OFF;
  113. # endif
  114. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  115. static uint16_t oneshot_layer_time = 0;
  116. inline bool has_oneshot_layer_timed_out(void) {
  117. return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
  118. }
  119. # ifdef SWAP_HANDS_ENABLE
  120. static uint16_t oneshot_swaphands_time = 0;
  121. inline bool has_oneshot_swaphands_timed_out(void) {
  122. return TIMER_DIFF_16(timer_read(), oneshot_swaphands_time) >= ONESHOT_TIMEOUT && (swap_hands_oneshot == SHO_ACTIVE);
  123. }
  124. # endif
  125. # endif
  126. # ifdef SWAP_HANDS_ENABLE
  127. void set_oneshot_swaphands(void) {
  128. swap_hands_oneshot = SHO_PRESSED;
  129. swap_hands = true;
  130. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  131. oneshot_swaphands_time = timer_read();
  132. if (oneshot_layer_time != 0) {
  133. oneshot_layer_time = oneshot_swaphands_time;
  134. }
  135. # endif
  136. }
  137. void release_oneshot_swaphands(void) {
  138. if (swap_hands_oneshot == SHO_PRESSED) {
  139. swap_hands_oneshot = SHO_ACTIVE;
  140. }
  141. if (swap_hands_oneshot == SHO_USED) {
  142. clear_oneshot_swaphands();
  143. }
  144. }
  145. void use_oneshot_swaphands(void) {
  146. if (swap_hands_oneshot == SHO_PRESSED) {
  147. swap_hands_oneshot = SHO_USED;
  148. }
  149. if (swap_hands_oneshot == SHO_ACTIVE) {
  150. clear_oneshot_swaphands();
  151. }
  152. }
  153. void clear_oneshot_swaphands(void) {
  154. swap_hands_oneshot = SHO_OFF;
  155. swap_hands = false;
  156. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  157. oneshot_swaphands_time = 0;
  158. # endif
  159. }
  160. # endif
  161. /** \brief Set oneshot layer
  162. *
  163. * FIXME: needs doc
  164. */
  165. void set_oneshot_layer(uint8_t layer, uint8_t state) {
  166. if (keymap_config.oneshot_enable) {
  167. oneshot_layer_data = layer << 3 | state;
  168. layer_on(layer);
  169. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  170. oneshot_layer_time = timer_read();
  171. # endif
  172. oneshot_layer_changed_kb(get_oneshot_layer());
  173. } else {
  174. layer_on(layer);
  175. }
  176. }
  177. /** \brief Reset oneshot layer
  178. *
  179. * FIXME: needs doc
  180. */
  181. void reset_oneshot_layer(void) {
  182. oneshot_layer_data = 0;
  183. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  184. oneshot_layer_time = 0;
  185. # endif
  186. oneshot_layer_changed_kb(get_oneshot_layer());
  187. }
  188. /** \brief Clear oneshot layer
  189. *
  190. * FIXME: needs doc
  191. */
  192. void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
  193. uint8_t start_state = oneshot_layer_data;
  194. oneshot_layer_data &= ~state;
  195. if ((!get_oneshot_layer_state() && start_state != oneshot_layer_data) && keymap_config.oneshot_enable) {
  196. layer_off(get_oneshot_layer());
  197. reset_oneshot_layer();
  198. }
  199. }
  200. /** \brief Is oneshot layer active
  201. *
  202. * FIXME: needs doc
  203. */
  204. bool is_oneshot_layer_active(void) {
  205. return get_oneshot_layer_state();
  206. }
  207. /** \brief set oneshot
  208. *
  209. * FIXME: needs doc
  210. */
  211. void oneshot_set(bool active) {
  212. if (keymap_config.oneshot_enable != active) {
  213. keymap_config.oneshot_enable = active;
  214. eeconfig_update_keymap(&keymap_config);
  215. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  216. dprintf("Oneshot: active: %d\n", active);
  217. }
  218. }
  219. /** \brief toggle oneshot
  220. *
  221. * FIXME: needs doc
  222. */
  223. void oneshot_toggle(void) {
  224. oneshot_set(!keymap_config.oneshot_enable);
  225. }
  226. /** \brief enable oneshot
  227. *
  228. * FIXME: needs doc
  229. */
  230. void oneshot_enable(void) {
  231. oneshot_set(true);
  232. }
  233. /** \brief disable oneshot
  234. *
  235. * FIXME: needs doc
  236. */
  237. void oneshot_disable(void) {
  238. oneshot_set(false);
  239. }
  240. bool is_oneshot_enabled(void) {
  241. return keymap_config.oneshot_enable;
  242. }
  243. #endif
  244. static uint8_t get_mods_for_report(void) {
  245. uint8_t mods = real_mods | weak_mods;
  246. #ifndef NO_ACTION_ONESHOT
  247. if (oneshot_mods) {
  248. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  249. if (has_oneshot_mods_timed_out()) {
  250. dprintf("Oneshot: timeout\n");
  251. clear_oneshot_mods();
  252. }
  253. # endif
  254. mods |= oneshot_mods;
  255. if (has_anykey()) {
  256. clear_oneshot_mods();
  257. }
  258. }
  259. #endif
  260. #ifdef SPECULATIVE_HOLD
  261. mods |= get_speculative_mods();
  262. #endif
  263. #ifdef KEY_OVERRIDE_ENABLE
  264. // These need to be last to be able to properly control key overrides
  265. mods &= ~suppressed_mods;
  266. mods |= weak_override_mods;
  267. #endif
  268. return mods;
  269. }
  270. void send_6kro_report(void) {
  271. keyboard_report->mods = get_mods_for_report();
  272. #ifdef PROTOCOL_VUSB
  273. host_keyboard_send(keyboard_report);
  274. #else
  275. static report_keyboard_t last_report;
  276. /* Only send the report if there are changes to propagate to the host. */
  277. if (memcmp(keyboard_report, &last_report, sizeof(report_keyboard_t)) != 0) {
  278. memcpy(&last_report, keyboard_report, sizeof(report_keyboard_t));
  279. host_keyboard_send(keyboard_report);
  280. }
  281. #endif
  282. }
  283. #ifdef NKRO_ENABLE
  284. void send_nkro_report(void) {
  285. nkro_report->mods = get_mods_for_report();
  286. static report_nkro_t last_report;
  287. /* Only send the report if there are changes to propagate to the host. */
  288. if (memcmp(nkro_report, &last_report, sizeof(report_nkro_t)) != 0) {
  289. memcpy(&last_report, nkro_report, sizeof(report_nkro_t));
  290. host_nkro_send(nkro_report);
  291. }
  292. }
  293. #endif
  294. /** \brief Send keyboard report
  295. *
  296. * FIXME: needs doc
  297. */
  298. void send_keyboard_report(void) {
  299. #ifdef NKRO_ENABLE
  300. if (host_can_send_nkro() && keymap_config.nkro) {
  301. send_nkro_report();
  302. return;
  303. }
  304. #endif
  305. send_6kro_report();
  306. }
  307. /**
  308. * @brief Retrieve current state of modifiers.
  309. *
  310. * @return Current state of the modifier keys as a bitmask.
  311. */
  312. uint8_t get_mods(void) {
  313. return real_mods;
  314. }
  315. /**
  316. * Same as \ref get_mods but returns \ref mod_t for convenience.
  317. */
  318. mod_t get_mod_state(void) {
  319. return (mod_t)get_mods();
  320. }
  321. /** \brief add mods
  322. *
  323. * FIXME: needs doc
  324. */
  325. void add_mods(uint8_t mods) {
  326. real_mods |= mods;
  327. }
  328. /** \brief del mods
  329. *
  330. * FIXME: needs doc
  331. */
  332. void del_mods(uint8_t mods) {
  333. real_mods &= ~mods;
  334. }
  335. /** \brief set mods
  336. *
  337. * FIXME: needs doc
  338. */
  339. void set_mods(uint8_t mods) {
  340. real_mods = mods;
  341. }
  342. /** \brief clear mods
  343. *
  344. * FIXME: needs doc
  345. */
  346. void clear_mods(void) {
  347. real_mods = 0;
  348. }
  349. /**
  350. * @brief Retrieve current state of weak modifiers.
  351. *
  352. * @return Current state of the weak modifier keys as a bitmask.
  353. */
  354. uint8_t get_weak_mods(void) {
  355. return weak_mods;
  356. }
  357. /**
  358. * Same as \ref get_weak_mods but returns \ref mod_t for convenience.
  359. */
  360. mod_t get_weak_mod_state(void) {
  361. return (mod_t)get_weak_mods();
  362. }
  363. /** \brief add weak mods
  364. *
  365. * FIXME: needs doc
  366. */
  367. void add_weak_mods(uint8_t mods) {
  368. weak_mods |= mods;
  369. }
  370. /** \brief del weak mods
  371. *
  372. * FIXME: needs doc
  373. */
  374. void del_weak_mods(uint8_t mods) {
  375. weak_mods &= ~mods;
  376. }
  377. /** \brief set weak mods
  378. *
  379. * FIXME: needs doc
  380. */
  381. void set_weak_mods(uint8_t mods) {
  382. weak_mods = mods;
  383. }
  384. /** \brief clear weak mods
  385. *
  386. * FIXME: needs doc
  387. */
  388. void clear_weak_mods(void) {
  389. weak_mods = 0;
  390. }
  391. #ifdef KEY_OVERRIDE_ENABLE
  392. /** \brief set weak mods used by key overrides. DO not call this manually
  393. */
  394. void set_weak_override_mods(uint8_t mods) {
  395. weak_override_mods = mods;
  396. }
  397. /** \brief clear weak mods used by key overrides. DO not call this manually
  398. */
  399. void clear_weak_override_mods(void) {
  400. weak_override_mods = 0;
  401. }
  402. /** \brief set suppressed mods used by key overrides. DO not call this manually
  403. */
  404. void set_suppressed_override_mods(uint8_t mods) {
  405. suppressed_mods = mods;
  406. }
  407. /** \brief clear suppressed mods used by key overrides. DO not call this manually
  408. */
  409. void clear_suppressed_override_mods(void) {
  410. suppressed_mods = 0;
  411. }
  412. #endif
  413. #ifndef NO_ACTION_ONESHOT
  414. /**
  415. * @brief Retrieve current state of oneshot modifiers.
  416. *
  417. * @return Current state of the oneshot modifier keys as a bitmask.
  418. */
  419. uint8_t get_oneshot_mods(void) {
  420. return oneshot_mods;
  421. }
  422. /**
  423. * Same as \ref get_oneshot_mods but returns \ref mod_t for convenience.
  424. */
  425. mod_t get_oneshot_mod_state(void) {
  426. return (mod_t)get_oneshot_mods();
  427. }
  428. void add_oneshot_mods(uint8_t mods) {
  429. if ((oneshot_mods & mods) != mods) {
  430. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  431. oneshot_time = timer_read();
  432. # endif
  433. oneshot_mods |= mods;
  434. oneshot_mods_changed_kb(mods);
  435. }
  436. }
  437. void del_oneshot_mods(uint8_t mods) {
  438. if (oneshot_mods & mods) {
  439. oneshot_mods &= ~mods;
  440. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  441. oneshot_time = oneshot_mods ? timer_read() : 0;
  442. # endif
  443. oneshot_mods_changed_kb(oneshot_mods);
  444. }
  445. }
  446. /** \brief set oneshot mods
  447. *
  448. * FIXME: needs doc
  449. */
  450. void set_oneshot_mods(uint8_t mods) {
  451. if (keymap_config.oneshot_enable) {
  452. if (oneshot_mods != mods) {
  453. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  454. oneshot_time = timer_read();
  455. # endif
  456. oneshot_mods = mods;
  457. oneshot_mods_changed_kb(mods);
  458. }
  459. }
  460. }
  461. /** \brief clear oneshot mods
  462. *
  463. * FIXME: needs doc
  464. */
  465. void clear_oneshot_mods(void) {
  466. if (oneshot_mods) {
  467. oneshot_mods = 0;
  468. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  469. oneshot_time = 0;
  470. # endif
  471. oneshot_mods_changed_kb(oneshot_mods);
  472. }
  473. }
  474. #endif
  475. /** \brief Called when the one shot modifiers have been changed.
  476. *
  477. * \param mods Contains the active modifiers active after the change.
  478. */
  479. __attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {}
  480. /** \brief Called when the locked one shot modifiers have been changed.
  481. *
  482. * \param mods Contains the active modifiers active after the change.
  483. */
  484. __attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) {
  485. oneshot_locked_mods_changed_user(mods);
  486. }
  487. /** \brief Called when the one shot modifiers have been changed.
  488. *
  489. * \param mods Contains the active modifiers active after the change.
  490. */
  491. __attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {}
  492. /** \brief Called when the one shot modifiers have been changed.
  493. *
  494. * \param mods Contains the active modifiers active after the change.
  495. */
  496. __attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) {
  497. oneshot_mods_changed_user(mods);
  498. }
  499. /** \brief Called when the one shot layers have been changed.
  500. *
  501. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  502. */
  503. __attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {}
  504. /** \brief Called when the one shot layers have been changed.
  505. *
  506. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  507. */
  508. __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) {
  509. oneshot_layer_changed_user(layer);
  510. }
  511. /** \brief inspect keyboard state
  512. *
  513. * FIXME: needs doc
  514. */
  515. uint8_t has_anymod(void) {
  516. return bitpop(real_mods);
  517. }
  518. #ifdef DUMMY_MOD_NEUTRALIZER_KEYCODE
  519. /** \brief Send a dummy keycode in between the register and unregister event of a modifier key, to neutralize the "flashing modifiers" phenomenon.
  520. *
  521. * \param active_mods 8-bit packed bit-array describing the currently active modifiers (in the format GASCGASC).
  522. *
  523. * Certain QMK features like key overrides or retro tap must unregister a previously
  524. * registered modifier before sending another keycode but this can trigger undesired
  525. * keyboard shortcuts if the clean tap of a single modifier key is bound to an action
  526. * on the host OS, as is for example the case for the left GUI key on Windows, which
  527. * opens the Start Menu when tapped.
  528. */
  529. void neutralize_flashing_modifiers(uint8_t active_mods) {
  530. // In most scenarios, the flashing modifiers phenomenon is a problem
  531. // only for a subset of modifier masks.
  532. const static uint8_t mods_to_neutralize[] = MODS_TO_NEUTRALIZE;
  533. const static uint8_t n_mods = ARRAY_SIZE(mods_to_neutralize);
  534. for (uint8_t i = 0; i < n_mods; ++i) {
  535. if (active_mods == mods_to_neutralize[i]) {
  536. tap_code(DUMMY_MOD_NEUTRALIZER_KEYCODE);
  537. break;
  538. }
  539. }
  540. }
  541. #endif