logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

action_util.c (14553B)


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