Received: from eggs.gnu.org ([2001:4830:134:3::10]:60884) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y7uqS-0007CR-6L for qemu-devel@nongnu.org; Sun, 04 Jan 2015 18:44:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y7uqO-0005ZG-10 for qemu-devel@nongnu.org; Sun, 04 Jan 2015 18:44:28 -0500 Received: from mail-qg0-x230.google.com ([2607:f8b0:400d:c04::230]:40809) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y7uqN-0005Z4-S0 for qemu-devel@nongnu.org; Sun, 04 Jan 2015 18:44:23 -0500 Received: by mail-qg0-f48.google.com with SMTP id j5so3620824qga.21 for ; Sun, 04 Jan 2015 15:44:22 -0800 (PST) From: Programmingkid Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Date: Sun, 4 Jan 2015 18:44:20 -0500 Message-Id: <134B9C06-2137-4B1F-AB58-CB9695D40268@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH] Fixes several full screen issues on Mac OS X List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel qemu-devel Cc: Peter Maydell This patch makes several changes: - Fixes issue of returning to window mode and QEMU not setting the right graphic settings if there was a change during full screen = mode. - Eliminated distorted full screen display. - Makes full screen mode available on Mac OS 10.7 and higher. - Removes unneeded global variables cdx, and cdy. - Changes a few comments to make them clearer. Signed-off-by: John Arbuckle --- ui/cocoa.m | 106 = ++++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 65 insertions(+), 41 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index d37c29b..704d199 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -40,7 +40,6 @@ #define MAC_OS_X_VERSION_10_6 1060 #endif =20 - //#define DEBUG =20 #ifdef DEBUG @@ -250,13 +249,14 @@ static int cocoa_keycode_to_qemu(int keycode) { QEMUScreen screen; NSWindow *fullScreenWindow; - float cx,cy,cw,ch,cdx,cdy; + float cx,cy,cw,ch; CGDataProviderRef dataProviderRef; int modifiers_state[256]; BOOL isMouseGrabbed; BOOL isFullscreen; BOOL isAbsoluteEnabled; BOOL isMouseDeassociated; + NSDictionary * window_mode_dict; /* keeps track of the guest' = graphic settings */ } - (void) switchSurface:(DisplaySurface *)surface; - (void) grabMouse; @@ -279,9 +279,10 @@ static int cocoa_keycode_to_qemu(int keycode) - (BOOL) isMouseGrabbed; - (BOOL) isAbsoluteEnabled; - (BOOL) isMouseDeassociated; -- (float) cdx; -- (float) cdy; - (QEMUScreen) gscreen; +- (void) updateWindowModeSettings; +- (void) switchDisplayToWindowMode; +- (void) switchDisplayToFullScreenMode; @end =20 QemuCocoaView *cocoaView; @@ -293,12 +294,11 @@ QemuCocoaView *cocoaView; =20 self =3D [super initWithFrame:frameRect]; if (self) { - screen.bitsPerComponent =3D 8; screen.bitsPerPixel =3D 32; screen.width =3D frameRect.size.width; screen.height =3D frameRect.size.height; - + [self updateWindowModeSettings]; } return self; } @@ -394,10 +394,10 @@ QemuCocoaView *cocoaView; =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)screen.height - = (rectList[i].origin.y + rectList[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; + clipRect.origin.y =3D (float)screen.height - = (rectList[i].origin.y + rectList[i].size.height); + clipRect.size.width =3D rectList[i].size.width; + clipRect.size.height =3D rectList[i].size.height; clipImageRef =3D CGImageCreateWithImageInRect( imageRef, clipRect @@ -416,10 +416,8 @@ QemuCocoaView *cocoaView; COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); =20 if (isFullscreen) { - cdx =3D [[NSScreen mainScreen] frame].size.width / = (float)screen.width; - cdy =3D [[NSScreen mainScreen] frame].size.height / = (float)screen.height; - cw =3D screen.width * cdx; - ch =3D screen.height * cdy; + cw =3D screen.width; + ch =3D screen.height; =20 cx =3D ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; cy =3D ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; } else { @@ -427,23 +425,15 @@ QemuCocoaView *cocoaView; cy =3D 0; cw =3D screen.width; ch =3D screen.height; - cdx =3D 1.0; - cdy =3D 1.0; } } =20 - (void) switchSurface:(DisplaySurface *)surface { COCOA_DEBUG("QemuCocoaView: switchSurface\n"); - int w =3D surface_width(surface); int h =3D surface_height(surface); - /* 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); - + bool isResize =3D (w !=3D screen.width || h !=3D screen.height); int oldh =3D screen.height; if (isResize) { // Resize before we trigger the redraw, or we'll redraw at the = wrong size @@ -452,6 +442,16 @@ QemuCocoaView *cocoaView; screen.height =3D h; [self setContentDimensions]; [self setFrame:NSMakeRect(cx, cy, cw, ch)]; + + if (isFullscreen) { + /* I know, calling toggleFullScreen: looks like a mistake, but + it is the solution that works. I spent a long time looking=20= + for another way. This way is simple and works very well. */ + [self toggleFullScreen: nil]; + [self toggleFullScreen: nil]; + } + else /* !isFullscreen */ + [self updateWindowModeSettings]; } =20 // update screenBuffer @@ -482,8 +482,8 @@ QemuCocoaView *cocoaView; - (void) toggleFullScreen:(id)sender { COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); - - if (isFullscreen) { // switch from fullscreen to desktop + if (isFullscreen) { // switch from fullscreen to window mode + [self switchDisplayToWindowMode]; isFullscreen =3D FALSE; [self ungrabMouse]; [self setContentDimensions]; @@ -500,7 +500,8 @@ QemuCocoaView *cocoaView; #if (MAC_OS_X_VERSION_MAX_ALLOWED >=3D MAC_OS_X_VERSION_10_5) } #endif - } else { // switch from desktop to fullscreen + } else { // switch from window to fullscreen mode + [self switchDisplayToFullScreenMode]; isFullscreen =3D TRUE; [self grabMouse]; [self setContentDimensions]; @@ -561,7 +562,7 @@ QemuCocoaView *cocoaView; } =20 // release Mouse grab when pressing ctrl+alt - if (!isFullscreen && ([event modifierFlags] & = NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { + if (([event modifierFlags] & NSControlKeyMask) && ([event = modifierFlags] & NSAlternateKeyMask)) { [self ungrabMouse]; } break; @@ -781,9 +782,33 @@ QemuCocoaView *cocoaView; - (BOOL) isMouseGrabbed {return isMouseGrabbed;} - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} - (BOOL) isMouseDeassociated {return isMouseDeassociated;} -- (float) cdx {return cdx;} -- (float) cdy {return cdy;} - (QEMUScreen) gscreen {return screen;} + +/* Learn the guest' graphic settings when in window mode */ +- (void) updateWindowModeSettings +{ + window_mode_dict =3D CGDisplayCurrentMode(kCGDirectMainDisplay); +} + +/* Switches the monitor's display settings for using QEMU in a window = mode */ +- (void) switchDisplayToWindowMode +{ + CGDisplaySwitchToMode(kCGDirectMainDisplay, window_mode_dict); +} + +/* Switches the monitor's display settings for using QEMU in full = screen mode */ +- (void) switchDisplayToFullScreenMode +{ + size_t desired_bit_depth =3D 32; + boolean_t exact_match; + NSDictionary * mode =3D = CGDisplayBestModeForParameters(kCGDirectMainDisplay, desired_bit_depth, = (size_t)cw, (size_t)ch, &exact_match); + COCOA_DEBUG("switching to full screen: %d x %d\n", [[mode = valueForKey: @"Width"] intValue], [[mode valueForKey: @"Height"] = intValue]); + if (mode !=3D nil) + CGDisplaySwitchToMode(kCGDirectMainDisplay, mode); + else + printf("ERROR: failed to determine best mode for display!\n"); +} + @end =20 =20 @@ -832,7 +857,6 @@ QemuCocoaView *cocoaView; [normalWindow useOptimizedDrawing:YES]; [normalWindow makeKeyAndOrderFront:self]; [normalWindow center]; - } return self; } @@ -921,6 +945,15 @@ QemuCocoaView *cocoaView; [self startEmulationWithArgc:3 argv:(char**)argv]; } } + +/* 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=20 +menu item's old selector's name toggleFullScreen: */ +- (void) doToggleFullScreen:(id)sender +{ + [self toggleFullScreen:(id)sender]; +} + - (void)toggleFullScreen:(id)sender { COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); @@ -1005,7 +1038,7 @@ int main (int argc, const char * argv[]) { =20 // View menu menu =3D [[NSMenu alloc] initWithTitle:@"View"]; - [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter = Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] = autorelease]]; // Fullscreen + [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter = Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] = autorelease]]; // Fullscreen menuItem =3D [[[NSMenuItem alloc] initWithTitle:@"View" action:nil = keyEquivalent:@""] autorelease]; [menuItem setSubmenu:menu]; [[NSApp mainMenu] addItem:menuItem]; @@ -1050,17 +1083,8 @@ static void cocoa_update(DisplayChangeListener = *dcl, 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]); - } + rect =3D NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); [cocoaView setNeedsDisplayInRect:rect]; - [pool release]; } =20 --=20 1.7.5.4