You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
12 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  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. extern keymap_config_t keymap_config;
  22. static uint8_t real_mods = 0;
  23. static uint8_t weak_mods = 0;
  24. static uint8_t macro_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) { return oneshot_locked_mods; }
  39. void set_oneshot_locked_mods(uint8_t mods) {
  40. if (mods != oneshot_locked_mods) {
  41. oneshot_locked_mods = mods;
  42. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  43. }
  44. }
  45. void clear_oneshot_locked_mods(void) {
  46. if (oneshot_locked_mods) {
  47. oneshot_locked_mods = 0;
  48. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  49. }
  50. }
  51. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  52. static uint16_t oneshot_time = 0;
  53. bool has_oneshot_mods_timed_out(void) { return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT; }
  54. # else
  55. bool has_oneshot_mods_timed_out(void) { return false; }
  56. # endif
  57. #endif
  58. /* oneshot layer */
  59. #ifndef NO_ACTION_ONESHOT
  60. /** \brief oneshot_layer_data bits
  61. * LLLL LSSS
  62. * where:
  63. * L => are layer bits
  64. * S => oneshot state bits
  65. */
  66. static int8_t oneshot_layer_data = 0;
  67. inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
  68. inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
  69. # ifdef SWAP_HANDS_ENABLE
  70. enum {
  71. SHO_OFF,
  72. SHO_ACTIVE, // Swap hands button was pressed, and we didn't send any swapped keys yet
  73. SHO_PRESSED, // Swap hands button is currently pressed
  74. SHO_USED, // Swap hands button is still pressed, and we already sent swapped keys
  75. } swap_hands_oneshot = SHO_OFF;
  76. # endif
  77. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  78. static uint16_t oneshot_layer_time = 0;
  79. inline bool has_oneshot_layer_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED); }
  80. # ifdef SWAP_HANDS_ENABLE
  81. static uint16_t oneshot_swaphands_time = 0;
  82. inline bool has_oneshot_swaphands_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_swaphands_time) >= ONESHOT_TIMEOUT && (swap_hands_oneshot == SHO_ACTIVE); }
  83. # endif
  84. # endif
  85. # ifdef SWAP_HANDS_ENABLE
  86. void set_oneshot_swaphands(void) {
  87. swap_hands_oneshot = SHO_PRESSED;
  88. swap_hands = true;
  89. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  90. oneshot_swaphands_time = timer_read();
  91. if (oneshot_layer_time != 0) {
  92. oneshot_layer_time = oneshot_swaphands_time;
  93. }
  94. # endif
  95. }
  96. void release_oneshot_swaphands(void) {
  97. if (swap_hands_oneshot == SHO_PRESSED) {
  98. swap_hands_oneshot = SHO_ACTIVE;
  99. }
  100. if (swap_hands_oneshot == SHO_USED) {
  101. clear_oneshot_swaphands();
  102. }
  103. }
  104. void use_oneshot_swaphands(void) {
  105. if (swap_hands_oneshot == SHO_PRESSED) {
  106. swap_hands_oneshot = SHO_USED;
  107. }
  108. if (swap_hands_oneshot == SHO_ACTIVE) {
  109. clear_oneshot_swaphands();
  110. }
  111. }
  112. void clear_oneshot_swaphands(void) {
  113. swap_hands_oneshot = SHO_OFF;
  114. swap_hands = false;
  115. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  116. oneshot_swaphands_time = 0;
  117. # endif
  118. }
  119. # endif
  120. /** \brief Set oneshot layer
  121. *
  122. * FIXME: needs doc
  123. */
  124. void set_oneshot_layer(uint8_t layer, uint8_t state) {
  125. if (!keymap_config.oneshot_disable) {
  126. oneshot_layer_data = layer << 3 | state;
  127. layer_on(layer);
  128. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  129. oneshot_layer_time = timer_read();
  130. # endif
  131. oneshot_layer_changed_kb(get_oneshot_layer());
  132. } else {
  133. layer_on(layer);
  134. }
  135. }
  136. /** \brief Reset oneshot layer
  137. *
  138. * FIXME: needs doc
  139. */
  140. void reset_oneshot_layer(void) {
  141. oneshot_layer_data = 0;
  142. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  143. oneshot_layer_time = 0;
  144. # endif
  145. oneshot_layer_changed_kb(get_oneshot_layer());
  146. }
  147. /** \brief Clear oneshot layer
  148. *
  149. * FIXME: needs doc
  150. */
  151. void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
  152. uint8_t start_state = oneshot_layer_data;
  153. oneshot_layer_data &= ~state;
  154. if ((!get_oneshot_layer_state() && start_state != oneshot_layer_data) || keymap_config.oneshot_disable) {
  155. layer_off(get_oneshot_layer());
  156. reset_oneshot_layer();
  157. }
  158. }
  159. /** \brief Is oneshot layer active
  160. *
  161. * FIXME: needs doc
  162. */
  163. bool is_oneshot_layer_active(void) { return get_oneshot_layer_state(); }
  164. /** \brief set oneshot
  165. *
  166. * FIXME: needs doc
  167. */
  168. void oneshot_set(bool active) {
  169. if (keymap_config.oneshot_disable != active) {
  170. keymap_config.oneshot_disable = active;
  171. eeconfig_update_keymap(keymap_config.raw);
  172. dprintf("Oneshot: active: %d\n", active);
  173. }
  174. }
  175. /** \brief toggle oneshot
  176. *
  177. * FIXME: needs doc
  178. */
  179. void oneshot_toggle(void) { oneshot_set(!keymap_config.oneshot_disable); }
  180. /** \brief enable oneshot
  181. *
  182. * FIXME: needs doc
  183. */
  184. void oneshot_enable(void) { oneshot_set(true); }
  185. /** \brief disable oneshot
  186. *
  187. * FIXME: needs doc
  188. */
  189. void oneshot_disable(void) { oneshot_set(false); }
  190. bool is_oneshot_enabled(void) { return keymap_config.oneshot_disable; }
  191. #endif
  192. /** \brief Send keyboard report
  193. *
  194. * FIXME: needs doc
  195. */
  196. void send_keyboard_report(void) {
  197. keyboard_report->mods = real_mods;
  198. keyboard_report->mods |= weak_mods;
  199. keyboard_report->mods |= macro_mods;
  200. #ifndef NO_ACTION_ONESHOT
  201. if (oneshot_mods) {
  202. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  203. if (has_oneshot_mods_timed_out()) {
  204. dprintf("Oneshot: timeout\n");
  205. clear_oneshot_mods();
  206. }
  207. # endif
  208. keyboard_report->mods |= oneshot_mods;
  209. if (has_anykey(keyboard_report)) {
  210. clear_oneshot_mods();
  211. }
  212. }
  213. #endif
  214. #ifdef KEY_OVERRIDE_ENABLE
  215. // These need to be last to be able to properly control key overrides
  216. keyboard_report->mods &= ~suppressed_mods;
  217. keyboard_report->mods |= weak_override_mods;
  218. #endif
  219. host_keyboard_send(keyboard_report);
  220. }
  221. /** \brief Get mods
  222. *
  223. * FIXME: needs doc
  224. */
  225. uint8_t get_mods(void) { return real_mods; }
  226. /** \brief add mods
  227. *
  228. * FIXME: needs doc
  229. */
  230. void add_mods(uint8_t mods) { real_mods |= mods; }
  231. /** \brief del mods
  232. *
  233. * FIXME: needs doc
  234. */
  235. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  236. /** \brief set mods
  237. *
  238. * FIXME: needs doc
  239. */
  240. void set_mods(uint8_t mods) { real_mods = mods; }
  241. /** \brief clear mods
  242. *
  243. * FIXME: needs doc
  244. */
  245. void clear_mods(void) { real_mods = 0; }
  246. /** \brief get weak mods
  247. *
  248. * FIXME: needs doc
  249. */
  250. uint8_t get_weak_mods(void) { return weak_mods; }
  251. /** \brief add weak mods
  252. *
  253. * FIXME: needs doc
  254. */
  255. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  256. /** \brief del weak mods
  257. *
  258. * FIXME: needs doc
  259. */
  260. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  261. /** \brief set weak mods
  262. *
  263. * FIXME: needs doc
  264. */
  265. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  266. /** \brief clear weak mods
  267. *
  268. * FIXME: needs doc
  269. */
  270. void clear_weak_mods(void) { weak_mods = 0; }
  271. #ifdef KEY_OVERRIDE_ENABLE
  272. /** \brief set weak mods used by key overrides. DO not call this manually
  273. */
  274. void set_weak_override_mods(uint8_t mods) { weak_override_mods = mods; }
  275. /** \brief clear weak mods used by key overrides. DO not call this manually
  276. */
  277. void clear_weak_override_mods(void) { weak_override_mods = 0; }
  278. /** \brief set suppressed mods used by key overrides. DO not call this manually
  279. */
  280. void set_suppressed_override_mods(uint8_t mods) { suppressed_mods = mods; }
  281. /** \brief clear suppressed mods used by key overrides. DO not call this manually
  282. */
  283. void clear_suppressed_override_mods(void) { suppressed_mods = 0; }
  284. #endif
  285. /* macro modifier */
  286. /** \brief get macro mods
  287. *
  288. * FIXME: needs doc
  289. */
  290. uint8_t get_macro_mods(void) { return macro_mods; }
  291. /** \brief add macro mods
  292. *
  293. * FIXME: needs doc
  294. */
  295. void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
  296. /** \brief del macro mods
  297. *
  298. * FIXME: needs doc
  299. */
  300. void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
  301. /** \brief set macro mods
  302. *
  303. * FIXME: needs doc
  304. */
  305. void set_macro_mods(uint8_t mods) { macro_mods = mods; }
  306. /** \brief clear macro mods
  307. *
  308. * FIXME: needs doc
  309. */
  310. void clear_macro_mods(void) { macro_mods = 0; }
  311. #ifndef NO_ACTION_ONESHOT
  312. /** \brief get oneshot mods
  313. *
  314. * FIXME: needs doc
  315. */
  316. uint8_t get_oneshot_mods(void) { return oneshot_mods; }
  317. void add_oneshot_mods(uint8_t mods) {
  318. if ((oneshot_mods & mods) != mods) {
  319. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  320. oneshot_time = timer_read();
  321. # endif
  322. oneshot_mods |= mods;
  323. oneshot_mods_changed_kb(mods);
  324. }
  325. }
  326. void del_oneshot_mods(uint8_t mods) {
  327. if (oneshot_mods & mods) {
  328. oneshot_mods &= ~mods;
  329. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  330. oneshot_time = oneshot_mods ? timer_read() : 0;
  331. # endif
  332. oneshot_mods_changed_kb(oneshot_mods);
  333. }
  334. }
  335. /** \brief set oneshot mods
  336. *
  337. * FIXME: needs doc
  338. */
  339. void set_oneshot_mods(uint8_t mods) {
  340. if (!keymap_config.oneshot_disable) {
  341. if (oneshot_mods != mods) {
  342. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  343. oneshot_time = timer_read();
  344. # endif
  345. oneshot_mods = mods;
  346. oneshot_mods_changed_kb(mods);
  347. }
  348. }
  349. }
  350. /** \brief clear oneshot mods
  351. *
  352. * FIXME: needs doc
  353. */
  354. void clear_oneshot_mods(void) {
  355. if (oneshot_mods) {
  356. oneshot_mods = 0;
  357. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  358. oneshot_time = 0;
  359. # endif
  360. oneshot_mods_changed_kb(oneshot_mods);
  361. }
  362. }
  363. #endif
  364. /** \brief Called when the one shot modifiers have been changed.
  365. *
  366. * \param mods Contains the active modifiers active after the change.
  367. */
  368. __attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {}
  369. /** \brief Called when the locked one shot modifiers have been changed.
  370. *
  371. * \param mods Contains the active modifiers active after the change.
  372. */
  373. __attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) { oneshot_locked_mods_changed_user(mods); }
  374. /** \brief Called when the one shot modifiers have been changed.
  375. *
  376. * \param mods Contains the active modifiers active after the change.
  377. */
  378. __attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {}
  379. /** \brief Called when the one shot modifiers have been changed.
  380. *
  381. * \param mods Contains the active modifiers active after the change.
  382. */
  383. __attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) { oneshot_mods_changed_user(mods); }
  384. /** \brief Called when the one shot layers have been changed.
  385. *
  386. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  387. */
  388. __attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {}
  389. /** \brief Called when the one shot layers have been changed.
  390. *
  391. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  392. */
  393. __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) { oneshot_layer_changed_user(layer); }
  394. /** \brief inspect keyboard state
  395. *
  396. * FIXME: needs doc
  397. */
  398. uint8_t has_anymod(void) { return bitpop(real_mods); }