From nobody Fri May 3 23:26:13 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1620171343; cv=none; d=zohomail.com; s=zohoarc; b=QcuLew5JBNsVzuKzb3WOlx8fCFtTxBNsxU2+8jL4uStD9WbXuHe5CuHlz/jMDQhFQfHHRUWAvuZmSmzBhO8Uw4IGzDzYIImzHOxhT4VzrT/RLl61JwYTUmSf5/dibdMJQs8hHlwCIayqaj9jZL3DRN8mudE7pGfT3lp0xSsJQJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620171343; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8jmGEAv85DLzJzIloblsgJK2DiPtX2lRLHYelg1P8Ts=; b=WY2JtcjeZXnBdgVJko5BXIzwpRTqZ3QzC+VT4JLg+hPNXu7NqozC55c3Ie5nA6Htuaj3RaRYy8m7YQ6Im33UagmcbMUtFGviOSrzBcP15PGtNbNh4sEP9JK67CsYmZhX3PhDq7mR49m9WSusk4VSg1uiukhkceUYB+Bv73XHs2U= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620171343815193.55519188475728; Tue, 4 May 2021 16:35:43 -0700 (PDT) Received: from localhost ([::1]:58976 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1le4Zq-00054T-NO for importer@patchew.org; Tue, 04 May 2021 19:35:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1le4X5-0002rA-4E for qemu-devel@nongnu.org; Tue, 04 May 2021 19:32:51 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:49735) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1le4X3-0004lj-4x for qemu-devel@nongnu.org; Tue, 04 May 2021 19:32:50 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 96F9B5C003F; Tue, 4 May 2021 19:32:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 04 May 2021 19:32:47 -0400 Received: from Gustavos-Mini.box (unknown [201.80.1.6]) by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 4 May 2021 19:32:45 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=noronha.eti.br; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=mesmtp; bh=8jmGEAv85D LzJzIloblsgJK2DiPtX2lRLHYelg1P8Ts=; b=DnKK66jOm/et1QfCgVVinmQr2v EUb2P20jjKaUKljm7mk24AF8CVQQesSTTu1725tQmF40ROnYSaXjauAkAu+APWU/ zQXydwJYi5MFR1fMP6SsFVqUR3IB6saSq9imhljqPCBlHEX+SMtIS5m9zbnHhq4F i6cESR4e7qEcy2WnA= 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= fm2; bh=8jmGEAv85DLzJzIloblsgJK2DiPtX2lRLHYelg1P8Ts=; b=h+YC1k9N 5yhb+PzkbEzUK+fKPxCx2JzL9WliGML3grDtJFtIw4bhvqT99GLbDDwp4xNU5fKy TvEPdcpFEzVURQFizCCoZFNWxNqLxTkCJobARZ5vJNNtGodDdKqOpzb1XNvg/aJM cTPC3zF8/L6Wx9siDjkxe4VqUdRB+74EHVPSLhmPILxgG7rp54Xjx33Py7jMm0b0 FTu4hrzfuTmzRjSAJBYmknRRkc4H/yub5ZYlIe3rQ8ds6qDT7EKwnuKwcUgqBzPR DdY1GEgeKzR3yMJDOKsKAoUcczEm1j4HIZ0I33HmHmbtdeGrvc4g0C6c5QAJS9pj +aZ6vNGxdapHbw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdefjedgudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpehguhhsthgr vhhosehnohhrohhnhhgrrdgvthhirdgsrhenucggtffrrghtthgvrhhnpeevhfdvjeelve etiedutdfhleegleffkeffjefgtdeggfekheehledttdffvdelleenucffohhmrghinhep rghpphhlvgdrtghomhenucfkphepvddtuddrkedtrddurdeinecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhushhtrghvohesnhhorhhonhhh rgdrvghtihdrsghr X-ME-Proxy: From: gustavo@noronha.eti.br To: qemu-devel@nongnu.org Subject: [PATCH v3 1/2] ui/cocoa: capture all keys and combos when mouse is grabbed Date: Tue, 4 May 2021 20:32:31 -0300 Message-Id: <20210504233232.28423-2-gustavo@noronha.eti.br> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210504233232.28423-1-gustavo@noronha.eti.br> References: <20210504233232.28423-1-gustavo@noronha.eti.br> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=66.111.4.27; envelope-from=gustavo@noronha.eti.br; helo=out3-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-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-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 ' , Gustavo Noronha Silva Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" 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-cgeventtapcr= eate?language=3Dobjc#discussion https://support.apple.com/en-in/guide/mac-help/mh32356/mac Acked-by: Markus Armbruster Signed-off-by: Gustavo Noronha Silva --- qapi/ui.json | 16 +++++++++++ qemu-options.hx | 3 +++ ui/cocoa.m | 71 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/qapi/ui.json b/qapi/ui.json index 1052ca9c38..4ccfae4bbb 100644 --- a/qapi/ui.json +++ b/qapi/ui.json @@ -1088,6 +1088,21 @@ { 'struct' : 'DisplayCurses', 'data' : { '*charset' : 'str' } } =20 +## +# @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/m= ac +# +# 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'} } =20 diff --git a/qemu-options.hx b/qemu-options.hx index fd21002bd6..a77505241f 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1783,6 +1783,9 @@ DEF("display", HAS_ARG, QEMU_OPTION_display, #if defined(CONFIG_CURSES) "-display curses[,charset=3D]\n" #endif +#if defined(CONFIG_COCOA) + "-display cocoa[,full_grab=3Don|off]\n" +#endif #if defined(CONFIG_OPENGL) "-display egl-headless[,rendernode=3D]\n" #endif diff --git a/ui/cocoa.m b/ui/cocoa.m index 37e1fb52eb..0a45ab30e6 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -72,6 +72,7 @@ typedef struct { int width; int height; + bool full_grab; } QEMUScreen; =20 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; =20 QemuCocoaView *cocoaView; =20 +static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, = CGEventRef cgEvent, void *userInfo) +{ + QemuCocoaView *cocoaView =3D (QemuCocoaView*) userInfo; + NSEvent* event =3D [NSEvent eventWithCGEvent:cgEvent]; + if ([cocoaView isFullGrabEnabled] && [cocoaView isMouseGrabbed] && [co= coaView 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 =3D qkbd_state_init(dcl.con); =20 } + + CGEventMask mask =3D CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(= kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged); + eventsTap =3D CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, = kCGEventTapOptionDefault, + mask, handleTapEvent, self); + if (!eventsTap) { + warn_report("Could not create event tap, system key combos will no= t be captured.\n"); + return self; + } else { + COCOA_DEBUG("Global events tap created! Will capture system key co= mbos.\n"); + } + + CFRunLoopRef runLoop =3D CFRunLoopGetCurrent(); + if (!runLoop) { + warn_report("Could not obtain current CF RunLoop, system key combo= s will not be captured.\n"); + return self; + } + + CFRunLoopSourceRef tapEventsSrc =3D CFMachPortCreateRunLoopSource(kCFA= llocatorDefault, eventsTap, 0); + if (!tapEventsSrc ) { + warn_report("Could not obtain current CF RunLoop, system key combo= s will not be captured.\n"); + return self; + } + + CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode); + CFRelease(tapEventsSrc); + return self; } =20 @@ -356,6 +399,11 @@ - (void) dealloc } =20 qkbd_state_free(kbd); + + if (eventsTap) { + CFRelease(eventsTap); + } + [super dealloc]; } =20 @@ -593,6 +641,13 @@ - (void) toggleFullScreen:(id)sender } } =20 +- (void) setFullGrab:(id)sender +{ + COCOA_DEBUG("QemuCocoaView: setFullGrab\n"); + + screen.full_grab =3D 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]; } =20 +- (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, Di= splayOptions *opts) qemu_sem_wait(&app_started_sem); COCOA_DEBUG("cocoa_display_init: app start completed\n"); =20 + QemuCocoaAppController* controller =3D (QemuCocoaAppController *)[NSAp= plication 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) { --=20 2.24.3 (Apple Git-128) From nobody Fri May 3 23:26:13 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1620171253; cv=none; d=zohomail.com; s=zohoarc; b=U6xlJnNKkqcPh+zNKLBCR0uIoH1lazNXt0zPT9mGl5pXSzDV45NqeFdxdipszwAe2w7ctb+VZ28uLZoQZ39Ml/Fu5VVL7ltJbvue5NYcsqKBjBDVQLQi+/tZzb0Qp99U81chrldY6m9jDsJjROMtFD8GbtEvODANNA7/yi7bDRM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620171253; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=L9ZXHAgM413byeCQUfxLJo/5edsa3TRY8s6NJ6VKK2M=; b=XHhpQrX/Ug5udQ6P+R0nEwvepbki2sPDw/1PBfRSIL+teKkgJnacUA4yWTG0skqDwAYfQ0UeQzYxRX46VEswyxbOSRK4/JDDeaDWY2ahTRjPjX87e2i2TbBPDucZFZFvds4LcnVBGVBt17w0rNAoVus1XHDFfUXHu9Cqe+Sm2lg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620171253070266.9709128858684; Tue, 4 May 2021 16:34:13 -0700 (PDT) Received: from localhost ([::1]:55656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1le4YN-0003iw-Ug for importer@patchew.org; Tue, 04 May 2021 19:34:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1le4X6-0002rm-Eh for qemu-devel@nongnu.org; Tue, 04 May 2021 19:32:52 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:32933) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1le4X4-0004n8-6C for qemu-devel@nongnu.org; Tue, 04 May 2021 19:32:52 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id B22FC5C00F4; Tue, 4 May 2021 19:32:49 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 04 May 2021 19:32:49 -0400 Received: from Gustavos-Mini.box (unknown [201.80.1.6]) by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 4 May 2021 19:32:47 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=noronha.eti.br; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=mesmtp; bh=L9ZXHAgM41 3byeCQUfxLJo/5edsa3TRY8s6NJ6VKK2M=; b=oqf8PUOOkSv7SwRnIyZ8uF1NwK 32ifpcZugZs5bd4DwgxaYlxysbOKk1z0qjPJBN4LO9rm7p9S7pI6jU5V82YWp7Q4 cR1GTamL2YgucIFY4pY+2nxUrECsXOgLNPqhTUF6LpQak3n03nE1uzIvOSaEly5g UF7dgxOE6bhee3klA= 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= fm2; bh=L9ZXHAgM413byeCQUfxLJo/5edsa3TRY8s6NJ6VKK2M=; b=KpWXQUlK 8Znk4kiUyGxhyw3z1J11HmLz9xRUZMFXowmcTj3w7XH1Zgr8qVEm4sOglTfWvoAy YRGTIZGICKncgNjHLaGzYmoekAV682uCxAPsqT2PS4eli/mzJdHoeEOpDBdbJTLq ZsFB3dqYO8XUcmuijCySMSEwoLOKOA/GTUZPL6QktmgYKl2dj8+ZAOAEPJF0HULM DbP23DEU890UTIgV/CL7meXrimitz37gdVLZJue8o79zMeJnpvr0f2vKy81/A77O oGhriKzW/xKcz0icdARef9xIRYcfSRRIOpPdN9ZIR3Fe+8hnQieaDxyEKGhogNbA k0GMHb4IVTHrDA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdefjedgudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpehguhhsthgr vhhosehnohhrohhnhhgrrdgvthhirdgsrhenucggtffrrghtthgvrhhnpeevhfdvjeelve etiedutdfhleegleffkeffjefgtdeggfekheehledttdffvdelleenucffohhmrghinhep rghpphhlvgdrtghomhenucfkphepvddtuddrkedtrddurdeinecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhushhtrghvohesnhhorhhonhhh rgdrvghtihdrsghr X-ME-Proxy: From: gustavo@noronha.eti.br To: qemu-devel@nongnu.org Subject: [PATCH v3 2/2] ui/cocoa: add option to swap Option and Command, enable by default Date: Tue, 4 May 2021 20:32:32 -0300 Message-Id: <20210504233232.28423-3-gustavo@noronha.eti.br> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210504233232.28423-1-gustavo@noronha.eti.br> References: <20210504233232.28423-1-gustavo@noronha.eti.br> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=66.111.4.27; envelope-from=gustavo@noronha.eti.br; helo=out3-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-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-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 ' , Gustavo Noronha Silva Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Gustavo Noronha Silva On Mac OS X the Option key maps to Alt and Command to Super/Meta. This chan= ge swaps them around so that Alt is the key closer to the space bar and Meta/S= uper is between Control and Alt, like on non-Mac keyboards. It is a cocoa display option, disabled by default. Acked-by: Markus Armbruster 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 4ccfae4bbb..ee6fde46d5 100644 --- a/qapi/ui.json +++ b/qapi/ui.json @@ -1098,10 +1098,16 @@ # a global grab on key events. (default: off) # See https://support.apple.com/en-in/guide/mac-help/mh32356/m= ac # +# @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 th= em. +# (default: off) +# # Since: 6.1 ## { 'struct' : 'DisplayCocoa', - 'data' : { '*full-grab' : 'bool' } } + 'data' : { '*full-grab' : 'bool', + '*swap-option-command' : 'bool' } } =20 ## # @DisplayType: diff --git a/qemu-options.hx b/qemu-options.hx index a77505241f..6fcb0b6aaa 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -1784,7 +1784,8 @@ DEF("display", HAS_ARG, QEMU_OPTION_display, "-display curses[,charset=3D]\n" #endif #if defined(CONFIG_COCOA) - "-display cocoa[,full_grab=3Don|off]\n" + "-display cocoa[,full-grab=3Don|off]\n" + " [,swap-option-command=3Don|off]\n" #endif #if defined(CONFIG_OPENGL) "-display egl-headless[,rendernode=3D]\n" diff --git a/ui/cocoa.m b/ui/cocoa.m index 0a45ab30e6..b8fe64e252 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -73,6 +73,7 @@ int width; int height; bool full_grab; + bool swap_option_command; } QEMUScreen; =20 static void cocoa_update(DisplayChangeListener *dcl, @@ -327,6 +328,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; - (BOOL) isMouseGrabbed; - (BOOL) isAbsoluteEnabled; - (BOOL) isFullGrabEnabled; +- (BOOL) isSwapOptionCommandEnabled; - (float) cdx; - (float) cdy; - (QEMUScreen) gscreen; @@ -648,6 +650,13 @@ - (void) setFullGrab:(id)sender screen.full_grab =3D true; } =20 +- (void) setSwapOptionCommand:(id)sender +{ + COCOA_DEBUG("QemuCocoaView: setSwapOptionCommand\n"); + + screen.swap_option_command =3D true; +} + - (void) toggleKey: (int)keycode { qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode)); } @@ -797,12 +806,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); + } } =20 switch ([event type]) { @@ -834,13 +853,21 @@ - (bool) handleEventLocked:(NSEvent *)event =20 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; =20 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; =20 @@ -848,14 +875,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; =20 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; } @@ -1085,6 +1120,7 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled { - (BOOL) isMouseGrabbed {return isMouseGrabbed;} - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} - (BOOL) isFullGrabEnabled {return screen.full_grab;} +- (BOOL) isSwapOptionCommandEnabled {return screen.swap_option_command;} - (float) cdx {return cdx;} - (float) cdy {return cdy;} - (QEMUScreen) gscreen {return screen;} @@ -1271,6 +1307,13 @@ - (void) setFullGrab:(id)sender [cocoaView setFullGrab:sender]; } =20 +- (void) setSwapOptionCommand:(id)sender +{ + COCOA_DEBUG("QemuCocoaAppController: setSwapOptionCommand\n"); + + [cocoaView setSwapOptionCommand:sender]; +} + /* Tries to find then open the specified filename */ - (void) openDocumentation: (NSString *) filename { @@ -1953,6 +1996,11 @@ static void cocoa_display_init(DisplayState *ds, Dis= playOptions *opts) [[controller delegate] setFullGrab: nil]; }); } + if (opts->u.cocoa.has_swap_option_command && opts->u.cocoa.swap_option= _command) { + dispatch_async(dispatch_get_main_queue(), ^{ + [[controller delegate] setSwapOptionCommand: nil]; + }); + } if (opts->has_show_cursor && opts->show_cursor) { cursor_hide =3D 0; } --=20 2.24.3 (Apple Git-128)