SDL: pen: Send virtual mouse motion without a button press when a pen is hovering.

From 32965b4bf11c6d0490294e7a6f7cecda22b909bc Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sat, 18 Jan 2025 21:51:03 -0500
Subject: [PATCH] pen: Send virtual mouse motion without a button press when a
 pen is hovering.

Fixes #11470.
---
 src/events/SDL_pen.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/events/SDL_pen.c b/src/events/SDL_pen.c
index 6db1b0ee14413..afc2ea031daa7 100644
--- a/src/events/SDL_pen.c
+++ b/src/events/SDL_pen.c
@@ -488,17 +488,22 @@ void SDL_SendPenMotion(Uint64 timestamp, SDL_PenID instance_id, SDL_Window *wind
         event.pmotion.y = y;
         SDL_PushEvent(&event);
 
-        if (window && (pen_touching == instance_id)) {
+        if (window) {
             SDL_Mouse *mouse = SDL_GetMouse();
             if (mouse) {
-                if (mouse->pen_mouse_events) {
-                    SDL_SendMouseMotion(timestamp, window, SDL_PEN_MOUSEID, false, x, y);
-                }
+                if (pen_touching == instance_id) {
+                    if (mouse->pen_mouse_events) {
+                        SDL_SendMouseMotion(timestamp, window, SDL_PEN_MOUSEID, false, x, y);
+                    }
 
-                if (mouse->pen_touch_events) {
-                    const float normalized_x = x / (float)window->w;
-                    const float normalized_y = y / (float)window->h;
-                    SDL_SendTouchMotion(timestamp, SDL_PEN_TOUCHID, SDL_BUTTON_LEFT, window, normalized_x, normalized_y, pen->axes[SDL_PEN_AXIS_PRESSURE]);
+                    if (mouse->pen_touch_events) {
+                        const float normalized_x = x / (float)window->w;
+                        const float normalized_y = y / (float)window->h;
+                        SDL_SendTouchMotion(timestamp, SDL_PEN_TOUCHID, SDL_BUTTON_LEFT, window, normalized_x, normalized_y, pen->axes[SDL_PEN_AXIS_PRESSURE]);
+                    }
+                } else if (pen_touching == 0) {  // send mouse motion (without a pressed button) for pens that aren't touching.
+                    // this might cause a little chaos if you have multiple pens hovering at the same time, but this seems unlikely in the real world, and also something you did to yourself.  :)
+                    SDL_SendMouseMotion(timestamp, window, SDL_PEN_MOUSEID, false, x, y);
                 }
             }
         }