@@ -1088,6 +1088,21 @@
{ 'struct' : 'DisplayCurses',
'data' : { '*charset' : 'str' } }
+##
+# @DisplayCocoa:
+#
+# Cocoa display options.
+#
+# @full-grab: Capture all key presses, including system combos. This
+# requires accessibility permissions, since it performs
+# a global grab on key events. (default: off)
+# See https://support.apple.com/en-in/guide/mac-help/mh32356/mac
+#
+# Since: 6.1
+##
+{ 'struct' : 'DisplayCocoa',
+ 'data' : { '*full-grab' : 'bool' } }
+
##
# @DisplayType:
#
@@ -1153,6 +1168,7 @@
'*gl' : 'DisplayGLMode' },
'discriminator' : 'type',
'data' : { 'gtk' : 'DisplayGTK',
+ 'cocoa' : 'DisplayCocoa',
'curses' : 'DisplayCurses',
'egl-headless' : 'DisplayEGLHeadless'} }
@@ -1783,6 +1783,9 @@ DEF("display", HAS_ARG, QEMU_OPTION_display,
#if defined(CONFIG_CURSES)
"-display curses[,charset=<encoding>]\n"
#endif
+#if defined(CONFIG_COCOA)
+ "-display cocoa[,full_grab=on|off]\n"
+#endif
#if defined(CONFIG_OPENGL)
"-display egl-headless[,rendernode=<file>]\n"
#endif
@@ -72,6 +72,7 @@
typedef struct {
int width;
int height;
+ bool full_grab;
} QEMUScreen;
static void cocoa_update(DisplayChangeListener *dcl,
@@ -304,11 +305,13 @@ @interface QemuCocoaView : NSView
BOOL isMouseGrabbed;
BOOL isFullscreen;
BOOL isAbsoluteEnabled;
+ CFMachPortRef eventsTap;
}
- (void) switchSurface:(pixman_image_t *)image;
- (void) grabMouse;
- (void) ungrabMouse;
- (void) toggleFullScreen:(id)sender;
+- (void) setFullGrab:(id)sender;
- (void) handleMonitorInput:(NSEvent *)event;
- (bool) handleEvent:(NSEvent *)event;
- (bool) handleEventLocked:(NSEvent *)event;
@@ -323,6 +326,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
*/
- (BOOL) isMouseGrabbed;
- (BOOL) isAbsoluteEnabled;
+- (BOOL) isFullGrabEnabled;
- (float) cdx;
- (float) cdy;
- (QEMUScreen) gscreen;
@@ -331,6 +335,19 @@ - (void) raiseAllKeys;
QemuCocoaView *cocoaView;
+static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *userInfo)
+{
+ QemuCocoaView *cocoaView = (QemuCocoaView*) userInfo;
+ NSEvent* event = [NSEvent eventWithCGEvent:cgEvent];
+ if ([cocoaView isFullGrabEnabled] && [cocoaView isMouseGrabbed] && [cocoaView handleEvent:event]) {
+ COCOA_DEBUG("Global events tap: qemu handled the event, capturing!\n");
+ return NULL;
+ }
+ COCOA_DEBUG("Global events tap: qemu did not handle the event, letting it through...\n");
+
+ return cgEvent;
+}
+
@implementation QemuCocoaView
- (id)initWithFrame:(NSRect)frameRect
{
@@ -344,6 +361,32 @@ - (id)initWithFrame:(NSRect)frameRect
kbd = qkbd_state_init(dcl.con);
}
+
+ CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged);
+ eventsTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
+ mask, handleTapEvent, self);
+ if (!eventsTap) {
+ warn_report("Could not create event tap, system key combos will not be captured.\n");
+ return self;
+ } else {
+ COCOA_DEBUG("Global events tap created! Will capture system key combos.\n");
+ }
+
+ CFRunLoopRef runLoop = CFRunLoopGetCurrent();
+ if (!runLoop) {
+ warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
+ return self;
+ }
+
+ CFRunLoopSourceRef tapEventsSrc = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventsTap, 0);
+ if (!tapEventsSrc ) {
+ warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
+ return self;
+ }
+
+ CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode);
+ CFRelease(tapEventsSrc);
+
return self;
}
@@ -356,6 +399,11 @@ - (void) dealloc
}
qkbd_state_free(kbd);
+
+ if (eventsTap) {
+ CFRelease(eventsTap);
+ }
+
[super dealloc];
}
@@ -593,6 +641,13 @@ - (void) toggleFullScreen:(id)sender
}
}
+- (void) setFullGrab:(id)sender
+{
+ COCOA_DEBUG("QemuCocoaView: setFullGrab\n");
+
+ screen.full_grab = true;
+}
+
- (void) toggleKey: (int)keycode {
qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode));
}
@@ -1029,6 +1084,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {
}
- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
+- (BOOL) isFullGrabEnabled {return screen.full_grab;}
- (float) cdx {return cdx;}
- (float) cdy {return cdy;}
- (QEMUScreen) gscreen {return screen;}
@@ -1208,6 +1264,13 @@ - (void)toggleFullScreen:(id)sender
[cocoaView toggleFullScreen:sender];
}
+- (void) setFullGrab:(id)sender
+{
+ COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n");
+
+ [cocoaView setFullGrab:sender];
+}
+
/* Tries to find then open the specified filename */
- (void) openDocumentation: (NSString *) filename
{
@@ -1877,11 +1940,17 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
qemu_sem_wait(&app_started_sem);
COCOA_DEBUG("cocoa_display_init: app start completed\n");
+ QemuCocoaAppController* controller = (QemuCocoaAppController *)[NSApplication sharedApplication];
/* if fullscreen mode is to be used */
if (opts->has_full_screen && opts->full_screen) {
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp activateIgnoringOtherApps: YES];
- [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
+ [[controller delegate] toggleFullScreen: nil];
+ });
+ }
+ if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [[controller delegate] setFullGrab: nil];
});
}
if (opts->has_show_cursor && opts->show_cursor) {