* [PATCH v2 0/2] ui/cocoa: Adds native absolute pointer support @ 2024-06-25 13:49 Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 1/2] ui/cocoa: Minor fixes to CALayer based cursors Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support Phil Dennis-Jordan 0 siblings, 2 replies; 4+ messages in thread From: Phil Dennis-Jordan @ 2024-06-25 13:49 UTC (permalink / raw) To: qemu-devel Cc: peter.maydell, philmd, marcandre.lureau, akihiko.odaki, lists, Phil Dennis-Jordan 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: <20240318-cursor-v1-2-0bbe6c382217@daynix.com> 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(-) -- 2.39.3 (Apple Git-146) ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] ui/cocoa: Minor fixes to CALayer based cursors 2024-06-25 13:49 [PATCH v2 0/2] ui/cocoa: Adds native absolute pointer support Phil Dennis-Jordan @ 2024-06-25 13:49 ` Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support Phil Dennis-Jordan 1 sibling, 0 replies; 4+ messages in thread From: Phil Dennis-Jordan @ 2024-06-25 13:49 UTC (permalink / raw) To: qemu-devel Cc: peter.maydell, philmd, marcandre.lureau, akihiko.odaki, lists, Phil Dennis-Jordan This change fixes some object lifetime issues. (Unreleased reference counts) Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu> --- ui/cocoa.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 06ca114800..cca987eac7 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -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]; -- 2.39.3 (Apple Git-146) ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support 2024-06-25 13:49 [PATCH v2 0/2] ui/cocoa: Adds native absolute pointer support Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 1/2] ui/cocoa: Minor fixes to CALayer based cursors Phil Dennis-Jordan @ 2024-06-25 13:49 ` Phil Dennis-Jordan 2024-06-27 11:48 ` Akihiko Odaki 1 sibling, 1 reply; 4+ messages in thread From: Phil Dennis-Jordan @ 2024-06-25 13:49 UTC (permalink / raw) To: qemu-devel Cc: peter.maydell, philmd, marcandre.lureau, akihiko.odaki, lists, 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 <phil@philjordan.eu> --- ui/cocoa.m | 82 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index cca987eac7..131c442e16 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -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]; } } } -- 2.39.3 (Apple Git-146) ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support 2024-06-25 13:49 ` [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support Phil Dennis-Jordan @ 2024-06-27 11:48 ` Akihiko Odaki 0 siblings, 0 replies; 4+ messages in thread From: Akihiko Odaki @ 2024-06-27 11:48 UTC (permalink / raw) To: Phil Dennis-Jordan, qemu-devel Cc: peter.maydell, philmd, marcandre.lureau, lists 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. On 2024/06/25 22:49, Phil Dennis-Jordan wrote: > 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 <phil@philjordan.eu> > --- > ui/cocoa.m | 82 +++++++++++++++++++++++++++++++++++++++++++----------- > 1 file changed, 65 insertions(+), 17 deletions(-) > > diff --git a/ui/cocoa.m b/ui/cocoa.m > index cca987eac7..131c442e16 100644 > --- a/ui/cocoa.m > +++ b/ui/cocoa.m > @@ -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) Don't add C functions in middle of Objective-C definition. > { > 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 }; Use NSMakePoint() for the consistency with the next line (among other similar constructs). > + 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) { For consistency, just do: if (!cocoaCursor) > + [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]; > + } [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 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-06-27 11:49 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-06-25 13:49 [PATCH v2 0/2] ui/cocoa: Adds native absolute pointer support Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 1/2] ui/cocoa: Minor fixes to CALayer based cursors Phil Dennis-Jordan 2024-06-25 13:49 ` [PATCH v2 2/2] ui/cocoa: Adds NSCursor absolute pointer support Phil Dennis-Jordan 2024-06-27 11:48 ` Akihiko Odaki
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).