Skip to content

Commit

Permalink
Add option for auto mouse movement threshold (qmk#21398)
Browse files Browse the repository at this point in the history
Fixes qmk#21396
  • Loading branch information
MarcelRobitaille authored and nuess0r committed Sep 8, 2024
1 parent cf295de commit d8307f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/feature_pointing_device.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ There are a few ways to control the auto mouse feature with both `config.h` opti
| `AUTO_MOUSE_TIME` | (Optional) Time layer remains active after activation | _ideally_ (250-1000) | _ms_ | `650 ms` |
| `AUTO_MOUSE_DELAY` | (Optional) Lockout time after non-mouse key is pressed | _ideally_ (100-1000) | _ms_ | `TAPPING_TERM` or `200 ms` |
| `AUTO_MOUSE_DEBOUNCE` | (Optional) Time delay from last activation to next update | _ideally_ (10 - 100) | _ms_ | `25 ms` |
| `AUTO_MOUSE_THRESHOLD` | (Optional) Amount of mouse movement required to switch layers | 0 - | _units_ | `10 units` |
### Adding mouse keys
Expand Down
15 changes: 11 additions & 4 deletions quantum/pointing_device/pointing_device_auto_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE

# include <stdlib.h>
# include <string.h>
# include "pointing_device_auto_mouse.h"
# include "debug.h"
Expand Down Expand Up @@ -217,7 +218,11 @@ void auto_mouse_layer_off(void) {
* @return bool of pointing_device activation
*/
__attribute__((weak)) bool auto_mouse_activation(report_mouse_t mouse_report) {
return mouse_report.x != 0 || mouse_report.y != 0 || mouse_report.h != 0 || mouse_report.v != 0 || mouse_report.buttons;
auto_mouse_context.total_mouse_movement.x += mouse_report.x;
auto_mouse_context.total_mouse_movement.y += mouse_report.y;
auto_mouse_context.total_mouse_movement.h += mouse_report.h;
auto_mouse_context.total_mouse_movement.v += mouse_report.v;
return abs(auto_mouse_context.total_mouse_movement.x) > AUTO_MOUSE_THRESHOLD || abs(auto_mouse_context.total_mouse_movement.y) > AUTO_MOUSE_THRESHOLD || abs(auto_mouse_context.total_mouse_movement.h) > AUTO_MOUSE_THRESHOLD || abs(auto_mouse_context.total_mouse_movement.v) > AUTO_MOUSE_THRESHOLD || mouse_report.buttons;
}

/**
Expand All @@ -235,14 +240,16 @@ void pointing_device_task_auto_mouse(report_mouse_t mouse_report) {
// update activation and reset debounce
auto_mouse_context.status.is_activated = auto_mouse_activation(mouse_report);
if (is_auto_mouse_active()) {
auto_mouse_context.timer.active = timer_read();
auto_mouse_context.timer.delay = 0;
auto_mouse_context.total_mouse_movement = (total_mouse_movement_t){.x = 0, .y = 0, .h = 0, .v = 0};
auto_mouse_context.timer.active = timer_read();
auto_mouse_context.timer.delay = 0;
if (!layer_state_is((AUTO_MOUSE_TARGET_LAYER))) {
layer_on((AUTO_MOUSE_TARGET_LAYER));
}
} else if (layer_state_is((AUTO_MOUSE_TARGET_LAYER)) && timer_elapsed(auto_mouse_context.timer.active) > auto_mouse_context.config.timeout) {
layer_off((AUTO_MOUSE_TARGET_LAYER));
auto_mouse_context.timer.active = 0;
auto_mouse_context.timer.active = 0;
auto_mouse_context.total_mouse_movement = (total_mouse_movement_t){.x = 0, .y = 0, .h = 0, .v = 0};
}
}

Expand Down
10 changes: 10 additions & 0 deletions quantum/pointing_device/pointing_device_auto_mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,17 @@
#ifndef AUTO_MOUSE_DEBOUNCE
# define AUTO_MOUSE_DEBOUNCE 25
#endif
#ifndef AUTO_MOUSE_THRESHOLD
# define AUTO_MOUSE_THRESHOLD 10
#endif

/* data structure */
typedef struct {
mouse_xy_report_t x;
mouse_xy_report_t y;
int8_t v;
int8_t h;
} total_mouse_movement_t;
typedef struct {
struct {
bool is_enabled;
Expand All @@ -60,6 +69,7 @@ typedef struct {
bool is_toggled;
int8_t mouse_key_tracker;
} status;
total_mouse_movement_t total_mouse_movement;
} auto_mouse_context_t;

/* ----------Set up and control------------------------------------------------------------------------------ */
Expand Down

0 comments on commit d8307f2

Please sign in to comment.