ui/cocoa: Fix handling of absolute positioning devices

Fix handling of absolute positioning devices, which were basically
unusable for two separate reasons:
 (1) as soon as you pressed the left mouse button we would call
     CGAssociateMouseAndMouseCursorPosition(FALSE), which means that
     the absolute coordinates of the mouse events are never updated
 (2) we didn't account for MacOSX coordinate origin being bottom left
     rather than top right, and so all the Y values sent to the guest
     were inverted

We fix (1) by aligning our behaviour with the SDL UI backend for
absolute devices:
 * when the mouse moves into the window we do a grab (which means
   hiding the host cursor and sending special keys to the guest)
 * when the mouse moves out of the window we un-grab
and fix (2) by doing the correct transformation in the call to
qemu_input_queue_abs().

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Message-id: 1403516125-14568-4-git-send-email-peter.maydell@linaro.org
This commit is contained in:
Peter Maydell 2014-06-23 10:35:24 +01:00
parent 5dd45bee58
commit f61c387ea6

View file

@ -256,7 +256,7 @@ static int cocoa_keycode_to_qemu(int keycode)
BOOL isMouseGrabbed; BOOL isMouseGrabbed;
BOOL isFullscreen; BOOL isFullscreen;
BOOL isAbsoluteEnabled; BOOL isAbsoluteEnabled;
BOOL isTabletEnabled; BOOL isMouseDeassociated;
} }
- (void) switchSurface:(DisplaySurface *)surface; - (void) switchSurface:(DisplaySurface *)surface;
- (void) grabMouse; - (void) grabMouse;
@ -264,8 +264,21 @@ static int cocoa_keycode_to_qemu(int keycode)
- (void) toggleFullScreen:(id)sender; - (void) toggleFullScreen:(id)sender;
- (void) handleEvent:(NSEvent *)event; - (void) handleEvent:(NSEvent *)event;
- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
/* The state surrounding mouse grabbing is potentially confusing.
* isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
* pointing device an absolute-position one?"], but is only updated on
* next refresh.
* isMouseGrabbed tracks whether GUI events are directed to the guest;
* it controls whether special keys like Cmd get sent to the guest,
* and whether we capture the mouse when in non-absolute mode.
* isMouseDeassociated tracks whether we've told MacOSX to disassociate
* the mouse and mouse cursor position by calling
* CGAssociateMouseAndMouseCursorPosition(FALSE)
* (which basically happens if we grab in non-absolute mode).
*/
- (BOOL) isMouseGrabbed; - (BOOL) isMouseGrabbed;
- (BOOL) isAbsoluteEnabled; - (BOOL) isAbsoluteEnabled;
- (BOOL) isMouseDeassociated;
- (float) cdx; - (float) cdx;
- (float) cdy; - (float) cdy;
- (QEMUScreen) gscreen; - (QEMUScreen) gscreen;
@ -613,14 +626,12 @@ QemuCocoaView *cocoaView;
case NSMouseMoved: case NSMouseMoved:
if (isAbsoluteEnabled) { if (isAbsoluteEnabled) {
if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) { if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
if (isTabletEnabled) { // if we leave the window, deactivate the tablet if (isMouseGrabbed) {
[NSCursor unhide]; [self ungrabMouse];
isTabletEnabled = FALSE;
} }
} else { } else {
if (!isTabletEnabled) { // if we enter the window, activate the tablet if (!isMouseGrabbed) {
[NSCursor hide]; [self grabMouse];
isTabletEnabled = TRUE;
} }
} }
} }
@ -659,16 +670,9 @@ QemuCocoaView *cocoaView;
mouse_event = true; mouse_event = true;
break; break;
case NSLeftMouseUp: case NSLeftMouseUp:
if (isTabletEnabled) { mouse_event = true;
mouse_event = true; if (!isMouseGrabbed && [self screenContainsPoint:p]) {
} else if (!isMouseGrabbed) { [self grabMouse];
if ([self screenContainsPoint:p]) {
[self grabMouse];
} else {
[NSApp sendEvent:event];
}
} else {
mouse_event = true;
} }
break; break;
case NSRightMouseUp: case NSRightMouseUp:
@ -678,13 +682,11 @@ QemuCocoaView *cocoaView;
mouse_event = true; mouse_event = true;
break; break;
case NSScrollWheel: case NSScrollWheel:
if (isTabletEnabled || isMouseGrabbed) { if (isMouseGrabbed) {
buttons |= ([event deltaY] < 0) ? buttons |= ([event deltaY] < 0) ?
MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN; MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
mouse_event = true;
} else {
[NSApp sendEvent:event];
} }
mouse_event = true;
break; break;
default: default:
[NSApp sendEvent:event]; [NSApp sendEvent:event];
@ -702,12 +704,20 @@ QemuCocoaView *cocoaView;
qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons); qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
last_buttons = buttons; last_buttons = buttons;
} }
if (isTabletEnabled) { if (isMouseGrabbed) {
qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width); if (isAbsoluteEnabled) {
qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, p.y, screen.height); /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
} else if (isMouseGrabbed) { * The check on screenContainsPoint is to avoid sending out of range values for
qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]); * clicks in the titlebar.
qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]); */
if ([self screenContainsPoint:p]) {
qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 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 { } else {
[NSApp sendEvent:event]; [NSApp sendEvent:event];
} }
@ -726,7 +736,10 @@ QemuCocoaView *cocoaView;
[normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
} }
[NSCursor hide]; [NSCursor hide];
CGAssociateMouseAndMouseCursorPosition(FALSE); if (!isAbsoluteEnabled) {
isMouseDeassociated = TRUE;
CGAssociateMouseAndMouseCursorPosition(FALSE);
}
isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
} }
@ -741,13 +754,17 @@ QemuCocoaView *cocoaView;
[normalWindow setTitle:@"QEMU"]; [normalWindow setTitle:@"QEMU"];
} }
[NSCursor unhide]; [NSCursor unhide];
CGAssociateMouseAndMouseCursorPosition(TRUE); if (isMouseDeassociated) {
CGAssociateMouseAndMouseCursorPosition(TRUE);
isMouseDeassociated = FALSE;
}
isMouseGrabbed = FALSE; isMouseGrabbed = FALSE;
} }
- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
- (BOOL) isMouseGrabbed {return isMouseGrabbed;} - (BOOL) isMouseGrabbed {return isMouseGrabbed;}
- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
- (float) cdx {return cdx;} - (float) cdx {return cdx;}
- (float) cdy {return cdy;} - (float) cdy {return cdy;}
- (QEMUScreen) gscreen {return screen;} - (QEMUScreen) gscreen {return screen;}