logo

qmk_firmware

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

tractyl_manuform.c (15727B)


  1. /* Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "tractyl_manuform.h"
  17. #ifdef POINTING_DEVICE_ENABLE
  18. # include "pointing_device.h"
  19. #endif
  20. #include "transactions.h"
  21. #include <string.h>
  22. #ifdef CONSOLE_ENABLE
  23. # include "print.h"
  24. #endif // CONSOLE_ENABLE
  25. #ifdef POINTING_DEVICE_ENABLE
  26. # ifndef CHARYBDIS_MINIMUM_DEFAULT_DPI
  27. # define CHARYBDIS_MINIMUM_DEFAULT_DPI 400
  28. # endif // CHARYBDIS_MINIMUM_DEFAULT_DPI
  29. # ifndef CHARYBDIS_DEFAULT_DPI_CONFIG_STEP
  30. # define CHARYBDIS_DEFAULT_DPI_CONFIG_STEP 200
  31. # endif // CHARYBDIS_DEFAULT_DPI_CONFIG_STEP
  32. # ifndef CHARYBDIS_MINIMUM_SNIPING_DPI
  33. # define CHARYBDIS_MINIMUM_SNIPING_DPI 200
  34. # endif // CHARYBDIS_MINIMUM_SNIPER_MODE_DPI
  35. # ifndef CHARYBDIS_SNIPING_DPI_CONFIG_STEP
  36. # define CHARYBDIS_SNIPING_DPI_CONFIG_STEP 100
  37. # endif // CHARYBDIS_SNIPING_DPI_CONFIG_STEP
  38. // Fixed DPI for drag-scroll.
  39. # ifndef CHARYBDIS_DRAGSCROLL_DPI
  40. # define CHARYBDIS_DRAGSCROLL_DPI 100
  41. # endif // CHARYBDIS_DRAGSCROLL_DPI
  42. # ifndef CHARYBDIS_DRAGSCROLL_BUFFER_SIZE
  43. # define CHARYBDIS_DRAGSCROLL_BUFFER_SIZE 6
  44. # endif // !CHARYBDIS_DRAGSCROLL_BUFFER_SIZE
  45. typedef union {
  46. uint8_t raw;
  47. struct {
  48. uint8_t pointer_default_dpi : 4; // 16 steps available.
  49. uint8_t pointer_sniping_dpi : 2; // 4 steps available.
  50. bool is_dragscroll_enabled : 1;
  51. bool is_sniping_enabled : 1;
  52. } __attribute__((packed));
  53. } charybdis_config_t;
  54. static charybdis_config_t g_charybdis_config = {0};
  55. /**
  56. * \brief Set the value of `config` from EEPROM.
  57. *
  58. * Note that `is_dragscroll_enabled` and `is_sniping_enabled` are purposefully
  59. * ignored since we do not want to persist this state to memory. In practice,
  60. * this state is always written to maximize write-performances. Therefore, we
  61. * explicitly set them to `false` in this function.
  62. */
  63. static void read_charybdis_config_from_eeprom(charybdis_config_t* config) {
  64. config->raw = eeconfig_read_kb() & 0xff;
  65. config->is_dragscroll_enabled = false;
  66. config->is_sniping_enabled = false;
  67. }
  68. /**
  69. * \brief Save the value of `config` to eeprom.
  70. *
  71. * Note that all values are written verbatim, including whether drag-scroll
  72. * and/or sniper mode are enabled. `read_charybdis_config_from_eeprom(…)`
  73. * resets these 2 values to `false` since it does not make sense to persist
  74. * these across reboots of the board.
  75. */
  76. static void write_charybdis_config_to_eeprom(charybdis_config_t* config) {
  77. eeconfig_update_kb(config->raw);
  78. }
  79. /** \brief Return the current value of the pointer's default DPI. */
  80. static uint16_t get_pointer_default_dpi(charybdis_config_t* config) {
  81. return (uint16_t)config->pointer_default_dpi * CHARYBDIS_DEFAULT_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_DEFAULT_DPI;
  82. }
  83. /** \brief Return the current value of the pointer's sniper-mode DPI. */
  84. static uint16_t get_pointer_sniping_dpi(charybdis_config_t* config) {
  85. return (uint16_t)config->pointer_sniping_dpi * CHARYBDIS_SNIPING_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_SNIPING_DPI;
  86. }
  87. /** \brief Set the appropriate DPI for the input config. */
  88. static void maybe_update_pointing_device_cpi(charybdis_config_t* config) {
  89. if (config->is_dragscroll_enabled) {
  90. pointing_device_set_cpi(CHARYBDIS_DRAGSCROLL_DPI);
  91. } else if (config->is_sniping_enabled) {
  92. pointing_device_set_cpi(get_pointer_sniping_dpi(config));
  93. } else {
  94. pointing_device_set_cpi(get_pointer_default_dpi(config));
  95. }
  96. }
  97. /**
  98. * \brief Update the pointer's default DPI to the next or previous step.
  99. *
  100. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  101. * The increment/decrement steps are equal to CHARYBDIS_DEFAULT_DPI_CONFIG_STEP.
  102. */
  103. static void step_pointer_default_dpi(charybdis_config_t* config, bool forward) {
  104. config->pointer_default_dpi += forward ? 1 : -1;
  105. maybe_update_pointing_device_cpi(config);
  106. }
  107. /**
  108. * \brief Update the pointer's sniper-mode DPI to the next or previous step.
  109. *
  110. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  111. * The increment/decrement steps are equal to CHARYBDIS_SNIPING_DPI_CONFIG_STEP.
  112. */
  113. static void step_pointer_sniping_dpi(charybdis_config_t* config, bool forward) {
  114. config->pointer_sniping_dpi += forward ? 1 : -1;
  115. maybe_update_pointing_device_cpi(config);
  116. }
  117. uint16_t charybdis_get_pointer_default_dpi(void) {
  118. return get_pointer_default_dpi(&g_charybdis_config);
  119. }
  120. uint16_t charybdis_get_pointer_sniping_dpi(void) {
  121. return get_pointer_sniping_dpi(&g_charybdis_config);
  122. }
  123. void charybdis_cycle_pointer_default_dpi_noeeprom(bool forward) {
  124. step_pointer_default_dpi(&g_charybdis_config, forward);
  125. }
  126. void charybdis_cycle_pointer_default_dpi(bool forward) {
  127. step_pointer_default_dpi(&g_charybdis_config, forward);
  128. write_charybdis_config_to_eeprom(&g_charybdis_config);
  129. }
  130. void charybdis_cycle_pointer_sniping_dpi_noeeprom(bool forward) {
  131. step_pointer_sniping_dpi(&g_charybdis_config, forward);
  132. }
  133. void charybdis_cycle_pointer_sniping_dpi(bool forward) {
  134. step_pointer_sniping_dpi(&g_charybdis_config, forward);
  135. write_charybdis_config_to_eeprom(&g_charybdis_config);
  136. }
  137. bool charybdis_get_pointer_sniping_enabled(void) {
  138. return g_charybdis_config.is_sniping_enabled;
  139. }
  140. void charybdis_set_pointer_sniping_enabled(bool enable) {
  141. g_charybdis_config.is_sniping_enabled = enable;
  142. maybe_update_pointing_device_cpi(&g_charybdis_config);
  143. }
  144. bool charybdis_get_pointer_dragscroll_enabled(void) {
  145. return g_charybdis_config.is_dragscroll_enabled;
  146. }
  147. void charybdis_set_pointer_dragscroll_enabled(bool enable) {
  148. g_charybdis_config.is_dragscroll_enabled = enable;
  149. maybe_update_pointing_device_cpi(&g_charybdis_config);
  150. }
  151. /**
  152. * \brief Augment the pointing device behavior.
  153. *
  154. * Implement drag-scroll.
  155. */
  156. static void pointing_device_task_charybdis(report_mouse_t* mouse_report) {
  157. static int16_t scroll_buffer_x = 0;
  158. static int16_t scroll_buffer_y = 0;
  159. if (g_charybdis_config.is_dragscroll_enabled) {
  160. # ifdef CHARYBDIS_DRAGSCROLL_REVERSE_X
  161. scroll_buffer_x -= mouse_report->x;
  162. # else
  163. scroll_buffer_x += mouse_report->x;
  164. # endif // CHARYBDIS_DRAGSCROLL_REVERSE_X
  165. # ifdef CHARYBDIS_DRAGSCROLL_REVERSE_Y
  166. scroll_buffer_y -= mouse_report->y;
  167. # else
  168. scroll_buffer_y += mouse_report->y;
  169. # endif // CHARYBDIS_DRAGSCROLL_REVERSE_Y
  170. mouse_report->x = 0;
  171. mouse_report->y = 0;
  172. if (abs(scroll_buffer_x) > CHARYBDIS_DRAGSCROLL_BUFFER_SIZE) {
  173. mouse_report->h = scroll_buffer_x > 0 ? 1 : -1;
  174. scroll_buffer_x = 0;
  175. }
  176. if (abs(scroll_buffer_y) > CHARYBDIS_DRAGSCROLL_BUFFER_SIZE) {
  177. mouse_report->v = scroll_buffer_y > 0 ? 1 : -1;
  178. scroll_buffer_y = 0;
  179. }
  180. }
  181. }
  182. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  183. if (is_keyboard_master()) {
  184. pointing_device_task_charybdis(&mouse_report);
  185. mouse_report = pointing_device_task_user(mouse_report);
  186. }
  187. return mouse_report;
  188. }
  189. # if defined(POINTING_DEVICE_ENABLE) && !defined(NO_CHARYBDIS_KEYCODES)
  190. /** \brief Whether SHIFT mod is enabled. */
  191. static bool has_shift_mod(void) {
  192. # ifdef NO_ACTION_ONESHOT
  193. return mod_config(get_mods()) & MOD_MASK_SHIFT;
  194. # else
  195. return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
  196. # endif // NO_ACTION_ONESHOT
  197. }
  198. # endif // POINTING_DEVICE_ENABLE && !NO_CHARYBDIS_KEYCODES
  199. /**
  200. * \brief Outputs the Charybdis configuration to console.
  201. *
  202. * Prints the in-memory configuration structure to console, for debugging.
  203. * Includes:
  204. * - raw value
  205. * - drag-scroll: on/off
  206. * - sniping: on/off
  207. * - default DPI: internal table index/actual DPI
  208. * - sniping DPI: internal table index/actual DPI
  209. */
  210. __attribute__((unused)) static void debug_charybdis_config_to_console(charybdis_config_t* config) {
  211. # ifdef CONSOLE_ENABLE
  212. dprintf("(charybdis) process_record_kb: config = {\n"
  213. "\traw = 0x%X,\n"
  214. "\t{\n"
  215. "\t\tis_dragscroll_enabled=%u\n"
  216. "\t\tis_sniping_enabled=%u\n"
  217. "\t\tdefault_dpi=0x%X (%u)\n"
  218. "\t\tsniping_dpi=0x%X (%u)\n"
  219. "\t}\n"
  220. "}\n",
  221. 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));
  222. # endif // CONSOLE_ENABLE
  223. }
  224. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  225. if (!process_record_user(keycode, record)) {
  226. return false;
  227. }
  228. # ifndef NO_CHARYBDIS_KEYCODES
  229. switch (keycode) {
  230. case POINTER_DEFAULT_DPI_FORWARD:
  231. if (record->event.pressed) {
  232. // Step backward if shifted, forward otherwise.
  233. charybdis_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
  234. }
  235. break;
  236. case POINTER_DEFAULT_DPI_REVERSE:
  237. if (record->event.pressed) {
  238. // Step forward if shifted, backward otherwise.
  239. charybdis_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
  240. }
  241. break;
  242. case POINTER_SNIPING_DPI_FORWARD:
  243. if (record->event.pressed) {
  244. // Step backward if shifted, forward otherwise.
  245. charybdis_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
  246. }
  247. break;
  248. case POINTER_SNIPING_DPI_REVERSE:
  249. if (record->event.pressed) {
  250. // Step forward if shifted, backward otherwise.
  251. charybdis_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
  252. }
  253. break;
  254. case SNIPING_MODE:
  255. charybdis_set_pointer_sniping_enabled(record->event.pressed);
  256. break;
  257. case SNIPING_MODE_TOGGLE:
  258. if (record->event.pressed) {
  259. charybdis_set_pointer_sniping_enabled(!charybdis_get_pointer_sniping_enabled());
  260. }
  261. break;
  262. case DRAGSCROLL_MODE:
  263. charybdis_set_pointer_dragscroll_enabled(record->event.pressed);
  264. break;
  265. case DRAGSCROLL_MODE_TOGGLE:
  266. if (record->event.pressed) {
  267. charybdis_set_pointer_dragscroll_enabled(!charybdis_get_pointer_dragscroll_enabled());
  268. }
  269. break;
  270. }
  271. # endif // !NO_CHARYBDIS_KEYCODES
  272. return true;
  273. }
  274. void eeconfig_init_kb(void) {
  275. g_charybdis_config.raw = 0;
  276. write_charybdis_config_to_eeprom(&g_charybdis_config);
  277. maybe_update_pointing_device_cpi(&g_charybdis_config);
  278. eeconfig_init_user();
  279. }
  280. void matrix_power_up(void) {
  281. pointing_device_task();
  282. }
  283. void charybdis_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  284. if (initiator2target_buffer_size == sizeof(g_charybdis_config)) {
  285. memcpy(&g_charybdis_config, initiator2target_buffer, sizeof(g_charybdis_config));
  286. }
  287. }
  288. #endif // POINTING_DEVICE_ENABLE
  289. __attribute__((weak)) void user_button_init(void) {
  290. #ifdef USER_BUTTON_PIN
  291. gpio_set_pin_input_high(USER_BUTTON_PIN);
  292. #endif // USER_BUTTON_PIN
  293. }
  294. __attribute__((weak)) bool check_user_button_state(void) {
  295. #ifdef USER_BUTTON_PIN
  296. return !gpio_read_pin(USER_BUTTON_PIN);
  297. #endif // USER_BUTTON_PIN
  298. return false;
  299. }
  300. void keyboard_post_init_kb(void) {
  301. #ifdef DEBUG_LED_PIN
  302. gpio_set_pin_output(DEBUG_LED_PIN);
  303. gpio_write_pin_low(DEBUG_LED_PIN);
  304. #endif // DEBUG_LED_PIN
  305. #ifdef POINTING_DEVICE_ENABLE
  306. maybe_update_pointing_device_cpi(&g_charybdis_config);
  307. transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, charybdis_config_sync_handler);
  308. #endif // POINTING_DEVICE_ENABLE
  309. keyboard_post_init_user();
  310. }
  311. void keyboard_pre_init_kb(void) {
  312. user_button_init();
  313. #ifdef POINTING_DEVICE_ENABLE
  314. read_charybdis_config_from_eeprom(&g_charybdis_config);
  315. #endif // POINTING_DEVICE_ENAcBLE
  316. keyboard_pre_init_user();
  317. }
  318. __attribute__((weak)) void execute_user_button_action(bool state) {
  319. if (state) {
  320. if (is_keyboard_master()) {
  321. reset_keyboard();
  322. } else {
  323. soft_reset_keyboard();
  324. }
  325. }
  326. }
  327. void housekeeping_task_kb(void) {
  328. static bool last_state = false;
  329. bool state = check_user_button_state();
  330. if (state != last_state) {
  331. last_state = state;
  332. execute_user_button_action(state);
  333. }
  334. #ifdef POINTING_DEVICE_ENABLE
  335. if (is_keyboard_master()) {
  336. // Keep track of the last state, so that we can tell if we need to propagate to slave
  337. static charybdis_config_t last_charybdis_config = {0};
  338. static uint32_t last_sync = 0;
  339. bool needs_sync = false;
  340. // Check if the state values are different
  341. if (memcmp(&g_charybdis_config, &last_charybdis_config, sizeof(g_charybdis_config))) {
  342. needs_sync = true;
  343. memcpy(&last_charybdis_config, &g_charybdis_config, sizeof(g_charybdis_config));
  344. }
  345. // Send to slave every 500ms regardless of state change
  346. if (timer_elapsed32(last_sync) > 500) {
  347. needs_sync = true;
  348. }
  349. // Perform the sync if requested
  350. if (needs_sync) {
  351. if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(g_charybdis_config), &g_charybdis_config)) {
  352. last_sync = timer_read32();
  353. }
  354. }
  355. }
  356. #endif // POINTING_DEVICE_ENABLE
  357. // no need for user function, is called already
  358. }
  359. #ifdef USER_BUTTON_PIN
  360. /**
  361. * @brief Replace and add upon the default bootmagic reset function.
  362. * In this case, we also check the user button.
  363. *
  364. * @return true if the user button is pressed, or normal bootmagic key position.
  365. * @return false if the user button is not pressed and normal bootmagic key position is not pressed.
  366. */
  367. __attribute__((weak)) bool bootmagic_should_reset(void) {
  368. uint8_t row = BOOTMAGIC_ROW;
  369. uint8_t col = BOOTMAGIC_COLUMN;
  370. # if defined(SPLIT_KEYBOARD) && defined(BOOTMAGIC_ROW_RIGHT) && defined(BOOTMAGIC_COLUMN_RIGHT)
  371. if (!is_keyboard_left()) {
  372. row = BOOTMAGIC_ROW_RIGHT;
  373. col = BOOTMAGIC_COLUMN_RIGHT;
  374. }
  375. # endif
  376. return matrix_get_row(row) & (1 << col) || check_user_button_state();
  377. }
  378. #endif // USER_BUTTON_PIN
  379. bool shutdown_kb(bool jump_to_bootloader) {
  380. if (!shutdown_user(jump_to_bootloader)) {
  381. return false;
  382. }
  383. #ifdef RGB_MATRIX_ENABLE
  384. void rgb_matrix_update_pwm_buffers(void);
  385. rgb_matrix_set_color_all(RGB_RED);
  386. rgb_matrix_update_pwm_buffers();
  387. #endif // RGB_MATRIX_ENABLE
  388. return true;
  389. }
  390. #ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE
  391. bool is_mouse_record_kb(uint16_t keycode, keyrecord_t* record) {
  392. if (IS_KB_KEYCODE(keycode)) {
  393. return true;
  394. }
  395. return is_mouse_record_user(keycode, record);
  396. }
  397. #endif // POINTING_DEVICE_AUTO_MOUSE_ENABLE