Skip to content

Commit

Permalink
Merge tag 'patchew/20210713213200.2547-1-gustavo@noronha.dev.br'
Browse files Browse the repository at this point in the history
  • Loading branch information
akihikodaki committed Jul 31, 2021
2 parents d941994 + bc73e87 commit 7871b0b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 9 deletions.
22 changes: 22 additions & 0 deletions qapi/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,27 @@
{ '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
#
# @swap-option-command: Swap the Option and Command keys so that their key
# codes match their position on non-Mac keyboards and
# you can use Meta/Super and Alt where you expect them.
# (default: off)
#
# Since: 6.1
##
{ 'struct' : 'DisplayCocoa',
'data' : { '*full-grab' : 'bool',
'*swap-option-command' : 'bool' } }

##
# @DisplayType:
#
Expand Down Expand Up @@ -1165,6 +1186,7 @@
'discriminator' : 'type',
'data' : {
'gtk': { 'type': 'DisplayGTK', 'if': 'defined(CONFIG_GTK)' },
'cocoa': { 'type': 'DisplayCocoa', 'if': 'defined(CONFIG_COCOA)' },
'curses': { 'type': 'DisplayCurses', 'if': 'defined(CONFIG_CURSES)' },
'egl-headless': { 'type': 'DisplayEGLHeadless',
'if': 'defined(CONFIG_OPENGL) && defined(CONFIG_GBM)' }
Expand Down
4 changes: 4 additions & 0 deletions qemu-options.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,10 @@ 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"
" [,swap-option-command=on|off]\n"
#endif
#if defined(CONFIG_OPENGL)
"-display egl-headless[,rendernode=<file>]\n"
#endif
Expand Down
129 changes: 120 additions & 9 deletions ui/cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
typedef struct {
int width;
int height;
bool swap_option_command;
} QEMUScreen;

static void cocoa_update(DisplayChangeListener *dcl,
Expand Down Expand Up @@ -309,11 +310,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;
Expand All @@ -328,6 +331,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
*/
- (BOOL) isMouseGrabbed;
- (BOOL) isAbsoluteEnabled;
- (BOOL) isSwapOptionCommandEnabled;
- (float) cdx;
- (float) cdy;
- (QEMUScreen) gscreen;
Expand All @@ -336,6 +340,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 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
{
Expand All @@ -361,6 +378,11 @@ - (void) dealloc
}

qkbd_state_free(kbd);

if (eventsTap) {
CFRelease(eventsTap);
}

[super dealloc];
}

Expand Down Expand Up @@ -635,6 +657,43 @@ - (void) toggleFullScreen:(id)sender
}
}

- (void) setFullGrab:(id)sender
{
COCOA_DEBUG("QemuCocoaView: setFullGrab\n");

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;
} 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;
}

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;
}

CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode);
CFRelease(tapEventsSrc);
}

- (void) setSwapOptionCommand:(id)sender
{
COCOA_DEBUG("QemuCocoaView: setSwapOptionCommand\n");

screen.swap_option_command = true;
}

- (void) toggleKey: (int)keycode {
qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode));
}
Expand Down Expand Up @@ -784,12 +843,22 @@ - (bool) handleEventLocked:(NSEvent *)event
qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false);
}
if (!(modifiers & NSEventModifierFlagOption)) {
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
if ([self isSwapOptionCommandEnabled]) {
qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
} else {
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
}
}
if (!(modifiers & NSEventModifierFlagCommand)) {
qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
if ([self isSwapOptionCommandEnabled]) {
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
} else {
qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
}
}

switch ([event type]) {
Expand Down Expand Up @@ -821,28 +890,44 @@ - (bool) handleEventLocked:(NSEvent *)event

case kVK_Option:
if (!!(modifiers & NSEventModifierFlagOption)) {
[self toggleKey:Q_KEY_CODE_ALT];
if ([self isSwapOptionCommandEnabled]) {
[self toggleKey:Q_KEY_CODE_META_L];
} else {
[self toggleKey:Q_KEY_CODE_ALT];
}
}
break;

case kVK_RightOption:
if (!!(modifiers & NSEventModifierFlagOption)) {
[self toggleKey:Q_KEY_CODE_ALT_R];
if ([self isSwapOptionCommandEnabled]) {
[self toggleKey:Q_KEY_CODE_META_R];
} else {
[self toggleKey:Q_KEY_CODE_ALT_R];
}
}
break;

/* Don't pass command key changes to guest unless mouse is grabbed */
case kVK_Command:
if (isMouseGrabbed &&
!!(modifiers & NSEventModifierFlagCommand)) {
[self toggleKey:Q_KEY_CODE_META_L];
if ([self isSwapOptionCommandEnabled]) {
[self toggleKey:Q_KEY_CODE_ALT];
} else {
[self toggleKey:Q_KEY_CODE_META_L];
}
}
break;

case kVK_RightCommand:
if (isMouseGrabbed &&
!!(modifiers & NSEventModifierFlagCommand)) {
[self toggleKey:Q_KEY_CODE_META_R];
if ([self isSwapOptionCommandEnabled]) {
[self toggleKey:Q_KEY_CODE_ALT_R];
} else {
[self toggleKey:Q_KEY_CODE_META_R];
}
}
break;
}
Expand Down Expand Up @@ -1071,6 +1156,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {
}
- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
- (BOOL) isSwapOptionCommandEnabled {return screen.swap_option_command;}
- (float) cdx {return cdx;}
- (float) cdy {return cdy;}
- (QEMUScreen) gscreen {return screen;}
Expand Down Expand Up @@ -1260,6 +1346,20 @@ - (void)toggleFullScreen:(id)sender
[cocoaView toggleFullScreen:sender];
}

- (void) setFullGrab:(id)sender
{
COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n");

[cocoaView setFullGrab:sender];
}

- (void) setSwapOptionCommand:(id)sender
{
COCOA_DEBUG("QemuCocoaAppController: setSwapOptionCommand\n");

[cocoaView setSwapOptionCommand:sender];
}

/* Tries to find then open the specified filename */
- (void) openDocumentation: (NSString *) filename
{
Expand Down Expand Up @@ -2031,11 +2131,22 @@ 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] delegate];
/* 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 toggleFullScreen: nil];
});
}
if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
dispatch_async(dispatch_get_main_queue(), ^{
[controller setFullGrab: nil];
});
}
if (opts->u.cocoa.has_swap_option_command && opts->u.cocoa.swap_option_command) {
dispatch_async(dispatch_get_main_queue(), ^{
[controller setSwapOptionCommand: nil];
});
}
if (opts->has_show_cursor && opts->show_cursor) {
Expand Down

0 comments on commit 7871b0b

Please sign in to comment.