From nobody Thu May 16 22:27:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543667578295395.440032244911; Sat, 1 Dec 2018 04:32:58 -0800 (PST) Received: from localhost ([::1]:40994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4Rx-0007Hn-2i for importer@patchew.org; Sat, 01 Dec 2018 07:32:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50869) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4QH-0005ah-GY for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gT4QG-0008QZ-H8 for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:01 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53122) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gT4QG-0008Pd-9J for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:00 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gT4QD-0008Mu-RL; Sat, 01 Dec 2018 12:30:57 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sat, 1 Dec 2018 12:30:52 +0000 Message-Id: <20181201123056.432-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181201123056.432-1-peter.maydell@linaro.org> References: <20181201123056.432-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: 2001:8b0:1d0::2 Subject: [Qemu-devel] [RFC 1/5] 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: John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 --- ui/cocoa.m | 83 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index ecf12bfc2e4..9148ecdeb4c 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -117,6 +117,21 @@ bool stretch_video; 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, @@ -294,6 +309,7 @@ static void handleAnyDeviceErrors(Error * err) - (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 @@ -632,8 +648,14 @@ QemuCocoaView *cocoaView; =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; @@ -928,15 +950,18 @@ QemuCocoaView *cocoaView; */ - (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 @@ -1198,13 +1223,17 @@ QemuCocoaView *cocoaView; /* 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. @@ -1220,9 +1249,11 @@ QemuCocoaView *cocoaView; 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 @@ -1254,16 +1285,18 @@ QemuCocoaView *cocoaView; 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); } } @@ -1402,7 +1435,9 @@ QemuCocoaView *cocoaView; // 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.19.2 From nobody Thu May 16 22:27:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.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 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543667806685975.9879126621702; Sat, 1 Dec 2018 04:36:46 -0800 (PST) Received: from localhost ([::1]:41014 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4Vp-00032n-NB for importer@patchew.org; Sat, 01 Dec 2018 07:36:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4QM-0005bP-Rc for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gT4QL-0008Sw-Ug for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:06 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gT4QL-0008Qj-Ne for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:05 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gT4QE-0008N7-FT; Sat, 01 Dec 2018 12:30:58 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sat, 1 Dec 2018 12:30:53 +0000 Message-Id: <20181201123056.432-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181201123056.432-1-peter.maydell@linaro.org> References: <20181201123056.432-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: 2001:8b0:1d0::2 Subject: [Qemu-devel] [RFC 2/5] 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: John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 --- ui/cocoa.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 9148ecdeb4c..997b0199c6c 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -303,7 +303,7 @@ static void handleAnyDeviceErrors(Error * err) BOOL isAbsoluteEnabled; BOOL isMouseDeassociated; } -- (void) switchSurface:(DisplaySurface *)surface; +- (void) switchSurface:(pixman_image_t *)image; - (void) grabMouse; - (void) ungrabMouse; - (void) toggleFullScreen:(id)sender; @@ -478,12 +478,13 @@ QemuCocoaView *cocoaView; } } =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. @@ -505,10 +506,10 @@ QemuCocoaView *cocoaView; 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) { @@ -1608,7 +1609,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.19.2 From nobody Thu May 16 22:27:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543667578292311.1602026130016; Sat, 1 Dec 2018 04:32:58 -0800 (PST) Received: from localhost ([::1]:40996 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4Rz-0007T1-JH for importer@patchew.org; Sat, 01 Dec 2018 07:32:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50930) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4QL-0005av-MX for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gT4QK-0008SV-Vj for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:05 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gT4QK-0008Qj-Ox for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:04 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gT4QF-0008NS-5k; Sat, 01 Dec 2018 12:30:59 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sat, 1 Dec 2018 12:30:54 +0000 Message-Id: <20181201123056.432-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181201123056.432-1-peter.maydell@linaro.org> References: <20181201123056.432-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: 2001:8b0:1d0::2 Subject: [Qemu-devel] [RFC 3/5] 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: John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 --- ui/cocoa.m | 78 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 997b0199c6c..da511bfb4fa 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1444,43 +1444,8 @@ QemuCocoaView *cocoaView; =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; @@ -1564,6 +1529,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.19.2 From nobody Thu May 16 22:27:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.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 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543667735162683.2268005997183; Sat, 1 Dec 2018 04:35:35 -0800 (PST) Received: from localhost ([::1]:41008 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4Uf-0002NG-Vg for importer@patchew.org; Sat, 01 Dec 2018 07:35:34 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50898) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4QK-0005ao-6K for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gT4QI-0008RX-Vs for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:04 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gT4QI-0008Qj-N0 for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:02 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gT4QF-0008Nm-Sk; Sat, 01 Dec 2018 12:30:59 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sat, 1 Dec 2018 12:30:55 +0000 Message-Id: <20181201123056.432-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181201123056.432-1-peter.maydell@linaro.org> References: <20181201123056.432-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: 2001:8b0:1d0::2 Subject: [Qemu-devel] [RFC 4/5] 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: John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 --- ui/cocoa.m | 184 ++++++++++++++++++++++++++--------------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index da511bfb4fa..2f533c77c47 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -1531,6 +1531,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; @@ -1659,98 +1751,6 @@ static const DisplayChangeListenerOps dcl_ops =3D { .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.19.2 From nobody Thu May 16 22:27:16 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.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 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543667662478980.5394595835287; Sat, 1 Dec 2018 04:34:22 -0800 (PST) Received: from localhost ([::1]:41001 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4TV-0001mn-BO for importer@patchew.org; Sat, 01 Dec 2018 07:34:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50887) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gT4QJ-0005an-90 for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gT4QH-0008R5-Sn for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:03 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:53156) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gT4QH-0008Qj-Gm for qemu-devel@nongnu.org; Sat, 01 Dec 2018 07:31:01 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1gT4QG-0008O4-Hj; Sat, 01 Dec 2018 12:31:00 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sat, 1 Dec 2018 12:30:56 +0000 Message-Id: <20181201123056.432-6-peter.maydell@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181201123056.432-1-peter.maydell@linaro.org> References: <20181201123056.432-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: 2001:8b0:1d0::2 Subject: [Qemu-devel] [RFC 5/5] 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: John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 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 --- ui/cocoa.m | 185 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 110 insertions(+), 75 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index 2f533c77c47..3897d2e370b 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -117,6 +117,9 @@ bool stretch_video; NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 +QemuSemaphore display_init_sem; +QemuSemaphore app_started_sem; + // Utility function to run specified code block with iothread lock held typedef void (^CodeBlock)(void); =20 @@ -297,6 +300,7 @@ static void handleAnyDeviceErrors(Error * err) NSWindow *fullScreenWindow; float cx,cy,cw,ch,cdx,cdy; CGDataProviderRef dataProviderRef; + pixman_image_t *pixman_image; BOOL modifiers_state[256]; BOOL isMouseGrabbed; BOOL isFullscreen; @@ -502,13 +506,16 @@ QemuCocoaView *cocoaView; } =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 @@ -979,7 +986,6 @@ QemuCocoaView *cocoaView; #endif { } -- (void)startEmulationWithArgc:(int)argc argv:(char**)argv; - (void)doToggleFullScreen:(id)sender; - (void)toggleFullScreen:(id)sender; - (void)showQEMUDoc:(id)sender; @@ -1067,8 +1073,8 @@ QemuCocoaView *cocoaView; - (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 @@ -1111,15 +1117,6 @@ QemuCocoaView *cocoaView; [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: @@ -1623,32 +1620,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 @@ -1661,12 +1685,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]; @@ -1684,17 +1720,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]; } @@ -1703,9 +1741,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 @@ -1717,24 +1765,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) { - [cocoaView handleEvent:event]; - } - } while(event !=3D nil); [pool release]; } =20 @@ -1755,10 +1794,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)); @@ -1769,17 +1815,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.19.2