Skip to contentSkip to conversation

ui/cocoa: Fix absolute and relative input issues on Mojave

PATCH Superseded Chen Zhang 2 participants 6 messages Apr 8, 2019 – Apr 22, 2019

lore
Share & tools
lore

Chen Zhang

author
#0 lore raw

cover ui/cocoa: Fix absolute and relative input issues on Mojave

The following patches fixed absolute and relative input device issues on macOS Mojave.

Chen Zhang (2):
ui/cocoa: Fix absolute input device grabbing issue on Mojave ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input
device

 ui/cocoa.m | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.2
author
#1 lore raw

PATCH 1/2 ui/cocoa: Fix absolute input device grabbing issue on

This patches fixed boundary check methods for cursor in normal and fullscreen modes with/without Zoom-to-Fit on Mojave.

On Mojave, absolute input device, i.e. tablet, had trouble re-grabbing the cursor in re-entry into the virtual screen area. In some cases, the window property of NSEvent object was nil after exit of cursor, meaning that the -locationInWindow method would return value in screen coordinates. The current implementation used raw locations frrom NSEvent without considering whether the value was for the window coordinates or the macOS screen coordinates, nor the zooming factor for Zoom-to-Fit in fullscreen mode.

In fullscreen mode, the fullscreen cocoa window might not be the key window, therefore the location of event in virtual coordinates should suffice.

Note: CGRect, -convertRectToScreen: and -convertRectFromScreen: were used in coordinates conversion for compatibility reason.

---
 ui/cocoa.m | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
ui/cocoa.m +41 -2
index 420b2411c1..474d44cb9f 100644
@@ -405,6 +405,41 @@ QemuCocoaView *cocoaView;
     return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
 }
 
+/* Get location of event and convert to virtual screen coordinate */
+- (CGPoint) screenLocationOfEvent:(NSEvent *)ev
+{
+    NSWindow *eventWindow = [ev window];
+    // XXX: Use CGRect and -convertRectFromScreen: to support macOS 10.10
+    CGRect r = CGRectZero;
+    r.origin = [ev locationInWindow];
+    if (!eventWindow) {
+        if (!isFullscreen) {
+            return [[self window] convertRectFromScreen:r].origin;
+        } else {
+            CGPoint locationInSelfWindow = [[self window] convertRectFromScreen:r].origin;
+            CGPoint loc = [self convertPoint:locationInSelfWindow fromView:nil];
+            if (stretch_video) {
+                loc.x /= cdx;
+                loc.y /= cdy;
+            }
+            return loc;
+        }
+    } else if ([[self window] isEqual:eventWindow]) {
+        if (!isFullscreen) {
+            return r.origin;
+        } else {
+            CGPoint loc = [self convertPoint:r.origin fromView:nil];
+            if (stretch_video) {
+                loc.x /= cdx;
+                loc.y /= cdy;
+            }
+            return loc;
+        }
+    } else {
+        return [[self window] convertRectFromScreen:[eventWindow convertRectToScreen:r]].origin;
+    }
+}
+
 - (void) hideCursor
 {
     if (!cursor_hide) {
@@ -704,7 +739,8 @@ QemuCocoaView *cocoaView;
     int keycode = 0;
     bool mouse_event = false;
     static bool switched_to_fullscreen = false;
-    NSPoint p = [event locationInWindow];
+    // Location of event in virtual screen coordinates
+    NSPoint p = [self screenLocationOfEvent:event];
 
     switch ([event type]) {
         case NSEventTypeFlagsChanged:
@@ -815,7 +851,10 @@ QemuCocoaView *cocoaView;
             break;
         case NSEventTypeMouseMoved:
             if (isAbsoluteEnabled) {
-                if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
+                // Cursor re-entered into a window might generate events bound to screen coordinates
+                // and `nil` window property, and in full screen mode, current window might not be
+                // key window, where event location alone should suffice.
+                if (![self screenContainsPoint:p] || !([[self window] isKeyWindow] || isFullscreen)) {
                     if (isMouseGrabbed) {
                         [self ungrabMouse];
                     }
Signature · 1 line
2.19.2
author
#2 lore raw

PATCH 2/2 ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input device

In reply to Chen Zhang

In fullscreen mode, the window property of cocoaView may not be the key window, and the current implementation would not grab mouse in fullscreen mode after user ungrabs cursor in fullscreen mode with hot-key, and left clicks the relative input devices to re-grab it.

This patch used value of isFullscreen as a short-cirtuit condition for relative input device grabbing.

---
 ui/cocoa.m | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
ui/cocoa.m +6 -1
index 474d44cb9f..aa7cf07368 100644
@@ -901,7 +901,12 @@ QemuCocoaView *cocoaView;
         case NSEventTypeLeftMouseUp:
             mouse_event = true;
             if (!isMouseGrabbed && [self screenContainsPoint:p]) {
-                if([[self window] isKeyWindow]) {
+                /*
+                 * In fullscreen mode, the window of cocoaView may not be the
+                 * key window, therefore the position relative to the virtual
+                 * screen alone will be sufficient.
+                 */
+                if(isFullscreen || [[self window] isKeyWindow]) {
                     [self grabMouse];
                 }
             }
Signature · 1 line
2.19.2
#3 lore raw
In reply to Chen Zhang
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Subject: [Qemu-devel] [PATCH 0/2] ui/cocoa: Fix absolute and relative input issues on Mojave
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
9c5c7f047d ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input device ffe76bafc1 ui/cocoa: Fix absolute input device grabbing issue on

=== OUTPUT BEGIN ===
1/2 Checking commit ffe76bafc1bb (ui/cocoa: Fix absolute input device grabbing issue on)
ERROR: Author email address is mangled by the mailing list
#2:
Author: Chen Zhang via Qemu-devel <[email protected]>

total: 1 errors, 0 warnings, 61 lines checked

Patch 1/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

2/2 Checking commit 9c5c7f047da8 (ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input device)
ERROR: Author email address is mangled by the mailing list
#2:
Author: Chen Zhang via Qemu-devel <[email protected]>

total: 1 errors, 0 warnings, 13 lines checked

Patch 2/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

author
#4 lore raw
In reply to Chen Zhang

ping

Quoted 15 lines · Chen Zhang

The following patches fixed absolute and relative input device issues on macOS Mojave.

Chen Zhang (2):
ui/cocoa: Fix absolute input device grabbing issue on Mojave ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input
device

 ui/cocoa.m | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.2
author
#5 lore raw
In reply to Chen Zhang

Ping

https://patchwork.kernel.org/patch/10888727/ <https://patchwork.kernel.org/patch/10888727/>

https://patchwork.kernel.org/patch/10888725/ <https://patchwork.kernel.org/patch/10888725/>

Quoted 15 lines · Chen Zhang

The following patches fixed absolute and relative input device issues on macOS Mojave.

Chen Zhang (2):
ui/cocoa: Fix absolute input device grabbing issue on Mojave ui/cocoa: Fix mouse grabbing in fullscreen mode for relative input
device

 ui/cocoa.m | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.2