From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15510905807391009.4776666444253; Mon, 25 Feb 2019 02:29:40 -0800 (PST) Received: from localhost ([127.0.0.1]:34801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDVz-0001Ap-K9 for importer@patchew.org; Mon, 25 Feb 2019 05:29:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33070) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRV-0006J9-1y for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRB-0002yt-Js for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:52 -0500 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:35613) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRB-0002vm-7m for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:41 -0500 Received: by mail-wr1-x441.google.com with SMTP id t18so9267015wrx.2 for ; Mon, 25 Feb 2019 02:24:39 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=I0kTAUlhb54s5r88zvNpSgW3m1NNSybQGlzrJJNrpNM=; b=KPWkLSUYlf/iMbA1pJsbyW7Akq+GqrnqFtKyD71vv/pgMk07ki5rVU7HlZvrhyv01K 3YjVhRwrZYrGYDcbsSn9t7830fxIgvK6g8cOH2A2IbDeDtSZ+9RA0MM9HWpLYob65ulN al0lUOPKwDUQiwUv78l1dtIIS5auRL5LpWEhcJxd/0/drE/9L8V4ghilyqvmKp6n4lfu yjEy5/nrPgJmxDcXTRj25DNFE5ZODTEhlqRP2JK7tV81G/uTqJXULsMmpF2UNuEekB3D pWsdirRcqixDPPYjJBZ8I8Lrp3NIg/R0kX8Bhnbfd1EBsdy69e2SBmBBwrgO2xCJKGv0 oCOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=I0kTAUlhb54s5r88zvNpSgW3m1NNSybQGlzrJJNrpNM=; b=oLmFtDWsb+qenOdt5BV9Z08naXZsd5Ayq/h58LnO84MqhlsuiJ75F1zeOHExPY7GGA eS3mkhzN97UAF1HzKZhlq+odptrAipJY73N6hIV4M6dEqdA6/LAQiBh3RZY0Wq+VBe9E wbjmYAXrEaotvpulRdpQ7cgcEp9Pfg6aOxA8U+xvmUWdatuLqNtUaLV18v+IoqtHq3/4 U4vxYD66NDK6akcmZnwsPDMQeYh2SolQcl3l9E+naU6OtSJP8jsOZk5+N93QzLPhABAK dzSmdQ4djdTRxgVEvoyu10u89lkB3MCfzexXWulu4ITd/43biBXukvLf/9JXgrIveI8C 3iLA== X-Gm-Message-State: AHQUAuYPffIuklFoLDtvsJ/4NfT+wNpD9H1c3Zoiux/Qyngvv+YC1Xfg rCa8OHjzmt0Ofq7pePCA05yo3wJWEdM= X-Google-Smtp-Source: AHgI3IZDzpNz5o6q+LP61BL57v8XYFv4dQW1M+W7ExwYix2OhQgu3NSEb6UcO3+Ojvqbf9mpnV56OQ== X-Received: by 2002:a5d:500e:: with SMTP id e14mr11404952wrt.219.1551090277813; Mon, 25 Feb 2019 02:24:37 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:27 +0000 Message-Id: <20190225102433.22401-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 Subject: [Qemu-devel] [PATCH v3 1/7] ui/cocoa: Ensure we have the iothread lock when calling into QEMU X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The Cocoa UI should run on the main thread; this is enforced in OSX Mojave. In order to be able to run on the main thread, we need to make sure we hold the iothread lock whenever we call into various QEMU UI midlayer functions. Signed-off-by: Peter Maydell Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-2-peter.maydell@linaro.org --- Changes since v2: add with_iothread_lock wrap to the qmp_stop()/qmp_cont() calls --- ui/cocoa.m | 91 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index e2567d6946..f1171c4865 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,6 +129,21 @@ NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 +// Utility function to run specified code block with iothread lock held +typedef void (^CodeBlock)(void); + +static void with_iothread_lock(CodeBlock block) +{ + bool locked =3D qemu_mutex_iothread_locked(); + if (!locked) { + qemu_mutex_lock_iothread(); + } + block(); + if (!locked) { + qemu_mutex_unlock_iothread(); + } +} + // Mac to QKeyCode conversion const int mac_to_qkeycode_map[] =3D { [kVK_ANSI_A] =3D Q_KEY_CODE_A, @@ -306,6 +321,7 @@ - (void) ungrabMouse; - (void) toggleFullScreen:(id)sender; - (void) handleMonitorInput:(NSEvent *)event; - (void) handleEvent:(NSEvent *)event; +- (void) handleEventLocked:(NSEvent *)event; - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; /* The state surrounding mouse grabbing is potentially confusing. * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated @@ -649,8 +665,14 @@ - (void) handleMonitorInput:(NSEvent *)event =20 - (void) handleEvent:(NSEvent *)event { - COCOA_DEBUG("QemuCocoaView: handleEvent\n"); + with_iothread_lock(^{ + [self handleEventLocked:event]; + }); +} =20 +- (void) handleEventLocked:(NSEvent *)event +{ + COCOA_DEBUG("QemuCocoaView: handleEvent\n"); int buttons =3D 0; int keycode =3D 0; bool mouse_event =3D false; @@ -945,15 +967,18 @@ - (QEMUScreen) gscreen {return screen;} */ - (void) raiseAllKeys { - int index; const int max_index =3D ARRAY_SIZE(modifiers_state); =20 - for (index =3D 0; index < max_index; index++) { - if (modifiers_state[index]) { - modifiers_state[index] =3D 0; - qemu_input_event_send_key_qcode(dcl->con, index, false); - } - } + with_iothread_lock(^{ + int index; + + for (index =3D 0; index < max_index; index++) { + if (modifiers_state[index]) { + modifiers_state[index] =3D 0; + qemu_input_event_send_key_qcode(dcl->con, index, false); + } + } + }); } @end =20 @@ -1178,7 +1203,9 @@ - (void)displayConsole:(id)sender /* Pause the guest */ - (void)pauseQEMU:(id)sender { - qmp_stop(NULL); + with_iothread_lock(^{ + qmp_stop(NULL); + }); [sender setEnabled: NO]; [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; [self displayPause]; @@ -1187,7 +1214,9 @@ - (void)pauseQEMU:(id)sender /* Resume running the guest operating system */ - (void)resumeQEMU:(id) sender { - qmp_cont(NULL); + with_iothread_lock(^{ + qmp_cont(NULL); + }); [sender setEnabled: NO]; [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; [self removePause]; @@ -1215,13 +1244,17 @@ - (void)removePause /* Restarts QEMU */ - (void)restartQEMU:(id)sender { - qmp_system_reset(NULL); + with_iothread_lock(^{ + qmp_system_reset(NULL); + }); } =20 /* Powers down QEMU */ - (void)powerDownQEMU:(id)sender { - qmp_system_powerdown(NULL); + with_iothread_lock(^{ + qmp_system_powerdown(NULL); + }); } =20 /* Ejects the media. @@ -1237,9 +1270,11 @@ - (void)ejectDeviceMedia:(id)sender return; } =20 - Error *err =3D NULL; - qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], - false, NULL, false, false, &err); + __block Error *err =3D NULL; + with_iothread_lock(^{ + qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding= ], + false, NULL, false, false, &err); + }); handleAnyDeviceErrors(err); } =20 @@ -1271,16 +1306,18 @@ - (void)changeDeviceMedia:(id)sender return; } =20 - Error *err =3D NULL; - qmp_blockdev_change_medium(true, - [drive cStringUsingEncoding: - NSASCIIStringEncoding], - false, NULL, - [file cStringUsingEncoding: - NSASCIIStringEncoding], - true, "raw", - false, 0, - &err); + __block Error *err =3D NULL; + with_iothread_lock(^{ + qmp_blockdev_change_medium(true, + [drive cStringUsingEncoding: + NSASCIIStringEncoding], + false, NULL, + [file cStringUsingEncoding: + NSASCIIStringEncoding], + true, "raw", + false, 0, + &err); + }); handleAnyDeviceErrors(err); } } @@ -1419,7 +1456,9 @@ - (void)adjustSpeed:(id)sender // get the throttle percentage throttle_pct =3D [sender tag]; =20 - cpu_throttle_set(throttle_pct); + with_iothread_lock(^{ + cpu_throttle_set(throttle_pct); + }); COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(),= '%'); } =20 --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090427098274.21169527219627; Mon, 25 Feb 2019 02:27:07 -0800 (PST) Received: from localhost ([127.0.0.1]:34774 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDTV-0007Wx-Un for importer@patchew.org; Mon, 25 Feb 2019 05:27:06 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33072) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRV-0006JD-25 for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRB-0002yk-ID for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:52 -0500 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:34085) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRB-0002xK-70 for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:41 -0500 Received: by mail-wm1-x344.google.com with SMTP id o10so2590278wmc.1 for ; Mon, 25 Feb 2019 02:24:40 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bmDgQhAr7tyLraO8i1ouLXjQaT6+r93opd/eZp7ELdo=; b=pRGpeWJz5vrS4mnjdmlsnOSZZ6wB2kwACIQBn4lYKCRgpxn8yhruTrIf9WoDkneG+r Z8zw/0PCM3Lj9PYPXIU3q4dhK4dZwlv3QCS2UKftACza0xSpe9smXSDeZGMZClqtGDpS uhCGm5NzOgr1XlDJ9GiTWMGChBgXGMFkFdpKBDQQ1+NunvUQRsh0cUv5Ynf8UxjuPb4G ekGAq7lB42WSrdhE3H0OPgsYwbr2njzwBZRuCePg/YaLqepTLBoNNWgTuslqHOQ1BEHV IGNdVRZfPcSvjOWNWeDv0sFestBTlwV1yoSX7Qv6xKYkX/pSlXhxXfCYGYn+mqhQIje9 MzWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bmDgQhAr7tyLraO8i1ouLXjQaT6+r93opd/eZp7ELdo=; b=KtshmHDt0Jm7GAjBkJlAYBw8ZYUq1e93hYxzwfT76VZrmyF3syZF2F1lIU2pDUweZM vTIXwoUbYP16IB3pcVIIy6We0qs6Zwsyr1iQOC78aQqeHMMDmnLpZ4PqoR3+8CD7jBO8 q8dUQibx4umycJtttUILISmkCcfXPygKuBdTUnxqrfQd5LWJpNLL1rESBZn+X03XeNr+ M1iSNXzMZ/gSSdFdvRcD52FA2gSMZqPWOBlyMLpVaYrgfTUbbm8faSWsQBAzU1OW3404 DfdW8mm9paiMNzyggqNuppycslxD1KKKkZjApQg478PU4/GoPIyYWF68yC6IcT/J6+iV PPiw== X-Gm-Message-State: AHQUAub56aqYrJKUdxKa2wzSe0FANUXQe4nHZI3p1/EZaBFEOiefTLhk JLbb/HUOgml/rMGwcf1XhNdy71p7QBU= X-Google-Smtp-Source: AHgI3IZvbedHkfi40MWvk/xzKy6OhIYUce/G5Rwh8rdnR7RgOg3sdd4TniiutJqitQPKlRWN144lSQ== X-Received: by 2002:a1c:9692:: with SMTP id y140mr10469724wmd.67.1551090279517; Mon, 25 Feb 2019 02:24:39 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:28 +0000 Message-Id: <20190225102433.22401-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::344 Subject: [Qemu-devel] [PATCH v3 2/7] ui/cocoa: Use the pixman image directly in switchSurface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Currently the switchSurface method takes a DisplaySurface. We want to change our DisplayChangeListener's dpy_gfx_switch callback to do this work asynchronously on a different thread. The caller of the switch callback will free the old DisplaySurface immediately the callback returns, so to ensure that the other thread doesn't access freed data we need to switch to using the underlying pixman image instead. The pixman image is reference counted, so we will be able to take a reference to it to avoid it vanishing too early. In this commit we only change the switchSurface method to take a pixman image, and keep the flow of control synchronous for now. Signed-off-by: Peter Maydell Reviewed-by: BALATON Zoltan Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-3-peter.maydell@linaro.org --- ui/cocoa.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index f1171c4865..a913a51a2d 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -315,7 +315,7 @@ @interface QemuCocoaView : NSView BOOL isAbsoluteEnabled; BOOL isMouseDeassociated; } -- (void) switchSurface:(DisplaySurface *)surface; +- (void) switchSurface:(pixman_image_t *)image; - (void) grabMouse; - (void) ungrabMouse; - (void) toggleFullScreen:(id)sender; @@ -495,12 +495,13 @@ - (void) setContentDimensions } } =20 -- (void) switchSurface:(DisplaySurface *)surface +- (void) switchSurface:(pixman_image_t *)image { COCOA_DEBUG("QemuCocoaView: switchSurface\n"); =20 - int w =3D surface_width(surface); - int h =3D surface_height(surface); + int w =3D pixman_image_get_width(image); + int h =3D pixman_image_get_height(image); + pixman_format_code_t image_format =3D pixman_image_get_format(image); /* cdx =3D=3D 0 means this is our very first surface, in which case we= need * to recalculate the content dimensions even if it happens to be the = size * of the initial empty window. @@ -522,10 +523,10 @@ - (void) switchSurface:(DisplaySurface *)surface CGDataProviderRelease(dataProviderRef); =20 //sync host window color space with guests - screen.bitsPerPixel =3D surface_bits_per_pixel(surface); - screen.bitsPerComponent =3D surface_bytes_per_pixel(surface) * 2; + screen.bitsPerPixel =3D PIXMAN_FORMAT_BPP(image_format); + screen.bitsPerComponent =3D DIV_ROUND_UP(screen.bitsPerPixel, 8) * 2; =20 - dataProviderRef =3D CGDataProviderCreateWithData(NULL, surface_data(su= rface), w * 4 * h, NULL); + dataProviderRef =3D CGDataProviderCreateWithData(NULL, pixman_image_ge= t_data(image), w * 4 * h, NULL); =20 // update windows if (isFullscreen) { @@ -1629,7 +1630,7 @@ static void cocoa_switch(DisplayChangeListener *dcl, NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; =20 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); - [cocoaView switchSurface:surface]; + [cocoaView switchSurface:surface->image]; [pool release]; } =20 --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090519955165.7092652060237; Mon, 25 Feb 2019 02:28:39 -0800 (PST) Received: from localhost ([127.0.0.1]:34797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDUx-0000L1-Ru for importer@patchew.org; Mon, 25 Feb 2019 05:28:35 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33049) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRE-0006AL-3A for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRC-000302-SJ for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:43 -0500 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:39455) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRC-0002yz-Ju for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:42 -0500 Received: by mail-wm1-x342.google.com with SMTP id z84so7468446wmg.4 for ; Mon, 25 Feb 2019 02:24:42 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i/Mt82y4dHNRk1P+265eWJmzywuWVVP6gso7jf/FCQQ=; b=cFocpPd9Gwptx1TKBkT80B1i9+2VIdupz4itXR8DwZtWjRV4OBgxVedAhDYUgdIze9 CVKLk2yANvU1BoM/wx7dAsiiDMvZ9kKxG08hNMrf+fUEHQ7B633++QcRxwDsnNWStV4c t3ccBicEApm87C8Pu1rs6etdnwF+hePEOfLDgZvvd9urq36brMGmCbHXPhxH6uR/5/Ro jG62wQwcpdQbgYEfTmz5RJvwuBLGyANx8GRj9Zs6K80bqP+C6SzrIWsbuqhCneztHNlV 9LXvtRMxwxLV3Cu6Pdc/8COfgaNqdIKd7Q+q8X6OqoEZcl+YUfpvQIAsyc9LqfrQ7DVU IlEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=i/Mt82y4dHNRk1P+265eWJmzywuWVVP6gso7jf/FCQQ=; b=tCZhtUQk/UXH+JxNc0I/p/qUdrey18DCR7Aqa+djKGYcTRIQUvnVvIfrb1zy1/LQpl 6DSP2+DduiX7ESnzCyef3p7+9MyvbeFzvgH18AjNg06kIEe/cBl23fiC/yplRGA9cNTZ bh9XPtQ/OY3+BoiWxMr7pXKjXO1pcyOiC/l3arv1SrZhj2dgtqqhoD3qxYKMWiIX4vkV T5xynBOq+9lYjFr+xl5V5xMm0rJF32id7AvcUCf8kh1Li0BcypFHxbovTJUic2OQmBQc mmsSkX4ROt4QZIZViHDPrOJ4aB+VnpFJW2LF3Z8pSsUvCSkcWGXcIg60yQy7roP6y8UG y/Hw== X-Gm-Message-State: AHQUAuaAlI0SC1BTb0oijPhjs8LAFGz491uje1BP4U8lPt0X0HLLwegz qC6PirP1HLhuflHZpwA+gPofDGC5UCE= X-Google-Smtp-Source: AHgI3IYn2oS6gdyz96u1iikweLWDY2KJZu3l2g4k/Z9GVVqHvXuucidc88OMRYODTPigTBgoOKLG9g== X-Received: by 2002:a1c:700a:: with SMTP id l10mr10904349wmc.13.1551090281075; Mon, 25 Feb 2019 02:24:41 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:29 +0000 Message-Id: <20190225102433.22401-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::342 Subject: [Qemu-devel] [PATCH v3 3/7] ui/cocoa: Factor out initial menu creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Factor out the long code sequence in main() which creates the initial set of menus. This will make later patches which move initialization code around a bit clearer. Signed-off-by: Peter Maydell Reviewed-by: BALATON Zoltan Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-4-peter.maydell@linaro.org --- ui/cocoa.m | 78 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index a913a51a2d..4baec0b2ff 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1465,43 +1465,8 @@ - (void)adjustSpeed:(id)sender =20 @end =20 - -int main (int argc, const char * argv[]) { - - gArgc =3D argc; - gArgv =3D (char **)argv; - int i; - - /* In case we don't need to display a window, let's not do that */ - for (i =3D 1; i < argc; i++) { - const char *opt =3D argv[i]; - - if (opt[0] =3D=3D '-') { - /* Treat --foo the same as -foo. */ - if (opt[1] =3D=3D '-') { - opt++; - } - if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || - !strcmp(opt, "-vnc") || - !strcmp(opt, "-nographic") || - !strcmp(opt, "-version") || - !strcmp(opt, "-curses") || - !strcmp(opt, "-display") || - !strcmp(opt, "-qtest")) { - return qemu_main(gArgc, gArgv, *_NSGetEnviron()); - } - } - } - - NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; - - // Pull this console process up to being a fully-fledged graphical - // app with a menubar and Dock icon - ProcessSerialNumber psn =3D { 0, kCurrentProcess }; - TransformProcessType(&psn, kProcessTransformToForegroundApplication); - - [NSApplication sharedApplication]; - +static void create_initial_menus(void) +{ // Add menus NSMenu *menu; NSMenuItem *menuItem; @@ -1585,6 +1550,45 @@ int main (int argc, const char * argv[]) { menuItem =3D [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil k= eyEquivalent:@""] autorelease]; [menuItem setSubmenu:menu]; [[NSApp mainMenu] addItem:menuItem]; +} + +int main (int argc, const char * argv[]) { + + gArgc =3D argc; + gArgv =3D (char **)argv; + int i; + + /* In case we don't need to display a window, let's not do that */ + for (i =3D 1; i < argc; i++) { + const char *opt =3D argv[i]; + + if (opt[0] =3D=3D '-') { + /* Treat --foo the same as -foo. */ + if (opt[1] =3D=3D '-') { + opt++; + } + if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || + !strcmp(opt, "-vnc") || + !strcmp(opt, "-nographic") || + !strcmp(opt, "-version") || + !strcmp(opt, "-curses") || + !strcmp(opt, "-display") || + !strcmp(opt, "-qtest")) { + return qemu_main(gArgc, gArgv, *_NSGetEnviron()); + } + } + } + + NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; + + // Pull this console process up to being a fully-fledged graphical + // app with a menubar and Dock icon + ProcessSerialNumber psn =3D { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToForegroundApplication); + + [NSApplication sharedApplication]; + + create_initial_menus(); =20 // Create an Application controller QemuCocoaAppController *appController =3D [[QemuCocoaAppController all= oc] init]; --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090435314813.0756211841983; Mon, 25 Feb 2019 02:27:15 -0800 (PST) Received: from localhost ([127.0.0.1]:34777 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDTb-0007c5-0o for importer@patchew.org; Mon, 25 Feb 2019 05:27:11 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33092) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRY-0006Ls-4v for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRO-000387-06 for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:58 -0500 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:35540) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRE-00030I-5v for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:24:44 -0500 Received: by mail-wm1-x342.google.com with SMTP id y15so7502054wma.0 for ; Mon, 25 Feb 2019 02:24:43 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qKHYmK4Qjd/L8Bxby+U0AcrdM9zpOx8LLP3umMgo5pY=; b=v/u0vHn9KwcBYMfLhyQQm3KF4O/JStEDEgcg5HhwD6BX4sX82HvKXrCzcAC8xkzLf+ QGk9ZWjij1sVIBB08UkSGEIHs+mqlz7YyBQZwuVPVNBfXhM92JTkMs8tTZ8YkfUzHDey TEmMOPiIwK0fw1KXYOETlYKuGxJ3pcypG/nvNzbyRqcuxoSyxaT7FjMVdgl1WyZJLj0c bXtIJpTSO83ws+NaWClIlRDWikI0jxKsGqvfkrmMeaN+ZxmdlSUcL1SZSmh/VQbVoejs UC3UWd6hOcsog5TFuqN7UBIAXFM72avUuA3V77/hDkKEg4xyg4iwXb8qmL0wH9C2uBx/ DnQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qKHYmK4Qjd/L8Bxby+U0AcrdM9zpOx8LLP3umMgo5pY=; b=hg/IWcKm8YWcHt84SeOHyILxeDWGfQmz/+ZaQxzaVkz0UrVPZrTmIG8lAQFU8B/IYh TeZNdXi4yAXkK/EwzxwlVS22j9ExsBhW+Ub4xzNkqOCuvHprMghloXYOJjjeAjoPdytd NUq8WoM+a2G8s2dDGRxIy9Y8V+JjSqflmHbT0Rps2gr+fVXMjEh4P6WqW76pwOYkrNe7 n6cu5xu/lfzjjBU34shU871ynbfiOqR6L4QQ0WflRmINXDeq0AiR973dIYbHMUVnm5p6 foDlolu16eAOBIVjQHtt3HTC4Otmsja7IM0COeC45onxpXfc4PSZUS1ByxvyO6YJE2J4 GaVA== X-Gm-Message-State: AHQUAuY1jh3G1HlY0OqL/JAm2IlgfFReGFTXHKQWNYZYQBgfyl6EOXB2 tKMZICOCyIPyjJIb45vYwT/oXBioUec= X-Google-Smtp-Source: AHgI3IaG9i0vQrA6L2HJT6rbuYYcTmyFUkielQ0yg2ZvqxbgLuZjzZ0C2mhfz/8AZPD3zy8DIfnQsA== X-Received: by 2002:a1c:5fc5:: with SMTP id t188mr9784494wmb.86.1551090282499; Mon, 25 Feb 2019 02:24:42 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:30 +0000 Message-Id: <20190225102433.22401-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::342 Subject: [Qemu-devel] [PATCH v3 4/7] ui/cocoa: Move console/device menu creation code up in file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Move the console/device menu creation code functions further up in the source file, next to the code which creates the initial menus. We're going to want to change the location we call these functions from in the next patch. This commit is a pure code move with no other changes. Signed-off-by: Peter Maydell Reviewed-by: BALATON Zoltan Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-5-peter.maydell@linaro.org --- ui/cocoa.m | 184 ++++++++++++++++++++++++++--------------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 4baec0b2ff..d1fc1a6aff 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1552,6 +1552,98 @@ static void create_initial_menus(void) [[NSApp mainMenu] addItem:menuItem]; } =20 +/* Returns a name for a given console */ +static NSString * getConsoleName(QemuConsole * console) +{ + return [NSString stringWithFormat: @"%s", qemu_console_get_label(conso= le)]; +} + +/* Add an entry to the View menu for each console */ +static void add_console_menu_entries(void) +{ + NSMenu *menu; + NSMenuItem *menuItem; + int index =3D 0; + + menu =3D [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; + + [menu addItem:[NSMenuItem separatorItem]]; + + while (qemu_console_lookup_by_index(index) !=3D NULL) { + menuItem =3D [[[NSMenuItem alloc] initWithTitle: getConsoleName(qe= mu_console_lookup_by_index(index)) + action: @selector(displayCo= nsole:) keyEquivalent: @""] autorelease]; + [menuItem setTag: index]; + [menu addItem: menuItem]; + index++; + } +} + +/* Make menu items for all removable devices. + * Each device is given an 'Eject' and 'Change' menu item. + */ +static void addRemovableDevicesMenuItems(void) +{ + NSMenu *menu; + NSMenuItem *menuItem; + BlockInfoList *currentDevice, *pointerToFree; + NSString *deviceName; + + currentDevice =3D qmp_query_block(NULL); + pointerToFree =3D currentDevice; + if(currentDevice =3D=3D NULL) { + NSBeep(); + QEMU_Alert(@"Failed to query for block devices!"); + return; + } + + menu =3D [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; + + // Add a separator between related groups of menu items + [menu addItem:[NSMenuItem separatorItem]]; + + // Set the attributes to the "Removable Media" menu item + NSString *titleString =3D @"Removable Media"; + NSMutableAttributedString *attString=3D[[NSMutableAttributedString all= oc] initWithString:titleString]; + NSColor *newColor =3D [NSColor blackColor]; + NSFontManager *fontManager =3D [NSFontManager sharedFontManager]; + NSFont *font =3D [fontManager fontWithFamily:@"Helvetica" + traits:NSBoldFontMask|NSItalicFo= ntMask + weight:0 + size:14]; + [attString addAttribute:NSFontAttributeName value:font range:NSMakeRan= ge(0, [titleString length])]; + [attString addAttribute:NSForegroundColorAttributeName value:newColor = range:NSMakeRange(0, [titleString length])]; + [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber = numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; + + // Add the "Removable Media" menu item + menuItem =3D [NSMenuItem new]; + [menuItem setAttributedTitle: attString]; + [menuItem setEnabled: NO]; + [menu addItem: menuItem]; + + /* Loop through all the block devices in the emulator */ + while (currentDevice) { + deviceName =3D [[NSString stringWithFormat: @"%s", currentDevice->= value->device] retain]; + + if(currentDevice->value->removable) { + menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString stri= ngWithFormat: @"Change %s...", currentDevice->value->device] + action: @selector(change= DeviceMedia:) + keyEquivalent: @""]; + [menu addItem: menuItem]; + [menuItem setRepresentedObject: deviceName]; + [menuItem autorelease]; + + menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString stri= ngWithFormat: @"Eject %s", currentDevice->value->device] + action: @selector(ejectD= eviceMedia:) + keyEquivalent: @""]; + [menu addItem: menuItem]; + [menuItem setRepresentedObject: deviceName]; + [menuItem autorelease]; + } + currentDevice =3D currentDevice->next; + } + qapi_free_BlockInfoList(pointerToFree); +} + int main (int argc, const char * argv[]) { =20 gArgc =3D argc; @@ -1680,98 +1772,6 @@ static void cocoa_cleanup(void) .dpy_refresh =3D cocoa_refresh, }; =20 -/* Returns a name for a given console */ -static NSString * getConsoleName(QemuConsole * console) -{ - return [NSString stringWithFormat: @"%s", qemu_console_get_label(conso= le)]; -} - -/* Add an entry to the View menu for each console */ -static void add_console_menu_entries(void) -{ - NSMenu *menu; - NSMenuItem *menuItem; - int index =3D 0; - - menu =3D [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; - - [menu addItem:[NSMenuItem separatorItem]]; - - while (qemu_console_lookup_by_index(index) !=3D NULL) { - menuItem =3D [[[NSMenuItem alloc] initWithTitle: getConsoleName(qe= mu_console_lookup_by_index(index)) - action: @selector(displayCo= nsole:) keyEquivalent: @""] autorelease]; - [menuItem setTag: index]; - [menu addItem: menuItem]; - index++; - } -} - -/* Make menu items for all removable devices. - * Each device is given an 'Eject' and 'Change' menu item. - */ -static void addRemovableDevicesMenuItems(void) -{ - NSMenu *menu; - NSMenuItem *menuItem; - BlockInfoList *currentDevice, *pointerToFree; - NSString *deviceName; - - currentDevice =3D qmp_query_block(NULL); - pointerToFree =3D currentDevice; - if(currentDevice =3D=3D NULL) { - NSBeep(); - QEMU_Alert(@"Failed to query for block devices!"); - return; - } - - menu =3D [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; - - // Add a separator between related groups of menu items - [menu addItem:[NSMenuItem separatorItem]]; - - // Set the attributes to the "Removable Media" menu item - NSString *titleString =3D @"Removable Media"; - NSMutableAttributedString *attString=3D[[NSMutableAttributedString all= oc] initWithString:titleString]; - NSColor *newColor =3D [NSColor blackColor]; - NSFontManager *fontManager =3D [NSFontManager sharedFontManager]; - NSFont *font =3D [fontManager fontWithFamily:@"Helvetica" - traits:NSBoldFontMask|NSItalicFo= ntMask - weight:0 - size:14]; - [attString addAttribute:NSFontAttributeName value:font range:NSMakeRan= ge(0, [titleString length])]; - [attString addAttribute:NSForegroundColorAttributeName value:newColor = range:NSMakeRange(0, [titleString length])]; - [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber = numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; - - // Add the "Removable Media" menu item - menuItem =3D [NSMenuItem new]; - [menuItem setAttributedTitle: attString]; - [menuItem setEnabled: NO]; - [menu addItem: menuItem]; - - /* Loop through all the block devices in the emulator */ - while (currentDevice) { - deviceName =3D [[NSString stringWithFormat: @"%s", currentDevice->= value->device] retain]; - - if(currentDevice->value->removable) { - menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString stri= ngWithFormat: @"Change %s...", currentDevice->value->device] - action: @selector(change= DeviceMedia:) - keyEquivalent: @""]; - [menu addItem: menuItem]; - [menuItem setRepresentedObject: deviceName]; - [menuItem autorelease]; - - menuItem =3D [[NSMenuItem alloc] initWithTitle: [NSString stri= ngWithFormat: @"Eject %s", currentDevice->value->device] - action: @selector(ejectD= eviceMedia:) - keyEquivalent: @""]; - [menu addItem: menuItem]; - [menuItem setRepresentedObject: deviceName]; - [menuItem autorelease]; - } - currentDevice =3D currentDevice->next; - } - qapi_free_BlockInfoList(pointerToFree); -} - static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts) { COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090582288761.34977210504; Mon, 25 Feb 2019 02:29:42 -0800 (PST) Received: from localhost ([127.0.0.1]:34803 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDW1-0001EJ-6L for importer@patchew.org; Mon, 25 Feb 2019 05:29:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33144) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRa-0006Nw-Kp for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRZ-0003GA-Gs for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:06 -0500 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:36371) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRZ-00031H-5C for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:05 -0500 Received: by mail-wr1-x441.google.com with SMTP id o17so9272107wrw.3 for ; Mon, 25 Feb 2019 02:24:45 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wbnpQMYk0xGvZOa47pDN5/IqA0K+yihWn/3R2zb8SJU=; b=ac6cQBZktnwzMsfE2h18B4tYKoXeFdNQxkSnrrCnMovjgAy84u4RckloC6+RXPx3/q hHooS0+bi4iSnVmEufqFUDQGTbxOyi1S/Hk8636/AT/42/sALEubWjZGwU8iQact3ZER WXVgo5YjdBi3eGOVfU3f6ROY77PlNJErr7yISLN6hpPpO5NwqrLi9d1JKU58hueUBfnY I5TfnUx474mkEZ2YaJxa4eDelFa+ynCldduoDR50ww3ofxGH+2mEs2hUxO/qf9kV51KH sa0vPFDs53lbblVx3lssSZrRhaG+5QGgWIJKQEsxmGujzsK8BGgtSAdG9XqdpwjYePxj l/Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wbnpQMYk0xGvZOa47pDN5/IqA0K+yihWn/3R2zb8SJU=; b=aUTSZLICac/J6w5Kihkaottc53d/Jep09xMhWHjjGYDez/THnYttOk0dmvZQ3Ac7Xm suIx2MEr4gbi/X8EXH9N2+PzobB+9ionoWpBx+q1brnSwu/dNFewzTZIOH4s/VNFxiAU FgCB0jsvWIFCuZk5zVb+WgrEZNR5VaEqc5vBTzdKnLsVMx614H9zIOE1fNrYETuRrLfR JnyBozrD3999v4eJStFbnDWotktWllZxEQDJq9JVozeAFOjUJIDPxgAIVpYXFYs2rD14 ulunNhzp9a2GJStS/UY2uRaJzu1BlqFw5XQhCgs2jgpsenXseqw+gx8Pte0XvDkXWq8t BG7A== X-Gm-Message-State: AHQUAuZt0HZfSIvhX9rPAve0BuZC8KpGJJSEJtnPodrE+af3EpPjWO+g nD8kzKEMmqSBGL3an/KA3LMaoQSkyDo= X-Google-Smtp-Source: AHgI3IaRDmkzOP1+CVDkIv5psfsOmgeMqJxHgRHI9sD3zv96GPPKmldp5Bc3Lk+qns6zXfZVemvypA== X-Received: by 2002:adf:f543:: with SMTP id j3mr11513733wrp.220.1551090284111; Mon, 25 Feb 2019 02:24:44 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:31 +0000 Message-Id: <20190225102433.22401-6-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 Subject: [Qemu-devel] [PATCH v3 5/7] ui/cocoa: Don't call NSApp sendEvent directly from handleEvent X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Currently the handleEvent method will directly call the NSApp sendEvent method for any events that we want to let OSX deal with. When we rearrange the event handling code, the way that we say "let OSX have this event" is going to change. Prepare for that by refactoring so that handleEvent returns a flag indicating whether it consumed the event. Suggested-by: BALATON Zoltan Signed-off-by: Peter Maydell Reviewed-by: BALATON Zoltan Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-6-peter.maydell@linaro.org --- Changes since v2: remove stray whitespace --- ui/cocoa.m | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index d1fc1a6aff..1b54d42aba 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,8 +129,9 @@ NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 -// Utility function to run specified code block with iothread lock held +// Utility functions to run specified code block with iothread lock held typedef void (^CodeBlock)(void); +typedef bool (^BoolCodeBlock)(void); =20 static void with_iothread_lock(CodeBlock block) { @@ -144,6 +145,21 @@ static void with_iothread_lock(CodeBlock block) } } =20 +static bool bool_with_iothread_lock(BoolCodeBlock block) +{ + bool locked =3D qemu_mutex_iothread_locked(); + bool val; + + if (!locked) { + qemu_mutex_lock_iothread(); + } + val =3D block(); + if (!locked) { + qemu_mutex_unlock_iothread(); + } + return val; +} + // Mac to QKeyCode conversion const int mac_to_qkeycode_map[] =3D { [kVK_ANSI_A] =3D Q_KEY_CODE_A, @@ -320,8 +336,8 @@ - (void) grabMouse; - (void) ungrabMouse; - (void) toggleFullScreen:(id)sender; - (void) handleMonitorInput:(NSEvent *)event; -- (void) handleEvent:(NSEvent *)event; -- (void) handleEventLocked:(NSEvent *)event; +- (bool) handleEvent:(NSEvent *)event; +- (bool) handleEventLocked:(NSEvent *)event; - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; /* The state surrounding mouse grabbing is potentially confusing. * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated @@ -664,15 +680,16 @@ - (void) handleMonitorInput:(NSEvent *)event } } =20 -- (void) handleEvent:(NSEvent *)event +- (bool) handleEvent:(NSEvent *)event { - with_iothread_lock(^{ - [self handleEventLocked:event]; + return bool_with_iothread_lock(^{ + return [self handleEventLocked:event]; }); } =20 -- (void) handleEventLocked:(NSEvent *)event +- (bool) handleEventLocked:(NSEvent *)event { + /* Return true if we handled the event, false if it should be given to= OSX */ COCOA_DEBUG("QemuCocoaView: handleEvent\n"); int buttons =3D 0; int keycode =3D 0; @@ -743,8 +760,7 @@ - (void) handleEventLocked:(NSEvent *)event if (keycode =3D=3D Q_KEY_CODE_F) { switched_to_fullscreen =3D true; } - [NSApp sendEvent:event]; - return; + return false; } =20 // default @@ -759,12 +775,12 @@ - (void) handleEventLocked:(NSEvent *)event // enable graphic console case '1' ... '9': console_select(key - '0' - 1); /* ascii math */ - return; + return true; =20 // release the mouse grab case 'g': [self ungrabMouse]; - return; + return true; } } } @@ -781,7 +797,7 @@ - (void) handleEventLocked:(NSEvent *)event // don't pass the guest a spurious key-up if we treated this // command-key combo as a host UI action if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifie= rFlagCommand)) { - return; + return true; } =20 if (qemu_console_is_graphic(NULL)) { @@ -875,7 +891,7 @@ - (void) handleEventLocked:(NSEvent *)event mouse_event =3D false; break; default: - [NSApp sendEvent:event]; + return false; } =20 if (mouse_event) { @@ -911,10 +927,11 @@ - (void) handleEventLocked:(NSEvent *)event qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event d= eltaY]); } } else { - [NSApp sendEvent:event]; + return false; } qemu_input_event_sync(); } + return true; } =20 - (void) grabMouse @@ -1753,7 +1770,9 @@ static void cocoa_refresh(DisplayChangeListener *dcl) event =3D [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:di= stantPast inMode: NSDefaultRunLoopMode dequeue:YES]; if (event !=3D nil) { - [cocoaView handleEvent:event]; + if (![cocoaView handleEvent:event]) { + [NSApp sendEvent:event]; + } } } while(event !=3D nil); [pool release]; --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090731733904.0881465296716; Mon, 25 Feb 2019 02:32:11 -0800 (PST) Received: from localhost ([127.0.0.1]:34855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDYQ-00032g-Om for importer@patchew.org; Mon, 25 Feb 2019 05:32:10 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33191) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRf-0006SI-2p for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRZ-0003G5-GI for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:11 -0500 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]:38763) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRZ-00031q-34 for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:05 -0500 Received: by mail-wr1-x433.google.com with SMTP id v13so9262182wrw.5 for ; Mon, 25 Feb 2019 02:24:46 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OQubsVuFR/3sd2NQ4tJInwiBhLa2q6Blp0Uha5UjYKY=; b=Gmm/dKIuXWVIqy+n02OVc9/4yWqq8L+li2EJfkdMwntmm7aV4iU6ZH89q8WdBD4Hfh bQxQeb3aQirrQaRg44XL0JTseSfB+LNhQIxvndCfZzcj9uXyzKFBetMgUOGG2sSeMw0M TmN9WqmW6hXPF8FM5Lr5n/aLRh5kyL++IlZ9WG0YocanIwQu7NZYNI259mjjv6L3jZO2 iWwOinjnrku5PqrALZFZKTsR4A0UUvaaPdY6ilvC5nV2ceeQmnuEDC+I9hkPNJfTN3Xy sQUCYytBqXEOLkMNpkp/Y9THl0ZWnq1uIOeb9yPdpeOgYPQRmB5QdHExmFGp+vbCxkYp BNOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OQubsVuFR/3sd2NQ4tJInwiBhLa2q6Blp0Uha5UjYKY=; b=p1Tpgi970MIoUvHYfFDWmprcxMaLfxmx5qSiADVWYRjouIWC0XJWFMwkrUYwWaqD0N wKovDrGeICYA5QOZD14AOa+akvNccIQxf7F+gy+r3/ToBTompR+i4AtEjL/ob7LQN4uy skuTumHUTdM5Np5EKTDjD0GNrnYbRm5TEj/hfUuuJsyQQq6WQ92o3j6VatHBIxYeHzG6 eyt+Z99lQB41BeJ2IVuW5mNFjgG2STlvMlHwyM0Ly1dhBxUGsA0fU+Z4F13tqTAzEdbM 06VOIHMv/TEu+6N72B/wluEjlfUr7Z43zm9E18S2Fbr1/VqUf8htAoZ9XkNMaF2y0jZ5 F9fw== X-Gm-Message-State: AHQUAuZGMU1X4dMOqK7B9NX19Nr4K9qkbDY+L1SWB8/W8YxXkJF6Tth7 HGNIAFKm5Iw6Mpe0Mjj1d1Cvd1Qwil8= X-Google-Smtp-Source: AHgI3IYPg9H2DA00gWqAPpAfzF8KiIS8qJowQHIXtOKW4io1SOYRf2ec2ATp5c+vRvAosXgxAnCC8Q== X-Received: by 2002:adf:b784:: with SMTP id s4mr11715726wre.155.1551090285461; Mon, 25 Feb 2019 02:24:45 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:32 +0000 Message-Id: <20190225102433.22401-7-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::433 Subject: [Qemu-devel] [PATCH v3 6/7] ui/cocoa: Subclass NSApplication so we can implement sendEvent X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When we switch away from our custom event handling, we still want to be able to have first go at any events our application receives, because in full-screen mode we want to send key events to the guest, even if they would be menu item activation events. There are several ways we could do that, but one simple approach is to subclass NSApplication so we can implement a custom sendEvent method. Do that, but for the moment have our sendEvent just invoke the superclass method. Signed-off-by: Peter Maydell Reviewed-by: BALATON Zoltan Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-7-peter.maydell@linaro.org --- ui/cocoa.m | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 1b54d42aba..00e3db69c9 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1482,6 +1482,17 @@ - (void)adjustSpeed:(id)sender =20 @end =20 +@interface QemuApplication : NSApplication +@end + +@implementation QemuApplication +- (void)sendEvent:(NSEvent *)event +{ + COCOA_DEBUG("QemuApplication: sendEvent\n"); + [super sendEvent: event]; +} +@end + static void create_initial_menus(void) { // Add menus @@ -1695,7 +1706,7 @@ int main (int argc, const char * argv[]) { ProcessSerialNumber psn =3D { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToForegroundApplication); =20 - [NSApplication sharedApplication]; + [QemuApplication sharedApplication]; =20 create_initial_menus(); =20 --=20 2.17.2 (Apple Git-113) From nobody Sun Feb 8 22:31:30 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551090664956943.1349562305097; Mon, 25 Feb 2019 02:31:04 -0800 (PST) Received: from localhost ([127.0.0.1]:34849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDXI-0002Fp-RX for importer@patchew.org; Mon, 25 Feb 2019 05:31:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33162) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyDRb-0006Oo-En for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyDRZ-0003GL-LQ for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:07 -0500 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:34245) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyDRZ-00032s-91 for qemu-devel@nongnu.org; Mon, 25 Feb 2019 05:25:05 -0500 Received: by mail-wr1-x444.google.com with SMTP id f14so9283362wrg.1 for ; Mon, 25 Feb 2019 02:24:48 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id v10sm1764692wrn.26.2019.02.25.02.24.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 02:24:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E+TwchfkM+TIEcmoNkECReQjSzjUD48vWmRIG1Td3oo=; b=CXp/+gMXAlqvnf2jofD/T8w5sGQGGjvPAWgkUsBwbad3IfydgAEGotn5GJraFxhMQX qKuJS1Ll81nEZm1oHJw1ukfCJc6+mNLpsh+ElEtTmoPpceuIHa/gH8JaySJoGgsq6bcf KdFGJpeLiDkYIXCKUzh1i34te/4xOi3aQtpobsPtUlTx8SJtqd12sp4VPsDkit/PSSBx aXaI4Kfzq8ROdfa0045npbehdK2392KziLNUC3vQBcpCCFUjcHAPHgtn/b6muBlLpdfB AesSqY1PaTis/cth1OdFtvDMqUAZ1VvtV3ToPxPHyo9bM2uSXBFfb1mxeJ8iOqj4iMXj iBeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=E+TwchfkM+TIEcmoNkECReQjSzjUD48vWmRIG1Td3oo=; b=rndjW7Q762Klfa6Iv5hdkKaZd7YS4OyCzg/5i3IyrpJwmWbEP1Lyw+nTG/JJMDD+Xf 13pPEhyJ6x8thjNb62xs0VKMHXyvC2nvrFzwQnU2jVEizXQ+c2FCMuUeNy1o0Z41rUQW 3ge5d7phDzEYbruBUEoRM5OtHwFxlr0Eaf3N5JZUiIQbFcSV/Xr+VW73IBZ6AxMNIJBK I4UHIE61HbKqZ9nZ2JleSMKkrAHoohrK39rFyGMotGLtsXrd9hPBIZqwOrUcWBW0Na8L /vdbRvdotXy1Qk4Cily59+lkqGQyQnd3nNdYZmqbs+BYtP3vcc7q3IzKZEfxb0p5XdV1 MRjA== X-Gm-Message-State: AHQUAuZ2uSJurR+K7iQMSa8pZtaLIJoAqhW/prbF39KMhFPnIo+2OeZZ 4dcXZIoU0+R8W8UhS/dSOiQ8GBencDc= X-Google-Smtp-Source: AHgI3IYPaegnOZd9wAA5f18K6Jmd/lpC+s6bp7x8oEhLFO5lX8Gwl/vlpNJAFkEWPAdTPKfaZxUxlg== X-Received: by 2002:adf:9c85:: with SMTP id d5mr11903955wre.68.1551090286967; Mon, 25 Feb 2019 02:24:46 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Feb 2019 10:24:33 +0000 Message-Id: <20190225102433.22401-8-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225102433.22401-1-peter.maydell@linaro.org> References: <20190225102433.22401-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::444 Subject: [Qemu-devel] [PATCH v3 7/7] ui/cocoa: Perform UI operations only on the main thread X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Bolshakov , patches@linaro.org, John Arbuckle , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The OSX Mojave release is more picky about enforcing the Cocoa API restriction that only the main thread may perform UI calls. To accommodate this we need to restructure the Cocoa code: * the special OSX main() creates a second thread and uses that to call the vl.c qemu_main(); the original main thread goes into the OSX event loop * the refresh, switch and update callbacks asynchronously tell the main thread to do the necessary work * the refresh callback no longer does the "get events from the UI event queue and handle them" loop, since we now use the stock OSX event loop. Instead our NSApplication sendEvent method will either deal with them or pass them on to OSX All these things have to be changed in one commit, to avoid breaking bisection. Note that since we use dispatch_get_main_queue(), this bumps our minimum version requirement to OSX 10.10 Yosemite (released in 2014, unsupported by Apple since 2017). Signed-off-by: Peter Maydell Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190214102816.3393-8-peter.maydell@linaro.org --- Changes since v2: * remove unnecessary null check on cocoaView * make semaphores file-local * deref pixman_image in dealloc --- ui/cocoa.m | 195 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 79 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 00e3db69c9..420b2411c1 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,6 +129,9 @@ NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 +static QemuSemaphore display_init_sem; +static QemuSemaphore app_started_sem; + // Utility functions to run specified code block with iothread lock held typedef void (^CodeBlock)(void); typedef bool (^BoolCodeBlock)(void); @@ -325,6 +328,7 @@ @interface QemuCocoaView : NSView NSWindow *fullScreenWindow; float cx,cy,cw,ch,cdx,cdy; CGDataProviderRef dataProviderRef; + pixman_image_t *pixman_image; BOOL modifiers_state[256]; BOOL isMouseGrabbed; BOOL isFullscreen; @@ -383,8 +387,10 @@ - (void) dealloc { COCOA_DEBUG("QemuCocoaView: dealloc\n"); =20 - if (dataProviderRef) + if (dataProviderRef) { CGDataProviderRelease(dataProviderRef); + pixman_image_unref(pixman_image); + } =20 [super dealloc]; } @@ -535,13 +541,16 @@ - (void) switchSurface:(pixman_image_t *)image } =20 // update screenBuffer - if (dataProviderRef) + if (dataProviderRef) { CGDataProviderRelease(dataProviderRef); + pixman_image_unref(pixman_image); + } =20 //sync host window color space with guests screen.bitsPerPixel =3D PIXMAN_FORMAT_BPP(image_format); screen.bitsPerComponent =3D DIV_ROUND_UP(screen.bitsPerPixel, 8) * 2; =20 + pixman_image =3D image; dataProviderRef =3D CGDataProviderCreateWithData(NULL, pixman_image_ge= t_data(image), w * 4 * h, NULL); =20 // update windows @@ -1013,7 +1022,6 @@ @interface QemuCocoaAppController : NSObject #endif { } -- (void)startEmulationWithArgc:(int)argc argv:(char**)argv; - (void)doToggleFullScreen:(id)sender; - (void)toggleFullScreen:(id)sender; - (void)showQEMUDoc:(id)sender; @@ -1101,8 +1109,8 @@ - (void) dealloc - (void)applicationDidFinishLaunching: (NSNotification *) note { COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); - // launch QEMU, with the global args - [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; + /* Tell cocoa_display_init to proceed */ + qemu_sem_post(&app_started_sem); } =20 - (void)applicationWillTerminate:(NSNotification *)aNotification @@ -1145,15 +1153,6 @@ - (void) applicationWillResignActive: (NSNotificatio= n *)aNotification [cocoaView raiseAllKeys]; } =20 -- (void)startEmulationWithArgc:(int)argc argv:(char**)argv -{ - COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); - - int status; - status =3D qemu_main(argc, argv, *_NSGetEnviron()); - exit(status); -} - /* We abstract the method called by the Enter Fullscreen menu item * because Mac OS 10.7 and higher disables it. This is because of the * menu item's old selector's name toggleFullScreen: @@ -1489,7 +1488,9 @@ @implementation QemuApplication - (void)sendEvent:(NSEvent *)event { COCOA_DEBUG("QemuApplication: sendEvent\n"); - [super sendEvent: event]; + if (![cocoaView handleEvent:event]) { + [super sendEvent: event]; + } } @end =20 @@ -1672,32 +1673,59 @@ static void addRemovableDevicesMenuItems(void) qapi_free_BlockInfoList(pointerToFree); } =20 -int main (int argc, const char * argv[]) { +/* + * The startup process for the OSX/Cocoa UI is complicated, because + * OSX insists that the UI runs on the initial main thread, and so we + * need to start a second thread which runs the vl.c qemu_main(): + * + * Initial thread: 2nd thread: + * in main(): + * create qemu-main thread + * wait on display_init semaphore + * call qemu_main() + * ... + * in cocoa_display_init(): + * post the display_init semaphore + * wait on app_started semaphore + * create application, menus, etc + * enter OSX run loop + * in applicationDidFinishLaunching: + * post app_started semaphore + * tell main thread to fullscreen if n= eeded + * [...] + * run qemu main-loop + * + * We do this in two stages so that we don't do the creation of the + * GUI application menus and so on for command line options like --help + * where we want to just print text to stdout and exit immediately. + */ =20 +static void *call_qemu_main(void *opaque) +{ + int status; + + COCOA_DEBUG("Second thread: calling qemu_main()\n"); + status =3D qemu_main(gArgc, gArgv, *_NSGetEnviron()); + COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n"); + exit(status); +} + +int main (int argc, const char * argv[]) { + QemuThread thread; + + COCOA_DEBUG("Entered main()\n"); gArgc =3D argc; gArgv =3D (char **)argv; - int i; =20 - /* In case we don't need to display a window, let's not do that */ - for (i =3D 1; i < argc; i++) { - const char *opt =3D argv[i]; + qemu_sem_init(&display_init_sem, 0); + qemu_sem_init(&app_started_sem, 0); =20 - if (opt[0] =3D=3D '-') { - /* Treat --foo the same as -foo. */ - if (opt[1] =3D=3D '-') { - opt++; - } - if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || - !strcmp(opt, "-vnc") || - !strcmp(opt, "-nographic") || - !strcmp(opt, "-version") || - !strcmp(opt, "-curses") || - !strcmp(opt, "-display") || - !strcmp(opt, "-qtest")) { - return qemu_main(gArgc, gArgv, *_NSGetEnviron()); - } - } - } + qemu_thread_create(&thread, "qemu_main", call_qemu_main, + NULL, QEMU_THREAD_DETACHED); + + COCOA_DEBUG("Main thread: waiting for display_init_sem\n"); + qemu_sem_wait(&display_init_sem); + COCOA_DEBUG("Main thread: initializing app\n"); =20 NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; =20 @@ -1710,12 +1738,24 @@ int main (int argc, const char * argv[]) { =20 create_initial_menus(); =20 + /* + * Create the menu entries which depend on QEMU state (for consoles + * and removeable devices). These make calls back into QEMU functions, + * which is OK because at this point we know that the second thread + * holds the iothread lock and is synchronously waiting for us to + * finish. + */ + add_console_menu_entries(); + addRemovableDevicesMenuItems(); + // Create an Application controller QemuCocoaAppController *appController =3D [[QemuCocoaAppController all= oc] init]; [NSApp setDelegate:appController]; =20 // Start the main event loop + COCOA_DEBUG("Main thread: entering OSX run loop\n"); [NSApp run]; + COCOA_DEBUG("Main thread: left OSX run loop, exiting\n"); =20 [appController release]; [pool release]; @@ -1733,17 +1773,19 @@ static void cocoa_update(DisplayChangeListener *dcl, =20 COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); =20 - NSRect rect; - if ([cocoaView cdx] =3D=3D 1.0) { - rect =3D NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); - } else { - rect =3D NSMakeRect( - x * [cocoaView cdx], - ([cocoaView gscreen].height - y - h) * [cocoaView cdy], - w * [cocoaView cdx], - h * [cocoaView cdy]); - } - [cocoaView setNeedsDisplayInRect:rect]; + dispatch_async(dispatch_get_main_queue(), ^{ + NSRect rect; + if ([cocoaView cdx] =3D=3D 1.0) { + rect =3D NSMakeRect(x, [cocoaView gscreen].height - y - h, w, = h); + } else { + rect =3D NSMakeRect( + x * [cocoaView cdx], + ([cocoaView gscreen].height - y - h) * [cocoaView cdy], + w * [cocoaView cdx], + h * [cocoaView cdy]); + } + [cocoaView setNeedsDisplayInRect:rect]; + }); =20 [pool release]; } @@ -1752,9 +1794,19 @@ static void cocoa_switch(DisplayChangeListener *dcl, DisplaySurface *surface) { NSAutoreleasePool * pool =3D [[NSAutoreleasePool alloc] init]; + pixman_image_t *image =3D surface->image; =20 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); - [cocoaView switchSurface:surface->image]; + + // The DisplaySurface will be freed as soon as this callback returns. + // We take a reference to the underlying pixman image here so it does + // not disappear from under our feet; the switchSurface method will + // deref the old image when it is done with it. + pixman_image_ref(image); + + dispatch_async(dispatch_get_main_queue(), ^{ + [cocoaView switchSurface:image]; + }); [pool release]; } =20 @@ -1766,26 +1818,15 @@ static void cocoa_refresh(DisplayChangeListener *dc= l) graphic_hw_update(NULL); =20 if (qemu_input_is_absolute()) { - if (![cocoaView isAbsoluteEnabled]) { - if ([cocoaView isMouseGrabbed]) { - [cocoaView ungrabMouse]; + dispatch_async(dispatch_get_main_queue(), ^{ + if (![cocoaView isAbsoluteEnabled]) { + if ([cocoaView isMouseGrabbed]) { + [cocoaView ungrabMouse]; + } } - } - [cocoaView setAbsoluteEnabled:YES]; + [cocoaView setAbsoluteEnabled:YES]; + }); } - - NSDate *distantPast; - NSEvent *event; - distantPast =3D [NSDate distantPast]; - do { - event =3D [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:di= stantPast - inMode: NSDefaultRunLoopMode dequeue:YES]; - if (event !=3D nil) { - if (![cocoaView handleEvent:event]) { - [NSApp sendEvent:event]; - } - } - } while(event !=3D nil); [pool release]; } =20 @@ -1806,10 +1847,17 @@ static void cocoa_display_init(DisplayState *ds, Di= splayOptions *opts) { COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); =20 + /* Tell main thread to go ahead and create the app and enter the run l= oop */ + qemu_sem_post(&display_init_sem); + qemu_sem_wait(&app_started_sem); + COCOA_DEBUG("cocoa_display_init: app start completed\n"); + /* if fullscreen mode is to be used */ if (opts->has_full_screen && opts->full_screen) { - [NSApp activateIgnoringOtherApps: YES]; - [(QemuCocoaAppController *)[[NSApplication sharedApplication] dele= gate] toggleFullScreen: nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [NSApp activateIgnoringOtherApps: YES]; + [(QemuCocoaAppController *)[[NSApplication sharedApplication] = delegate] toggleFullScreen: nil]; + }); } =20 dcl =3D g_malloc0(sizeof(DisplayChangeListener)); @@ -1820,17 +1868,6 @@ static void cocoa_display_init(DisplayState *ds, Dis= playOptions *opts) =20 // register cleanup function atexit(cocoa_cleanup); - - /* At this point QEMU has created all the consoles, so we can add View - * menu entries for them. - */ - add_console_menu_entries(); - - /* Give all removable devices a menu item. - * Has to be called after QEMU has started to - * find out what removable devices it has. - */ - addRemovableDevicesMenuItems(); } =20 static QemuDisplay qemu_display_cocoa =3D { --=20 2.17.2 (Apple Git-113)