action_util.c 14 KB

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