logo

qmk_firmware

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

dilemma.c (12779B)


  1. /**
  2. * Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  3. * Copyright 2021 Quentin LEBASTARD <qlebastard@gmail.com>
  4. * Copyright 2022 Charly Delay <charly@codesink.dev> (@0xcharly)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Publicw License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "dilemma.h"
  20. #ifdef CONSOLE_ENABLE
  21. # include "print.h"
  22. #endif // CONSOLE_ENABLE
  23. #ifdef POINTING_DEVICE_ENABLE
  24. # ifndef DILEMMA_MINIMUM_DEFAULT_DPI
  25. # define DILEMMA_MINIMUM_DEFAULT_DPI 400
  26. # endif // DILEMMA_MINIMUM_DEFAULT_DPI
  27. # ifndef DILEMMA_DEFAULT_DPI_CONFIG_STEP
  28. # define DILEMMA_DEFAULT_DPI_CONFIG_STEP 200
  29. # endif // DILEMMA_DEFAULT_DPI_CONFIG_STEP
  30. # ifndef DILEMMA_MINIMUM_SNIPING_DPI
  31. # define DILEMMA_MINIMUM_SNIPING_DPI 200
  32. # endif // DILEMMA_MINIMUM_SNIPING_DPI
  33. # ifndef DILEMMA_SNIPING_DPI_CONFIG_STEP
  34. # define DILEMMA_SNIPING_DPI_CONFIG_STEP 100
  35. # endif // DILEMMA_SNIPING_DPI_CONFIG_STEP
  36. // Fixed DPI for drag-scroll.
  37. # ifndef DILEMMA_DRAGSCROLL_DPI
  38. # define DILEMMA_DRAGSCROLL_DPI 100
  39. # endif // DILEMMA_DRAGSCROLL_DPI
  40. # ifndef DILEMMA_DRAGSCROLL_BUFFER_SIZE
  41. # define DILEMMA_DRAGSCROLL_BUFFER_SIZE 6
  42. # endif // !DILEMMA_DRAGSCROLL_BUFFER_SIZE
  43. typedef union {
  44. uint8_t raw;
  45. struct {
  46. uint8_t pointer_default_dpi : 4; // 16 steps available.
  47. uint8_t pointer_sniping_dpi : 2; // 4 steps available.
  48. bool is_dragscroll_enabled : 1;
  49. bool is_sniping_enabled : 1;
  50. } __attribute__((packed));
  51. } dilemma_config_t;
  52. static dilemma_config_t g_dilemma_config = {0};
  53. /**
  54. * \brief Set the value of `config` from EEPROM.
  55. *
  56. * Note that `is_dragscroll_enabled` and `is_sniping_enabled` are purposefully
  57. * ignored since we do not want to persist this state to memory. In practice,
  58. * this state is always written to maximize write-performances. Therefore, we
  59. * explicitly set them to `false` in this function.
  60. */
  61. static void read_dilemma_config_from_eeprom(dilemma_config_t* config) {
  62. config->raw = eeconfig_read_kb() & 0xff;
  63. config->is_dragscroll_enabled = false;
  64. config->is_sniping_enabled = false;
  65. }
  66. /**
  67. * \brief Save the value of `config` to eeprom.
  68. *
  69. * Note that all values are written verbatim, including whether drag-scroll
  70. * and/or sniper mode are enabled. `read_dilemma_config_from_eeprom(…)`
  71. * resets these 2 values to `false` since it does not make sense to persist
  72. * these across reboots of the board.
  73. */
  74. static void write_dilemma_config_to_eeprom(dilemma_config_t* config) {
  75. eeconfig_update_kb(config->raw);
  76. }
  77. /** \brief Return the current value of the pointer's default DPI. */
  78. static uint16_t get_pointer_default_dpi(dilemma_config_t* config) {
  79. return (uint16_t)config->pointer_default_dpi * DILEMMA_DEFAULT_DPI_CONFIG_STEP + DILEMMA_MINIMUM_DEFAULT_DPI;
  80. }
  81. /** \brief Return the current value of the pointer's sniper-mode DPI. */
  82. static uint16_t get_pointer_sniping_dpi(dilemma_config_t* config) {
  83. return (uint16_t)config->pointer_sniping_dpi * DILEMMA_SNIPING_DPI_CONFIG_STEP + DILEMMA_MINIMUM_SNIPING_DPI;
  84. }
  85. /** \brief Set the appropriate DPI for the input config. */
  86. static void maybe_update_pointing_device_cpi(dilemma_config_t* config) {
  87. if (config->is_dragscroll_enabled) {
  88. pointing_device_set_cpi(DILEMMA_DRAGSCROLL_DPI);
  89. } else if (config->is_sniping_enabled) {
  90. pointing_device_set_cpi(get_pointer_sniping_dpi(config));
  91. } else {
  92. pointing_device_set_cpi(get_pointer_default_dpi(config));
  93. }
  94. }
  95. /**
  96. * \brief Update the pointer's default DPI to the next or previous step.
  97. *
  98. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  99. * The increment/decrement steps are equal to DILEMMA_DEFAULT_DPI_CONFIG_STEP.
  100. */
  101. static void step_pointer_default_dpi(dilemma_config_t* config, bool forward) {
  102. config->pointer_default_dpi += forward ? 1 : -1;
  103. maybe_update_pointing_device_cpi(config);
  104. }
  105. /**
  106. * \brief Update the pointer's sniper-mode DPI to the next or previous step.
  107. *
  108. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  109. * The increment/decrement steps are equal to DILEMMA_SNIPING_DPI_CONFIG_STEP.
  110. */
  111. static void step_pointer_sniping_dpi(dilemma_config_t* config, bool forward) {
  112. config->pointer_sniping_dpi += forward ? 1 : -1;
  113. maybe_update_pointing_device_cpi(config);
  114. }
  115. uint16_t dilemma_get_pointer_default_dpi(void) {
  116. return get_pointer_default_dpi(&g_dilemma_config);
  117. }
  118. uint16_t dilemma_get_pointer_sniping_dpi(void) {
  119. return get_pointer_sniping_dpi(&g_dilemma_config);
  120. }
  121. void dilemma_cycle_pointer_default_dpi_noeeprom(bool forward) {
  122. step_pointer_default_dpi(&g_dilemma_config, forward);
  123. }
  124. void dilemma_cycle_pointer_default_dpi(bool forward) {
  125. step_pointer_default_dpi(&g_dilemma_config, forward);
  126. write_dilemma_config_to_eeprom(&g_dilemma_config);
  127. }
  128. void dilemma_cycle_pointer_sniping_dpi_noeeprom(bool forward) {
  129. step_pointer_sniping_dpi(&g_dilemma_config, forward);
  130. }
  131. void dilemma_cycle_pointer_sniping_dpi(bool forward) {
  132. step_pointer_sniping_dpi(&g_dilemma_config, forward);
  133. write_dilemma_config_to_eeprom(&g_dilemma_config);
  134. }
  135. bool dilemma_get_pointer_sniping_enabled(void) {
  136. return g_dilemma_config.is_sniping_enabled;
  137. }
  138. void dilemma_set_pointer_sniping_enabled(bool enable) {
  139. g_dilemma_config.is_sniping_enabled = enable;
  140. maybe_update_pointing_device_cpi(&g_dilemma_config);
  141. }
  142. bool dilemma_get_pointer_dragscroll_enabled(void) {
  143. return g_dilemma_config.is_dragscroll_enabled;
  144. }
  145. void dilemma_set_pointer_dragscroll_enabled(bool enable) {
  146. g_dilemma_config.is_dragscroll_enabled = enable;
  147. maybe_update_pointing_device_cpi(&g_dilemma_config);
  148. }
  149. void pointing_device_init_kb(void) {
  150. maybe_update_pointing_device_cpi(&g_dilemma_config);
  151. pointing_device_init_user();
  152. }
  153. /**
  154. * \brief Augment the pointing device behavior.
  155. *
  156. * Implement drag-scroll.
  157. */
  158. static void pointing_device_task_dilemma(report_mouse_t* mouse_report) {
  159. static int16_t scroll_buffer_x = 0;
  160. static int16_t scroll_buffer_y = 0;
  161. if (g_dilemma_config.is_dragscroll_enabled) {
  162. # ifdef DILEMMA_DRAGSCROLL_REVERSE_X
  163. scroll_buffer_x -= mouse_report->x;
  164. # else
  165. scroll_buffer_x += mouse_report->x;
  166. # endif // DILEMMA_DRAGSCROLL_REVERSE_X
  167. # ifdef DILEMMA_DRAGSCROLL_REVERSE_Y
  168. scroll_buffer_y -= mouse_report->y;
  169. # else
  170. scroll_buffer_y += mouse_report->y;
  171. # endif // DILEMMA_DRAGSCROLL_REVERSE_Y
  172. mouse_report->x = 0;
  173. mouse_report->y = 0;
  174. if (abs(scroll_buffer_x) > DILEMMA_DRAGSCROLL_BUFFER_SIZE) {
  175. mouse_report->h = scroll_buffer_x > 0 ? 1 : -1;
  176. scroll_buffer_x = 0;
  177. }
  178. if (abs(scroll_buffer_y) > DILEMMA_DRAGSCROLL_BUFFER_SIZE) {
  179. mouse_report->v = scroll_buffer_y > 0 ? 1 : -1;
  180. scroll_buffer_y = 0;
  181. }
  182. }
  183. }
  184. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  185. if (is_keyboard_master()) {
  186. pointing_device_task_dilemma(&mouse_report);
  187. mouse_report = pointing_device_task_user(mouse_report);
  188. }
  189. return mouse_report;
  190. }
  191. # if defined(POINTING_DEVICE_ENABLE) && !defined(NO_DILEMMA_KEYCODES)
  192. /** \brief Whether SHIFT mod is enabled. */
  193. static bool has_shift_mod(void) {
  194. # ifdef NO_ACTION_ONESHOT
  195. return mod_config(get_mods()) & MOD_MASK_SHIFT;
  196. # else
  197. return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
  198. # endif // NO_ACTION_ONESHOT
  199. }
  200. # endif // POINTING_DEVICE_ENABLE && !NO_DILEMMA_KEYCODES
  201. /**
  202. * \brief Outputs the Dilemma configuration to console.
  203. *
  204. * Prints the in-memory configuration structure to console, for debugging.
  205. * Includes:
  206. * - raw value
  207. * - drag-scroll: on/off
  208. * - sniping: on/off
  209. * - default DPI: internal table index/actual DPI
  210. * - sniping DPI: internal table index/actual DPI
  211. */
  212. static void debug_dilemma_config_to_console(dilemma_config_t* config) {
  213. # ifdef CONSOLE_ENABLE
  214. dprintf("(dilemma) process_record_kb: config = {\n"
  215. "\traw = 0x%X,\n"
  216. "\t{\n"
  217. "\t\tis_dragscroll_enabled=%u\n"
  218. "\t\tis_sniping_enabled=%u\n"
  219. "\t\tdefault_dpi=0x%X (%u)\n"
  220. "\t\tsniping_dpi=0x%X (%u)\n"
  221. "\t}\n"
  222. "}\n",
  223. config->raw, config->is_dragscroll_enabled, config->is_sniping_enabled, config->pointer_default_dpi, get_pointer_default_dpi(config), config->pointer_sniping_dpi, get_pointer_sniping_dpi(config));
  224. # endif // CONSOLE_ENABLE
  225. }
  226. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  227. if (!process_record_user(keycode, record)) {
  228. debug_dilemma_config_to_console(&g_dilemma_config);
  229. return false;
  230. }
  231. # ifdef POINTING_DEVICE_ENABLE
  232. # ifndef NO_DILEMMA_KEYCODES
  233. switch (keycode) {
  234. case POINTER_DEFAULT_DPI_FORWARD:
  235. if (record->event.pressed) {
  236. // Step backward if shifted, forward otherwise.
  237. dilemma_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
  238. }
  239. break;
  240. case POINTER_DEFAULT_DPI_REVERSE:
  241. if (record->event.pressed) {
  242. // Step forward if shifted, backward otherwise.
  243. dilemma_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
  244. }
  245. break;
  246. case POINTER_SNIPING_DPI_FORWARD:
  247. if (record->event.pressed) {
  248. // Step backward if shifted, forward otherwise.
  249. dilemma_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
  250. }
  251. break;
  252. case POINTER_SNIPING_DPI_REVERSE:
  253. if (record->event.pressed) {
  254. // Step forward if shifted, backward otherwise.
  255. dilemma_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
  256. }
  257. break;
  258. case SNIPING_MODE:
  259. dilemma_set_pointer_sniping_enabled(record->event.pressed);
  260. break;
  261. case SNIPING_MODE_TOGGLE:
  262. if (record->event.pressed) {
  263. dilemma_set_pointer_sniping_enabled(!dilemma_get_pointer_sniping_enabled());
  264. }
  265. break;
  266. case DRAGSCROLL_MODE:
  267. dilemma_set_pointer_dragscroll_enabled(record->event.pressed);
  268. break;
  269. case DRAGSCROLL_MODE_TOGGLE:
  270. if (record->event.pressed) {
  271. dilemma_set_pointer_dragscroll_enabled(!dilemma_get_pointer_dragscroll_enabled());
  272. }
  273. break;
  274. }
  275. # endif // !NO_DILEMMA_KEYCODES
  276. # endif // POINTING_DEVICE_ENABLE
  277. debug_dilemma_config_to_console(&g_dilemma_config);
  278. if (IS_QK_KB(keycode) || IS_MOUSEKEY(keycode)) {
  279. debug_dilemma_config_to_console(&g_dilemma_config);
  280. }
  281. return true;
  282. }
  283. void eeconfig_init_kb(void) {
  284. g_dilemma_config.raw = 0;
  285. g_dilemma_config.pointer_default_dpi = 3; // DPI=1000
  286. write_dilemma_config_to_eeprom(&g_dilemma_config);
  287. maybe_update_pointing_device_cpi(&g_dilemma_config);
  288. eeconfig_init_user();
  289. }
  290. void matrix_init_kb(void) {
  291. read_dilemma_config_from_eeprom(&g_dilemma_config);
  292. matrix_init_user();
  293. }
  294. #endif // POINTING_DEVICE_ENABLE
  295. // Forward declare RP2040 SDK declaration.
  296. void gpio_init(uint gpio);
  297. void keyboard_pre_init_kb(void) {
  298. // Ensures that GP26 through GP29 are initialized as digital inputs (as
  299. // opposed to analog inputs). These GPIOs are shared with A0 through A3,
  300. // respectively. On RP2040-B2 and later, the digital inputs are disabled by
  301. // default (see RP2040-E6).
  302. gpio_init(GP26);
  303. gpio_init(GP27);
  304. gpio_init(GP28);
  305. gpio_init(GP29);
  306. keyboard_pre_init_user();
  307. }
  308. bool shutdown_kb(bool jump_to_bootloader) {
  309. if (!shutdown_user(jump_to_bootloader)) {
  310. return false;
  311. }
  312. #ifdef RGBLIGHT_ENABLE
  313. rgblight_enable_noeeprom();
  314. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  315. rgblight_setrgb(RGB_RED);
  316. #endif // RGBLIGHT_ENABLE
  317. #ifdef RGB_MATRIX_ENABLE
  318. rgb_matrix_set_color_all(RGB_RED);
  319. rgb_matrix_update_pwm_buffers();
  320. #endif // RGB_MATRIX_ENABLE
  321. return true;
  322. }