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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * controls.c
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 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. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdlib.h>
  19. #include "pico/stdlib.h"
  20. #include "config.h"
  21. #include "log.h"
  22. #include "pmw3360.h"
  23. #include "controls.h"
  24. static struct mouse_state mouse, last_mouse;
  25. static uint64_t scroll_sum = 0;
  26. void controls_init(void) {
  27. for (int i = 0; i < MOUSE_BUTTONS_COUNT; i++) {
  28. mouse.button[i] = false;
  29. }
  30. mouse.changed = false;
  31. mouse.delta_x = 0;
  32. mouse.delta_y = 0;
  33. mouse.scroll_x = 0;
  34. mouse.scroll_y = 0;
  35. mouse.scroll_lock = false;
  36. mouse.fake_middle = 0;
  37. last_mouse = mouse;
  38. }
  39. void controls_mouse_new(int id, bool state) {
  40. //debug("button %d %s", id, state ? "pressed" : "released");
  41. switch (id) {
  42. case 0:
  43. mouse.button[MOUSE_BACK] = state;
  44. break;
  45. case 1:
  46. mouse.scroll_lock = state;
  47. break;
  48. case 2:
  49. mouse.button[MOUSE_LEFT] = state;
  50. break;
  51. case 3:
  52. mouse.button[MOUSE_RIGHT] = state;
  53. break;
  54. }
  55. }
  56. static bool mouse_state_changed(struct mouse_state a, struct mouse_state b) {
  57. for (int i = 0; i < MOUSE_BUTTONS_COUNT; i++) {
  58. if (a.button[i] != b.button[i]) {
  59. return true;
  60. }
  61. }
  62. if ((a.delta_x != b.delta_x)
  63. || (a.delta_y != b.delta_y)
  64. || (a.scroll_x != b.scroll_x)
  65. || (a.scroll_y != b.scroll_y)) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. struct mouse_state controls_mouse_read(void) {
  71. struct pmw_motion motion = pmw_get();
  72. if (motion.motion) {
  73. mouse.delta_x = motion.delta_x;
  74. mouse.delta_y = motion.delta_y;
  75. }
  76. mouse.scroll_x = 0;
  77. mouse.scroll_y = 0;
  78. if (mouse.scroll_lock) {
  79. scroll_sum += abs(mouse.delta_x);
  80. scroll_sum += abs(mouse.delta_y);
  81. mouse.internal_scroll_x += mouse.delta_x;
  82. mouse.internal_scroll_y += mouse.delta_y;
  83. mouse.delta_x = 0;
  84. mouse.delta_y = 0;
  85. while (mouse.internal_scroll_x > SCROLL_REDUCE_SENSITIVITY) {
  86. mouse.scroll_x += 1;
  87. mouse.internal_scroll_x -= SCROLL_REDUCE_SENSITIVITY;
  88. }
  89. while (mouse.internal_scroll_x < -SCROLL_REDUCE_SENSITIVITY) {
  90. mouse.scroll_x -= 1;
  91. mouse.internal_scroll_x += SCROLL_REDUCE_SENSITIVITY;
  92. }
  93. while (mouse.internal_scroll_y > SCROLL_REDUCE_SENSITIVITY) {
  94. mouse.scroll_y += 1;
  95. mouse.internal_scroll_y -= SCROLL_REDUCE_SENSITIVITY;
  96. }
  97. while (mouse.internal_scroll_y < -SCROLL_REDUCE_SENSITIVITY) {
  98. mouse.scroll_y -= 1;
  99. mouse.internal_scroll_y += SCROLL_REDUCE_SENSITIVITY;
  100. }
  101. } else {
  102. mouse.internal_scroll_x = 0;
  103. mouse.internal_scroll_y = 0;
  104. }
  105. if (mouse.fake_middle > 0) {
  106. mouse.fake_middle++;
  107. if (mouse.fake_middle > MOUSE_FAKE_MIDDLE_CLICK_TIME) {
  108. mouse.fake_middle = 0;
  109. mouse.button[MOUSE_MIDDLE] = false;
  110. }
  111. }
  112. if (!mouse.scroll_lock && last_mouse.scroll_lock) {
  113. // middle mouse button was held and has now been released
  114. if (scroll_sum < MIN_SCROLL_SUPPRESS_CLICK) {
  115. // fake middle mouse click, user was not scrolling
  116. mouse.button[MOUSE_MIDDLE] = true;
  117. mouse.fake_middle = 1;
  118. }
  119. scroll_sum = 0;
  120. }
  121. if (mouse_state_changed(mouse, last_mouse)
  122. || (mouse.delta_x != 0)
  123. || (mouse.delta_y != 0)
  124. || (mouse.scroll_x != 0)
  125. || (mouse.scroll_y != 0)) {
  126. mouse.changed = true;
  127. }
  128. last_mouse = mouse;
  129. return mouse;
  130. }