From patchwork Tue Jul 13 21:31:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gustavo@noronha.dev.br X-Patchwork-Id: 1504933 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=noronha.dev.br header.i=@noronha.dev.br header.a=rsa-sha256 header.s=fm1 header.b=sc+n45tw; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=RIfCSbm8; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GPbkZ4YlMz9sX2 for ; Wed, 14 Jul 2021 09:02:29 +1000 (AEST) Received: from localhost ([::1]:35594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m3RQ2-000055-3C for incoming@patchwork.ozlabs.org; Tue, 13 Jul 2021 19:02:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47692) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m3Q0u-0001AN-UG for qemu-devel@nongnu.org; Tue, 13 Jul 2021 17:32:25 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:57575) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m3Q0p-0007W0-7i for qemu-devel@nongnu.org; Tue, 13 Jul 2021 17:32:24 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 85B5432003F4; Tue, 13 Jul 2021 17:32:16 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 13 Jul 2021 17:32:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=noronha.dev.br; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=naaBrd+2qLINT K+xuVpCpntXavXHYDURD0Oy4NYVtRM=; b=sc+n45twM8efMdnFfX0c5d2vwFwkF AWgOQtXjYgGjIsnmXtZ0R1adU/qttvFgscVwphfCrQC0PSR9YQO3EX7m+HOsHWSf 4y3VRmtKzpS9TewFOHrfVlfhvbXd9ARyejPnKrosVrfKIf7g13WTyGypxgaEHDVG N7VKKx6KyMVmcStrojNZ8QiOamsV1E+eHaeMnnY5tAKppPvCrE6FD7x2ZmvoIBOV dOnCdzO7lTOmRGdKmXWKaiQn5uykQOJQWY4V8uXHJWa7wDuc2OqmVgE4FYKJSgOu d36qyRF9ddZGdKCRxYgyTxPiVUJaTnGYRr1BjEP1NOcfK71heJuI65WtQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=naaBrd+2qLINTK+xuVpCpntXavXHYDURD0Oy4NYVtRM=; b=RIfCSbm8 zOiy6dqiDCHK+42oNr/1d2DsZoj0XHGuHzJyS98oiBp6OmzEU3M+ae5Xye9leBXs qBfg/bdmxnG6NnE+6YQFqj7ibxbLO72Kym69/Qt3kLfTIalaQAp2AlDCCAmx4cDy yg5J3RZL2dUPPBfJ3vOG38tB/XRwDnHtvtHf9oBFj73oVR4tNItUPRY+xnHiN4Gq BFwN/U/p+34xcbcsJvcXwKGvN8rHJUmWSl8eyvuBdBf1q+1M9IEl9u42I+8XyMSx ZavuKS8i3/R1CVP6ubvUNWXPfCNT7NKsBBAtJeSsezjsDKfBGflPl4l4skpFb6WW Nma9vPDEicw/LA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudeigddukecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepghhushhtrghv ohesnhhorhhonhhhrgdruggvvhdrsghrnecuggftrfgrthhtvghrnhepheeigfeigfejje ekiedtgfehieetheeikeetudffveeifefgtefhveehvdfhgeehnecuffhomhgrihhnpegr phhplhgvrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomhepghhushhtrghvohesnhhorhhonhhhrgdruggvvhdrsghr X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 13 Jul 2021 17:32:13 -0400 (EDT) From: gustavo@noronha.dev.br To: qemu-devel@nongnu.org Subject: [PATCH v5 1/2] ui/cocoa: capture all keys and combos when mouse is grabbed Date: Tue, 13 Jul 2021 18:31:59 -0300 Message-Id: <20210713213200.2547-2-gustavo@noronha.dev.br> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20210713213200.2547-1-gustavo@noronha.dev.br> References: <20210713213200.2547-1-gustavo@noronha.dev.br> MIME-Version: 1.0 Received-SPF: pass client-ip=64.147.123.25; envelope-from=gustavo@noronha.dev.br; helo=wout2-smtp.messagingengine.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 13 Jul 2021 19:00:59 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: 'Peter Maydell ' , 'Markus Armbruster ' , 'Gerd Hoffmann ' , 'Akihiko Odaki ' , 'Eric Blake ' , Gustavo Noronha Silva Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Gustavo Noronha Silva Applications such as Gnome may use Alt-Tab and Super-Tab for different purposes, some use Ctrl-arrows so we want to allow qemu to handle everything when it captures the mouse/keyboard. However, Mac OS handles some combos like Command-Tab and Ctrl-arrows at an earlier part of the event handling chain, not letting qemu see it. We add a global Event Tap that allows qemu to see all events when the mouse is grabbed. Note that this requires additional permissions. See: https://developer.apple.com/documentation/coregraphics/1454426-cgeventtapcreate?language=objc#discussion https://support.apple.com/en-in/guide/mac-help/mh32356/mac Acked-by: Markus Armbruster Reviewed-by: Akihiko Odaki Signed-off-by: Gustavo Noronha Silva --- qapi/ui.json | 16 ++++++++++++ qemu-options.hx | 3 +++ ui/cocoa.m | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/qapi/ui.json b/qapi/ui.json index fd9677d48e..b2d27ce167 100644 --- a/qapi/ui.json +++ b/qapi/ui.json @@ -1092,6 +1092,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: # @@ -1165,6 +1180,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)' } diff --git a/qemu-options.hx b/qemu-options.hx index 8965dabc83..8308d28d1f 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1796,6 +1796,9 @@ DEF("display", HAS_ARG, QEMU_OPTION_display, #if defined(CONFIG_CURSES) "-display curses[,charset=]\n" #endif +#if defined(CONFIG_COCOA) + "-display cocoa[,full_grab=on|off]\n" +#endif #if defined(CONFIG_OPENGL) "-display egl-headless[,rendernode=]\n" #endif diff --git a/ui/cocoa.m b/ui/cocoa.m index 9f72844b07..d9d98aa9de 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -309,11 +309,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; @@ -336,6 +338,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 { @@ -361,6 +376,11 @@ - (void) dealloc } qkbd_state_free(kbd); + + if (eventsTap) { + CFRelease(eventsTap); + } + [super dealloc]; } @@ -635,6 +655,36 @@ - (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) toggleKey: (int)keycode { qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode)); } @@ -1260,6 +1310,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 { @@ -2031,11 +2088,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] 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->has_show_cursor && opts->show_cursor) { From patchwork Tue Jul 13 21:32:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gustavo@noronha.dev.br X-Patchwork-Id: 1504935 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=noronha.dev.br header.i=@noronha.dev.br header.a=rsa-sha256 header.s=fm1 header.b=l1YixMrb; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=Pr0XpPr+; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GPbky6SNNz9sW8 for ; Wed, 14 Jul 2021 09:02:50 +1000 (AEST) Received: from localhost ([::1]:37870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m3RQO-0001kJ-Jk for incoming@patchwork.ozlabs.org; Tue, 13 Jul 2021 19:02:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47714) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m3Q10-0001Bp-H2 for qemu-devel@nongnu.org; Tue, 13 Jul 2021 17:32:31 -0400 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:52697) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m3Q0r-0007ZP-1y for qemu-devel@nongnu.org; Tue, 13 Jul 2021 17:32:28 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 828943200805; Tue, 13 Jul 2021 17:32:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 13 Jul 2021 17:32:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=noronha.dev.br; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=gdfQ+oG0IrXyq pJsobMKx2n7+BD6BP5j4n/cFUf3l6c=; b=l1YixMrbIsv4xtyxexQZ4kEODGhkd aYCaG10zuVmSqaH5ocayc82/Hc/TJQDik9nSLXluifQBUISLVurJGjw9a2rt6Ggf V1cDn+zuw2w2uE0v/jo/IdIAsj/0CTN63DmbvBScKIJ24MzZZ7a08hO/PuxgQemA YiWX8851k7KnaSPNK5dIILWOWoTQ1BQk++IXLuRiLvzNJOneDNZthmRJJQ/ZoF+2 H7JPj+oycDmb8jseordISKDP8RlWn++jJUmCV+gq/9eQ6t6SZmIr841XnmJz1qQk JSSGLIewqgPh9SakLaYTLXf84WFCnnfnw/vCJQbFQstLSBFb8TvT6xngQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=gdfQ+oG0IrXyqpJsobMKx2n7+BD6BP5j4n/cFUf3l6c=; b=Pr0XpPr+ xbhqIL/uyxoAhd+BoW5fcjrePpo+kL2WXjkZqney06IAuPWEbLYEICGHc5s/LjIs iWI74Xy0uJ/8inp6hRMSNkUNJx4SIgQ17p0RVTcawEQTQNh3UAjlT5O0HyfCWuX7 BqBjxyEIokBlzNc6aelhQJw0B0SrjOF71lnGX7jAIOuMmDbgE/dNOgDiBJksfy8M 1i5qlrvjdvdqBMMBiFnW6nbxhMAtFpO+uvbPeH16YmWGjktoR0eecYEJJy99oCsg YtkUXJI1ONL7NJ9S4FtY1qITtVJ3KKfhFBfL+JkldRl1i1tHDk14AlAqt3fra38I LacwKtFkYKOKzg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudeigddukecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepghhushhtrghv ohesnhhorhhonhhhrgdruggvvhdrsghrnecuggftrfgrthhtvghrnhepheeigfeigfejje ekiedtgfehieetheeikeetudffveeifefgtefhveehvdfhgeehnecuffhomhgrihhnpegr phhplhgvrdgtohhmnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilh hfrhhomhepghhushhtrghvohesnhhorhhonhhhrgdruggvvhdrsghr X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 13 Jul 2021 17:32:17 -0400 (EDT) From: gustavo@noronha.dev.br To: qemu-devel@nongnu.org Subject: [PATCH v5 2/2] ui/cocoa: add option to swap Option and Command Date: Tue, 13 Jul 2021 18:32:00 -0300 Message-Id: <20210713213200.2547-3-gustavo@noronha.dev.br> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20210713213200.2547-1-gustavo@noronha.dev.br> References: <20210713213200.2547-1-gustavo@noronha.dev.br> MIME-Version: 1.0 Received-SPF: pass client-ip=64.147.123.25; envelope-from=gustavo@noronha.dev.br; helo=wout2-smtp.messagingengine.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 13 Jul 2021 19:01:06 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: 'Peter Maydell ' , 'Markus Armbruster ' , 'Gerd Hoffmann ' , 'Akihiko Odaki ' , 'Eric Blake ' , Gustavo Noronha Silva Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Gustavo Noronha Silva 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 Reviewed-by: Akihiko Odaki Signed-off-by: Gustavo Noronha Silva --- qapi/ui.json | 8 ++++++- qemu-options.hx | 3 ++- ui/cocoa.m | 64 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/qapi/ui.json b/qapi/ui.json index b2d27ce167..7d8d3b532e 100644 --- a/qapi/ui.json +++ b/qapi/ui.json @@ -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: diff --git a/qemu-options.hx b/qemu-options.hx index 8308d28d1f..7cdacc83ae 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1797,7 +1797,8 @@ DEF("display", HAS_ARG, QEMU_OPTION_display, "-display curses[,charset=]\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=]\n" diff --git a/ui/cocoa.m b/ui/cocoa.m index d9d98aa9de..e23060da53 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -73,6 +73,7 @@ typedef struct { int width; int height; + bool swap_option_command; } QEMUScreen; static void cocoa_update(DisplayChangeListener *dcl, @@ -330,6 +331,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; */ - (BOOL) isMouseGrabbed; - (BOOL) isAbsoluteEnabled; +- (BOOL) isSwapOptionCommandEnabled; - (float) cdx; - (float) cdy; - (QEMUScreen) gscreen; @@ -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)); } @@ -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]) { @@ -871,13 +890,21 @@ - (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; @@ -885,14 +912,22 @@ - (bool) handleEventLocked:(NSEvent *)event 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; } @@ -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;} @@ -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 { @@ -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; }