action_util.c 14 KB

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