diff mbox

cocoa: stop using MOUSE_EVENT_*

Message ID 1484132292-21458-1-git-send-email-kraxel@redhat.com
State New
Headers show

Commit Message

Gerd Hoffmann Jan. 11, 2017, 10:58 a.m. UTC
No need to go the indirect route with a bitfield and mapping the bits to
INPUT_BUTTON_*.  We can use a bool array and INPUT_BUTTON_* directly
instead.

Untested, not even compiled, due to lack of a osx^Wmacos machine.
Test results are very welcome.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 ui/cocoa.m | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

Comments

Peter Maydell Jan. 16, 2017, 6:35 p.m. UTC | #1
On 11 January 2017 at 10:58, Gerd Hoffmann <kraxel@redhat.com> wrote:
> No need to go the indirect route with a bitfield and mapping the bits to
> INPUT_BUTTON_*.  We can use a bool array and INPUT_BUTTON_* directly
> instead.
>
> Untested, not even compiled, due to lack of a osx^Wmacos machine.
> Test results are very welcome.

So what does the patch gain us? We'll now loop round
that button array for every event rather than only
the ones where the button state changed, which I
don't suppose is a big deal but does somewhat incline
me towards "just leave the code the way it is"...

thanks
-- PMM
Gerd Hoffmann Jan. 17, 2017, 8:20 a.m. UTC | #2
On Mo, 2017-01-16 at 18:35 +0000, Peter Maydell wrote:
> On 11 January 2017 at 10:58, Gerd Hoffmann <kraxel@redhat.com> wrote:
> > No need to go the indirect route with a bitfield and mapping the bits to
> > INPUT_BUTTON_*.  We can use a bool array and INPUT_BUTTON_* directly
> > instead.
> >
> > Untested, not even compiled, due to lack of a osx^Wmacos machine.
> > Test results are very welcome.
> 
> So what does the patch gain us?

I can drop MOUSE_EVENT_WHEEL* without breaking the osx build.

> We'll now loop round
> that button array for every event rather than only
> the ones where the button state changed,

Well, we loop round anyway, you just don't see it directly b/c it is
hidden in the qemu_input_update_buttons() helper function.

cheers,
  Gerd
Peter Maydell Jan. 17, 2017, 10:44 a.m. UTC | #3
On 17 January 2017 at 08:20, Gerd Hoffmann <kraxel@redhat.com> wrote:
> On Mo, 2017-01-16 at 18:35 +0000, Peter Maydell wrote:
>> On 11 January 2017 at 10:58, Gerd Hoffmann <kraxel@redhat.com> wrote:
>> > No need to go the indirect route with a bitfield and mapping the bits to
>> > INPUT_BUTTON_*.  We can use a bool array and INPUT_BUTTON_* directly
>> > instead.
>> >
>> > Untested, not even compiled, due to lack of a osx^Wmacos machine.
>> > Test results are very welcome.
>>
>> So what does the patch gain us?
>
> I can drop MOUSE_EVENT_WHEEL* without breaking the osx build.

Why do you want to drop it, though? It's a valid mouse event
type, and you're not going to drop the whole MOUSE_EVENT_* set
of defines, I assume, because they're used in lots of devices.

>> We'll now loop round
>> that button array for every event rather than only
>> the ones where the button state changed,
>
> Well, we loop round anyway, you just don't see it directly b/c it is
> hidden in the qemu_input_update_buttons() helper function.

No, we only call qemu_input_update_buttons() if last_buttons != buttons.

thanks
-- PMM
Gerd Hoffmann Jan. 17, 2017, 3:20 p.m. UTC | #4
On Di, 2017-01-17 at 10:44 +0000, Peter Maydell wrote:
> On 17 January 2017 at 08:20, Gerd Hoffmann <kraxel@redhat.com> wrote:
> > On Mo, 2017-01-16 at 18:35 +0000, Peter Maydell wrote:
> >> On 11 January 2017 at 10:58, Gerd Hoffmann <kraxel@redhat.com> wrote:
> >> > No need to go the indirect route with a bitfield and mapping the bits to
> >> > INPUT_BUTTON_*.  We can use a bool array and INPUT_BUTTON_* directly
> >> > instead.
> >> >
> >> > Untested, not even compiled, due to lack of a osx^Wmacos machine.
> >> > Test results are very welcome.
> >>
> >> So what does the patch gain us?
> >
> > I can drop MOUSE_EVENT_WHEEL* without breaking the osx build.
> 
> Why do you want to drop it, though? It's a valid mouse event
> type, and you're not going to drop the whole MOUSE_EVENT_* set
> of defines, I assume, because they're used in lots of devices.

Dunno where the MOUSE_EVENT_* #defines originally come from, I suspect
from old ps2 mouse protocols, for historical reasons, simliar to the
numeric key codes we have in qemu which are pc scancodes too.

So, yes, I considered dropping them all and use hardware-specific
#defines instead.  But it seems at least for the left/right/middle
buttons most (all?) hardware agrees on which bit is which.  So the
attempt to drop the button defines looks like pointless churn.

For the wheel that isn't the case though, that is either not present at
all (older devices) or implemented in different ways (virtual buttons,
wheel axis, ...).

cheers,
  Gerd
diff mbox

Patch

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 26d4a1c..599b899 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -66,7 +66,7 @@  typedef struct {
 
 NSWindow *normalWindow, *about_window;
 static DisplayChangeListener *dcl;
-static int last_buttons;
+static bool last_buttons[INPUT_BUTTON__MAX];
 
 int gArgc;
 char **gArgv;
@@ -511,7 +511,9 @@  QemuCocoaView *cocoaView;
 {
     COCOA_DEBUG("QemuCocoaView: handleEvent\n");
 
-    int buttons = 0;
+    bool buttons[INPUT_BUTTON__MAX] = {
+        [ 0 ... (INPUT_BUTTON__MAX-1) ] = false;
+    };
     int keycode;
     bool mouse_event = false;
     NSPoint p = [event locationInWindow];
@@ -638,34 +640,34 @@  QemuCocoaView *cocoaView;
             break;
         case NSLeftMouseDown:
             if ([event modifierFlags] & NSCommandKeyMask) {
-                buttons |= MOUSE_EVENT_RBUTTON;
+                buttons[INPUT_BUTTON_RIGHT] = true;
             } else {
-                buttons |= MOUSE_EVENT_LBUTTON;
+                buttons[INPUT_BUTTON_LEFT] = true;
             }
             mouse_event = true;
             break;
         case NSRightMouseDown:
-            buttons |= MOUSE_EVENT_RBUTTON;
+            buttons[INPUT_BUTTON_RIGHT] = true;
             mouse_event = true;
             break;
         case NSOtherMouseDown:
-            buttons |= MOUSE_EVENT_MBUTTON;
+            buttons[INPUT_BUTTON_MIDDLE] = true;
             mouse_event = true;
             break;
         case NSLeftMouseDragged:
             if ([event modifierFlags] & NSCommandKeyMask) {
-                buttons |= MOUSE_EVENT_RBUTTON;
+                buttons[INPUT_BUTTON_RIGHT] = true;
             } else {
-                buttons |= MOUSE_EVENT_LBUTTON;
+                buttons[INPUT_BUTTON_LEFT] = true;
             }
             mouse_event = true;
             break;
         case NSRightMouseDragged:
-            buttons |= MOUSE_EVENT_RBUTTON;
+            buttons[INPUT_BUTTON_RIGHT] = true;
             mouse_event = true;
             break;
         case NSOtherMouseDragged:
-            buttons |= MOUSE_EVENT_MBUTTON;
+            buttons[INPUT_BUTTON_MIDDLE] = true;
             mouse_event = true;
             break;
         case NSLeftMouseUp:
@@ -684,8 +686,11 @@  QemuCocoaView *cocoaView;
             break;
         case NSScrollWheel:
             if (isMouseGrabbed) {
-                buttons |= ([event deltaY] < 0) ?
-                    MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
+                if ([event deltaY] < 0) {
+                    buttons[INPUT_BUTTON_WHEEL_UP] = true;
+                } else {
+                    buttons[INPUT_BUTTON_WHEEL_DOWN] = true;
+                }
             }
             mouse_event = true;
             break;
@@ -703,15 +708,14 @@  QemuCocoaView *cocoaView;
          */
         if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
             (last_buttons != buttons)) {
-            static uint32_t bmap[INPUT_BUTTON__MAX] = {
-                [INPUT_BUTTON_LEFT]       = MOUSE_EVENT_LBUTTON,
-                [INPUT_BUTTON_MIDDLE]     = MOUSE_EVENT_MBUTTON,
-                [INPUT_BUTTON_RIGHT]      = MOUSE_EVENT_RBUTTON,
-                [INPUT_BUTTON_WHEEL_UP]   = MOUSE_EVENT_WHEELUP,
-                [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
-            };
-            qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
-            last_buttons = buttons;
+            InputButton btn;
+
+            for (btn = 0; btn < INPUT_BUTTON__MAX; btn++) {
+                if (last_button[btn] != button[btn]) {
+                    qemu_input_queue_btn(dcl->con, btn, button[btn]);
+                    last_button[btn] = button[btn];
+                }
+            }
         }
         if (isMouseGrabbed) {
             if (isAbsoluteEnabled) {