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.

133 lines
4.5 KiB

  1. /* Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
  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 <string.h>
  17. #include "pointing_device_gestures.h"
  18. #include "timer.h"
  19. #ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
  20. # ifdef POINTING_DEVICE_MOTION_PIN
  21. # error POINTING_DEVICE_MOTION_PIN not supported when using inertial cursor. Need repeated calls to get_report() to generate glide events.
  22. # endif
  23. static void cursor_glide_stop(cursor_glide_context_t* glide) {
  24. memset(&glide->status, 0, sizeof(glide->status));
  25. }
  26. static cursor_glide_t cursor_glide(cursor_glide_context_t* glide) {
  27. cursor_glide_status_t* status = &glide->status;
  28. cursor_glide_t report;
  29. int32_t p;
  30. int32_t x, y;
  31. if (status->v0 == 0) {
  32. report.dx = 0;
  33. report.dy = 0;
  34. report.valid = false;
  35. cursor_glide_stop(glide);
  36. goto exit;
  37. }
  38. status->counter++;
  39. /* Calculate current 1D position */
  40. p = status->v0 * status->counter - (int32_t)glide->config.coef * status->counter * status->counter / 2;
  41. /*
  42. * Translate to x & y axes
  43. * Done this way instead of applying friction to each axis separately, so we don't end up with the shorter axis stuck at 0 towards the end of diagonal movements.
  44. */
  45. x = (int32_t)(p * status->dx0 / status->v0);
  46. y = (int32_t)(p * status->dy0 / status->v0);
  47. report.dx = (mouse_xy_report_t)(x - status->x);
  48. report.dy = (mouse_xy_report_t)(y - status->y);
  49. report.valid = true;
  50. if (report.dx <= 1 && report.dx >= -1 && report.dy <= 1 && report.dy >= -1) {
  51. /* Stop gliding once speed is low enough */
  52. cursor_glide_stop(glide);
  53. goto exit;
  54. }
  55. status->x = x;
  56. status->y = y;
  57. status->timer = timer_read();
  58. exit:
  59. return report;
  60. }
  61. cursor_glide_t cursor_glide_check(cursor_glide_context_t* glide) {
  62. cursor_glide_t invalid_report = {0, 0, false};
  63. cursor_glide_status_t* status = &glide->status;
  64. if (status->z || (status->dx0 == 0 && status->dy0 == 0) || timer_elapsed(status->timer) < glide->config.interval) {
  65. return invalid_report;
  66. } else {
  67. return cursor_glide(glide);
  68. }
  69. }
  70. static inline uint16_t sqrt32(uint32_t x) {
  71. uint32_t l, m, h;
  72. if (x == 0) {
  73. return 0;
  74. } else if (x > (UINT16_MAX >> 2)) {
  75. /* Safe upper bound to avoid integer overflow with m * m */
  76. h = UINT16_MAX;
  77. } else {
  78. /* Upper bound based on closest log2 */
  79. h = (1 << (((__builtin_clzl(1) - __builtin_clzl(x) + 1) + 1) >> 1));
  80. }
  81. /* Lower bound based on closest log2 */
  82. l = (1 << ((__builtin_clzl(1) - __builtin_clzl(x)) >> 1));
  83. /* Binary search to find integer square root */
  84. while (l != h - 1) {
  85. m = (l + h) / 2;
  86. if (m * m <= x) {
  87. l = m;
  88. } else {
  89. h = m;
  90. }
  91. }
  92. return l;
  93. }
  94. cursor_glide_t cursor_glide_start(cursor_glide_context_t* glide) {
  95. cursor_glide_t invalid_report = {0, 0, false};
  96. cursor_glide_status_t* status = &glide->status;
  97. status->timer = timer_read();
  98. status->counter = 0;
  99. status->v0 = (status->dx0 == 0 && status->dy0 == 0) ? 0.0 : sqrt32(((int32_t)status->dx0 * 256 * status->dx0 * 256) + ((int32_t)status->dy0 * 256 * status->dy0 * 256)); // skip trigonometry if not needed, calculate distance in Q8
  100. status->x = 0;
  101. status->y = 0;
  102. status->z = 0;
  103. if (status->v0 < ((uint32_t)glide->config.trigger_px * 256)) { /* Q8 comparison */
  104. /* Not enough velocity to be worth gliding, abort */
  105. cursor_glide_stop(glide);
  106. return invalid_report;
  107. }
  108. return cursor_glide(glide);
  109. }
  110. void cursor_glide_update(cursor_glide_context_t* glide, mouse_xy_report_t dx, mouse_xy_report_t dy, uint16_t z) {
  111. cursor_glide_status_t* status = &glide->status;
  112. status->dx0 = dx;
  113. status->dy0 = dy;
  114. status->z = z;
  115. }
  116. #endif