From: Soumyajyotii Ssarkar <[email protected]> The USB mouse report descriptor only describes buttons, X, Y and a vertical wheel, so horizontal scrolling from the host never reaches the guest.
Rework the descriptor following the layout in Microsoft's "Enhanced Wheel Support in Windows" paper: the pointer fields move into input report 1 and the vertical Wheel and the Consumer AC Pan usages each get their own logical collection. The report descriptor grows from 52 to 90 bytes and the interrupt endpoint packet size from 4 to 6 bytes. On the HID side, track horizontal movement in a new 'pan' field of the pointer event queue, fed from WHEEL_LEFT/RIGHT button clicks and from horizontal scroll events, with the same residual handling as the vertical wheel. In report protocol hid_pointer_poll() now emits a 6-byte report: report ID, buttons, X, Y, wheel and one pan byte. Boot protocol keeps the 4-byte layout without report ID and pan. The pan value is not inverted: a positive AC Pan value means scrolling right, which matches the sign of the input layer's horizontal scroll. The queued pan values and the pan residual are migrated in the optional wheel subsection. Signed-off-by: Soumyajyotii Ssarkar <[email protected]> --- hw/input/hid.c | 75 ++++++++++++++++++++++++++++------- hw/usb/dev-hid.c | 88 +++++++++++++++++++++++++++++------------- include/hw/input/hid.h | 2 + 3 files changed, 124 insertions(+), 41 deletions(-) diff --git a/hw/input/hid.c b/hw/input/hid.c index 2855f6a3d9..9f26850112 100644 --- a/hw/input/hid.c +++ b/hw/input/hid.c @@ -148,6 +148,10 @@ static void hid_pointer_event(DeviceState *dev, QemuConsole *src, e->dz -= INPUT_EVENT_SCROLL_DETENT; } else if (evt->btn.button == INPUT_BUTTON_WHEEL_DOWN) { e->dz += INPUT_EVENT_SCROLL_DETENT; + } else if (evt->btn.button == INPUT_BUTTON_WHEEL_LEFT) { + e->pan -= INPUT_EVENT_SCROLL_DETENT; + } else if (evt->btn.button == INPUT_BUTTON_WHEEL_RIGHT) { + e->pan += INPUT_EVENT_SCROLL_DETENT; } } else { e->buttons_state &= ~bmap[evt->btn.button]; @@ -157,6 +161,8 @@ static void hid_pointer_event(DeviceState *dev, QemuConsole *src, case INPUT_EVENT_KIND_SCROLL: if (evt->scroll.axis == INPUT_SCROLL_AXIS_VERTICAL) { e->dz += evt->scroll.value; + } else if (evt->scroll.axis == INPUT_SCROLL_AXIS_HORIZONTAL) { + e->pan += evt->scroll.value; } break; @@ -210,6 +216,8 @@ static void hid_pointer_sync(DeviceState *dev) } prev->dz += curr->dz; curr->dz = 0; + prev->pan += curr->pan; + curr->pan = 0; } else { /* prepare next (clear rel, copy abs + btns) */ if (hs->kind == HID_MOUSE) { @@ -220,6 +228,7 @@ static void hid_pointer_sync(DeviceState *dev) next->ydy = curr->ydy; } next->dz = 0; + next->pan = 0; next->buttons_state = curr->buttons_state; /* make current guest visible, notify guest */ hs->n++; @@ -369,7 +378,7 @@ static int hid_scroll_counts(int32_t *pending, int32_t *residual, int mult) int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len) { - int dx, dy, dz, l; + int dx, dy, dz, pan, l; int index; HIDPointerEvent *e; @@ -392,9 +401,11 @@ int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len) dy = e->ydy; } dz = hid_scroll_counts(&e->dz, &hs->ptr.wheel_residual, 1); + pan = hid_scroll_counts(&e->pan, &hs->ptr.pan_residual, 1); if (hs->n && !e->dz && + !e->pan && (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) { /* that deals with this event */ QUEUE_INCR(hs->head); @@ -406,17 +417,40 @@ int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len) l = 0; switch (hs->kind) { case HID_MOUSE: - if (len > l) { - buf[l++] = e->buttons_state; - } - if (len > l) { - buf[l++] = dx; - } - if (len > l) { - buf[l++] = dy; - } - if (len > l) { - buf[l++] = dz; + if (hs->protocol == 0) { + /* boot protocol (0) */ + if (len > l) { + buf[l++] = e->buttons_state; + } + if (len > l) { + buf[l++] = dx; + } + if (len > l) { + buf[l++] = dy; + } + if (len > l) { + buf[l++] = dz; + } + } else { + /* report protocol (1) */ + if (len > l) { + buf[l++] = 0x01; + } + if (len > l) { + buf[l++] = e->buttons_state; + } + if (len > l) { + buf[l++] = dx; + } + if (len > l) { + buf[l++] = dy; + } + if (len > l) { + buf[l++] = dz; + } + if (len > l) { + buf[l++] = pan; + } } break; @@ -603,6 +637,16 @@ static const VMStateDescription vmstate_hid_ptr_queue = { } }; +static const VMStateDescription vmstate_hid_ptr_queue_pan = { + .name = "HIDPointerEventQueue/pan", + .version_id = 1, + .minimum_version_id = 1, + .fields = (const VMStateField[]) { + VMSTATE_INT32(pan, HIDPointerEvent), + VMSTATE_END_OF_LIST() + } +}; + /* * Wheel movement used to be queued in whole detents and is now kept in * 1/120 detent units. Without this subsection the source either was an @@ -613,11 +657,11 @@ static bool hid_ptr_wheel_needed(void *opaque) HIDState *s = opaque; int i; - if (s->ptr.wheel_residual) { + if (s->ptr.wheel_residual || s->ptr.pan_residual) { return true; } for (i = 0; i < QUEUE_LENGTH; i++) { - if (s->ptr.queue[i].dz) { + if (s->ptr.queue[i].dz || s->ptr.queue[i].pan) { return true; } } @@ -660,7 +704,10 @@ static const VMStateDescription vmstate_hid_ptr_wheel = { .needed = hid_ptr_wheel_needed, .post_load = hid_ptr_wheel_post_load, .fields = (const VMStateField[]) { + VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0, + vmstate_hid_ptr_queue_pan, HIDPointerEvent), VMSTATE_INT32(ptr.wheel_residual, HIDState), + VMSTATE_INT32(ptr.pan_residual, HIDState), VMSTATE_END_OF_LIST() } }; diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c index 2c10e88c85..804119006b 100644 --- a/hw/usb/dev-hid.c +++ b/hw/usb/dev-hid.c @@ -85,31 +85,65 @@ static const USBDescStrings desc_strings = { static const uint8_t qemu_mouse_hid_report_descriptor[] = { 0x05, 0x01, /* Usage Page (Generic Desktop) */ 0x09, 0x02, /* Usage (Mouse) */ - 0xa1, 0x01, /* Collection (Application) */ - 0x09, 0x01, /* Usage (Pointer) */ - 0xa1, 0x00, /* Collection (Physical) */ - 0x05, 0x09, /* Usage Page (Button) */ - 0x19, 0x01, /* Usage Minimum (1) */ - 0x29, 0x05, /* Usage Maximum (5) */ - 0x15, 0x00, /* Logical Minimum (0) */ - 0x25, 0x01, /* Logical Maximum (1) */ - 0x95, 0x05, /* Report Count (5) */ - 0x75, 0x01, /* Report Size (1) */ - 0x81, 0x02, /* Input (Data, Variable, Absolute) */ - 0x95, 0x01, /* Report Count (1) */ - 0x75, 0x03, /* Report Size (3) */ - 0x81, 0x01, /* Input (Constant) */ - 0x05, 0x01, /* Usage Page (Generic Desktop) */ - 0x09, 0x30, /* Usage (X) */ - 0x09, 0x31, /* Usage (Y) */ - 0x09, 0x38, /* Usage (Wheel) */ - 0x15, 0x81, /* Logical Minimum (-0x7f) */ - 0x25, 0x7f, /* Logical Maximum (0x7f) */ - 0x75, 0x08, /* Report Size (8) */ - 0x95, 0x03, /* Report Count (3) */ - 0x81, 0x06, /* Input (Data, Variable, Relative) */ - 0xc0, /* End Collection */ - 0xc0, /* End Collection */ + 0xA1, 0x01, /* Collection (Application) */ + + 0x09, 0x02, /* Usage (Mouse) */ + 0xA1, 0x02, /* Collection (Logical) */ + + 0x85, 0x01, /* Report ID (0x01) */ + 0x09, 0x01, /* Usage (Pointer) */ + 0xA1, 0x00, /* Collection (Physical) */ + + 0x05, 0x09, /* Usage Page (Button) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x05, /* Usage Maximum (5) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x95, 0x05, /* Report Count (5) */ + 0x75, 0x01, /* Report Size (1) */ + 0x81, 0x02, /* Input (Data,Var,Abs) */ + + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x03, /* Report Size (3) */ + 0x81, 0x01, /* Input (Const) */ + + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x30, /* Usage (X) */ + 0x09, 0x31, /* Usage (Y) */ + 0x95, 0x02, /* Report Count (2) */ + 0x75, 0x08, /* Report Size (8) */ + 0x15, 0x81, /* Logical Minimum (-127) */ + 0x25, 0x7F, /* Logical Maximum (127) */ + 0x81, 0x06, /* Input (Data,Var,Rel) */ + + /* Vertical wheel logical collection */ + 0xA1, 0x02, /* Collection (Logical) */ + + 0x09, 0x38, /* Usage (Wheel) */ + 0x15, 0x81, /* Logical Minimum (-0x7f) */ + 0x25, 0x7F, /* Logical Maximum (0x7f) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x01, /* Report Count (1) */ + 0x81, 0x06, /* Input (Data,Var,Rel) */ + + 0xC0, /* End Collection */ + + /* Horizontal wheel logical collection */ + 0xA1, 0x02, /* Collection (Logical) */ + + 0x05, 0x0C, /* Usage Page (Consumer Devices) */ + 0x0A, 0x38, 0x02, /* Usage (AC Pan) */ + 0x15, 0x81, /* Logical Minimum (-0x7f) */ + 0x25, 0x7F, /* Logical Maximum (0x7f) */ + 0x75, 0x08, /* Report Size (8) */ + 0x95, 0x01, /* Report Count (1) */ + 0x81, 0x06, /* Input (Data,Var,Rel) */ + + 0xC0, /* End Collection */ + + 0xC0, /* End Collection (Physical) */ + 0xC0, /* End Collection (Logical) */ + 0xC0 /* End Collection (Application) */ }; static const USBDescIface desc_iface_mouse = { @@ -139,7 +173,7 @@ static const USBDescIface desc_iface_mouse = { { .bEndpointAddress = USB_DIR_IN | 0x01, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = 4, + .wMaxPacketSize = 6, .bInterval = 0x0a, }, }, @@ -172,7 +206,7 @@ static const USBDescIface desc_iface_mouse2 = { { .bEndpointAddress = USB_DIR_IN | 0x01, .bmAttributes = USB_ENDPOINT_XFER_INT, - .wMaxPacketSize = 4, + .wMaxPacketSize = 6, .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */ }, }, diff --git a/include/hw/input/hid.h b/include/hw/input/hid.h index 1e29f4ada4..b7cdeb54c2 100644 --- a/include/hw/input/hid.h +++ b/include/hw/input/hid.h @@ -10,6 +10,7 @@ typedef struct HIDPointerEvent { int32_t xdx, ydy; /* relative iff it's a mouse, otherwise absolute */ int32_t dz, buttons_state; + int32_t pan; } HIDPointerEvent; #define QUEUE_LENGTH 16 /* should be enough for a triple-click */ @@ -23,6 +24,7 @@ typedef struct HIDMouseState { HIDPointerEvent queue[QUEUE_LENGTH]; int mouse_grabbed; int32_t wheel_residual; + int32_t pan_residual; bool wheel_state_loaded; } HIDMouseState; -- 2.56.0
