From nobody Tue Feb 10 17:14:51 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=1620759483; cv=none; d=zohomail.com; s=zohoarc; b=WlbYxlPjAbECIP3Gy8vLmh+DrSkOCHqIX+tTettE0sDGTdTOjmOPMLnzkpWW68PMHShQvfatFa5b5e6rhR6muEkCKdYrALWPncTPS3NYG24cm+4IRpobJ9kKuIxxf08WA3T0eG4uZOvDUylwRX9/umzjupq7TK3Vhrel0V5BVcc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620759483; 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=D2T8NY1H7DNsJIBk09fp01HKORi2P63E6m7ppSDfpZk=; b=W49wlSPWWbpWmOuYJ8tkoD9vWZHAc8BJOc4uh95jhHxPcwdwX8VtjY7quWepMsUryp8PlpCfWcJIAK/EXCq0+pRFfBX4eTOeDLG2JPr6ei4KPIp2qnC/h+Z5pMLN1gUjgwpkJ+oHPTWqcLy8BQtFPIsRGsidLWdXIajaIkFHRU0= 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 1620759483338234.0412311944766; Tue, 11 May 2021 11:58:03 -0700 (PDT) Received: from localhost ([::1]:51376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgXZy-0004QZ-6Z for importer@patchew.org; Tue, 11 May 2021 14:58:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46336) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgXY4-00007e-TH for qemu-devel@nongnu.org; Tue, 11 May 2021 14:56:04 -0400 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:47581) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgXY0-0005RS-Qs for qemu-devel@nongnu.org; Tue, 11 May 2021 14:56:04 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 0B8811714; Tue, 11 May 2021 14:55:57 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 11 May 2021 14:55:58 -0400 Received: from localhost.localdomain (unknown [201.80.1.6]) by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 11 May 2021 14:55:54 -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=D2T8NY1H7D NsJIBk09fp01HKORi2P63E6m7ppSDfpZk=; b=WaS+wRoMPXdMBcyE1Jo5PfuJC5 Do0BOtcWZR2gqOcw8yeLTJYnrekv09NfKcYevOUeQKx232HJR1RfBJGY3irXQ3qc s+wkPRbdF2IxE5a1tZ8a3NI5toFEFktHaPQmomKDldUZtTL7jeVb9B1OjMCa0Vk5 K0NiStfe5Z2Qbd1Bc= 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=D2T8NY1H7DNsJIBk09fp01HKORi2P63E6m7ppSDfpZk=; b=LEDRLijk 93XmwAzNFRN/0ZhiYZDYl+P/CdFf/1DY108TR+jJAcTHfWR/5eQmKBXDuqVDL+PH netUDfJt8F6tufyOMinNGrktDZYJgfUaOovrsYX83DFt+g5tWmoM7NrBqyDBQePK m/qM0zx1V0S2KJY1DkYxlwdbpZlDMa0r+LSIvIndGTUUAHgsnwwpnYk/vR/fPdIe iTXuDX+BS259zxvs3Wamo87hm+BTpv9uK2yMjfLOFNMm8qlTE7GIYhNBWnWBZHRu c03h+FcO0RINEaQlV0BCVLSq2ilOvCWEaDpQ6LbKD++kLNHbCa9lTzkGspS3zZO1 gaZ9lLZv2D1T0w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdehtddgudefvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepghhushht rghvohesnhhorhhonhhhrgdrvghtihdrsghrnecuggftrfgrthhtvghrnhepvefhvdejle evteeiuddthfelgeelffekffejgfdtgefgkeehheeltddtffdvleelnecuffhomhgrihhn pegrphhplhgvrdgtohhmnecukfhppedvtddurdektddruddrieenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehguhhsthgrvhhosehnohhrohhn hhgrrdgvthhirdgsrh X-ME-Proxy: From: gustavo@noronha.eti.br To: qemu-devel@nongnu.org Subject: [PATCH v4 1/2] ui/cocoa: capture all keys and combos when mouse is grabbed Date: Tue, 11 May 2021 15:55:37 -0300 Message-Id: <20210511185538.29344-2-gustavo@noronha.eti.br> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20210511185538.29344-1-gustavo@noronha.eti.br> References: <20210511185538.29344-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=64.147.123.21; envelope-from=gustavo@noronha.eti.br; helo=wout5-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 ' , 'Akihiko Odaki ' , 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 | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 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 7c825f81fc..eeaa5c73e9 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..236352deac 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -304,11 +304,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; @@ -331,6 +333,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 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 { @@ -356,6 +371,11 @@ - (void) dealloc } =20 qkbd_state_free(kbd); + + if (eventsTap) { + CFRelease(eventsTap); + } + [super dealloc]; } =20 @@ -593,6 +613,36 @@ - (void) toggleFullScreen:(id)sender } } =20 +- (void) setFullGrab:(id)sender +{ + COCOA_DEBUG("QemuCocoaView: setFullGrab\n"); + + 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; + } 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; + } + + 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; + } + + CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode); + CFRelease(tapEventsSrc); +} + - (void) toggleKey: (int)keycode { qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode)); } @@ -1208,6 +1258,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 +1934,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.30.1 (Apple Git-130)