From nobody Wed May 22 04:35:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647411556897741.5509278548755; Tue, 15 Mar 2022 23:19:16 -0700 (PDT) Received: from localhost ([::1]:39860 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nUN07-00013M-Na for importer@patchew.org; Wed, 16 Mar 2022 02:19:15 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59534) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nUMkI-00023U-S6 for qemu-devel@nongnu.org; Wed, 16 Mar 2022 02:02:56 -0400 Received: from [2607:f8b0:4864:20::1033] (port=32807 helo=mail-pj1-x1033.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nUMkG-00076q-6I for qemu-devel@nongnu.org; Wed, 16 Mar 2022 02:02:54 -0400 Received: by mail-pj1-x1033.google.com with SMTP id q1-20020a17090a4f8100b001c6575ae105so1103439pjh.0 for ; Tue, 15 Mar 2022 23:02:51 -0700 (PDT) Received: from localhost.localdomain ([2400:4050:c360:8200:dd81:833a:94c3:358a]) by smtp.gmail.com with ESMTPSA id a11-20020a63cd4b000000b00378b9167493sm1151885pgj.52.2022.03.15.23.02.48 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 15 Mar 2022 23:02:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7y63XFrJZVH2qxK5poaDqOgmbgGTKSGS1NZnVOjoXhc=; b=PXNjhOO0pVV5kaxIA50IFA53mKejl349TNpYlBWkKKOY6/RXdJdT3xPozbxTtbkdxF umquNVPuHxbZejJWCBUfc5mZKd7RXcdrf8Yhok/Rb/I3S7SktB3rpmgpRqneqk/CBUuW 6FFUCMbC2rhtvlf/YFp06slo1HM/sfxu8QGs1P3A5VAABB0yS6BJpL98PLcLSt7EX7LN URw85F2IIoaqJzNUZh8iMizuVxpHI78lWn/4g1BxdXYxAv1fV1u8+DzfzIzMyBvKOSpI /H7+s4e7J/HMN+5XfYXDdqucA3SVLQvKkvWgNW3+z2UfrAQPDo/uA/PT+YjyqcCTxkfq SOwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7y63XFrJZVH2qxK5poaDqOgmbgGTKSGS1NZnVOjoXhc=; b=lnZ0Ob/htja2jC84NyejGT2jHUXPdUDuJspnB2qIeXycxXWMY5EvKh1OjuqzFitb80 n0xdonRTou12J5phdaFhcXnzFf/Q07qxbW47MwetNEph4+6uHGT0WOSy83qakR/1ZgfO 2w1QKWwX7C60CuBDcmcIHaNtPJNKOMVB75B7SIrsLqv2GRUIXnb81sdbn0YoRZPjCijF Dpj9Dz35Y3qkQ6NhQXgEjjo600/Uy0ip+0ybwUEVd0i+BdWghXTg90p+zcciIHJ/9hp7 XSlNxRRGCayF/dGkn/3ggEdy6hOYDgawvhjXq4BYJ3GcEmRBCQvgcGW4c3PW2LyxBqbK FgJg== X-Gm-Message-State: AOAM530meFOXlpuTKbG5RJslnrshY3rd3eNejTaekUx12KLG7IULRqcO sxxJKTyA1aizyVIVKSRRrMWt7tyHLnc= X-Google-Smtp-Source: ABdhPJwVf6jZTHMm/5fQ6SVkRtaf7kHiOghVseML9d7DVmsGOpZ0O7zVkUFNg9sCAj03Hs8r9zFhUQ== X-Received: by 2002:a17:902:e5d1:b0:153:9dcf:de77 with SMTP id u17-20020a170902e5d100b001539dcfde77mr6081066plf.34.1647410570208; Tue, 15 Mar 2022 23:02:50 -0700 (PDT) From: Akihiko Odaki To: Subject: [PATCH v5] ui/cocoa: Use NSWindow's ability to resize Date: Wed, 16 Mar 2022 15:02:44 +0900 Message-Id: <20220316060244.46669-1-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1033 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=akihiko.odaki@gmail.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Gerd Hoffmann , qemu-devel@nongnu.org, Akihiko Odaki Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1647411558130100001 Content-Type: text/plain; charset="utf-8" This change brings two new features: - The window will be resizable if "Zoom To Fit" is eanbled - The window can be made full screen by clicking full screen button provided by the platform. (The left-top green button.) Signed-off-by: Akihiko Odaki --- ui/cocoa.m | 539 ++++++++++++++++++++++++----------------------------- 1 file changed, 248 insertions(+), 291 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index cb6e7c41dc6..a75e1a4450a 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -93,14 +93,12 @@ static void cocoa_switch(DisplayChangeListener *dcl, static DisplayChangeListener dcl =3D { .ops =3D &dcl_ops, }; -static int last_buttons; static int cursor_hide =3D 1; static int left_command_key_enabled =3D 1; static bool swap_opt_cmd; =20 static int gArgc; static char **gArgv; -static bool stretch_video; static NSTextField *pauseLabel; =20 static QemuSemaphore display_init_sem; @@ -302,20 +300,17 @@ static void handleAnyDeviceErrors(Error * err) */ @interface QemuCocoaView : NSView { + NSTrackingArea *trackingArea; QEMUScreen screen; - NSWindow *fullScreenWindow; - float cx,cy,cw,ch,cdx,cdy; pixman_image_t *pixman_image; QKbdState *kbd; BOOL isMouseGrabbed; - BOOL isFullscreen; BOOL isAbsoluteEnabled; CFMachPortRef eventsTap; } - (void) switchSurface:(pixman_image_t *)image; - (void) grabMouse; - (void) ungrabMouse; -- (void) toggleFullScreen:(id)sender; - (void) setFullGrab:(id)sender; - (void) handleMonitorInput:(NSEvent *)event; - (bool) handleEvent:(NSEvent *)event; @@ -331,8 +326,6 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; */ - (BOOL) isMouseGrabbed; - (BOOL) isAbsoluteEnabled; -- (float) cdx; -- (float) cdy; - (QEMUScreen) gscreen; - (void) raiseAllKeys; @end @@ -390,46 +383,43 @@ - (BOOL) isOpaque return YES; } =20 -- (BOOL) screenContainsPoint:(NSPoint) p +- (void) removeTrackingRect { - return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.hei= ght); + if (trackingArea) { + [self removeTrackingArea:trackingArea]; + [trackingArea release]; + trackingArea =3D nil; + } } =20 -/* Get location of event and convert to virtual screen coordinate */ -- (CGPoint) screenLocationOfEvent:(NSEvent *)ev +- (void) frameUpdated { - NSWindow *eventWindow =3D [ev window]; - // XXX: Use CGRect and -convertRectFromScreen: to support macOS 10.10 - CGRect r =3D CGRectZero; - r.origin =3D [ev locationInWindow]; - if (!eventWindow) { - if (!isFullscreen) { - return [[self window] convertRectFromScreen:r].origin; - } else { - CGPoint locationInSelfWindow =3D [[self window] convertRectFro= mScreen:r].origin; - CGPoint loc =3D [self convertPoint:locationInSelfWindow fromVi= ew:nil]; - if (stretch_video) { - loc.x /=3D cdx; - loc.y /=3D cdy; - } - return loc; - } - } else if ([[self window] isEqual:eventWindow]) { - if (!isFullscreen) { - return r.origin; - } else { - CGPoint loc =3D [self convertPoint:r.origin fromView:nil]; - if (stretch_video) { - loc.x /=3D cdx; - loc.y /=3D cdy; - } - return loc; - } - } else { - return [[self window] convertRectFromScreen:[eventWindow convertRe= ctToScreen:r]].origin; + [self removeTrackingRect]; + + if ([self window]) { + NSTrackingAreaOptions options =3D NSTrackingActiveInKeyWindow | + NSTrackingMouseEnteredAndExited | + NSTrackingMouseMoved; + trackingArea =3D [[NSTrackingArea alloc] initWithRect:[self frame] + options:options + owner:self + userInfo:nil]; + [self addTrackingArea:trackingArea]; + [self updateUIInfo]; } } =20 +- (void) viewDidMoveToWindow +{ + [self resizeWindow]; + [self frameUpdated]; +} + +- (void) viewWillMoveToWindow:(NSWindow *)newWindow +{ + [self removeTrackingRect]; +} + - (void) hideCursor { if (!cursor_hide) { @@ -492,13 +482,14 @@ - (void) drawRect:(NSRect) rect int i; CGImageRef clipImageRef; CGRect clipRect; + CGFloat d =3D (CGFloat)h / [self frame].size.height; =20 [self getRectsBeingDrawn:&rectList count:&rectCount]; for (i =3D 0; i < rectCount; i++) { - clipRect.origin.x =3D rectList[i].origin.x / cdx; - clipRect.origin.y =3D (float)h - (rectList[i].origin.y + rectL= ist[i].size.height) / cdy; - clipRect.size.width =3D rectList[i].size.width / cdx; - clipRect.size.height =3D rectList[i].size.height / cdy; + clipRect.origin.x =3D rectList[i].origin.x * d; + clipRect.origin.y =3D (float)h - (rectList[i].origin.y + rectL= ist[i].size.height) * d; + clipRect.size.width =3D rectList[i].size.width * d; + clipRect.size.height =3D rectList[i].size.height * d; clipImageRef =3D CGImageCreateWithImageInRect( imageRef, clipRect @@ -511,36 +502,34 @@ - (void) drawRect:(NSRect) rect } } =20 -- (void) setContentDimensions +- (NSSize) fixZoomedFullScreenSize:(NSSize)proposedSize { - COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); + NSSize size; =20 - if (isFullscreen) { - cdx =3D [[NSScreen mainScreen] frame].size.width / (float)screen.w= idth; - cdy =3D [[NSScreen mainScreen] frame].size.height / (float)screen.= height; + size.width =3D (CGFloat)screen.width * proposedSize.height; + size.height =3D (CGFloat)screen.height * proposedSize.width; =20 - /* stretches video, but keeps same aspect ratio */ - if (stretch_video =3D=3D true) { - /* use smallest stretch value - prevents clipping on sides */ - if (MIN(cdx, cdy) =3D=3D cdx) { - cdy =3D cdx; - } else { - cdx =3D cdy; - } - } else { /* No stretching */ - cdx =3D cdy =3D 1; - } - cw =3D screen.width * cdx; - ch =3D screen.height * cdy; - cx =3D ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; - cy =3D ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; + if (size.width < size.height) { + size.width /=3D screen.height; + size.height =3D proposedSize.height; } else { - cx =3D 0; - cy =3D 0; - cw =3D screen.width; - ch =3D screen.height; - cdx =3D 1.0; - cdy =3D 1.0; + size.width =3D proposedSize.width; + size.height /=3D screen.width; + } + + return size; +} + +- (void) resizeWindow +{ + [[self window] setContentAspectRatio:NSMakeSize(screen.width, screen.h= eight)]; + + if (([[self window] styleMask] & NSWindowStyleMaskResizable) =3D=3D 0)= { + [[self window] setContentSize:NSMakeSize(screen.width, screen.heig= ht)]; + [[self window] center]; + } else if (([[self window] styleMask] & NSWindowStyleMaskFullScreen) != =3D 0) { + [[self window] setContentSize:[self fixZoomedFullScreenSize:[[[sel= f window] screen] frame].size]]; + [[self window] center]; } } =20 @@ -560,7 +549,12 @@ - (void) updateUIInfoLocked NSSize screenSize =3D [[[self window] screen] frame].size; CGSize screenPhysicalSize =3D CGDisplayScreenSize(display); =20 - frameSize =3D isFullscreen ? screenSize : [self frame].size; + if (([[self window] styleMask] & NSWindowStyleMaskFullScreen) =3D= =3D 0) { + frameSize =3D [self frame].size; + } else { + frameSize =3D screenSize; + } + info.width_mm =3D frameSize.width / screenSize.width * screenPhysi= calSize.width; info.height_mm =3D frameSize.height / screenSize.height * screenPh= ysicalSize.height; } else { @@ -596,31 +590,19 @@ - (void) updateUIInfo }); } =20 -- (void)viewDidMoveToWindow -{ - [self updateUIInfo]; -} - - (void) switchSurface:(pixman_image_t *)image { COCOA_DEBUG("QemuCocoaView: switchSurface\n"); =20 int w =3D pixman_image_get_width(image); int h =3D pixman_image_get_height(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. - */ - bool isResize =3D (w !=3D screen.width || h !=3D screen.height || cdx = =3D=3D 0.0); =20 - int oldh =3D screen.height; - if (isResize) { + if (w !=3D screen.width || h !=3D screen.height) { // Resize before we trigger the redraw, or we'll redraw at the wro= ng size COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); screen.width =3D w; screen.height =3D h; - [self setContentDimensions]; - [self setFrame:NSMakeRect(cx, cy, cw, ch)]; + [self resizeWindow]; } =20 // update screenBuffer @@ -629,51 +611,6 @@ - (void) switchSurface:(pixman_image_t *)image } =20 pixman_image =3D image; - - // update windows - if (isFullscreen) { - [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] fr= ame]]; - [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [= normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.h= eight - oldh) display:NO animate:NO]; - } else { - if (qemu_name) - [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", = qemu_name]]; - [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [= normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.h= eight - oldh) display:YES animate:NO]; - } - - if (isResize) { - [normalWindow center]; - } -} - -- (void) toggleFullScreen:(id)sender -{ - COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); - - if (isFullscreen) { // switch from fullscreen to desktop - isFullscreen =3D FALSE; - [self ungrabMouse]; - [self setContentDimensions]; - [fullScreenWindow close]; - [normalWindow setContentView: self]; - [normalWindow makeKeyAndOrderFront: self]; - [NSMenu setMenuBarVisible:YES]; - } else { // switch from desktop to fullscreen - isFullscreen =3D TRUE; - [normalWindow orderOut: nil]; /* Hide the window */ - [self grabMouse]; - [self setContentDimensions]; - [NSMenu setMenuBarVisible:NO]; - fullScreenWindow =3D [[NSWindow alloc] initWithContentRect:[[NSScr= een mainScreen] frame] - styleMask:NSWindowStyleMaskBorderless - backing:NSBackingStoreBuffered - defer:NO]; - [fullScreenWindow setAcceptsMouseMovedEvents: YES]; - [fullScreenWindow setHasShadow:NO]; - [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; - [self setFrame:NSMakeRect(cx, cy, cw, ch)]; - [[fullScreenWindow contentView] addSubview: self]; - [fullScreenWindow makeKeyAndOrderFront:self]; - } } =20 - (void) setFullGrab:(id)sender @@ -797,10 +734,6 @@ - (bool) handleEventLocked:(NSEvent *)event COCOA_DEBUG("QemuCocoaView: handleEvent\n"); int buttons =3D 0; int keycode =3D 0; - bool mouse_event =3D false; - static bool switched_to_fullscreen =3D false; - // Location of event in virtual screen coordinates - NSPoint p =3D [self screenLocationOfEvent:event]; NSUInteger modifiers =3D [event modifierFlags]; =20 /* @@ -880,25 +813,25 @@ - (bool) handleEventLocked:(NSEvent *)event if (!!(modifiers & NSEventModifierFlagShift)) { [self toggleKey:Q_KEY_CODE_SHIFT]; } - break; + return true; =20 case kVK_RightShift: if (!!(modifiers & NSEventModifierFlagShift)) { [self toggleKey:Q_KEY_CODE_SHIFT_R]; } - break; + return true; =20 case kVK_Control: if (!!(modifiers & NSEventModifierFlagControl)) { [self toggleKey:Q_KEY_CODE_CTRL]; } - break; + return true; =20 case kVK_RightControl: if (!!(modifiers & NSEventModifierFlagControl)) { [self toggleKey:Q_KEY_CODE_CTRL_R]; } - break; + return true; =20 case kVK_Option: if (!!(modifiers & NSEventModifierFlagOption)) { @@ -908,7 +841,7 @@ - (bool) handleEventLocked:(NSEvent *)event [self toggleKey:Q_KEY_CODE_ALT]; } } - break; + return true; =20 case kVK_RightOption: if (!!(modifiers & NSEventModifierFlagOption)) { @@ -918,7 +851,7 @@ - (bool) handleEventLocked:(NSEvent *)event [self toggleKey:Q_KEY_CODE_ALT_R]; } } - break; + return true; =20 /* Don't pass command key changes to guest unless mouse is= grabbed */ case kVK_Command: @@ -930,7 +863,7 @@ - (bool) handleEventLocked:(NSEvent *)event [self toggleKey:Q_KEY_CODE_META_L]; } } - break; + return true; =20 case kVK_RightCommand: if (isMouseGrabbed && @@ -941,21 +874,16 @@ - (bool) handleEventLocked:(NSEvent *)event [self toggleKey:Q_KEY_CODE_META_R]; } } - break; + return true; + + default: + return true; } - break; case NSEventTypeKeyDown: keycode =3D cocoa_keycode_to_qemu([event keyCode]); =20 // forward command key combos to the host UI unless the mouse = is grabbed if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifie= rFlagCommand)) { - /* - * Prevent the command key from being stuck down in the gu= est - * when using Command-F to switch to full screen mode. - */ - if (keycode =3D=3D Q_KEY_CODE_F) { - switched_to_fullscreen =3D true; - } return false; } =20 @@ -986,7 +914,7 @@ - (bool) handleEventLocked:(NSEvent *)event } else { [self handleMonitorInput: event]; } - break; + return true; case NSEventTypeKeyUp: keycode =3D cocoa_keycode_to_qemu([event keyCode]); =20 @@ -999,67 +927,7 @@ - (bool) handleEventLocked:(NSEvent *)event if (qemu_console_is_graphic(NULL)) { qkbd_state_key_event(kbd, keycode, false); } - break; - case NSEventTypeMouseMoved: - if (isAbsoluteEnabled) { - // Cursor re-entered into a window might generate events b= ound to screen coordinates - // and `nil` window property, and in full screen mode, cur= rent window might not be - // key window, where event location alone should suffice. - if (![self screenContainsPoint:p] || !([[self window] isKe= yWindow] || isFullscreen)) { - if (isMouseGrabbed) { - [self ungrabMouse]; - } - } else { - if (!isMouseGrabbed) { - [self grabMouse]; - } - } - } - mouse_event =3D true; - break; - case NSEventTypeLeftMouseDown: - buttons |=3D MOUSE_EVENT_LBUTTON; - mouse_event =3D true; - break; - case NSEventTypeRightMouseDown: - buttons |=3D MOUSE_EVENT_RBUTTON; - mouse_event =3D true; - break; - case NSEventTypeOtherMouseDown: - buttons |=3D MOUSE_EVENT_MBUTTON; - mouse_event =3D true; - break; - case NSEventTypeLeftMouseDragged: - buttons |=3D MOUSE_EVENT_LBUTTON; - mouse_event =3D true; - break; - case NSEventTypeRightMouseDragged: - buttons |=3D MOUSE_EVENT_RBUTTON; - mouse_event =3D true; - break; - case NSEventTypeOtherMouseDragged: - buttons |=3D MOUSE_EVENT_MBUTTON; - mouse_event =3D true; - break; - case NSEventTypeLeftMouseUp: - mouse_event =3D true; - if (!isMouseGrabbed && [self screenContainsPoint:p]) { - /* - * In fullscreen mode, the window of cocoaView may not be = the - * key window, therefore the position relative to the virt= ual - * screen alone will be sufficient. - */ - if(isFullscreen || [[self window] isKeyWindow]) { - [self grabMouse]; - } - } - break; - case NSEventTypeRightMouseUp: - mouse_event =3D true; - break; - case NSEventTypeOtherMouseUp: - mouse_event =3D true; - break; + return true; case NSEventTypeScrollWheel: /* * Send wheel events to the guest regardless of window focus. @@ -1090,62 +958,124 @@ - (bool) handleEventLocked:(NSEvent *)event * Since deltaX/deltaY also report scroll wheel events we prev= ent mouse * movement code from executing. */ - mouse_event =3D false; - break; + return true; default: return false; } +} =20 - if (mouse_event) { - /* Don't send button events to the guest unless we've got a - * mouse grab or window focus. If we have neither then this event - * is the user clicking on the background window to activate and - * bring us to the front, which will be done by the sendEvent - * call below. We definitely don't want to pass that click through - * to the guest. - */ - if ((isMouseGrabbed || [[self window] isKeyWindow]) && - (last_buttons !=3D buttons)) { - static uint32_t bmap[INPUT_BUTTON__MAX] =3D { - [INPUT_BUTTON_LEFT] =3D MOUSE_EVENT_LBUTTON, - [INPUT_BUTTON_MIDDLE] =3D MOUSE_EVENT_MBUTTON, - [INPUT_BUTTON_RIGHT] =3D MOUSE_EVENT_RBUTTON - }; - qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons= ); - last_buttons =3D buttons; - } - if (isMouseGrabbed) { - if (isAbsoluteEnabled) { - /* Note that the origin for Cocoa mouse coords is bottom l= eft, not top left. - * The check on screenContainsPoint is to avoid sending ou= t of range values for - * clicks in the titlebar. - */ - if ([self screenContainsPoint:p]) { - qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, sc= reen.width); - qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.hei= ght - p.y, 0, screen.height); - } - } else { - qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event de= ltaX]); - qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event de= ltaY]); - } +- (void) handleMouseEvent:(NSEvent *)event +{ + if (!isMouseGrabbed) { + return; + } + + with_iothread_lock(^{ + if (isAbsoluteEnabled) { + CGFloat d =3D (CGFloat)screen.height / [self frame].size.heigh= t; + NSPoint p =3D [event locationInWindow]; + // Note that the origin for Cocoa mouse coords is bottom left,= not top left. + qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x * d, 0, screen= .width); + qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.= y * d, 0, screen.height); } else { - return false; + CGFloat d =3D (CGFloat)screen.height / [self convertSizeToBack= ing:[self frame].size].height; + qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, [event deltaX] * d= ); + qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, [event deltaY] * d= ); } + qemu_input_event_sync(); + }); +} + +- (void) handleMouseEvent:(NSEvent *)event button:(InputButton)button down= :(bool)down +{ + if (!isMouseGrabbed) { + return; + } + + with_iothread_lock(^{ + qemu_input_queue_btn(dcl.con, button, down); + }); + + [self handleMouseEvent:event]; +} + +- (void) mouseExited:(NSEvent *)event +{ + if (isAbsoluteEnabled && isMouseGrabbed) { + [self ungrabMouse]; + } +} + +- (void) mouseEntered:(NSEvent *)event +{ + if (isAbsoluteEnabled && !isMouseGrabbed) { + [self grabMouse]; } - return true; +} + +- (void) mouseMoved:(NSEvent *)event +{ + [self handleMouseEvent:event]; +} + +- (void) mouseDown:(NSEvent *)event +{ + [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:true]; +} + +- (void) rightMouseDown:(NSEvent *)event +{ + [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:true]; +} + +- (void) otherMouseDown:(NSEvent *)event +{ + [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:true]; +} + +- (void) mouseDragged:(NSEvent *)event +{ + [self handleMouseEvent:event]; +} + +- (void) rightMouseDragged:(NSEvent *)event +{ + [self handleMouseEvent:event]; +} + +- (void) otherMouseDragged:(NSEvent *)event +{ + [self handleMouseEvent:event]; +} + +- (void) mouseUp:(NSEvent *)event +{ + if (!isMouseGrabbed) { + [self grabMouse]; + } + + [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:false]; +} + +- (void) rightMouseUp:(NSEvent *)event +{ + [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:false]; +} + +- (void) otherMouseUp:(NSEvent *)event +{ + [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:false]; } =20 - (void) grabMouse { COCOA_DEBUG("QemuCocoaView: grabMouse\n"); =20 - if (!isFullscreen) { - if (qemu_name) - [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - = (Press ctrl + alt + g to release Mouse)", qemu_name]]; - else - [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to relea= se Mouse)"]; - } + if (qemu_name) + [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Pre= ss ctrl + alt + g to release Mouse)", qemu_name]]; + else + [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release M= ouse)"]; [self hideCursor]; CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); isMouseGrabbed =3D TRUE; // while isMouseGrabbed =3D TRUE, QemuCocoaAp= p sends all events to [cocoaView handleEvent:] @@ -1155,15 +1085,14 @@ - (void) ungrabMouse { COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); =20 - if (!isFullscreen) { - if (qemu_name) - [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", = qemu_name]]; - else - [normalWindow setTitle:@"QEMU"]; - } + if (qemu_name) + [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu= _name]]; + else + [normalWindow setTitle:@"QEMU"]; [self unhideCursor]; CGAssociateMouseAndMouseCursorPosition(TRUE); isMouseGrabbed =3D FALSE; + [self raiseAllButtons]; } =20 - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled { @@ -1174,10 +1103,17 @@ - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnable= d { } - (BOOL) isMouseGrabbed {return isMouseGrabbed;} - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} -- (float) cdx {return cdx;} -- (float) cdy {return cdy;} - (QEMUScreen) gscreen {return screen;} =20 +- (void) raiseAllButtons +{ + with_iothread_lock(^{ + qemu_input_queue_btn(dcl.con, INPUT_BUTTON_LEFT, false); + qemu_input_queue_btn(dcl.con, INPUT_BUTTON_RIGHT, false); + qemu_input_queue_btn(dcl.con, INPUT_BUTTON_MIDDLE, false); + }); +} + /* * Makes the target think all down keys are being released. * This prevents a stuck key problem, since we will not see @@ -1203,7 +1139,6 @@ @interface QemuCocoaAppController : NSObject { } - (void)doToggleFullScreen:(id)sender; -- (void)toggleFullScreen:(id)sender; - (void)showQEMUDoc:(id)sender; - (void)zoomToFit:(id) sender; - (void)displayConsole:(id)sender; @@ -1245,12 +1180,12 @@ - (id) init exit(1); } [normalWindow setAcceptsMouseMovedEvents:YES]; - [normalWindow setTitle:@"QEMU"]; + [normalWindow setCollectionBehavior:NSWindowCollectionBehaviorFull= ScreenPrimary]; + [normalWindow setTitle:qemu_name ? [NSString stringWithFormat:@"QE= MU %s", qemu_name] : @"QEMU"]; [normalWindow setContentView:cocoaView]; [normalWindow makeKeyAndOrderFront:self]; [normalWindow center]; [normalWindow setDelegate: self]; - stretch_video =3D false; =20 /* Used for displaying pause on the screen */ pauseLabel =3D [NSTextField new]; @@ -1315,9 +1250,20 @@ - (void)windowDidChangeScreen:(NSNotification *)noti= fication [cocoaView updateUIInfo]; } =20 +- (void)windowDidEnterFullScreen:(NSNotification *)notification +{ + [cocoaView grabMouse]; +} + +- (void)windowDidExitFullScreen:(NSNotification *)notification +{ + [cocoaView resizeWindow]; + [cocoaView ungrabMouse]; +} + - (void)windowDidResize:(NSNotification *)notification { - [cocoaView updateUIInfo]; + [cocoaView frameUpdated]; } =20 /* Called when the user clicks on a window's close button */ @@ -1333,6 +1279,23 @@ - (BOOL)windowShouldClose:(id)sender return NO; } =20 +- (NSSize) window:(NSWindow *)window willUseFullScreenContentSize:(NSSize)= proposedSize +{ + if (([normalWindow styleMask] & NSWindowStyleMaskResizable) =3D=3D 0) { + return NSMakeSize([cocoaView gscreen].width, [cocoaView gscreen].h= eight); + } + + return [cocoaView fixZoomedFullScreenSize:proposedSize]; +} + +- (NSApplicationPresentationOptions) window:(NSWindow *)window + willUseFullScreenPresentationOptions:= (NSApplicationPresentationOptions)proposedOptions; + +{ + return (proposedOptions & ~(NSApplicationPresentationAutoHideDock | NS= ApplicationPresentationAutoHideMenuBar)) | + NSApplicationPresentationHideDock | NSApplicationPresentationHi= deMenuBar; +} + /* Called when QEMU goes into the background */ - (void) applicationWillResignActive: (NSNotification *)aNotification { @@ -1347,14 +1310,7 @@ - (void) applicationWillResignActive: (NSNotificatio= n *)aNotification */ - (void) doToggleFullScreen:(id)sender { - [self toggleFullScreen:(id)sender]; -} - -- (void)toggleFullScreen:(id)sender -{ - COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); - - [cocoaView toggleFullScreen:sender]; + [normalWindow toggleFullScreen:sender]; } =20 - (void) setFullGrab:(id)sender @@ -1398,13 +1354,15 @@ - (void)showQEMUDoc:(id)sender [self openDocumentation: @"index.html"]; } =20 -/* Stretches video to fit host monitor size */ +/* Toggles the flag which stretches video to fit host window size */ - (void)zoomToFit:(id) sender { - stretch_video =3D !stretch_video; - if (stretch_video =3D=3D true) { + if (([normalWindow styleMask] & NSWindowStyleMaskResizable) =3D=3D 0) { + [normalWindow setStyleMask:[normalWindow styleMask] | NSWindowStyl= eMaskResizable]; [sender setState: NSControlStateValueOn]; } else { + [normalWindow setStyleMask:[normalWindow styleMask] & ~NSWindowSty= leMaskResizable]; + [cocoaView resizeWindow]; [sender setState: NSControlStateValueOff]; } } @@ -1412,7 +1370,9 @@ - (void)zoomToFit:(id) sender /* Displays the console on the screen */ - (void)displayConsole:(id)sender { - console_select([sender tag]); + with_iothread_lock(^{ + console_select([sender tag]); + }); } =20 /* Pause the guest */ @@ -2003,16 +1963,14 @@ static void cocoa_update(DisplayChangeListener *dcl, COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); =20 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]); - } + CGFloat d =3D [cocoaView frame].size.height / (CGFloat)[cocoaView = gscreen].height; + + NSRect rect =3D NSMakeRect( + x * d, + ([cocoaView gscreen].height - y - h) * d, + w * d, + h * d); + [cocoaView setNeedsDisplayInRect:rect]; }); } @@ -2081,8 +2039,7 @@ static void cocoa_display_init(DisplayState *ds, Disp= layOptions *opts) /* if fullscreen mode is to be used */ if (opts->has_full_screen && opts->full_screen) { dispatch_async(dispatch_get_main_queue(), ^{ - [NSApp activateIgnoringOtherApps: YES]; - [controller toggleFullScreen: nil]; + [normalWindow toggleFullScreen: nil]; }); } if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) { --=20 2.32.0 (Apple Git-132)