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.

387 lines
11 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 USB_6KRO_ENABLE
  26. # define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
  27. # define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
  28. # define RO_INC(a) RO_ADD(a, 1)
  29. # define RO_DEC(a) RO_SUB(a, 1)
  30. static int8_t cb_head = 0;
  31. static int8_t cb_tail = 0;
  32. static int8_t cb_count = 0;
  33. #endif
  34. // TODO: pointer variable is not needed
  35. // report_keyboard_t keyboard_report = {};
  36. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  37. extern inline void add_key(uint8_t key);
  38. extern inline void del_key(uint8_t key);
  39. extern inline void clear_keys(void);
  40. #ifndef NO_ACTION_ONESHOT
  41. static uint8_t oneshot_mods = 0;
  42. static uint8_t oneshot_locked_mods = 0;
  43. uint8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
  44. void set_oneshot_locked_mods(uint8_t mods) {
  45. if (mods != oneshot_locked_mods) {
  46. oneshot_locked_mods = mods;
  47. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  48. }
  49. }
  50. void clear_oneshot_locked_mods(void) {
  51. if (oneshot_locked_mods) {
  52. oneshot_locked_mods = 0;
  53. oneshot_locked_mods_changed_kb(oneshot_locked_mods);
  54. }
  55. }
  56. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  57. static uint16_t oneshot_time = 0;
  58. bool has_oneshot_mods_timed_out(void) { return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT; }
  59. # else
  60. bool has_oneshot_mods_timed_out(void) { return false; }
  61. # endif
  62. #endif
  63. /* oneshot layer */
  64. #ifndef NO_ACTION_ONESHOT
  65. /** \brief oneshot_layer_data bits
  66. * LLLL LSSS
  67. * where:
  68. * L => are layer bits
  69. * S => oneshot state bits
  70. */
  71. static int8_t oneshot_layer_data = 0;
  72. inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
  73. inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
  74. # ifdef SWAP_HANDS_ENABLE
  75. enum {
  76. SHO_OFF,
  77. SHO_ACTIVE, // Swap hands button was pressed, and we didn't send any swapped keys yet
  78. SHO_PRESSED, // Swap hands button is currently pressed
  79. SHO_USED, // Swap hands button is still pressed, and we already sent swapped keys
  80. } swap_hands_oneshot = SHO_OFF;
  81. # endif
  82. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  83. static uint16_t oneshot_layer_time = 0;
  84. inline bool has_oneshot_layer_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED); }
  85. # ifdef SWAP_HANDS_ENABLE
  86. static uint16_t oneshot_swaphands_time = 0;
  87. inline bool has_oneshot_swaphands_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_swaphands_time) >= ONESHOT_TIMEOUT && (swap_hands_oneshot == SHO_ACTIVE); }
  88. # endif
  89. # endif
  90. # ifdef SWAP_HANDS_ENABLE
  91. void set_oneshot_swaphands(void) {
  92. swap_hands_oneshot = SHO_PRESSED;
  93. swap_hands = true;
  94. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  95. oneshot_swaphands_time = timer_read();
  96. if (oneshot_layer_time != 0) {
  97. oneshot_layer_time = oneshot_swaphands_time;
  98. }
  99. # endif
  100. }
  101. void release_oneshot_swaphands(void) {
  102. if (swap_hands_oneshot == SHO_PRESSED) {
  103. swap_hands_oneshot = SHO_ACTIVE;
  104. }
  105. if (swap_hands_oneshot == SHO_USED) {
  106. clear_oneshot_swaphands();
  107. }
  108. }
  109. void use_oneshot_swaphands(void) {
  110. if (swap_hands_oneshot == SHO_PRESSED) {
  111. swap_hands_oneshot = SHO_USED;
  112. }
  113. if (swap_hands_oneshot == SHO_ACTIVE) {
  114. clear_oneshot_swaphands();
  115. }
  116. }
  117. void clear_oneshot_swaphands(void) {
  118. swap_hands_oneshot = SHO_OFF;
  119. swap_hands = false;
  120. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  121. oneshot_swaphands_time = 0;
  122. # endif
  123. }
  124. # endif
  125. /** \brief Set oneshot layer
  126. *
  127. * FIXME: needs doc
  128. */
  129. void set_oneshot_layer(uint8_t layer, uint8_t state) {
  130. oneshot_layer_data = layer << 3 | state;
  131. layer_on(layer);
  132. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  133. oneshot_layer_time = timer_read();
  134. # endif
  135. oneshot_layer_changed_kb(get_oneshot_layer());
  136. }
  137. /** \brief Reset oneshot layer
  138. *
  139. * FIXME: needs doc
  140. */
  141. void reset_oneshot_layer(void) {
  142. oneshot_layer_data = 0;
  143. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  144. oneshot_layer_time = 0;
  145. # endif
  146. oneshot_layer_changed_kb(get_oneshot_layer());
  147. }
  148. /** \brief Clear oneshot layer
  149. *
  150. * FIXME: needs doc
  151. */
  152. void clear_oneshot_layer_state(oneshot_fullfillment_t state) {
  153. uint8_t start_state = oneshot_layer_data;
  154. oneshot_layer_data &= ~state;
  155. if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
  156. layer_off(get_oneshot_layer());
  157. reset_oneshot_layer();
  158. }
  159. }
  160. /** \brief Is oneshot layer active
  161. *
  162. * FIXME: needs doc
  163. */
  164. bool is_oneshot_layer_active(void) { return get_oneshot_layer_state(); }
  165. #endif
  166. /** \brief Send keyboard report
  167. *
  168. * FIXME: needs doc
  169. */
  170. void send_keyboard_report(void) {
  171. keyboard_report->mods = real_mods;
  172. keyboard_report->mods |= weak_mods;
  173. keyboard_report->mods |= macro_mods;
  174. #ifndef NO_ACTION_ONESHOT
  175. if (oneshot_mods) {
  176. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  177. if (has_oneshot_mods_timed_out()) {
  178. dprintf("Oneshot: timeout\n");
  179. clear_oneshot_mods();
  180. }
  181. # endif
  182. keyboard_report->mods |= oneshot_mods;
  183. if (has_anykey(keyboard_report)) {
  184. clear_oneshot_mods();
  185. }
  186. }
  187. #endif
  188. host_keyboard_send(keyboard_report);
  189. }
  190. /** \brief Get mods
  191. *
  192. * FIXME: needs doc
  193. */
  194. uint8_t get_mods(void) { return real_mods; }
  195. /** \brief add mods
  196. *
  197. * FIXME: needs doc
  198. */
  199. void add_mods(uint8_t mods) { real_mods |= mods; }
  200. /** \brief del mods
  201. *
  202. * FIXME: needs doc
  203. */
  204. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  205. /** \brief set mods
  206. *
  207. * FIXME: needs doc
  208. */
  209. void set_mods(uint8_t mods) { real_mods = mods; }
  210. /** \brief clear mods
  211. *
  212. * FIXME: needs doc
  213. */
  214. void clear_mods(void) { real_mods = 0; }
  215. /** \brief get weak mods
  216. *
  217. * FIXME: needs doc
  218. */
  219. uint8_t get_weak_mods(void) { return weak_mods; }
  220. /** \brief add weak mods
  221. *
  222. * FIXME: needs doc
  223. */
  224. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  225. /** \brief del weak mods
  226. *
  227. * FIXME: needs doc
  228. */
  229. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  230. /** \brief set weak mods
  231. *
  232. * FIXME: needs doc
  233. */
  234. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  235. /** \brief clear weak mods
  236. *
  237. * FIXME: needs doc
  238. */
  239. void clear_weak_mods(void) { weak_mods = 0; }
  240. /* macro modifier */
  241. /** \brief get macro mods
  242. *
  243. * FIXME: needs doc
  244. */
  245. uint8_t get_macro_mods(void) { return macro_mods; }
  246. /** \brief add macro mods
  247. *
  248. * FIXME: needs doc
  249. */
  250. void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
  251. /** \brief del macro mods
  252. *
  253. * FIXME: needs doc
  254. */
  255. void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
  256. /** \brief set macro mods
  257. *
  258. * FIXME: needs doc
  259. */
  260. void set_macro_mods(uint8_t mods) { macro_mods = mods; }
  261. /** \brief clear macro mods
  262. *
  263. * FIXME: needs doc
  264. */
  265. void clear_macro_mods(void) { macro_mods = 0; }
  266. #ifndef NO_ACTION_ONESHOT
  267. /** \brief get oneshot mods
  268. *
  269. * FIXME: needs doc
  270. */
  271. uint8_t get_oneshot_mods(void) { return oneshot_mods; }
  272. void add_oneshot_mods(uint8_t mods) {
  273. if ((oneshot_mods & mods) != mods) {
  274. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  275. oneshot_time = timer_read();
  276. # endif
  277. oneshot_mods |= mods;
  278. oneshot_mods_changed_kb(mods);
  279. }
  280. }
  281. void del_oneshot_mods(uint8_t mods) {
  282. if (oneshot_mods & mods) {
  283. oneshot_mods &= ~mods;
  284. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  285. oneshot_time = oneshot_mods ? timer_read() : 0;
  286. # endif
  287. oneshot_mods_changed_kb(oneshot_mods);
  288. }
  289. }
  290. /** \brief set oneshot mods
  291. *
  292. * FIXME: needs doc
  293. */
  294. void set_oneshot_mods(uint8_t mods) {
  295. if (oneshot_mods != mods) {
  296. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  297. oneshot_time = timer_read();
  298. # endif
  299. oneshot_mods = mods;
  300. oneshot_mods_changed_kb(mods);
  301. }
  302. }
  303. /** \brief clear oneshot mods
  304. *
  305. * FIXME: needs doc
  306. */
  307. void clear_oneshot_mods(void) {
  308. if (oneshot_mods) {
  309. oneshot_mods = 0;
  310. # if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  311. oneshot_time = 0;
  312. # endif
  313. oneshot_mods_changed_kb(oneshot_mods);
  314. }
  315. }
  316. #endif
  317. /** \brief Called when the one shot modifiers have been changed.
  318. *
  319. * \param mods Contains the active modifiers active after the change.
  320. */
  321. __attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {}
  322. /** \brief Called when the locked one shot modifiers have been changed.
  323. *
  324. * \param mods Contains the active modifiers active after the change.
  325. */
  326. __attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) { oneshot_locked_mods_changed_user(mods); }
  327. /** \brief Called when the one shot modifiers have been changed.
  328. *
  329. * \param mods Contains the active modifiers active after the change.
  330. */
  331. __attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {}
  332. /** \brief Called when the one shot modifiers have been changed.
  333. *
  334. * \param mods Contains the active modifiers active after the change.
  335. */
  336. __attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) { oneshot_mods_changed_user(mods); }
  337. /** \brief Called when the one shot layers have been changed.
  338. *
  339. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  340. */
  341. __attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {}
  342. /** \brief Called when the one shot layers have been changed.
  343. *
  344. * \param layer Contains the layer that is toggled on, or zero when toggled off.
  345. */
  346. __attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) { oneshot_layer_changed_user(layer); }
  347. /** \brief inspect keyboard state
  348. *
  349. * FIXME: needs doc
  350. */
  351. uint8_t has_anymod(void) { return bitpop(real_mods); }