From: Soumyajyotii Ssarkar <[email protected]> Even with high-resolution scroll events reaching the HID mouse, each wheel report count is still a whole detent, so guests see the same coarse steps as before.
Add a Resolution Multiplier feature (report ID 2) to the vertical wheel and AC Pan logical collections, as described in Microsoft's "Enhanced Wheel Support in Windows" docs. The feature byte holds the wheel multiplier in bits [1:0] and the pan multiplier in bits [3:2], each with logical range 0..1 and physical range 1..120. The report descriptor grows to 144 bytes. Handle GET_REPORT and SET_REPORT for the feature report, accepting the payload with or without the leading report ID byte. Windows (Vista and later) and Linux set each multiplier to its logical maximum. While a multiplier is enabled in report protocol, hid_pointer_poll() reports that axis in counts of 1/120 detent, so one count reaches applications as one WHEEL_DELTA unit. Boot protocol and guests that never enable the multiplier keep getting whole detents. The multiplier state is migrated in the optional wheel subsection. Signed-off-by: Soumyajyotii Ssarkar <[email protected]> --- hw/input/hid.c | 22 ++++++++-- hw/usb/dev-hid.c | 97 ++++++++++++++++++++++++++++++++++++++---- include/hw/input/hid.h | 4 ++ 3 files changed, 111 insertions(+), 12 deletions(-) diff --git a/hw/input/hid.c b/hw/input/hid.c index 9f26850112..bc84e99dbc 100644 --- a/hw/input/hid.c +++ b/hw/input/hid.c @@ -378,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, pan, l; + int dx, dy, dz, pan, l, mult; int index; HIDPointerEvent *e; @@ -400,8 +400,19 @@ int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len) dx = e->xdx; 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); + mult = 1; + if (hs->kind == HID_MOUSE && hs->protocol == 1 && + hs->ptr.wheel_multiplier) { + mult = HID_WHEEL_RESOLUTION_MULTIPLIER; + } + dz = hid_scroll_counts(&e->dz, &hs->ptr.wheel_residual, mult); + + mult = 1; + if (hs->kind == HID_MOUSE && hs->protocol == 1 && + hs->ptr.pan_multiplier) { + mult = HID_WHEEL_RESOLUTION_MULTIPLIER; + } + pan = hid_scroll_counts(&e->pan, &hs->ptr.pan_residual, mult); if (hs->n && !e->dz && @@ -657,7 +668,8 @@ static bool hid_ptr_wheel_needed(void *opaque) HIDState *s = opaque; int i; - if (s->ptr.wheel_residual || s->ptr.pan_residual) { + if (s->ptr.wheel_multiplier || s->ptr.pan_multiplier || + s->ptr.wheel_residual || s->ptr.pan_residual) { return true; } for (i = 0; i < QUEUE_LENGTH; i++) { @@ -706,6 +718,8 @@ static const VMStateDescription vmstate_hid_ptr_wheel = { .fields = (const VMStateField[]) { VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0, vmstate_hid_ptr_queue_pan, HIDPointerEvent), + VMSTATE_UINT8(ptr.wheel_multiplier, HIDState), + VMSTATE_UINT8(ptr.pan_multiplier, HIDState), 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 804119006b..ffe07ff68b 100644 --- a/hw/usb/dev-hid.c +++ b/hw/usb/dev-hid.c @@ -81,6 +81,8 @@ static const USBDescStrings desc_strings = { * descriptor. * Note: Windows stops recognising the mouse if it does not match * exactly. + * The enhanced wheel support grew the descriptor from 52 to 144 + * bytes. */ static const uint8_t qemu_mouse_hid_report_descriptor[] = { 0x05, 0x01, /* Usage Page (Generic Desktop) */ @@ -119,7 +121,20 @@ static const uint8_t qemu_mouse_hid_report_descriptor[] = { /* Vertical wheel logical collection */ 0xA1, 0x02, /* Collection (Logical) */ + 0x85, 0x02, /* Report ID (0x02) */ + 0x09, 0x48, /* Usage (Resolution Multiplier) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x35, 0x01, /* Physical Minimum (1) */ + 0x45, 0x78, /* Physical Maximum (120) */ + 0x75, 0x02, /* Report Size (2) */ + 0x95, 0x01, /* Report Count (1) */ + 0xB1, 0x02, /* Feature (Data,Var,Abs) */ + + 0x85, 0x01, /* Report ID (0x01) */ 0x09, 0x38, /* Usage (Wheel) */ + 0x35, 0x00, /* Physical Minimum (0) */ + 0x45, 0x00, /* Physical Maximum (0) */ 0x15, 0x81, /* Logical Minimum (-0x7f) */ 0x25, 0x7F, /* Logical Maximum (0x7f) */ 0x75, 0x08, /* Report Size (8) */ @@ -131,6 +146,23 @@ static const uint8_t qemu_mouse_hid_report_descriptor[] = { /* Horizontal wheel logical collection */ 0xA1, 0x02, /* Collection (Logical) */ + 0x85, 0x02, /* Report ID (0x02) */ + 0x09, 0x48, /* Usage (Resolution Multiplier) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x35, 0x01, /* Physical Minimum (1) */ + 0x45, 0x78, /* Physical Maximum (120) */ + 0x75, 0x02, /* Report Size (2) */ + 0x95, 0x01, /* Report Count (1) */ + 0xB1, 0x02, /* Feature (Data,Var,Abs) */ + + 0x35, 0x00, /* Physical Minimum (0) */ + 0x45, 0x00, /* Physical Maximum (0) */ + 0x75, 0x04, /* Report Size (4) */ + 0x95, 0x01, /* Report Count (1) */ + 0xB1, 0x01, /* Feature (Const) */ + + 0x85, 0x01, /* Report ID (0x01) */ 0x05, 0x0C, /* Usage Page (Consumer Devices) */ 0x0A, 0x38, 0x02, /* Usage (AC Pan) */ 0x15, 0x81, /* Logical Minimum (-0x7f) */ @@ -652,17 +684,66 @@ static void usb_hid_handle_control(USBDevice *dev, USBPacket *p, } break; case HID_GET_REPORT: - if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) { - p->actual_length = hid_pointer_poll(hs, data, length); - } else if (hs->kind == HID_KEYBOARD) { - p->actual_length = hid_keyboard_poll(hs, data, length); + { + uint8_t mult; + uint8_t report_type = (value >> 8) & 0xFF; + uint8_t report_id = value & 0xFF; + + if (report_type == 0x03 && report_id == 0x02 && + hs->kind == HID_MOUSE) { + + if (length < 1) { + goto fail; + } + + mult = ((hs->ptr.pan_multiplier & 0x03) << 2) | + (hs->ptr.wheel_multiplier & 0x03); + if (length >= 2) { + data[0] = 0x02; + data[1] = mult; + p->actual_length = 2; + } else { + data[0] = mult; + p->actual_length = 1; + } + + } else if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) { + p->actual_length = hid_pointer_poll(hs, data, length); + } else if (hs->kind == HID_KEYBOARD) { + p->actual_length = hid_keyboard_poll(hs, data, length); + } else { + goto fail; + } } break; case HID_SET_REPORT: - if (hs->kind == HID_KEYBOARD) { - p->actual_length = hid_keyboard_write(hs, data, length); - } else { - goto fail; + { + uint8_t mult; + uint8_t report_type = (value >> 8) & 0xFF; + uint8_t report_id = value & 0xFF; + + if (report_type == 0x03 && report_id == 0x02 && + hs->kind == HID_MOUSE) { + + if (length < 1) { + goto fail; + } + + if (length >= 2 && data[0] == 0x02) { + mult = data[1]; + } else { + mult = data[0]; + } + + hs->ptr.wheel_multiplier = mult & 0x03; + hs->ptr.pan_multiplier = (mult >> 2) & 0x03; + p->actual_length = length; + + } else if (hs->kind == HID_KEYBOARD) { + p->actual_length = hid_keyboard_write(hs, data, length); + } else { + goto fail; + } } break; case HID_GET_PROTOCOL: diff --git a/include/hw/input/hid.h b/include/hw/input/hid.h index b7cdeb54c2..458aa07cae 100644 --- a/include/hw/input/hid.h +++ b/include/hw/input/hid.h @@ -7,6 +7,8 @@ #define HID_TABLET 2 #define HID_KEYBOARD 3 +#define HID_WHEEL_RESOLUTION_MULTIPLIER 120 + typedef struct HIDPointerEvent { int32_t xdx, ydy; /* relative iff it's a mouse, otherwise absolute */ int32_t dz, buttons_state; @@ -23,6 +25,8 @@ typedef void (*HIDEventFunc)(HIDState *s); typedef struct HIDMouseState { HIDPointerEvent queue[QUEUE_LENGTH]; int mouse_grabbed; + uint8_t wheel_multiplier; + uint8_t pan_multiplier; int32_t wheel_residual; int32_t pan_residual; bool wheel_state_loaded; -- 2.56.0
