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.

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