| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/fullscreen.h" | 5 #import "chrome/browser/fullscreen.h" |
| 6 | 6 |
| 7 #import <Carbon/Carbon.h> |
| 7 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 8 | 9 |
| 9 #import "base/logging.h" | 10 #import "base/logging.h" |
| 10 | 11 |
| 11 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | |
| 12 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
| 13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
| 14 | |
| 15 enum { | |
| 16 NSApplicationPresentationFullScreen = 1 << 10 | |
| 17 }; | |
| 18 | |
| 19 #endif // MAC_OS_X_VERSION_10_7 | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 BOOL AreOptionsFullScreen(NSApplicationPresentationOptions options) { | |
| 24 // If both dock and menu bar are hidden, that is the equivalent of the Carbon | |
| 25 // SystemUIMode (or Info.plist's LSUIPresentationMode) kUIModeAllHidden. | |
| 26 if (((options & NSApplicationPresentationHideDock) || | |
| 27 (options & NSApplicationPresentationAutoHideDock)) && | |
| 28 ((options & NSApplicationPresentationHideMenuBar) || | |
| 29 (options & NSApplicationPresentationAutoHideMenuBar))) { | |
| 30 return YES; | |
| 31 } | |
| 32 | |
| 33 if (options & NSApplicationPresentationFullScreen) | |
| 34 return YES; | |
| 35 | |
| 36 return NO; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 @interface FullScreenMonitor : NSObject { | 12 @interface FullScreenMonitor : NSObject { |
| 42 @private | 13 @private |
| 43 BOOL fullScreen_; | 14 BOOL fullScreen_; |
| 15 EventHandlerRef eventHandler_; |
| 44 } | 16 } |
| 45 | 17 |
| 46 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; | 18 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; |
| 47 | 19 |
| 48 @end | 20 @end |
| 49 | 21 |
| 22 static OSStatus handleAppEvent(EventHandlerCallRef myHandler, |
| 23 EventRef event, |
| 24 void* userData) { |
| 25 DCHECK(userData); |
| 26 |
| 27 FullScreenMonitor* fullScreenMonitor = |
| 28 reinterpret_cast<FullScreenMonitor*>(userData); |
| 29 |
| 30 UInt32 mode = 0; |
| 31 OSStatus status = GetEventParameter(event, |
| 32 kEventParamSystemUIMode, |
| 33 typeUInt32, |
| 34 NULL, |
| 35 sizeof(UInt32), |
| 36 NULL, |
| 37 &mode); |
| 38 if (status != noErr) |
| 39 return status; |
| 40 BOOL isFullScreenMode = mode == kUIModeAllHidden; |
| 41 [fullScreenMonitor setFullScreen:isFullScreenMode]; |
| 42 return noErr; |
| 43 } |
| 44 |
| 50 @implementation FullScreenMonitor | 45 @implementation FullScreenMonitor |
| 51 | 46 |
| 52 @synthesize fullScreen = fullScreen_; | 47 @synthesize fullScreen = fullScreen_; |
| 53 | 48 |
| 54 - (id)init { | 49 - (id)init { |
| 55 if ((self = [super init])) { | 50 if ((self = [super init])) { |
| 56 [NSApp addObserver:self | 51 // Check if the user is in presentation mode initially. |
| 57 forKeyPath:@"currentSystemPresentationOptions" | 52 SystemUIMode currentMode; |
| 58 options:NSKeyValueObservingOptionNew | | 53 GetSystemUIMode(¤tMode, NULL); |
| 59 NSKeyValueObservingOptionInitial | 54 fullScreen_ = currentMode == kUIModeAllHidden; |
| 60 context:nil]; | 55 |
| 56 // Register a Carbon event to receive the notification about the login |
| 57 // session's UI mode change. |
| 58 EventTypeSpec events[] = |
| 59 {{ kEventClassApplication, kEventAppSystemUIModeChanged }}; |
| 60 OSStatus status = InstallApplicationEventHandler( |
| 61 NewEventHandlerUPP(handleAppEvent), |
| 62 GetEventTypeCount(events), |
| 63 events, |
| 64 self, |
| 65 &eventHandler_); |
| 66 if (status) { |
| 67 [self release]; |
| 68 self = nil; |
| 69 } |
| 61 } | 70 } |
| 62 return self; | 71 return self; |
| 63 } | 72 } |
| 64 | 73 |
| 65 - (void)dealloc { | 74 - (void)dealloc { |
| 66 [NSApp removeObserver:self | 75 if (eventHandler_) |
| 67 forKeyPath:@"currentSystemPresentationOptions"]; | 76 RemoveEventHandler(eventHandler_); |
| 68 [super dealloc]; | 77 [super dealloc]; |
| 69 } | 78 } |
| 70 | 79 |
| 71 - (void)observeValueForKeyPath:(NSString*)keyPath | |
| 72 ofObject:(id)object | |
| 73 change:(NSDictionary*)change | |
| 74 context:(void*)context { | |
| 75 NSApplicationPresentationOptions options = | |
| 76 [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; | |
| 77 [self setFullScreen:AreOptionsFullScreen(options)]; | |
| 78 } | |
| 79 | |
| 80 @end | 80 @end |
| 81 | 81 |
| 82 static FullScreenMonitor* g_fullScreenMonitor = nil; | 82 static FullScreenMonitor* g_fullScreenMonitor = nil; |
| 83 | 83 |
| 84 void InitFullScreenMonitor() { | 84 void InitFullScreenMonitor() { |
| 85 if (!g_fullScreenMonitor) | 85 if (!g_fullScreenMonitor) |
| 86 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; | 86 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; |
| 87 } | 87 } |
| 88 | 88 |
| 89 void StopFullScreenMonitor() { | 89 void StopFullScreenMonitor() { |
| 90 [g_fullScreenMonitor release]; | 90 [g_fullScreenMonitor release]; |
| 91 g_fullScreenMonitor = nil; | 91 g_fullScreenMonitor = nil; |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool IsFullScreenMode() { | 94 bool IsFullScreenMode() { |
| 95 // Check if the main display has been captured (by games in particular). | 95 // Check if the main display has been captured (game in particular). |
| 96 if (CGDisplayIsCaptured(CGMainDisplayID())) | 96 if (CGDisplayIsCaptured(CGMainDisplayID())) |
| 97 return true; | 97 return true; |
| 98 | 98 |
| 99 return [g_fullScreenMonitor isFullScreen]; | 99 return [g_fullScreenMonitor isFullScreen]; |
| 100 } | 100 } |
| OLD | NEW |