From: Soumyajyotii Ssarkar <[email protected]>

The input layer can only describe wheel movement as WHEEL_UP/DOWN/
LEFT/RIGHT button clicks, one per detent. Host UIs that receive
smooth scroll deltas (touchpads, free-spinning or high-resolution
wheels) have to round them to whole clicks and the guest never sees
the finer movement.

Add a new 'scroll' input event kind carrying a scroll axis (vertical
or horizontal) and a distance in 1/120 of a wheel detent, the same
unit Windows uses for WHEEL_DELTA and Linux for REL_WHEEL_HI_RES.
Positive values scroll down or right.

Scroll events are routed to the handler that receives pointer button
events for the console. Handlers that do not set
INPUT_EVENT_MASK_SCROLL keep working unchanged: the input core
accumulates the distance and emits one wheel button click per full
detent.

Also wire the new event into input-send-event, tracing and
record/replay.

Signed-off-by: Soumyajyotii Ssarkar <[email protected]>
---
 include/ui/input.h    |  6 ++++
 qapi/ui.json          | 45 ++++++++++++++++++++++--
 replay/replay-input.c |  8 +++++
 ui/input.c            | 82 ++++++++++++++++++++++++++++++++++++++++++-
 ui/trace-events       |  1 +
 5 files changed, 139 insertions(+), 3 deletions(-)

diff --git a/include/ui/input.h b/include/ui/input.h
index a1560cac0f..daedb547ff 100644
--- a/include/ui/input.h
+++ b/include/ui/input.h
@@ -9,6 +9,9 @@
 #define INPUT_EVENT_MASK_REL   (1<<INPUT_EVENT_KIND_REL)
 #define INPUT_EVENT_MASK_ABS   (1<<INPUT_EVENT_KIND_ABS)
 #define INPUT_EVENT_MASK_MTT   (1<<INPUT_EVENT_KIND_MTT)
+#define INPUT_EVENT_MASK_SCROLL (1 << INPUT_EVENT_KIND_SCROLL)
+
+#define INPUT_EVENT_SCROLL_DETENT 120
 
 #define INPUT_EVENT_ABS_MIN    0x0000
 #define INPUT_EVENT_ABS_MAX    0x7FFF
@@ -31,6 +34,7 @@ typedef struct QemuInputEvent {
         InputMoveEvent rel;
         InputMoveEvent abs;
         InputMultiTouchEvent mtt;
+        InputScrollEvent scroll;
     };
 } QemuInputEvent;
 
@@ -80,6 +84,8 @@ int qemu_input_scale_axis(int value,
 void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value);
 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
                           int min_in, int max_in);
+void qemu_input_queue_scroll(QemuConsole *src, InputScrollAxis axis,
+                             int value);
 void qemu_input_queue_mtt(QemuConsole *src, InputMultiTouchType type, int slot,
                           int tracking_id);
 void qemu_input_queue_mtt_abs(QemuConsole *src, InputAxis axis, int value,
diff --git a/qapi/ui.json b/qapi/ui.json
index 9e3e80a3d7..e31fd74f87 100644
--- a/qapi/ui.json
+++ b/qapi/ui.json
@@ -1120,6 +1120,34 @@
               'axis'       : 'InputAxis',
               'value'      : 'int' } }
 
+##
+# @InputScrollAxis:
+#     Scroll axis of a pointer input device.
+#
+# @vertical: vertical wheel
+#
+# @horizontal: horizontal wheel (pan)
+#
+# Since: 11.2
+##
+{ 'enum'  : 'InputScrollAxis',
+  'data'  : [ 'vertical', 'horizontal' ] }
+
+##
+# @InputScrollEvent:
+#     High-resolution scroll input event.
+#
+# @axis: Which scroll axis is referenced by @value.
+#
+# @value: Scroll distance in 1/120 of a wheel detent.  Positive
+#     values scroll down (vertical) or right (horizontal).
+#
+# Since: 11.2
+##
+{ 'struct'  : 'InputScrollEvent',
+  'data'  : { 'axis'    : 'InputScrollAxis',
+              'value'   : 'int' } }
+
 ##
 # @InputEventKind:
 #
@@ -1133,10 +1161,12 @@
 #
 # @mtt: a multi-touch input event
 #
+# @scroll: a high-resolution scroll input event (since 11.2)
+#
 # Since: 2.0
 ##
 { 'enum': 'InputEventKind',
-  'data': [ 'key', 'btn', 'rel', 'abs', 'mtt' ] }
+  'data': [ 'key', 'btn', 'rel', 'abs', 'mtt', 'scroll' ] }
 
 ##
 # @InputKeyEventWrapper:
@@ -1178,6 +1208,16 @@
 { 'struct': 'InputMultiTouchEventWrapper',
   'data': { 'data': 'InputMultiTouchEvent' } }
 
+##
+# @InputScrollEventWrapper:
+#
+# @data: High-resolution scroll input event
+#
+# Since: 11.2
+##
+{ 'struct': 'InputScrollEventWrapper',
+  'data': { 'data': 'InputScrollEvent' } }
+
 ##
 # @InputEvent:
 #     Input event union.
@@ -1193,7 +1233,8 @@
               'btn'     : 'InputBtnEventWrapper',
               'rel'     : 'InputMoveEventWrapper',
               'abs'     : 'InputMoveEventWrapper',
-              'mtt'     : 'InputMultiTouchEventWrapper' } }
+              'mtt'     : 'InputMultiTouchEventWrapper',
+              'scroll'  : 'InputScrollEventWrapper' } }
 
 ##
 # @input-send-event:
diff --git a/replay/replay-input.c b/replay/replay-input.c
index c86e00af0e..188c43a137 100644
--- a/replay/replay-input.c
+++ b/replay/replay-input.c
@@ -45,6 +45,10 @@ void replay_save_input_event(QemuInputEvent *evt)
         replay_put_dword(evt->mtt.axis);
         replay_put_qword(evt->mtt.value);
         break;
+    case INPUT_EVENT_KIND_SCROLL:
+        replay_put_dword(evt->scroll.axis);
+        replay_put_qword(evt->scroll.value);
+        break;
     case INPUT_EVENT_KIND__MAX:
         /* keep gcc happy */
         break;
@@ -80,6 +84,10 @@ QemuInputEvent *replay_read_input_event(void)
         evt->mtt.axis = (InputAxis)replay_get_dword();
         evt->mtt.value = replay_get_qword();
         break;
+    case INPUT_EVENT_KIND_SCROLL:
+        evt->scroll.axis = (InputScrollAxis)replay_get_dword();
+        evt->scroll.value = replay_get_qword();
+        break;
     case INPUT_EVENT_KIND__MAX:
         /* keep gcc happy */
         break;
diff --git a/ui/input.c b/ui/input.c
index f40a988370..f9ae49e275 100644
--- a/ui/input.c
+++ b/ui/input.c
@@ -181,7 +181,12 @@ void qmp_input_send_event(const char *device,
     for (e = events; e != NULL; e = e->next) {
         InputEvent *event = e->value;
 
-        if (!qemu_input_find_handler(1 << event->type, con)) {
+        uint32_t mask = 1 << event->type;
+
+        if (event->type == INPUT_EVENT_KIND_SCROLL) {
+            mask |= INPUT_EVENT_MASK_BTN;
+        }
+        if (!qemu_input_find_handler(mask, con)) {
             error_setg(errp, "Input handler not found for "
                              "event type %s",
                             InputEventKind_str(event->type));
@@ -217,6 +222,10 @@ void qmp_input_send_event(const char *device,
             evt.mtt = *qapi->u.mtt.data;
             break;
 
+        case INPUT_EVENT_KIND_SCROLL:
+            evt.scroll = *qapi->u.scroll.data;
+            break;
+
         default:
             g_assert_not_reached();
         }
@@ -235,6 +244,7 @@ static void qemu_input_event_trace(QemuConsole *src, 
QemuInputEvent *evt)
     InputBtnEvent *btn;
     InputMoveEvent *move;
     InputMultiTouchEvent *mtt;
+    InputScrollEvent *scroll;
 
     if (src) {
         idx = qemu_console_get_index(src);
@@ -265,6 +275,12 @@ static void qemu_input_event_trace(QemuConsole *src, 
QemuInputEvent *evt)
         name = mtt->axis < INPUT_AXIS__MAX ? InputAxis_str(mtt->axis) : 
"invalid";
         trace_input_event_mtt(idx, name, mtt->value);
         break;
+    case INPUT_EVENT_KIND_SCROLL:
+        scroll = &evt->scroll;
+        name = scroll->axis < INPUT_SCROLL_AXIS__MAX ?
+            InputScrollAxis_str(scroll->axis) : "invalid";
+        trace_input_event_scroll(idx, name, scroll->value);
+        break;
     case INPUT_EVENT_KIND__MAX:
         /* keep gcc happy */
         break;
@@ -342,12 +358,62 @@ static void qemu_input_queue_sync(QemuInputEventQueueHead 
*queue)
     queue_count++;
 }
 
+static void qemu_input_scroll_to_btn(QemuInputHandlerState *s,
+                                     QemuConsole *src, QemuInputEvent *evt)
+{
+    static int residual[INPUT_SCROLL_AXIS__MAX];
+    QemuInputEvent btn = { .type = INPUT_EVENT_KIND_BTN };
+    InputScrollAxis axis = evt->scroll.axis;
+
+    if (axis >= INPUT_SCROLL_AXIS__MAX) {
+        return;
+    }
+
+    residual[axis] += evt->scroll.value;
+    while (abs(residual[axis]) >= INPUT_EVENT_SCROLL_DETENT) {
+        bool fwd = residual[axis] > 0;
+
+        if (axis == INPUT_SCROLL_AXIS_VERTICAL) {
+            btn.btn.button = fwd ? INPUT_BUTTON_WHEEL_DOWN
+                                 : INPUT_BUTTON_WHEEL_UP;
+        } else {
+            btn.btn.button = fwd ? INPUT_BUTTON_WHEEL_RIGHT
+                                 : INPUT_BUTTON_WHEEL_LEFT;
+        }
+        residual[axis] -= fwd ? INPUT_EVENT_SCROLL_DETENT
+                              : -INPUT_EVENT_SCROLL_DETENT;
+
+        btn.btn.down = true;
+        qemu_input_event_trace(src, &btn);
+        s->handler->event(s->dev, src, &btn);
+        btn.btn.down = false;
+        qemu_input_event_trace(src, &btn);
+        s->handler->event(s->dev, src, &btn);
+        s->events++;
+    }
+}
+
 void qemu_input_event_send_impl(QemuConsole *src, QemuInputEvent *evt)
 {
     QemuInputHandlerState *s;
 
     qemu_input_event_trace(src, evt);
 
+    if (evt->type == INPUT_EVENT_KIND_SCROLL) {
+        s = qemu_input_find_handler(INPUT_EVENT_MASK_BTN |
+                                    INPUT_EVENT_MASK_SCROLL, src);
+        if (!s) {
+            return;
+        }
+        if (s->handler->mask & INPUT_EVENT_MASK_SCROLL) {
+            s->handler->event(s->dev, src, evt);
+            s->events++;
+        } else {
+            qemu_input_scroll_to_btn(s, src, evt);
+        }
+        return;
+    }
+
     /* send event */
     s = qemu_input_find_handler(1 << evt->type, src);
     if (!s) {
@@ -518,6 +584,20 @@ void qemu_input_queue_rel(QemuConsole *src, InputAxis 
axis, int value)
     qemu_input_event_send(src, &evt);
 }
 
+void qemu_input_queue_scroll(QemuConsole *src, InputScrollAxis axis,
+                             int value)
+{
+    QemuInputEvent evt = {
+        .type = INPUT_EVENT_KIND_SCROLL,
+        .scroll = {
+            .axis = axis,
+            .value = value,
+        },
+    };
+
+    qemu_input_event_send(src, &evt);
+}
+
 void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value,
                           int min_in, int max_in)
 {
diff --git a/ui/trace-events b/ui/trace-events
index 339a61f341..638096607a 100644
--- a/ui/trace-events
+++ b/ui/trace-events
@@ -132,6 +132,7 @@ input_event_btn(int conidx, const char *btn, bool down) 
"con %d, button %s, down
 input_event_rel(int conidx, const char *axis, int value) "con %d, axis %s, 
value %d"
 input_event_abs(int conidx, const char *axis, int value) "con %d, axis %s, 
value 0x%x"
 input_event_mtt(int conidx, const char *axis, int value) "con %d, axis %s, 
value 0x%x"
+input_event_scroll(int conidx, const char *axis, int value) "con %d, axis %s, 
value %d"
 input_event_sync(void) ""
 
 # sdl2-input.c
-- 
2.56.0


Reply via email to