ui/cocoa: Adds native absolute pointer support
v2 cover ui/cocoa: Adds native absolute pointer support
This change implements passing guest cursors through to the native Cocoa host NSCursor on macOS when using absolute pointing device input.
The first pass at this was based purely on NSCursor, which drew some criticism due to the somewhat complex nature of the code which was required to generate correct relative pointer input events after teleporting the host cursor.
This new version builds on Akihiko Odaki's work implementing CALayer based cursors. We retain CALayer for compositing cursors when the input is relative and the pointer thus completely captured by the guest. When using absolute positioning of the pointer, we use the fully host-integrated NSCursor, with no offsetting or teleporting needed.
The first patch consists of a few reference counting fixes to the existing CALayer patch, the second implements the NSCursor logic and switches between CALayer and NSCursor depending on whether relative or absolute input is used.
Based-on: <[email protected]>
Phil Dennis-Jordan (2):
ui/cocoa: Minor fixes to CALayer based cursors ui/cocoa: Adds NSCursor absolute pointer support
ui/cocoa.m | 93 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 18 deletions(-)
Signature · 1 line
2.39.3 (Apple Git-146)
PATCH v2 1/2 ui/cocoa: Minor fixes to CALayer based cursors
This change fixes some object lifetime issues. (Unreleased reference counts)
--- ui/cocoa.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
ui/cocoa.m +10 -1
@@ -395,6 +395,13 @@ - (void) dealloc CFRelease(eventsTap); } + if (cursor) { + cursor_unref(cursor); + cursor = NULL; + } + + [cursorLayer release]; + cursorLayer = nil; [super dealloc]; }
@@ -474,6 +481,7 @@ - (void)setCursor:(QEMUCursor *)given_cursor bounds.size.width = cursor->width; bounds.size.height = cursor->height; + CGColorSpaceRef color_space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); provider = CGDataProviderCreateWithData( NULL,
@@ -488,7 +496,7 @@ - (void)setCursor:(QEMUCursor *)given_cursor 8, //bitsPerComponent 32, //bitsPerPixel cursor->width * 4, //bytesPerRow - CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace + color_space, //colorspace kCGBitmapByteOrder32Little | kCGImageAlphaFirst, //bitmapInfo provider, //provider NULL, //decode
@@ -497,6 +505,7 @@ - (void)setCursor:(QEMUCursor *)given_cursor ); CGDataProviderRelease(provider); + CGColorSpaceRelease(color_space); [CATransaction begin]; [CATransaction setDisableActions:YES]; [cursorLayer setBounds:bounds];
Signature · 1 line
2.39.3 (Apple Git-146)
PATCH v2 2/2 ui/cocoa: Adds NSCursor absolute pointer support
In reply to Phil Dennis-JordanWhen pointer input is absolute, use the native macOS host’s Cocoa NSCursor to render the guest’s cursor. The rendered cursor is no longer cropped to the guest viewport, and the correct cursor image is passed to anything tapping into the host system’s native cursor. (such as remote access)
The CALayer is retained for rendering the cursor in relative pointer input mode. Cropping the cursor here gives a visual indication of the captured pointer (the mouse must be explicitly ungrabbed before allowing the cursor to leave the Qemu window), and teleporting the host cursor when its position is changed by the guest causes a feedback loop in input events.
--- ui/cocoa.m | 82 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 17 deletions(-)
ui/cocoa.m +65 -17
@@ -314,6 +314,7 @@ @interface QemuCocoaView : NSView CFMachPortRef eventsTap; CALayer *cursorLayer; QEMUCursor *cursor; + NSCursor *cocoaCursor; int mouseX; int mouseY; int mouseOn;
@@ -402,6 +403,9 @@ - (void) dealloc [cursorLayer release]; cursorLayer = nil; + [cocoaCursor release]; + cocoaCursor = nil; + [super dealloc]; }
@@ -460,27 +464,14 @@ - (void)setMouseX:(int)x y:(int)y on:(int)on [CATransaction begin]; [CATransaction setDisableActions:YES]; [cursorLayer setPosition:position]; - [cursorLayer setHidden:!mouseOn]; + [cursorLayer setHidden:!mouseOn || isAbsoluteEnabled]; [CATransaction commit]; } -- (void)setCursor:(QEMUCursor *)given_cursor +static CGImageRef cursor_cgimage_create(QEMUCursor *cursor) { CGDataProviderRef provider; CGImageRef image; - CGRect bounds = CGRectZero; - - cursor_unref(cursor); - cursor = given_cursor; - - if (!cursor) { - return; - } - - cursor_ref(cursor); - - bounds.size.width = cursor->width; - bounds.size.height = cursor->height; CGColorSpaceRef color_space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); provider = CGDataProviderCreateWithData(
@@ -506,6 +497,43 @@ - (void)setCursor:(QEMUCursor *)given_cursor CGDataProviderRelease(provider); CGColorSpaceRelease(color_space); + return image; +} + +static NSCursor *cocoa_cursor_create(QEMUCursor *cursor, CGImageRef image) +{ + NSPoint hotspot = { cursor->hot_x, cursor->hot_y }; + NSSize size = NSMakeSize(cursor->width, cursor->height); + NSImage *cursor_image = [[NSImage alloc] initWithCGImage:image size:size]; + NSCursor *cocoa_cursor = + [[NSCursor alloc] initWithImage:cursor_image hotSpot:hotspot]; + [cursor_image release]; + return cocoa_cursor; +} + +- (void)setCursor:(QEMUCursor *)given_cursor +{ + CGImageRef image; + NSImage *cursor_nsimage = nil; + CGRect bounds = CGRectZero; + + cursor_unref(cursor); + cursor = given_cursor; + + if (!cursor) { + return; + } + + cursor_ref(cursor); + + bounds.size.width = cursor->width; + bounds.size.height = cursor->height; + + image = cursor_cgimage_create(cursor); + [cocoaCursor release]; + cocoaCursor = cocoa_cursor_create(cursor, image); + [self.window invalidateCursorRectsForView:self]; + [CATransaction begin]; [CATransaction setDisableActions:YES]; [cursorLayer setBounds:bounds];
@@ -514,6 +542,16 @@ - (void)setCursor:(QEMUCursor *)given_cursor CGImageRelease(image); } +- (void) resetCursorRects +{ + if (self->cocoaCursor == nil) { + [super resetCursorRects]; + } else { + NSRect guest_area = {{ 0.0, 0.0 }, { screen.width, screen.height }}; + [self addCursorRect:guest_area cursor:cocoaCursor]; + } +} + - (void) drawRect:(NSRect) rect { COCOA_DEBUG("QemuCocoaView: drawRect\n");
@@ -1181,7 +1219,12 @@ - (void) grabMouse [[self window] setTitle:[NSString stringWithFormat:@"QEMU %s - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)", qemu_name]]; else [[self window] setTitle:@"QEMU - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)"]; - [self hideCursor]; + + [cursorLayer setHidden:!mouseOn || isAbsoluteEnabled]; + if (!isAbsoluteEnabled) { + [self hideCursor]; + } + CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] }
@@ -1194,7 +1237,11 @@ - (void) ungrabMouse [[self window] setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; else [[self window] setTitle:@"QEMU"]; - [self unhideCursor]; + + [cursorLayer setHidden:!mouseOn || isAbsoluteEnabled]; + if (!isAbsoluteEnabled) { + [self unhideCursor]; + } CGAssociateMouseAndMouseCursorPosition(TRUE); isMouseGrabbed = FALSE; [self raiseAllButtons];
@@ -1216,6 +1263,7 @@ - (void) notifyMouseModeChange { [self ungrabMouse]; } else { CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); + [self hideCursor]; } } }
Signature · 1 line
2.39.3 (Apple Git-146)
Re: [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support
Hi,
Thanks for fixing my patch and adding this follow-up.
I incorporated your fix with some change with v2 so please review it and rebase this patch to it.
Quoted 40 lines · Phil Dennis-Jordan
When pointer input is absolute, use the native macOS host’s Cocoa NSCursor to render the guest’s cursor. The rendered cursor is no longer cropped to the guest viewport, and the correct cursor image is passed to anything tapping into the host system’s native cursor. (such as remote access)
The CALayer is retained for rendering the cursor in relative pointer input mode. Cropping the cursor here gives a visual indication of the captured pointer (the mouse must be explicitly ungrabbed before allowing the cursor to leave the Qemu window), and teleporting the host cursor when its position is changed by the guest causes a feedback loop in input events. >
Signed-off-by Phil Dennis-Jordan <[email protected]>--- ui/cocoa.m | 82 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 17 deletions(-)ui/cocoa.m +4
-0@@ -314,6 +314,7 @@ @interface QemuCocoaView : NSView CFMachPortRef eventsTap; CALayer *cursorLayer; QEMUCursor *cursor; + NSCursor *cocoaCursor; int mouseX; int mouseY; int mouseOn;@@ -402,6 +403,9 @@ - (void) dealloc [cursorLayer release]; cursorLayer = nil; + [cocoaCursor release]; + cocoaCursor = nil; + [super dealloc]; }
ui/cocoa.m - (void)setMouseX:(int)x y:(int)y on:(int)on · line 471
Quoted 10 lines
ui/cocoa.m +2
-2@@ -460,27 +464,14 @@ - (void)setMouseX:(int)x y:(int)y on:(int)on [CATransaction begin]; [CATransaction setDisableActions:YES]; [cursorLayer setPosition:position]; - [cursorLayer setHidden:!mouseOn]; + [cursorLayer setHidden:!mouseOn || isAbsoluteEnabled]; [CATransaction commit]; } -- (void)setCursor:(QEMUCursor *)given_cursor +static CGImageRef cursor_cgimage_create(QEMUCursor *cursor)
Don't add C functions in middle of Objective-C definition.
Quoted 20 lines
ui/cocoa.m +0
-13{ CGDataProviderRef provider; CGImageRef image; - CGRect bounds = CGRectZero; - - cursor_unref(cursor); - cursor = given_cursor; - - if (!cursor) { - return; - } - - cursor_ref(cursor); - - bounds.size.width = cursor->width; - bounds.size.height = cursor->height; CGColorSpaceRef color_space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); provider = CGDataProviderCreateWithData(
ui/cocoa.m - (void)setCursor:(QEMUCursor *)given_cursor · line 505
Quoted 9 lines
ui/cocoa.m +6
-0@@ -506,6 +497,43 @@ - (void)setCursor:(QEMUCursor *)given_cursor CGDataProviderRelease(provider); CGColorSpaceRelease(color_space); + return image; +} + +static NSCursor *cocoa_cursor_create(QEMUCursor *cursor, CGImageRef image) +{ + NSPoint hotspot = { cursor->hot_x, cursor->hot_y };
Use NSMakePoint() for the consistency with the next line (among other similar constructs).
Quoted 35 lines
ui/cocoa.m +31
-0+ NSSize size = NSMakeSize(cursor->width, cursor->height); + NSImage *cursor_image = [[NSImage alloc] initWithCGImage:image size:size]; + NSCursor *cocoa_cursor = + [[NSCursor alloc] initWithImage:cursor_image hotSpot:hotspot]; + [cursor_image release]; + return cocoa_cursor; +} + +- (void)setCursor:(QEMUCursor *)given_cursor +{ + CGImageRef image; + NSImage *cursor_nsimage = nil; + CGRect bounds = CGRectZero; + + cursor_unref(cursor); + cursor = given_cursor; + + if (!cursor) { + return; + } + + cursor_ref(cursor); + + bounds.size.width = cursor->width; + bounds.size.height = cursor->height; + + image = cursor_cgimage_create(cursor); + [cocoaCursor release]; + cocoaCursor = cocoa_cursor_create(cursor, image); + [self.window invalidateCursorRectsForView:self]; + [CATransaction begin]; [CATransaction setDisableActions:YES]; [cursorLayer setBounds:bounds];
ui/cocoa.m - (void)setCursor:(QEMUCursor *)given_cursor · line 547
Quoted 6 lines
ui/cocoa.m +3
-0@@ -514,6 +542,16 @@ - (void)setCursor:(QEMUCursor *)given_cursor CGImageRelease(image); } +- (void) resetCursorRects +{ + if (self->cocoaCursor == nil) {
For consistency, just do: if (!cocoaCursor)
Quoted 11 lines
ui/cocoa.m +7
-0+ [super resetCursorRects]; + } else { + NSRect guest_area = {{ 0.0, 0.0 }, { screen.width, screen.height }}; + [self addCursorRect:guest_area cursor:cocoaCursor]; + } +} + - (void) drawRect:(NSRect) rect { COCOA_DEBUG("QemuCocoaView: drawRect\n");
ui/cocoa.m - (void) grabMouse · line 1226
Quoted 9 lines
ui/cocoa.m +5
-1@@ -1181,7 +1219,12 @@ - (void) grabMouse [[self window] setTitle:[NSString stringWithFormat:@"QEMU %s - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)", qemu_name]]; else [[self window] setTitle:@"QEMU - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)"]; - [self hideCursor]; + + [cursorLayer setHidden:!mouseOn || isAbsoluteEnabled]; + if (!isAbsoluteEnabled) { + [self hideCursor]; + }
[self hideCursor] should also be called for an absolute pointer device if the guest does not set the cursor. See ui/gtk.c and ui/sdl2.c to know how the show-cursor option should behave.
Regards,
Akihiko Odaki