action_util.c 15 KB

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