[PULL 03/12] ui/cocoa: Split [-QemuCocoaView handleEventLocked:]

Philippe Mathieu-Daudé posted 12 patches 8 months, 3 weeks ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Akihiko Odaki <akihiko.odaki@daynix.com>
[PULL 03/12] ui/cocoa: Split [-QemuCocoaView handleEventLocked:]
Posted by Philippe Mathieu-Daudé 8 months, 3 weeks ago
From: Akihiko Odaki <akihiko.odaki@daynix.com>

Currently [-QemuCocoaView handleEventLocked:] parses the passed event,
stores operations to be done to variables, and perform them according
to the variables. This construct will be cluttered with variables and
hard to read when we need more different operations for different
events.

Split the methods so that we can call appropriate methods depending on
events instead of relying on variables.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Tested-by: Rene Engel <ReneEngel80@emailn.de>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-ID: <20240224-cocoa-v12-1-e89f70bdda71@daynix.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 ui/cocoa.m | 86 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 51 insertions(+), 35 deletions(-)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 5618d294c4..aecd60df2a 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -1102,45 +1102,61 @@ - (bool) handleEventLocked:(NSEvent *)event
     }
 
     if (mouse_event) {
-        /* Don't send button events to the guest unless we've got a
-         * mouse grab or window focus. If we have neither then this event
-         * is the user clicking on the background window to activate and
-         * bring us to the front, which will be done by the sendEvent
-         * call below. We definitely don't want to pass that click through
-         * to the guest.
-         */
-        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
-            };
-            qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons);
-            last_buttons = buttons;
-        }
-        if (isMouseGrabbed) {
-            if (isAbsoluteEnabled) {
-                /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
-                 * The check on screenContainsPoint is to avoid sending out of range values for
-                 * clicks in the titlebar.
-                 */
-                if ([self screenContainsPoint:p]) {
-                    qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width);
-                    qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
-                }
-            } else {
-                qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]);
-                qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]);
-            }
-        } else {
-            return false;
-        }
-        qemu_input_event_sync();
+        return [self handleMouseEvent:event buttons:buttons];
     }
     return true;
 }
 
+- (bool) handleMouseEvent:(NSEvent *)event buttons:(uint32_t)buttons
+{
+    /* Don't send button events to the guest unless we've got a
+     * mouse grab or window focus. If we have neither then this event
+     * is the user clicking on the background window to activate and
+     * bring us to the front, which will be done by the sendEvent
+     * call below. We definitely don't want to pass that click through
+     * to the guest.
+     */
+    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
+        };
+        qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons);
+        last_buttons = buttons;
+    }
+
+    return [self handleMouseEvent:event];
+}
+
+- (bool) handleMouseEvent:(NSEvent *)event
+{
+    if (!isMouseGrabbed) {
+        return false;
+    }
+
+    if (isAbsoluteEnabled) {
+        NSPoint p = [self screenLocationOfEvent:event];
+
+        /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
+         * The check on screenContainsPoint is to avoid sending out of range values for
+         * clicks in the titlebar.
+         */
+        if ([self screenContainsPoint:p]) {
+            qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width);
+            qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
+        }
+    } else {
+        qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]);
+        qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]);
+    }
+
+    qemu_input_event_sync();
+
+    return true;
+}
+
 - (void) grabMouse
 {
     COCOA_DEBUG("QemuCocoaView: grabMouse\n");
-- 
2.41.0