Skip to content

Commit

Permalink
ui/cocoa: add option to swap Option and Command
Browse files Browse the repository at this point in the history
On Mac OS X the Option key maps to Alt and Command to Super/Meta. This change
swaps them around so that Alt is the key closer to the space bar and Meta/Super
is between Control and Alt, like on non-Mac keyboards.

It is a cocoa display option, disabled by default.

Acked-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Akihiko Odaki <akihiko.odaki@gmail.com>
Signed-off-by: Gustavo Noronha Silva <gustavo@noronha.dev.br>
Message-Id: <20210713213200.2547-3-gustavo@noronha.dev.br>
  • Loading branch information
Gustavo Noronha Silva authored and Patchew Applier committed Jul 13, 2021
1 parent 7292f1f commit bc73e87
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
8 changes: 7 additions & 1 deletion qapi/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,16 @@
# 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' } }
'data' : { '*full-grab' : 'bool',
'*swap-option-command' : 'bool' } }

##
# @DisplayType:
Expand Down
3 changes: 2 additions & 1 deletion qemu-options.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,8 @@ DEF("display", HAS_ARG, QEMU_OPTION_display,
"-display curses[,charset=<encoding>]\n"
#endif
#if defined(CONFIG_COCOA)
"-display cocoa[,full_grab=on|off]\n"
"-display cocoa[,full-grab=on|off]\n"
" [,swap-option-command=on|off]\n"
#endif
#if defined(CONFIG_OPENGL)
"-display egl-headless[,rendernode=<file>]\n"
Expand Down
64 changes: 56 additions & 8 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 @@ -330,6 +331,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
*/
- (BOOL) isMouseGrabbed;
- (BOOL) isAbsoluteEnabled;
- (BOOL) isSwapOptionCommandEnabled;
- (float) cdx;
- (float) cdy;
- (QEMUScreen) gscreen;
Expand Down Expand Up @@ -685,6 +687,13 @@ - (void) setFullGrab:(id)sender
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 @@ -834,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 @@ -871,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 @@ -1121,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 @@ -1317,6 +1353,13 @@ - (void) setFullGrab:(id)sender
[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 @@ -2101,6 +2144,11 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
[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) {
cursor_hide = 0;
}
Expand Down

0 comments on commit bc73e87

Please sign in to comment.