Skip to contentSkip to conversation

Add horizontal wheel support to mice, where possible

RFC PATCH In review Brad Jorsch 2 participants 8 messages May 4, 2010 – Jun 2, 2010

lore
Share & tools
lore
#0 lore raw

cover Add horizontal wheel support to mice, where possible

The emulated mice should emulate a horizontal wheel when possible. This patch series does that for the USB mouse and tablet and the ExPS/2 mouse. As far as I can tell the vmmouse protocol doesn't handle a horizontal wheel, and I have no idea if emulating a wheel might make sense for any of the other mouse types.

I've tested this using the SDL interface and an Ububtu 10.04 cd image; xev reports the correct events for all three devices, and a gedit window with sufficient text to create scrollbars scrolls correctly with both mice (didn't test the tablet). More testing would be good.

See also Debian bug #579968.[1]

[1] http://bugs.debian.org/579968

#1 lore raw

PATCH 1/5 Add function parameters for hwheel

Add a parameter for the hwheel delta to QEMUPutMouseEntry and kbd_mouse_event, and adjust all users of those to match. At the moment, all calls to kbd_mouse_event will pass 0 for the delta.

---
 cocoa.m             |    6 +++---
 console.h           |    4 ++--
 hw/adb.c            |    3 ++-
 hw/ads7846.c        |    2 +-
 hw/escc.c           |    3 ++-
 hw/msmouse.c        |    2 +-
 hw/ps2.c            |    4 ++--
 hw/syborg_pointer.c |    2 +-
 hw/tsc2005.c        |    2 +-
 hw/tsc210x.c        |    2 +-
 hw/usb-hid.c        |    5 +++--
 hw/usb-wacom.c      |    5 +++--
 hw/vmmouse.c        |    3 ++-
 hw/xenfb.c          |    2 +-
 input.c             |    6 +++---
 monitor.c           |    4 ++--
 sdl.c               |   10 ++++++----
 vnc.c               |    6 +++---
 18 files changed, 39 insertions(+), 32 deletions(-)
cocoa.m +3 -3
index 56c789a..c247833 100644
@@ -47,9 +47,9 @@
 #define cgrect(nsrect) (*(CGRect *)&(nsrect))
 #define COCOA_MOUSE_EVENT \
         if (isTabletEnabled) { \
-            kbd_mouse_event((int)(p.x * 0x7FFF / (screen.width - 1)), (int)((screen.height - p.y) * 0x7FFF / (screen.height - 1)), 0, buttons); \
+            kbd_mouse_event((int)(p.x * 0x7FFF / (screen.width - 1)), (int)((screen.height - p.y) * 0x7FFF / (screen.height - 1)), 0, 0, buttons); \
         } else if (isMouseGrabed) { \
-            kbd_mouse_event((int)[event deltaX], (int)[event deltaY], 0, buttons); \
+            kbd_mouse_event((int)[event deltaX], (int)[event deltaY], 0, 0, buttons); \
         } else { \
             [NSApp sendEvent:event]; \
         }
@@ -649,7 +649,7 @@ static int cocoa_keycode_to_qemu(int keycode)
             break;
         case NSScrollWheel:
             if (isTabletEnabled || isMouseGrabed) {
-                kbd_mouse_event(0, 0, -[event deltaY], 0);
+                kbd_mouse_event(0, 0, -[event deltaY], 0, 0);
             } else {
                 [NSApp sendEvent:event];
             }
console.h +2 -2
index 6def115..7ec693d 100644
@@ -21,7 +21,7 @@
 
 typedef void QEMUPutKBDEvent(void *opaque, int keycode);
 typedef void QEMUPutLEDEvent(void *opaque, int ledstate);
-typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int buttons_state);
+typedef void QEMUPutMouseEvent(void *opaque, int dx, int dy, int dz, int dw, int buttons_state);
 
 typedef struct QEMUPutMouseEntry {
     QEMUPutMouseEvent *qemu_put_mouse_event;
@@ -53,7 +53,7 @@ void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry);
 
 void kbd_put_keycode(int keycode);
 void kbd_put_ledstate(int ledstate);
-void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
+void kbd_mouse_event(int dx, int dy, int dz, int dw, int buttons_state);
 
 /* Does the current mouse generate absolute events */
 int kbd_mouse_is_absolute(void);
hw/adb.c +2 -1
index 4fb7a62..88be567 100644
@@ -318,7 +318,8 @@ typedef struct MouseState {
 } MouseState;
 
 static void adb_mouse_event(void *opaque,
-                            int dx1, int dy1, int dz1, int buttons_state)
+                            int dx1, int dy1, int dz1, int dw1,
+                            int buttons_state)
 {
     ADBDevice *d = opaque;
     MouseState *s = d->opaque;
hw/ads7846.c +1 -1
index 184b3dd..544db09 100644
@@ -86,7 +86,7 @@ static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
 }
 
 static void ads7846_ts_event(void *opaque,
-                int x, int y, int z, int buttons_state)
+                int x, int y, int z, int w, int buttons_state)
 {
     ADS7846State *s = opaque;
 
hw/escc.c +2 -1
index 6d2fd36..c7b420d 100644
@@ -827,7 +827,8 @@ static void handle_kbd_command(ChannelState *s, int val)
 }
 
 static void sunmouse_event(void *opaque,
-                               int dx, int dy, int dz, int buttons_state)
+                               int dx, int dy, int dz, int dw,
+                               int buttons_state)
 {
     ChannelState *s = opaque;
     int ch;
hw/msmouse.c +1 -1
index 05f893c..f1bfd0d 100644
@@ -31,7 +31,7 @@
 #define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
 
 static void msmouse_event(void *opaque,
-                          int dx, int dy, int dz, int buttons_state)
+                          int dx, int dy, int dz, int dw, int buttons_state)
 {
     CharDriverState *chr = (CharDriverState *)opaque;
 
hw/ps2.c +2 -2
index f0b206a..db5605d 100644
@@ -330,7 +330,7 @@ static void ps2_mouse_send_packet(PS2MouseState *s)
 }
 
 static void ps2_mouse_event(void *opaque,
-                            int dx, int dy, int dz, int buttons_state)
+                            int dx, int dy, int dz, int dw, int buttons_state)
 {
     PS2MouseState *s = opaque;
 
@@ -361,7 +361,7 @@ static void ps2_mouse_event(void *opaque,
 
 void ps2_mouse_fake_event(void *opaque)
 {
-    ps2_mouse_event(opaque, 1, 0, 0, 0);
+    ps2_mouse_event(opaque, 1, 0, 0, 0, 0);
 }
 
 void ps2_write_mouse(void *opaque, int val)
hw/syborg_pointer.c +1 -1
index 563d730..04aa55b 100644
@@ -122,7 +122,7 @@ static CPUWriteMemoryFunc * const syborg_pointer_writefn[] = {
    syborg_pointer_write
 };
 
-static void syborg_pointer_event(void *opaque, int dx, int dy, int dz,
+static void syborg_pointer_event(void *opaque, int dx, int dy, int dz, int dw,
                                  int buttons_state)
 {
     SyborgPointerState *s = (SyborgPointerState *)opaque;
hw/tsc2005.c +1 -1
index b75cc86..6bf991d 100644
@@ -412,7 +412,7 @@ static void tsc2005_timer_tick(void *opaque)
 }
 
 static void tsc2005_touchscreen_event(void *opaque,
-                int x, int y, int z, int buttons_state)
+                int x, int y, int z, int dw, int buttons_state)
 {
     TSC2005State *s = opaque;
     int p = s->pressure;
hw/tsc210x.c +1 -1
index e851ca1..c97aaf2 100644
@@ -968,7 +968,7 @@ static void tsc210x_timer_tick(void *opaque)
 }
 
 static void tsc210x_touchscreen_event(void *opaque,
-                int x, int y, int z, int buttons_state)
+                int x, int y, int z, int w, int buttons_state)
 {
     TSC210xState *s = opaque;
     int p = s->pressure;
hw/usb-hid.c +3 -2
index 476fcfd..deb4731 100644
@@ -414,7 +414,8 @@ static void usb_hid_changed(USBHIDState *hs)
 }
 
 static void usb_mouse_event(void *opaque,
-                            int dx1, int dy1, int dz1, int buttons_state)
+                            int dx1, int dy1, int dz1, int dw1,
+                            int buttons_state)
 {
     USBHIDState *hs = opaque;
     USBMouseState *s = &hs->ptr;
@@ -428,7 +429,7 @@ static void usb_mouse_event(void *opaque,
 }
 
 static void usb_tablet_event(void *opaque,
-			     int x, int y, int dz, int buttons_state)
+			     int x, int y, int dz, int dw, int buttons_state)
 {
     USBHIDState *hs = opaque;
     USBMouseState *s = &hs->ptr;
hw/usb-wacom.c +3 -2
index fe052eb..b14e58b 100644
@@ -119,7 +119,8 @@ static const uint8_t qemu_wacom_config_descriptor[] = {
 };
 
 static void usb_mouse_event(void *opaque,
-                            int dx1, int dy1, int dz1, int buttons_state)
+                            int dx1, int dy1, int dz1, int dw1,
+                            int buttons_state)
 {
     USBWacomState *s = opaque;
 
@@ -131,7 +132,7 @@ static void usb_mouse_event(void *opaque,
 }
 
 static void usb_wacom_event(void *opaque,
-                            int x, int y, int dz, int buttons_state)
+                            int x, int y, int dz, int dw, int buttons_state)
 {
     USBWacomState *s = opaque;
 
hw/vmmouse.c +2 -1
index bb6e605..b619170 100644
@@ -67,7 +67,8 @@ static uint32_t vmmouse_get_status(VMMouseState *s)
     return (s->status << 16) | s->nb_queue;
 }
 
-static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
+static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int dw,
+                                int buttons_state)
 {
     VMMouseState *s = opaque;
     int buttons = 0;
hw/xenfb.c +1 -1
index 422cd53..0d83810 100644
@@ -320,7 +320,7 @@ static void xenfb_key_event(void *opaque, int scancode)
  * the button state.
  */
 static void xenfb_mouse_event(void *opaque,
-			      int dx, int dy, int dz, int button_state)
+			      int dx, int dy, int dz, int dz2, int button_state)
 {
     struct XenInput *xenfb = opaque;
     int dw = ds_get_width(xenfb->c.ds);
input.c +3 -3
index 8f0941e..ea7c246 100644
@@ -137,7 +137,7 @@ void kbd_put_ledstate(int ledstate)
     }
 }
 
-void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
+void kbd_mouse_event(int dx, int dy, int dz, int dw, int buttons_state)
 {
     QEMUPutMouseEntry *entry;
     QEMUPutMouseEvent *mouse_event;
@@ -160,10 +160,10 @@ void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
             else
                 width = graphic_width - 1;
             mouse_event(mouse_event_opaque,
-                        width - dy, dx, dz, buttons_state);
+                        width - dy, dx, dz, dw, buttons_state);
         } else
             mouse_event(mouse_event_opaque,
-                        dx, dy, dz, buttons_state);
+                        dx, dy, dz, dw, buttons_state);
     }
 }
 
monitor.c +2 -2
index 46d0b47..520d48d 100644
@@ -1841,14 +1841,14 @@ static void do_mouse_move(Monitor *mon, const QDict *qdict)
     dz = 0;
     if (dz_str)
         dz = strtol(dz_str, NULL, 0);
-    kbd_mouse_event(dx, dy, dz, mouse_button_state);
+    kbd_mouse_event(dx, dy, dz, 0, mouse_button_state);
 }
 
 static void do_mouse_button(Monitor *mon, const QDict *qdict)
 {
     int button_state = qdict_get_int(qdict, "button_state");
     mouse_button_state = button_state;
-    kbd_mouse_event(0, 0, 0, mouse_button_state);
+    kbd_mouse_event(0, 0, 0, 0, mouse_button_state);
 }
 
 static void do_ioport_read(Monitor *mon, const QDict *qdict)
sdl.c +6 -4
index 16a48e9..616a7eb 100644
@@ -503,7 +503,8 @@ static void sdl_mouse_mode_change(Notifier *notify)
     }
 }
 
-static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
+static void sdl_send_mouse_event(int dx, int dy, int dz, int dw,
+                                 int x, int y, int state)
 {
     int buttons;
     buttons = 0;
@@ -526,7 +527,7 @@ static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state
         dy = y;
     }
 
-    kbd_mouse_event(dx, dy, dz, buttons);
+    kbd_mouse_event(dx, dy, dz, dw, buttons);
 }
 
 static void toggle_full_screen(DisplayState *ds)
@@ -684,7 +685,7 @@ static void sdl_refresh(DisplayState *ds)
         case SDL_MOUSEMOTION:
             if (gui_grab || kbd_mouse_is_absolute() ||
                 absolute_enabled) {
-                sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
+                sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0, 0,
                        ev->motion.x, ev->motion.y, ev->motion.state);
             }
             break;
@@ -713,7 +714,8 @@ static void sdl_refresh(DisplayState *ds)
                         dz = 1;
                     }
 #endif
-                    sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
+                    sdl_send_mouse_event(0, 0, dz, 0, bev->x, bev->y,
+                                         buttonstate);
                 }
             }
             break;
vnc.c +3 -3
index 5241a6a..332c14a 100644
@@ -1292,17 +1292,17 @@ static void pointer_event(VncState *vs, int button_mask, int x, int y)
                           x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000,
                         ds_get_height(vs->ds) > 1 ?
                           y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000,
-                        dz, buttons);
+                        dz, 0, buttons);
     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
         x -= 0x7FFF;
         y -= 0x7FFF;
 
-        kbd_mouse_event(x, y, dz, buttons);
+        kbd_mouse_event(x, y, dz, 0, buttons);
     } else {
         if (vs->last_x != -1)
             kbd_mouse_event(x - vs->last_x,
                             y - vs->last_y,
-                            dz, buttons);
+                            dz, 0, buttons);
         vs->last_x = x;
         vs->last_y = y;
     }
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/5 Add hwheel to monitor mouse_move

In reply to Brad Jorsch

Adds a parameter to the monitor's mouse_move command to specify the hwheel delta.

---
 monitor.c       |    8 ++++++--
 qemu-monitor.hx |    4 ++--
 2 files changed, 8 insertions(+), 4 deletions(-)
monitor.c +6 -2
index 520d48d..baff2cf 100644
@@ -1832,16 +1832,20 @@ static int mouse_button_state;
 
 static void do_mouse_move(Monitor *mon, const QDict *qdict)
 {
-    int dx, dy, dz;
+    int dx, dy, dz, dw;
     const char *dx_str = qdict_get_str(qdict, "dx_str");
     const char *dy_str = qdict_get_str(qdict, "dy_str");
     const char *dz_str = qdict_get_try_str(qdict, "dz_str");
+    const char *dw_str = qdict_get_try_str(qdict, "dw_str");
     dx = strtol(dx_str, NULL, 0);
     dy = strtol(dy_str, NULL, 0);
     dz = 0;
+    dw = 0;
     if (dz_str)
         dz = strtol(dz_str, NULL, 0);
-    kbd_mouse_event(dx, dy, dz, 0, mouse_button_state);
+    if (dw_str)
+        dw = strtol(dw_str, NULL, 0);
+    kbd_mouse_event(dx, dy, dz, dw, mouse_button_state);
 }
 
 static void do_mouse_button(Monitor *mon, const QDict *qdict)
qemu-monitor.hx +2 -2
index 5ea5748..00067ba 100644
@@ -616,8 +616,8 @@ ETEXI
 
     {
         .name       = "mouse_move",
-        .args_type  = "dx_str:s,dy_str:s,dz_str:s?",
-        .params     = "dx dy [dz]",
+        .args_type  = "dx_str:s,dy_str:s,dz_str:s?,dw_str:s?",
+        .params     = "dx dy [dz [dw]]",
         .help       = "send mouse move events",
         .mhandler.cmd = do_mouse_move,
     },
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/5 Pass hwheel events from the front-ends

In reply to Brad Jorsch

SDL seems to report hwheel events as SDL_BUTTON_X1 and SDL_BUTTON_X2. VNC I am guessing is similar, and online docs indicate that Cocoa reports hwheel deltas in deltaX for NSScrollWheel.

---
 cocoa.m |    2 +-
 sdl.c   |   12 ++++++++++--
 vnc.c   |   11 ++++++++---
 3 files changed, 19 insertions(+), 6 deletions(-)
cocoa.m +1 -1
index c247833..afc1b5f 100644
@@ -649,7 +649,7 @@ static int cocoa_keycode_to_qemu(int keycode)
             break;
         case NSScrollWheel:
             if (isTabletEnabled || isMouseGrabed) {
-                kbd_mouse_event(0, 0, -[event deltaY], 0, 0);
+                kbd_mouse_event(0, 0, -[event deltaY], -[event deltaX], 0);
             } else {
                 [NSApp sendEvent:event];
             }
sdl.c +10 -2
index 616a7eb..f6fabf1 100644
@@ -700,8 +700,9 @@ static void sdl_refresh(DisplayState *ds)
                         sdl_grab_start();
                     }
                 } else {
-                    int dz;
+                    int dz, dw;
                     dz = 0;
+                    dw = 0;
                     if (ev->type == SDL_MOUSEBUTTONDOWN) {
                         buttonstate |= SDL_BUTTON(bev->button);
                     } else {
@@ -714,7 +715,14 @@ static void sdl_refresh(DisplayState *ds)
                         dz = 1;
                     }
 #endif
-                    sdl_send_mouse_event(0, 0, dz, 0, bev->x, bev->y,
+#ifdef SDL_BUTTON_X1
+                    if (bev->button == SDL_BUTTON_X1 && ev->type == SDL_MOUSEBUTTONDOWN) {
+                        dw = -1;
+                    } else if (bev->button == SDL_BUTTON_X2 && ev->type == SDL_MOUSEBUTTONDOWN) {
+                        dw = 1;
+                    }
+#endif
+                    sdl_send_mouse_event(0, 0, dz, dw, bev->x, bev->y,
                                          buttonstate);
                 }
             }
vnc.c +8 -3
index 332c14a..c193963 100644
@@ -1275,6 +1275,7 @@ static void pointer_event(VncState *vs, int button_mask, int x, int y)
 {
     int buttons = 0;
     int dz = 0;
+    int dw = 0;
 
     if (button_mask & 0x01)
         buttons |= MOUSE_EVENT_LBUTTON;
@@ -1286,23 +1287,27 @@ static void pointer_event(VncState *vs, int button_mask, int x, int y)
         dz = -1;
     if (button_mask & 0x10)
         dz = 1;
+    if (button_mask & 0x20)
+        dw = -1;
+    if (button_mask & 0x40)
+        dw = 1;
 
     if (vs->absolute) {
         kbd_mouse_event(ds_get_width(vs->ds) > 1 ?
                           x * 0x7FFF / (ds_get_width(vs->ds) - 1) : 0x4000,
                         ds_get_height(vs->ds) > 1 ?
                           y * 0x7FFF / (ds_get_height(vs->ds) - 1) : 0x4000,
-                        dz, 0, buttons);
+                        dz, dw, buttons);
     } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) {
         x -= 0x7FFF;
         y -= 0x7FFF;
 
-        kbd_mouse_event(x, y, dz, 0, buttons);
+        kbd_mouse_event(x, y, dz, dw, buttons);
     } else {
         if (vs->last_x != -1)
             kbd_mouse_event(x - vs->last_x,
                             y - vs->last_y,
-                            dz, 0, buttons);
+                            dz, dw, buttons);
         vs->last_x = x;
         vs->last_y = y;
     }
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/5 Add a horizontal wheel to the USB mouse and tablet

In reply to Brad Jorsch

Adjust the USB report descriptors to indicate that the mouse and tablet have horizontal wheels, and then report the delta when polled.

---
 hw/usb-hid.c |   44 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 36 insertions(+), 8 deletions(-)
hw/usb-hid.c +36 -8
index deb4731..15cc2a1 100644
@@ -44,7 +44,7 @@
 #define USB_KEYBOARD  3
 
 typedef struct USBMouseState {
-    int dx, dy, dz, buttons_state;
+    int dx, dy, dz, dw, buttons_state;
     int x, y;
     int mouse_grabbed;
     QEMUPutMouseEntry *eh_entry;
@@ -137,14 +137,14 @@ static const uint8_t qemu_mouse_config_descriptor[] = {
         0x00,        /*  u8 country_code */
         0x01,        /*  u8 num_descriptors */
         0x22,        /*  u8 type; Report */
-        52, 0,       /*  u16 len */
+        67, 0,       /*  u16 len */
 
 	/* one endpoint (status change endpoint) */
 	0x07,       /*  u8  ep_bLength; */
 	0x05,       /*  u8  ep_bDescriptorType; Endpoint */
 	0x81,       /*  u8  ep_bEndpointAddress; IN Endpoint 1 */
  	0x03,       /*  u8  ep_bmAttributes; Interrupt */
- 	0x04, 0x00, /*  u16 ep_wMaxPacketSize; */
+ 	0x05, 0x00, /*  u16 ep_wMaxPacketSize; */
 	0x0a,       /*  u8  ep_bInterval; (255ms -- usb 2.0 spec) */
 };
 
@@ -192,14 +192,14 @@ static const uint8_t qemu_tablet_config_descriptor[] = {
         0x00,        /*  u8 country_code */
         0x01,        /*  u8 num_descriptors */
         0x22,        /*  u8 type; Report */
-        74, 0,       /*  u16 len */
+        93, 0,       /*  u16 len */
 
 	/* one endpoint (status change endpoint) */
 	0x07,       /*  u8  ep_bLength; */
 	0x05,       /*  u8  ep_bDescriptorType; Endpoint */
 	0x81,       /*  u8  ep_bEndpointAddress; IN Endpoint 1 */
  	0x03,       /*  u8  ep_bmAttributes; Interrupt */
- 	0x08, 0x00, /*  u16 ep_wMaxPacketSize; */
+ 	0x09, 0x00, /*  u16 ep_wMaxPacketSize; */
 	0x0a,       /*  u8  ep_bInterval; (255ms -- usb 2.0 spec) */
 };
 
@@ -284,6 +284,13 @@ static const uint8_t qemu_mouse_hid_report_descriptor[] = {
     0x75, 0x08,		/*     Report Size (8) */
     0x95, 0x03,		/*     Report Count (3) */
     0x81, 0x06,		/*     Input (Data, Variable, Relative) */
+    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, Variable, Relative) */
     0xc0,		/*   End Collection */
     0xc0,		/* End Collection */
 };
@@ -324,6 +331,15 @@ static const uint8_t qemu_tablet_hid_report_descriptor[] = {
     0x75, 0x08,		/*     Report Size (8) */
     0x95, 0x01,		/*     Report Count (1) */
     0x81, 0x06,		/*     Input (Data, Variable, Relative) */
+    0x05, 0x0c,		/*     Usage Page (Consumer Devices) */
+    0x0a, 0x38, 0x02,	/*     Usage (AC Pan) */
+    0x15, 0x81,		/*     Logical Minimum (-0x7f) */
+    0x25, 0x7f,		/*     Logical Maximum (0x7f) */
+    0x35, 0x00,		/*     Physical Minimum (same as logical) */
+    0x45, 0x00,		/*     Physical Maximum (same as logical) */
+    0x75, 0x08,		/*     Report Size (8) */
+    0x95, 0x01,		/*     Report Count (1) */
+    0x81, 0x06,		/*     Input (Data, Variable, Relative) */
     0xc0,		/*   End Collection */
     0xc0,		/* End Collection */
 };
@@ -423,6 +439,7 @@ static void usb_mouse_event(void *opaque,
     s->dx += dx1;
     s->dy += dy1;
     s->dz += dz1;
+    s->dw += dw1;
     s->buttons_state = buttons_state;
 
     usb_hid_changed(hs);
@@ -437,6 +454,7 @@ static void usb_tablet_event(void *opaque,
     s->x = x;
     s->y = y;
     s->dz += dz;
+    s->dw += dw;
     s->buttons_state = buttons_state;
 
     usb_hid_changed(hs);
@@ -508,7 +526,7 @@ static inline int int_clamp(int val, int vmin, int vmax)
 
 static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len)
 {
-    int dx, dy, dz, b, l;
+    int dx, dy, dz, dw, b, l;
     USBMouseState *s = &hs->ptr;
 
     if (!s->mouse_grabbed) {
@@ -519,10 +537,12 @@ static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len)
     dx = int_clamp(s->dx, -127, 127);
     dy = int_clamp(s->dy, -127, 127);
     dz = int_clamp(s->dz, -127, 127);
+    dw = int_clamp(s->dw, -127, 127);
 
     s->dx -= dx;
     s->dy -= dy;
     s->dz -= dz;
+    s->dw -= dw;
 
     /* Appears we have to invert the wheel direction */
     dz = 0 - dz;
@@ -544,12 +564,15 @@ static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len)
         buf[l ++] = dy;
     if (len > l)
         buf[l ++] = dz;
+    if (len > l)
+        buf[l ++] = dw;
+
     return l;
 }
 
 static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len)
 {
-    int dz, b, l;
+    int dz, dw, b, l;
     USBMouseState *s = &hs->ptr;
 
     if (!s->mouse_grabbed) {
@@ -560,6 +583,9 @@ static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len)
     dz = int_clamp(s->dz, -127, 127);
     s->dz -= dz;
 
+    dw = int_clamp(s->dw, -127, 127);
+    s->dw -= dw;
+
     /* Appears we have to invert the wheel direction */
     dz = 0 - dz;
     b = 0;
@@ -576,7 +602,8 @@ static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len)
     buf[3] = s->y & 0xff;
     buf[4] = s->y >> 8;
     buf[5] = dz;
-    l = 6;
+    buf[6] = dw;
+    l = 7;
 
     return l;
 }
@@ -624,6 +651,7 @@ static void usb_mouse_handle_reset(USBDevice *dev)
     s->ptr.dx = 0;
     s->ptr.dy = 0;
     s->ptr.dz = 0;
+    s->ptr.dw = 0;
     s->ptr.x = 0;
     s->ptr.y = 0;
     s->ptr.buttons_state = 0;
Signature · 1 line
1.7.1
#5 lore raw

PATCH 5/5 Add a horizontal wheel to the exps/2 mouse

In reply to Brad Jorsch

Have the emulated mouse report horizontal wheel events when in exps/2 mode.

---
 hw/ps2.c |   56 +++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 43 insertions(+), 13 deletions(-)
hw/ps2.c +43 -13
index db5605d..09e4365 100644
@@ -105,7 +105,9 @@ typedef struct {
     int mouse_dx; /* current values, needed for 'poll' mode */
     int mouse_dy;
     int mouse_dz;
+    int mouse_dw;
     uint8_t mouse_buttons;
+    uint8_t mouse_buttons_changed;
 } PS2MouseState;
 
 /* Table to convert from PC scancodes to raw scancodes.  */
@@ -284,11 +286,12 @@ void ps2_keyboard_set_translation(void *opaque, int mode)
 static void ps2_mouse_send_packet(PS2MouseState *s)
 {
     unsigned int b;
-    int dx1, dy1, dz1;
+    int dx1, dy1, dz1, dw1;
 
     dx1 = s->mouse_dx;
     dy1 = s->mouse_dy;
     dz1 = s->mouse_dz;
+    dw1 = s->mouse_dw;
     /* XXX: increase range to 8 bits ? */
     if (dx1 > 127)
         dx1 = 127;
@@ -299,12 +302,17 @@ static void ps2_mouse_send_packet(PS2MouseState *s)
     else if (dy1 < -127)
         dy1 = -127;
     b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
+    s->mouse_buttons_changed &= ~0x07;
     ps2_queue(&s->common, b);
     ps2_queue(&s->common, dx1 & 0xff);
     ps2_queue(&s->common, dy1 & 0xff);
     /* extra byte for IMPS/2 or IMEX */
     switch(s->mouse_type) {
     default:
+        /* Just ignore the wheels if not supported */
+        s->mouse_dz = 0;
+        s->mouse_dw = 0;
+        s->mouse_buttons_changed &= 0x07;
         break;
     case 3:
         if (dz1 > 127)
@@ -312,13 +320,29 @@ static void ps2_mouse_send_packet(PS2MouseState *s)
         else if (dz1 < -127)
                 dz1 = -127;
         ps2_queue(&s->common, dz1 & 0xff);
+        s->mouse_dz -= dz1;
+        s->mouse_dw = 0;
+        s->mouse_buttons_changed &= 0x07;
         break;
     case 4:
-        if (dz1 > 7)
-            dz1 = 7;
-        else if (dz1 < -7)
-            dz1 = -7;
-        b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
+        /* This matches what the Linux kernel expects for exps/2 in
+         * drivers/input/mouse/psmouse-base.c. */
+        if (dw1 != 0) {
+            if (dw1 > 31)
+                dw1 = 31;
+            else if (dw1 < -31)
+                dw1 = -31;
+            b = (dw1 & 0x3f) | 0x40;
+            s->mouse_dw -= dw1;
+        } else {
+            if (dz1 > 7)
+                dz1 = 7;
+            else if (dz1 < -7)
+                dz1 = -7;
+            b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
+            s->mouse_dz -= dz1;
+            s->mouse_buttons_changed &= 0x07;
+        }
         ps2_queue(&s->common, b);
         break;
     }
@@ -326,7 +350,6 @@ static void ps2_mouse_send_packet(PS2MouseState *s)
     /* update deltas */
     s->mouse_dx -= dx1;
     s->mouse_dy -= dy1;
-    s->mouse_dz -= dz1;
 }
 
 static void ps2_mouse_event(void *opaque,
@@ -341,11 +364,13 @@ static void ps2_mouse_event(void *opaque,
     s->mouse_dx += dx;
     s->mouse_dy -= dy;
     s->mouse_dz += dz;
+    s->mouse_dw -= dw;
+    s->mouse_buttons_changed |= s->mouse_buttons ^ buttons_state;
+    s->mouse_buttons = buttons_state;
     /* XXX: SDL sometimes generates nul events: we delete them */
     if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 &&
-        s->mouse_buttons == buttons_state)
+        s->mouse_dw == 0 && s->mouse_buttons_changed == 0)
 	return;
-    s->mouse_buttons = buttons_state;
 
     if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
         (s->common.queue.count < (PS2_QUEUE_SIZE - 16))) {
@@ -353,7 +378,8 @@ static void ps2_mouse_event(void *opaque,
             /* if not remote, send event. Multiple events are sent if
                too big deltas */
             ps2_mouse_send_packet(s);
-            if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
+            if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 &&
+                s->mouse_dw == 0 && s->mouse_buttons_changed == 0)
                 break;
         }
     }
@@ -525,7 +551,9 @@ static void ps2_mouse_reset(void *opaque)
     s->mouse_dx = 0;
     s->mouse_dy = 0;
     s->mouse_dz = 0;
+    s->mouse_dw = 0;
     s->mouse_buttons = 0;
+    s->mouse_buttons_changed = 0;
 }
 
 static const VMStateDescription vmstate_ps2_common = {
@@ -569,9 +597,9 @@ static const VMStateDescription vmstate_ps2_keyboard = {
 
 static const VMStateDescription vmstate_ps2_mouse = {
     .name = "ps2mouse",
-    .version_id = 2,
-    .minimum_version_id = 2,
-    .minimum_version_id_old = 2,
+    .version_id = 3,
+    .minimum_version_id = 3,
+    .minimum_version_id_old = 3,
     .fields      = (VMStateField []) {
         VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
         VMSTATE_UINT8(mouse_status, PS2MouseState),
@@ -583,7 +611,9 @@ static const VMStateDescription vmstate_ps2_mouse = {
         VMSTATE_INT32(mouse_dx, PS2MouseState),
         VMSTATE_INT32(mouse_dy, PS2MouseState),
         VMSTATE_INT32(mouse_dz, PS2MouseState),
+        VMSTATE_INT32(mouse_dw, PS2MouseState),
         VMSTATE_UINT8(mouse_buttons, PS2MouseState),
+        VMSTATE_UINT8(mouse_buttons_changed, PS2MouseState),
         VMSTATE_END_OF_LIST()
     }
 };
Signature · 1 line
1.7.1
#6 lore raw
In reply to Brad Jorsch
Quoted 6 lines · Brad Jorsch

The emulated mice should emulate a horizontal wheel when possible. This patch series does that for the USB mouse and tablet and the ExPS/2 mouse. As far as I can tell the vmmouse protocol doesn't handle a horizontal wheel, and I have no idea if emulating a wheel might make sense for any of the other mouse types.

At this point, are we just talking about adding additional mouse buttons?

I think instead of adding an additional parameter for horizontal wheel, we should look at making the API capable of accepting/generating arbitrary button presses.

Really, we should just drop dz and treat vertical wheel as two button presses within button_state. Likewise, horizontal wheel should just be two additional bits within button_state.

Regards,

Anthony Liguori

Quoted 12 lines

I've tested this using the SDL interface and an Ububtu 10.04 cd image; xev reports the correct events for all three devices, and a gedit window with sufficient text to create scrollbars scrolls correctly with both mice (didn't test the tablet). More testing would be good.

See also Debian bug #579968.[1]

[1] http://bugs.debian.org/579968

#7 lore raw
Quoted 8 lines · Anthony Liguori

I think instead of adding an additional parameter for horizontal wheel, we should look at making the API capable of accepting/generating arbitrary button presses.

Really, we should just drop dz and treat vertical wheel as two button presses within button_state. Likewise, horizontal wheel should just be two additional bits within button_state.

Wheel handling in cocoa.m and in monitor.c may supply a delta greater than 1 for the mouse wheels. Those front-ends would have to translate those deltas into a series of press+release events. Then the emulated devices (at least the PS/2 and USB devices) would have to sum the designated button presses for outputting again as wheel data.

If that's the way we want to go, I can rework the patch easily enough.