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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = false;
  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. if (mouse.scroll_lock) {
  63. mouse.scroll_x = mouse.delta_x;
  64. mouse.scroll_y = mouse.delta_y;
  65. mouse.delta_x = 0;
  66. mouse.delta_y = 0;
  67. scroll_sum += abs(mouse.scroll_x);
  68. scroll_sum += abs(mouse.scroll_y);
  69. } else {
  70. mouse.scroll_x = 0;
  71. mouse.scroll_y = 0;
  72. }
  73. if (mouse.fake_middle) {
  74. mouse.button[MOUSE_MIDDLE] = false;
  75. mouse.fake_middle = false;
  76. }
  77. if (!mouse.scroll_lock && last_mouse.scroll_lock) {
  78. // middle mouse button was held and has now been released
  79. if (scroll_sum < MIN_SCROLL_SUPPRESS_CLICK) {
  80. // fake middle mouse click, user was not scrolling
  81. mouse.button[MOUSE_MIDDLE] = true;
  82. mouse.fake_middle = true;
  83. }
  84. }
  85. if (mouse_state_changed(mouse, last_mouse)
  86. || (mouse.delta_x != 0)
  87. || (mouse.delta_y != 0)
  88. || (mouse.scroll_x != 0)
  89. || (mouse.scroll_y != 0)) {
  90. mouse.changed = true;
  91. }
  92. last_mouse = mouse;
  93. return mouse;
  94. }