From: Soumyajyotii Ssarkar <[email protected]>

For GDK_SCROLL_SMOOTH events gd_scroll_event() only looked at the sign
of the deltas and sent a single wheel button click, so any amount of
scrolling became one detent.  It also dropped the horizontal delta
whenever there was vertical movement.

Convert the deltas, which GDK reports in detents, into 1/120 detent
units and send them as scroll events on both axes.  Fractions are
carried over to the next event so slow scrolling is not lost.
Discrete GDK_SCROLL_UP/DOWN/LEFT/RIGHT events still send button
clicks.

Signed-off-by: Soumyajyotii Ssarkar <[email protected]>
---
 ui/gtk.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/ui/gtk.c b/ui/gtk.c
index 8d312d6a4b..9df83b6225 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -1152,27 +1152,33 @@ static gboolean gd_scroll_event(GtkWidget *widget, 
GdkEventScroll *scroll,
         btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
         has_horizontal = true;
     } else if (scroll->direction == GDK_SCROLL_SMOOTH) {
+        static double residual_x, residual_y;
         gdouble delta_x, delta_y;
+        int units;
+
         if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll,
                                          &delta_x, &delta_y)) {
             return TRUE;
         }
 
-        if (delta_y > 0) {
-            btn_vertical = INPUT_BUTTON_WHEEL_DOWN;
-            has_vertical = true;
-        } else if (delta_y < 0) {
-            btn_vertical = INPUT_BUTTON_WHEEL_UP;
-            has_vertical = true;
-        } else if (delta_x > 0) {
-            btn_horizontal = INPUT_BUTTON_WHEEL_RIGHT;
-            has_horizontal = true;
-        } else if (delta_x < 0) {
-            btn_horizontal = INPUT_BUTTON_WHEEL_LEFT;
-            has_horizontal = true;
-        } else {
-            return TRUE;
+        residual_y += delta_y * INPUT_EVENT_SCROLL_DETENT;
+        units = (int)residual_y;
+        residual_y -= units;
+        if (units) {
+            qemu_input_queue_scroll(vc->gfx.dcl.con,
+                                    INPUT_SCROLL_AXIS_VERTICAL, units);
+        }
+
+        residual_x += delta_x * INPUT_EVENT_SCROLL_DETENT;
+        units = (int)residual_x;
+        residual_x -= units;
+        if (units) {
+            qemu_input_queue_scroll(vc->gfx.dcl.con,
+                                    INPUT_SCROLL_AXIS_HORIZONTAL, units);
         }
+
+        qemu_input_event_sync();
+        return TRUE;
     } else {
         return TRUE;
     }
-- 
2.56.0


Reply via email to