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.

156 lines
5.0 KiB

  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. #include "transactions.h"
  18. #include <string.h>
  19. #include "drivers/sensors/pmw3360.h"
  20. #ifndef TRACKBALL_DPI_OPTIONS
  21. # define TRACKBALL_DPI_OPTIONS \
  22. { 1200, 1600, 2400 }
  23. # ifndef TRACKBALL_DPI_DEFAULT
  24. # define TRACKBALL_DPI_DEFAULT 1
  25. # endif
  26. #endif
  27. #ifndef TRACKBALL_DPI_DEFAULT
  28. # define TRACKBALL_DPI_DEFAULT 0
  29. #endif
  30. keyboard_config_t keyboard_config;
  31. uint16_t dpi_array[] = TRACKBALL_DPI_OPTIONS;
  32. #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
  33. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  34. if (!process_record_user(keycode, record)) {
  35. return false;
  36. }
  37. #ifdef POINTING_DEVICE_ENABLE
  38. if (keycode == DPI_CONFIG && record->event.pressed) {
  39. if ((get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT) {
  40. keyboard_config.dpi_config = (keyboard_config.dpi_config - 1) % DPI_OPTION_SIZE;
  41. } else {
  42. keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
  43. }
  44. eeconfig_update_kb(keyboard_config.raw);
  45. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  46. }
  47. #endif
  48. /* If Mousekeys is disabled, then use handle the mouse button
  49. * keycodes. This makes things simpler, and allows usage of
  50. * the keycodes in a consistent manner. But only do this if
  51. * Mousekeys is not enable, so it's not handled twice.
  52. */
  53. #ifndef MOUSEKEY_ENABLE
  54. if (IS_MOUSEKEY_BUTTON(keycode)) {
  55. report_mouse_t currentReport = pointing_device_get_report();
  56. currentReport.buttons = pointing_device_handle_buttons(currentReport.buttons, record->event.pressed, keycode - KC_MS_BTN1);
  57. pointing_device_set_report(currentReport);
  58. pointing_device_send();
  59. }
  60. #endif
  61. return true;
  62. }
  63. __attribute__((weak)) void keyboard_pre_init_sync(void) {}
  64. void keyboard_pre_init_kb(void) {
  65. // debug_enable = true;
  66. // debug_matrix = true;
  67. // debug_mouse = true;
  68. // debug_encoder = true;
  69. // This is the debug LED.
  70. #if defined(DEBUG_LED_PIN)
  71. setPinOutput(DEBUG_LED_PIN);
  72. writePin(DEBUG_LED_PIN, !debug_enable);
  73. #endif
  74. keyboard_pre_init_sync();
  75. keyboard_pre_init_user();
  76. }
  77. __attribute__((weak)) void keyboard_post_init_sync(void) {}
  78. void keyboard_post_init_kb(void) {
  79. keyboard_post_init_sync();
  80. keyboard_post_init_user();
  81. }
  82. #ifdef POINTING_DEVICE_ENABLE
  83. void pointing_device_init_kb(void) {
  84. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  85. pointing_device_init_user();
  86. }
  87. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  88. if (is_keyboard_left()) {
  89. if (is_keyboard_master()) {
  90. transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(sync_mouse_report), &sync_mouse_report);
  91. mouse_report.x = sync_mouse_report.x;
  92. mouse_report.y = sync_mouse_report.y;
  93. pointing_device_task_user(mouse_report);
  94. }
  95. } else {
  96. if (is_keyboard_master()) {
  97. pointing_device_task_user(mouse_report);
  98. } else {
  99. sync_mouse_report.x = mouse_report.x;
  100. sync_mouse_report.y = mouse_report.y;
  101. }
  102. }
  103. return mouse_report;
  104. }
  105. #endif
  106. void eeconfig_init_kb(void) {
  107. keyboard_config.dpi_config = TRACKBALL_DPI_DEFAULT;
  108. #ifdef POINTING_DEVICE_ENABLE
  109. trackball_set_cpi(dpi_array[keyboard_config.dpi_config]);
  110. #endif
  111. eeconfig_update_kb(keyboard_config.raw);
  112. eeconfig_init_user();
  113. }
  114. __attribute__((weak)) void matrix_init_sub_kb(void) {}
  115. void matrix_init_kb(void) {
  116. // is safe to just read DPI setting since matrix init
  117. // comes before pointing device init.
  118. keyboard_config.raw = eeconfig_read_kb();
  119. if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
  120. eeconfig_init_kb();
  121. }
  122. matrix_init_sub_kb();
  123. matrix_init_user();
  124. }
  125. __attribute__((weak)) void matrix_scan_sub_kb(void) {}
  126. void matrix_scan_kb(void) {
  127. matrix_scan_sub_kb();
  128. matrix_scan_user();
  129. }
  130. __attribute__((weak)) void housekeeping_task_sync(void) {}
  131. void housekeeping_task_kb(void) {
  132. housekeeping_task_sync();
  133. // no need for user function, is called already
  134. }
  135. #ifdef POINTING_DEVICE_ENABLE
  136. void matrix_power_up(void) { pointing_device_task(); }
  137. #endif