From e768fb83bdf1bc292cefc9f3f70cb1597c3108fc Mon Sep 17 00:00:00 2001
From: Drashna Jaelre <drashna@live.com>
Date: Sun, 14 Feb 2021 20:09:24 -0800
Subject: [Keyboard] PloopyCo VIA updates (#11290)

Co-authored-by: ridingqwerty <george.g.koenig@gmail.com>
Co-authored-by: Glen D'souza <gdsouza@linuxmail.org>
---
 .../ploopyco/mouse/keymaps/drag_scroll/keymap.c    | 43 ++------------
 keyboards/ploopyco/mouse/keymaps/via/keymap.c      |  2 +-
 keyboards/ploopyco/mouse/mouse.c                   | 66 +++++++++++++++++-----
 keyboards/ploopyco/mouse/mouse.h                   | 10 ++++
 keyboards/ploopyco/mouse/rules.mk                  |  6 +-
 .../trackball/keymaps/drag_scroll/keymap.c         | 36 ------------
 keyboards/ploopyco/trackball/rev1/readme.md        |  4 +-
 keyboards/ploopyco/trackball/rules.mk              |  6 +-
 keyboards/ploopyco/trackball/trackball.c           | 65 ++++++++++++++++-----
 keyboards/ploopyco/trackball/trackball.h           | 18 ++++--
 10 files changed, 142 insertions(+), 114 deletions(-)

diff --git a/keyboards/ploopyco/mouse/keymaps/drag_scroll/keymap.c b/keyboards/ploopyco/mouse/keymaps/drag_scroll/keymap.c
index 438e2406fa..da328fff04 100644
--- a/keyboards/ploopyco/mouse/keymaps/drag_scroll/keymap.c
+++ b/keyboards/ploopyco/mouse/keymaps/drag_scroll/keymap.c
@@ -17,46 +17,11 @@
  */
 #include QMK_KEYBOARD_H
 
-// used for tracking the state
-bool is_drag_scroll = false;
-
-enum custom_keycodes {
-    DRAG_SCROLL = PLOOPY_SAFE_RANGE,
-};
-
 
 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     [0] = LAYOUT(/* Base */
-                 C(KC_C), KC_BTN1, KC_BTN3, KC_BTN2, C(KC_V), KC_BTN4, KC_BTN5, DPI_CONFIG)
-};
+                 C(KC_C), KC_BTN1, KC_BTN3, LT(1, KC_BTN2), C(KC_V), KC_BTN4, KC_BTN5, DPI_CONFIG),
+    [1] = LAYOUT(/* Base */
+                 _______, DRAG_SCROLL, _______, _______, _______, _______, _______, RESET),
 
-
-bool process_record_user(uint16_t keycode, keyrecord_t *record) {
-    switch (keycode) {
-        case DRAG_SCROLL:
-            if (record->event.pressed) {
-                // this toggles the state each time you tap it
-                is_drag_scroll ^= 1;
-            }
-            break;
-    }
-    return true;
-}
-
-// The real magic is here.
-// This function is called to translate the processed sensor movement
-// from the mouse sensor and translates it into x and y movement for
-// the mouse report. Normally.  So if "drag scroll" is toggled on,
-// moving the ball scrolls instead.  You could remove the  x or y here
-//  to only scroll in one direction, if you wanted, as well. In fact,
-// there is no reason that you need to send this to the mouse report.
-// You could have it register a key, instead.
-void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
-    if (is_drag_scroll) {
-        mouse_report->h = x;
-        mouse_report->v = y;
-    } else {
-        mouse_report->x = x;
-        mouse_report->y = y;
-    }
-}
+};
diff --git a/keyboards/ploopyco/mouse/keymaps/via/keymap.c b/keyboards/ploopyco/mouse/keymaps/via/keymap.c
index cd8b7f6c6f..dd5e7eb5a7 100644
--- a/keyboards/ploopyco/mouse/keymaps/via/keymap.c
+++ b/keyboards/ploopyco/mouse/keymaps/via/keymap.c
@@ -18,7 +18,7 @@
 #include QMK_KEYBOARD_H
 
 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
-    [0] = LAYOUT(C(KC_C), KC_BTN1, KC_BTN3, KC_BTN2, C(KC_V), KC_BTN4, KC_BTN5, C(KC_Z)),
+    [0] = LAYOUT(C(KC_C), KC_BTN1, KC_BTN3, KC_BTN2, C(KC_V), KC_BTN4, KC_BTN5, DPI_CONFIG),
     [1] = LAYOUT(_______, _______, _______, _______, _______, _______, _______, _______),
     [2] = LAYOUT(_______, _______, _______, _______, _______, _______, _______, _______),
     [3] = LAYOUT(_______, _______, _______, _______, _______, _______, _______, _______),
diff --git a/keyboards/ploopyco/mouse/mouse.c b/keyboards/ploopyco/mouse/mouse.c
index 788a0a1f08..1a1e5648b2 100644
--- a/keyboards/ploopyco/mouse/mouse.c
+++ b/keyboards/ploopyco/mouse/mouse.c
@@ -39,9 +39,15 @@
 #ifndef PLOOPY_DPI_DEFAULT
 #    define PLOOPY_DPI_DEFAULT 0
 #endif
+#ifndef PLOOPY_DRAGSCROLL_DPI
+#    define PLOOPY_DRAGSCROLL_DPI 100 // Fixed-DPI Drag Scroll
+#endif
+#ifndef PLOOPY_DRAGSCROLL_MULTIPLIER
+#    define PLOOPY_DRAGSCROLL_MULTIPLIER 0.75 // Variable-DPI Drag Scroll
+#endif
 
 keyboard_config_t keyboard_config;
-uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
+uint16_t          dpi_array[] = PLOOPY_DPI_OPTIONS;
 #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
 
 // TODO: Implement libinput profiles
@@ -57,6 +63,7 @@ uint16_t lastScroll        = 0;      // Previous confirmed wheel event
 uint16_t lastMidClick      = 0;      // Stops scrollwheel from being read if it was pressed
 uint8_t  OptLowPin         = OPT_ENC1;
 bool     debug_encoder     = false;
+bool     is_drag_scroll    = false;
 
 __attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) {
     mouse_report->h = h;
@@ -151,17 +158,34 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
 
     // Update Timer to prevent accidental scrolls
     if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
-        lastMidClick = timer_read();
+        lastMidClick      = timer_read();
         is_scroll_clicked = record->event.pressed;
     }
 
-    if (!process_record_user(keycode, record)) { return false; }
+    if (!process_record_user(keycode, record)) {
+        return false;
+    }
 
     if (keycode == DPI_CONFIG && record->event.pressed) {
         keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
         eeconfig_update_kb(keyboard_config.raw);
         pmw_set_cpi(dpi_array[keyboard_config.dpi_config]);
     }
+
+    if (keycode == DRAG_SCROLL) {
+#ifndef PLOOPY_DRAGSCROLL_MOMENTARY
+        if (record->event.pressed)
+#endif
+        {
+            is_drag_scroll ^= 1;
+        }
+#ifdef PLOOPY_DRAGSCROLL_FIXED
+        pmw_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]);
+#else
+        pmw_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]);
+#endif
+    }
+
 /* If Mousekeys is disabled, then use handle the mouse button
  * keycodes.  This makes things simpler, and allows usage of
  * the keycodes in a consistent manner.  But only do this if
@@ -223,24 +247,40 @@ void pointing_device_init(void) {
     opt_encoder_init();
 }
 
-bool has_report_changed (report_mouse_t first, report_mouse_t second) {
-    return !(
-        (!first.buttons && first.buttons == second.buttons) &&
-        (!first.x && first.x == second.x) &&
-        (!first.y && first.y == second.y) &&
-        (!first.h && first.h == second.h) &&
-        (!first.v && first.v == second.v) );
-}
+bool has_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); }
 
 void pointing_device_task(void) {
     report_mouse_t mouse_report = pointing_device_get_report();
     process_wheel(&mouse_report);
     process_mouse(&mouse_report);
 
+    if (is_drag_scroll) {
+        mouse_report.h = mouse_report.x;
+        mouse_report.v = mouse_report.y;
+        mouse_report.x = 0;
+        mouse_report.y = 0;
+    }
+
     pointing_device_set_report(mouse_report);
-    if (has_report_changed(mouse_report, pointing_device_get_report())) {
-        pointing_device_send();
+    pointing_device_send();
+}
+
+void pointing_device_send(void) {
+    static report_mouse_t old_report  = {};
+    report_mouse_t        mouseReport = pointing_device_get_report();
+
+    // If you need to do other things, like debugging, this is the place to do it.
+    if (has_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;
+    pointing_device_set_report(mouseReport);
+    old_report = mouseReport;
 }
 
 void eeconfig_init_kb(void) {
diff --git a/keyboards/ploopyco/mouse/mouse.h b/keyboards/ploopyco/mouse/mouse.h
index f80449c187..5d49d2f2d2 100644
--- a/keyboards/ploopyco/mouse/mouse.h
+++ b/keyboards/ploopyco/mouse/mouse.h
@@ -47,8 +47,18 @@ typedef union {
 } keyboard_config_t;
 
 extern keyboard_config_t keyboard_config;
+extern uint16_t          dpi_array[];
 
 enum ploopy_keycodes {
+#ifdef VIA_ENABLE
+    DPI_CONFIG = USER00,
+#else
     DPI_CONFIG = SAFE_RANGE,
+#endif
+    DRAG_SCROLL,
+#ifdef VIA_ENABLE
+    PLOOPY_SAFE_RANGE = SAFE_RANGE,
+#else
     PLOOPY_SAFE_RANGE,
+#endif
 };
diff --git a/keyboards/ploopyco/mouse/rules.mk b/keyboards/ploopyco/mouse/rules.mk
index 863f335c85..f998672f14 100644
--- a/keyboards/ploopyco/mouse/rules.mk
+++ b/keyboards/ploopyco/mouse/rules.mk
@@ -12,8 +12,8 @@ BOOTLOADER = atmel-dfu
 #
 BOOTMAGIC_ENABLE = lite     # Virtual DIP switch configuration
 EXTRAKEY_ENABLE = yes       # Audio control and System control
-CONSOLE_ENABLE = yes         # Console for debug
-COMMAND_ENABLE = no        # Commands for debug and configuration
+CONSOLE_ENABLE = no         # Console for debug
+COMMAND_ENABLE = no         # Commands for debug and configuration
 # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
 SLEEP_LED_ENABLE = no       # Breathing sleep LED during USB suspend
 # if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
@@ -24,7 +24,7 @@ UNICODE_ENABLE = no         # Unicode
 BLUETOOTH_ENABLE = no       # Enable Bluetooth
 AUDIO_ENABLE = no           # Audio output
 POINTING_DEVICE_ENABLE = yes
-MOUSEKEY_ENABLE = no        # Mouse keys
+MOUSEKEY_ENABLE = yes       # Mouse keys
 
 QUANTUM_LIB_SRC += analog.c spi_master.c
 SRC += pmw3360.c opt_encoder.c
diff --git a/keyboards/ploopyco/trackball/keymaps/drag_scroll/keymap.c b/keyboards/ploopyco/trackball/keymaps/drag_scroll/keymap.c
index 7784bc8553..fbf07935c8 100644
--- a/keyboards/ploopyco/trackball/keymaps/drag_scroll/keymap.c
+++ b/keyboards/ploopyco/trackball/keymaps/drag_scroll/keymap.c
@@ -17,42 +17,6 @@
  */
 #include QMK_KEYBOARD_H
 
-// used for tracking the state
-bool is_drag_scroll = false;
-
-enum custom_keycodes {
-    DRAG_SCROLL = PLOOPY_SAFE_RANGE,
-};
-
-bool process_record_user(uint16_t keycode, keyrecord_t *record) {
-    switch (keycode) {
-        case DRAG_SCROLL:
-            if (record->event.pressed) {
-                // this toggles the state each time you tap it
-                is_drag_scroll ^= 1;
-            }
-            break;
-    }
-    return true;
-}
-
-// The real magic is here.
-// This function is called to translate the processed sensor movement
-// from the mouse sensor and translates it into x and y movement for
-// the mouse report. Normally.  So if "drag scroll" is toggled on,
-// moving the ball scrolls instead.  You could remove the  x or y here
-//  to only scroll in one direction, if you wanted, as well. In fact,
-// there is no reason that you need to send this to the mouse report.
-// You could have it register a key, instead.
-void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y) {
-    if (is_drag_scroll) {
-        mouse_report->h = x;
-        mouse_report->v = y;
-    } else {
-        mouse_report->x = x;
-        mouse_report->y = y;
-    }
-}
 
 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     [0] = LAYOUT( /* Base */
diff --git a/keyboards/ploopyco/trackball/rev1/readme.md b/keyboards/ploopyco/trackball/rev1/readme.md
index c0b0223723..a824c4690e 100644
--- a/keyboards/ploopyco/trackball/rev1/readme.md
+++ b/keyboards/ploopyco/trackball/rev1/readme.md
@@ -13,8 +13,8 @@ Once you have that, you'll need to [ISP Flash](https://docs.qmk.fm/#/isp_flashin
 
 | Fuse     | Setting          |
 |----------|------------------|
-| Low      | `0xDF`           |
-| High     | `0xD8` or `0x98` |
+| Low      | `0x52`           |
+| High     | `0x99`           |
 | Extended | `0xCB`           |
 
 Original (Caterina) settings: 
diff --git a/keyboards/ploopyco/trackball/rules.mk b/keyboards/ploopyco/trackball/rules.mk
index 4ec870e659..e1eb998fe0 100644
--- a/keyboards/ploopyco/trackball/rules.mk
+++ b/keyboards/ploopyco/trackball/rules.mk
@@ -9,8 +9,8 @@ F_CPU = 8000000
 #
 BOOTMAGIC_ENABLE = lite     # Virtual DIP switch configuration
 EXTRAKEY_ENABLE = yes       # Audio control and System control
-CONSOLE_ENABLE = yes         # Console for debug
-COMMAND_ENABLE = no        # Commands for debug and configuration
+CONSOLE_ENABLE = no         # Console for debug
+COMMAND_ENABLE = no         # Commands for debug and configuration
 # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
 SLEEP_LED_ENABLE = no       # Breathing sleep LED during USB suspend
 # if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
@@ -21,7 +21,7 @@ UNICODE_ENABLE = no         # Unicode
 BLUETOOTH_ENABLE = no       # Enable Bluetooth
 AUDIO_ENABLE = no           # Audio output
 POINTING_DEVICE_ENABLE = yes
-MOUSEKEY_ENABLE = no        # Mouse keys
+MOUSEKEY_ENABLE = yes       # Mouse keys
 
 QUANTUM_LIB_SRC += analog.c spi_master.c
 SRC += pmw3360.c opt_encoder.c
diff --git a/keyboards/ploopyco/trackball/trackball.c b/keyboards/ploopyco/trackball/trackball.c
index 94f2843460..f59f9788f9 100644
--- a/keyboards/ploopyco/trackball/trackball.c
+++ b/keyboards/ploopyco/trackball/trackball.c
@@ -39,9 +39,15 @@
 #ifndef PLOOPY_DPI_DEFAULT
 #    define PLOOPY_DPI_DEFAULT 0
 #endif
+#ifndef PLOOPY_DRAGSCROLL_DPI
+#    define PLOOPY_DRAGSCROLL_DPI 100 // Fixed-DPI Drag Scroll
+#endif
+#ifndef PLOOPY_DRAGSCROLL_MULTIPLIER
+#    define PLOOPY_DRAGSCROLL_MULTIPLIER 0.75 // Variable-DPI Drag Scroll
+#endif
 
 keyboard_config_t keyboard_config;
-uint16_t dpi_array[] = PLOOPY_DPI_OPTIONS;
+uint16_t          dpi_array[] = PLOOPY_DPI_OPTIONS;
 #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))
 
 // TODO: Implement libinput profiles
@@ -57,6 +63,7 @@ uint16_t lastScroll        = 0;      // Previous confirmed wheel event
 uint16_t lastMidClick      = 0;      // Stops scrollwheel from being read if it was pressed
 uint8_t  OptLowPin         = OPT_ENC1;
 bool     debug_encoder     = false;
+bool     is_drag_scroll    = false;
 
 __attribute__((weak)) void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v) {
     mouse_report->h = h;
@@ -151,11 +158,13 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
 
     // Update Timer to prevent accidental scrolls
     if ((record->event.key.col == 1) && (record->event.key.row == 0)) {
-        lastMidClick = timer_read();
+        lastMidClick      = timer_read();
         is_scroll_clicked = record->event.pressed;
     }
 
-    if (!process_record_user(keycode, record)) { return false; }
+    if (!process_record_user(keycode, record)) {
+        return false;
+    }
 
     if (keycode == DPI_CONFIG && record->event.pressed) {
         keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
@@ -163,6 +172,20 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
         pmw_set_cpi(dpi_array[keyboard_config.dpi_config]);
     }
 
+    if (keycode == DRAG_SCROLL) {
+#ifndef PLOOPY_DRAGSCROLL_MOMENTARY
+        if (record->event.pressed)
+#endif
+        {
+            is_drag_scroll ^= 1;
+        }
+#ifdef PLOOPY_DRAGSCROLL_FIXED
+        pmw_set_cpi(is_drag_scroll ? PLOOPY_DRAGSCROLL_DPI : dpi_array[keyboard_config.dpi_config]);
+#else
+        pmw_set_cpi(is_drag_scroll ? (dpi_array[keyboard_config.dpi_config] * PLOOPY_DRAGSCROLL_MULTIPLIER) : dpi_array[keyboard_config.dpi_config]);
+#endif
+    }
+
 /* If Mousekeys is disabled, then use handle the mouse button
  * keycodes.  This makes things simpler, and allows usage of
  * the keycodes in a consistent manner.  But only do this if
@@ -223,24 +246,40 @@ void pointing_device_init(void) {
     opt_encoder_init();
 }
 
-bool has_report_changed (report_mouse_t first, report_mouse_t second) {
-    return !(
-        (!first.buttons && first.buttons == second.buttons) &&
-        (!first.x && first.x == second.x) &&
-        (!first.y && first.y == second.y) &&
-        (!first.h && first.h == second.h) &&
-        (!first.v && first.v == second.v) );
-}
+bool has_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); }
 
 void pointing_device_task(void) {
     report_mouse_t mouse_report = pointing_device_get_report();
     process_wheel(&mouse_report);
     process_mouse(&mouse_report);
 
+    if (is_drag_scroll) {
+        mouse_report.h = mouse_report.x;
+        mouse_report.v = mouse_report.y;
+        mouse_report.x = 0;
+        mouse_report.y = 0;
+    }
+
     pointing_device_set_report(mouse_report);
-    if (has_report_changed(mouse_report, pointing_device_get_report())) {
-        pointing_device_send();
+    pointing_device_send();
+}
+
+void pointing_device_send(void) {
+    static report_mouse_t old_report  = {};
+    report_mouse_t        mouseReport = pointing_device_get_report();
+
+    // If you need to do other things, like debugging, this is the place to do it.
+    if (has_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;
+    pointing_device_set_report(mouseReport);
+    old_report = mouseReport;
 }
 
 void eeconfig_init_kb(void) {
diff --git a/keyboards/ploopyco/trackball/trackball.h b/keyboards/ploopyco/trackball/trackball.h
index c58be6c84a..fd2bf465a5 100644
--- a/keyboards/ploopyco/trackball/trackball.h
+++ b/keyboards/ploopyco/trackball/trackball.h
@@ -45,15 +45,25 @@ void process_wheel_user(report_mouse_t* mouse_report, int16_t h, int16_t v);
     { {BL, BM, BR, BF, BB}, }
 
 typedef union {
-  uint32_t raw;
-  struct {
-    uint8_t    dpi_config;
-  };
+    uint32_t raw;
+    struct {
+        uint8_t dpi_config;
+    };
 } keyboard_config_t;
 
 extern keyboard_config_t keyboard_config;
+extern uint16_t          dpi_array[];
 
 enum ploopy_keycodes {
+#ifdef VIA_ENABLE
+    DPI_CONFIG = USER00,
+#else
     DPI_CONFIG = SAFE_RANGE,
+#endif
+    DRAG_SCROLL,
+#ifdef VIA_ENABLE
+    PLOOPY_SAFE_RANGE = SAFE_RANGE,
+#else
     PLOOPY_SAFE_RANGE,
+#endif
 };
-- 
cgit v1.2.3