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
3// found in the LICENSE file.
4
5#import "chrome/browser/fullscreen.h"
6
7#import <Carbon/Carbon.h>
8#import <Cocoa/Cocoa.h>
9
10#import "base/logging.h"
11
12@interface FullScreenMonitor : NSObject {
13 @private
14  BOOL fullScreen_;
15  EventHandlerRef eventHandler_;
16}
17
18@property (nonatomic, getter=isFullScreen) BOOL fullScreen;
19
20@end
21
22static 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
45@implementation FullScreenMonitor
46
47@synthesize fullScreen = fullScreen_;
48
49- (id)init {
50  if ((self = [super init])) {
51    // Check if the user is in presentation mode initially.
52    SystemUIMode currentMode;
53    GetSystemUIMode(&currentMode, NULL);
54    fullScreen_ = currentMode == kUIModeAllHidden;
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    }
70  }
71  return self;
72}
73
74- (void)dealloc {
75  if (eventHandler_)
76    RemoveEventHandler(eventHandler_);
77  [super dealloc];
78}
79
80@end
81
82static FullScreenMonitor* g_fullScreenMonitor = nil;
83
84void InitFullScreenMonitor() {
85  if (!g_fullScreenMonitor)
86    g_fullScreenMonitor = [[FullScreenMonitor alloc] init];
87}
88
89void StopFullScreenMonitor() {
90  [g_fullScreenMonitor release];
91  g_fullScreenMonitor = nil;
92}
93
94bool IsFullScreenMode() {
95  // Check if the main display has been captured (game in particular).
96  if (CGDisplayIsCaptured(CGMainDisplayID()))
97    return true;
98
99  return [g_fullScreenMonitor isFullScreen];
100}
101