From: Soumyajyotii Ssarkar <[email protected]>

handle_mousewheel() only looked at the sign of the integer wheel
deltas and sent a single wheel button click, so any amount of
scrolling became one detent, and the horizontal axis was dropped
whenever there was vertical movement.

SDL 2.0.18 added preciseX and preciseY, which carry the wheel movement
in fractions of a detent.  When building against SDL 2.0.18 or newer,
convert them into 1/120 detent units and send them as scroll events on
both axes, carrying fractions over to the next event.  The direction
follows SDL's convention (positive x scrolls right, positive y scrolls
up) and honours SDL_MOUSEWHEEL_FLIPPED.  Older SDL versions keep the
existing button behaviour.

Signed-off-by: Soumyajyotii Ssarkar <[email protected]>
---
 ui/sdl2.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/ui/sdl2.c b/ui/sdl2.c
index 1c97d23a47..0a052a8f2d 100644
--- a/ui/sdl2.c
+++ b/ui/sdl2.c
@@ -566,12 +566,39 @@ static void handle_mousewheel(SDL_Event *ev)
 {
     struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
     SDL_MouseWheelEvent *wev = &ev->wheel;
+#if SDL_VERSION_ATLEAST(2, 0, 18)
+    static float residual_x, residual_y;
+    float flip;
+    int units;
+#else
     InputButton btn;
+#endif
 
     if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
         return;
     }
 
+#if SDL_VERSION_ATLEAST(2, 0, 18)
+    flip = wev->direction == SDL_MOUSEWHEEL_FLIPPED ? -1.0f : 1.0f;
+
+    residual_y -= flip * wev->preciseY * INPUT_EVENT_SCROLL_DETENT;
+    units = (int)residual_y;
+    residual_y -= units;
+    if (units) {
+        qemu_input_queue_scroll(scon->dcl.con,
+                                INPUT_SCROLL_AXIS_VERTICAL, units);
+    }
+
+    residual_x += flip * wev->preciseX * INPUT_EVENT_SCROLL_DETENT;
+    units = (int)residual_x;
+    residual_x -= units;
+    if (units) {
+        qemu_input_queue_scroll(scon->dcl.con,
+                                INPUT_SCROLL_AXIS_HORIZONTAL, units);
+    }
+
+    qemu_input_event_sync();
+#else
     if (wev->y > 0) {
         btn = INPUT_BUTTON_WHEEL_UP;
     } else if (wev->y < 0) {
@@ -588,6 +615,7 @@ static void handle_mousewheel(SDL_Event *ev)
     qemu_input_event_sync();
     qemu_input_queue_btn(scon->dcl.con, btn, false);
     qemu_input_event_sync();
+#endif
 }
 
 static void handle_windowevent(SDL_Event *ev)
-- 
2.56.0


Reply via email to