From 780ca5565d2bdb8e03aa2669a3203373a0a4d9dd Mon Sep 17 00:00:00 2001 From: Drashna Jaelre Date: Wed, 3 Feb 2021 17:25:05 -0800 Subject: [PATCH] Improve Pointing Device report sending (#11064) * Improve Pointing Device report sending * Hide old report behind preprocessors too * put host_mouse_send() in curly brackets * Remove POINTING_DEVICE_ALWAYS_SEND_REPORT functionality * Fix typo * fix function ref in docs Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> --- docs/feature_pointing_device.md | 4 ++++ quantum/pointing_device.c | 16 +++++++++++++++- quantum/pointing_device.h | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/feature_pointing_device.md b/docs/feature_pointing_device.md index c9309d6975..37edac5e6b 100644 --- a/docs/feature_pointing_device.md +++ b/docs/feature_pointing_device.md @@ -27,6 +27,10 @@ Once you have made the necessary changes to the mouse report, you need to send i When the mouse report is sent, the x, y, v, and h values are set to 0 (this is done in `pointing_device_send()`, which can be overridden to avoid this behavior). This way, button states persist, but movement will only occur once. For further customization, both `pointing_device_init` and `pointing_device_task` can be overridden. +Additionally, by default, `pointing_device_send()` will only send a report when the report has actually changed. This prevents it from continuously sending mouse reports, which will keep the host system awake. This behavior can be changed by creating your own `pointing_device_send()` function. + +Also, you use the `has_mouse_report_changed(new, old)` function to check to see if the report has changed. + In the following example, a custom key is used to click the mouse and scroll 127 units vertically and horizontally, then undo all of that when released - because that's a totally useful function. Listen, this is an example: ```c diff --git a/quantum/pointing_device.c b/quantum/pointing_device.c index 9b7629f307..fbcc08e6dd 100644 --- a/quantum/pointing_device.c +++ b/quantum/pointing_device.c @@ -25,18 +25,32 @@ along with this program. If not, see . static report_mouse_t mouseReport = {}; +__attribute__((weak)) bool has_mouse_report_changed(report_mouse_t new, report_mouse_t old) { + return (new.buttons != old.buttons) || + (new.x && new.x != old.x) || + (new.y && new.y != old.y) || + (new.h && new.h != old.h) || + (new.v && new.v != old.v); +} + + __attribute__((weak)) void pointing_device_init(void) { // initialize device, if that needs to be done. } __attribute__((weak)) void pointing_device_send(void) { + static report_mouse_t old_report = {}; + // If you need to do other things, like debugging, this is the place to do it. - host_mouse_send(&mouseReport); + if (has_mouse_report_changed(mouseReport, old_report)) { + host_mouse_send(&mouseReport); + } // send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device mouseReport.x = 0; mouseReport.y = 0; mouseReport.v = 0; mouseReport.h = 0; + old_report = mouseReport; } __attribute__((weak)) void pointing_device_task(void) { diff --git a/quantum/pointing_device.h b/quantum/pointing_device.h index aa074bb37d..56a542d545 100644 --- a/quantum/pointing_device.h +++ b/quantum/pointing_device.h @@ -26,3 +26,4 @@ void pointing_device_task(void); void pointing_device_send(void); report_mouse_t pointing_device_get_report(void); void pointing_device_set_report(report_mouse_t newMouseReport); +bool has_mouse_report_changed(report_mouse_t new, report_mouse_t old);