No Description
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.

controls.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * controls.c
  3. */
  4. #include <stdlib.h>
  5. #include "pico/stdlib.h"
  6. #include "config.h"
  7. #include "log.h"
  8. #include "pmw3360.h"
  9. #include "controls.h"
  10. static struct mouse_state mouse, last_mouse;
  11. static uint64_t scroll_sum = 0;
  12. void controls_init(void) {
  13. for (int i = 0; i < MOUSE_BUTTONS_COUNT; i++) {
  14. mouse.button[i] = false;
  15. }
  16. mouse.changed = false;
  17. mouse.delta_x = 0;
  18. mouse.delta_y = 0;
  19. mouse.scroll_x = 0;
  20. mouse.scroll_y = 0;
  21. mouse.scroll_lock = false;
  22. mouse.fake_middle = 0;
  23. last_mouse = mouse;
  24. }
  25. void controls_mouse_new(int id, bool state) {
  26. //debug("button %d %s", id, state ? "pressed" : "released");
  27. switch (id) {
  28. case 0:
  29. mouse.button[MOUSE_BACK] = state;
  30. break;
  31. case 1:
  32. mouse.scroll_lock = state;
  33. break;
  34. case 2:
  35. mouse.button[MOUSE_LEFT] = state;
  36. break;
  37. case 3:
  38. mouse.button[MOUSE_RIGHT] = state;
  39. break;
  40. }
  41. }
  42. static bool mouse_state_changed(struct mouse_state a, struct mouse_state b) {
  43. for (int i = 0; i < MOUSE_BUTTONS_COUNT; i++) {
  44. if (a.button[i] != b.button[i]) {
  45. return true;
  46. }
  47. }
  48. if ((a.delta_x != b.delta_x)
  49. || (a.delta_y != b.delta_y)
  50. || (a.scroll_x != b.scroll_x)
  51. || (a.scroll_y != b.scroll_y)) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. struct mouse_state controls_mouse_read(void) {
  57. struct pmw_motion motion = pmw_get();
  58. if (motion.motion) {
  59. mouse.delta_x = motion.delta_x;
  60. mouse.delta_y = motion.delta_y;
  61. }
  62. mouse.scroll_x = 0;
  63. mouse.scroll_y = 0;
  64. if (mouse.scroll_lock) {
  65. scroll_sum += abs(mouse.delta_x);
  66. scroll_sum += abs(mouse.delta_y);
  67. mouse.internal_scroll_x += mouse.delta_x;
  68. mouse.internal_scroll_y += mouse.delta_y;
  69. mouse.delta_x = 0;
  70. mouse.delta_y = 0;
  71. while (mouse.internal_scroll_x > SCROLL_REDUCE_SENSITIVITY) {
  72. mouse.scroll_x += 1;
  73. mouse.internal_scroll_x -= SCROLL_REDUCE_SENSITIVITY;
  74. }
  75. while (mouse.internal_scroll_x < -SCROLL_REDUCE_SENSITIVITY) {
  76. mouse.scroll_x -= 1;
  77. mouse.internal_scroll_x += SCROLL_REDUCE_SENSITIVITY;
  78. }
  79. while (mouse.internal_scroll_y > SCROLL_REDUCE_SENSITIVITY) {
  80. mouse.scroll_y += 1;
  81. mouse.internal_scroll_y -= SCROLL_REDUCE_SENSITIVITY;
  82. }
  83. while (mouse.internal_scroll_y < -SCROLL_REDUCE_SENSITIVITY) {
  84. mouse.scroll_y -= 1;
  85. mouse.internal_scroll_y += SCROLL_REDUCE_SENSITIVITY;
  86. }
  87. } else {
  88. mouse.internal_scroll_x = 0;
  89. mouse.internal_scroll_y = 0;
  90. }
  91. if (mouse.fake_middle > 0) {
  92. mouse.fake_middle++;
  93. if (mouse.fake_middle > MOUSE_FAKE_MIDDLE_CLICK_TIME) {
  94. mouse.fake_middle = 0;
  95. mouse.button[MOUSE_MIDDLE] = false;
  96. }
  97. }
  98. if (!mouse.scroll_lock && last_mouse.scroll_lock) {
  99. // middle mouse button was held and has now been released
  100. if (scroll_sum < MIN_SCROLL_SUPPRESS_CLICK) {
  101. // fake middle mouse click, user was not scrolling
  102. mouse.button[MOUSE_MIDDLE] = true;
  103. mouse.fake_middle = 1;
  104. }
  105. }
  106. if (mouse_state_changed(mouse, last_mouse)
  107. || (mouse.delta_x != 0)
  108. || (mouse.delta_y != 0)
  109. || (mouse.scroll_x != 0)
  110. || (mouse.scroll_y != 0)) {
  111. mouse.changed = true;
  112. }
  113. last_mouse = mouse;
  114. return mouse;
  115. }