From nobody Tue Feb 10 13:16:55 2026 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=1619743760; cv=none; d=zohomail.com; s=zohoarc; b=Efq6SXOMISZPtioRI60EcN7A/8EhwB/I6v2TUPKHR9yh0tKTy1UwQk1gTKmhIzR4ETbs8QUnf4Q8GjbLtll3D/Zo0nwyAbllbMwaCS/dEjrtj34WdV2iAGUJ5bYbTtHglUX8IMgOC+AnHx81/nssHJ9RofJUZLd1qJ4IR1MRNm8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1619743760; 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=+UHQmShk/9uBSP5zRHcNmBIM4hq62zux78UOIbsrsgE=; b=FgE/eYYlIuEDpmeA2zqs2z2sjm+NUGWUpYlGRAWY1jg7Qp5QBisEnDTv9YHLLABP+Z01O2aRcIRWIFotC+j1N4P6OP3QaWPbyzJAfdGA2CBwd5U6QgOL6xALoDArK2E2Nqrl4Um8CniqakewURGTpVgV89ZU/mqvJoMXdTKk4iQ= 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 1619743760009423.66156466673783; Thu, 29 Apr 2021 17:49:20 -0700 (PDT) Received: from localhost ([::1]:46986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lcHLK-0004ao-Hi for importer@patchew.org; Thu, 29 Apr 2021 20:49:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50684) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lcGNq-0003Z3-L6 for qemu-devel@nongnu.org; Thu, 29 Apr 2021 19:47:50 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:36563) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lcGNm-0007dS-Ib for qemu-devel@nongnu.org; Thu, 29 Apr 2021 19:47:50 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 1E3055C00E0; Thu, 29 Apr 2021 19:47:45 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 29 Apr 2021 19:47:45 -0400 Received: from Gustavos-Mini.box (unknown [201.80.1.6]) by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 29 Apr 2021 19:47:43 -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=+UHQmShk/9 uBSP5zRHcNmBIM4hq62zux78UOIbsrsgE=; b=NxxkKYbx3U9/XTPuAVMIfXeBEs tbDSxkOHxp+aNuH7SNwv7AnHhL1s+cDaR9Pgt4iusbVgE2NXALpmIP95bg+QZIBz WG+Kr67cw2bQreUXMLDFvPiZ9juK165VcjEyrIIlusD08nfwUEg66JFidzyBr2Gn iEhzhBoCWcCx6jsVk= 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=+UHQmShk/9uBSP5zRHcNmBIM4hq62zux78UOIbsrsgE=; b=HmStJk4z vSVdjC/8sRz1UexShck8IsOkcyDjY0S5SOCufe4WKobL17X2/Fr34z/p3LvEgtA/ 1mg7e1VUuRpyXlOZhuQyuXg0cV1Md8u717TcS9Zhx060n/oX7WD7ZkQKM7AA/hlp VdpJ0N3lFOLvHZzT5hV6K5G+0WxBbhALzTGDnFbr28Tn7/29WVc64oDbxoW2xyH8 DwvQKhp3e1L+XNIHFuXFbkV3LKBCf/kMMxhfrfN0O6WGN/GMkjxKK5xGyHlmtmvp XOD4SbSuY3+K9s888ZORT7s+0TvMOWqqqzikRm9tVBFEhZYrrX4hLtYTG4wAldX0 kDRON7msYqmQdw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddvhedgvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpehguhhsthgr vhhosehnohhrohhnhhgrrdgvthhirdgsrhenucggtffrrghtthgvrhhnpeevhfdvjeelve etiedutdfhleegleffkeffjefgtdeggfekheehledttdffvdelleenucffohhmrghinhep rghpphhlvgdrtghomhenucfkphepvddtuddrkedtrddurdeinecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhushhtrghvohesnhhorhhonhhh rgdrvghtihdrsghr X-ME-Proxy: From: gustavo@noronha.eti.br To: qemu-devel@nongnu.org Subject: [PATCH 1/2] ui/cocoa: capture all keys and combos when mouse is grabbed Date: Thu, 29 Apr 2021 20:47:04 -0300 Message-Id: <20210429234705.83206-2-gustavo@noronha.eti.br> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210429234705.83206-1-gustavo@noronha.eti.br> References: <20210429234705.83206-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.26; envelope-from=gustavo@noronha.eti.br; helo=out2-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_H3=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: Thu, 29 Apr 2021 20:48: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 ' , Gustavo Noronha Silva , 'Gerd Hoffmann ' 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 Signed-off-by: Gustavo Noronha Silva Acked-by: Markus Armbruster --- qapi/ui.json | 15 ++++++++++ qemu-options.hx | 3 ++ ui/cocoa.m | 73 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/qapi/ui.json b/qapi/ui.json index 1052ca9c38..77bc00fd0d 100644 --- a/qapi/ui.json +++ b/qapi/ui.json @@ -1088,6 +1088,20 @@ { '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/mh3= 2356/mac +# +## +{ 'struct' : 'DisplayCocoa', + 'data' : { '*full-grab' : 'bool' } } + ## # @DisplayType: # @@ -1153,6 +1167,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..f1e4449082 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 to:(BOOL)value; - (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 to:(BOOL)value +{ + COCOA_DEBUG("QemuCocoaView: setFullGrab\n"); + + screen.full_grab =3D value; +} + - (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 to:(BOOL)value +{ + COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n"); + + [cocoaView setFullGrab:sender to:value]; +} + /* Tries to find then open the specified filename */ - (void) openDocumentation: (NSString *) filename { @@ -1877,16 +1940,22 @@ 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 to:opts->u.cocoa.full_= grab]; }); } if (opts->has_show_cursor && opts->show_cursor) { cursor_hide =3D 0; - } + }; =20 // register vga output callbacks register_displaychangelistener(&dcl); --=20 2.24.3 (Apple Git-128)