Added support for new style fullscreen transitions on Mac OS X
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
diff --git a/src/video/cocoa/SDL_cocoaevents.m b/src/video/cocoa/SDL_cocoaevents.m
index e6b292b..30a63d8 100644
--- a/src/video/cocoa/SDL_cocoaevents.m
+++ b/src/video/cocoa/SDL_cocoaevents.m
@@ -147,6 +147,7 @@ CreateApplicationMenus(void)
NSMenu *appleMenu;
NSMenu *serviceMenu;
NSMenu *windowMenu;
+ NSMenu *viewMenu;
NSMenuItem *menuItem;
if (NSApp == nil) {
@@ -220,6 +221,25 @@ CreateApplicationMenus(void)
/* Tell the application object that this is now the window menu */
[NSApp setWindowsMenu:windowMenu];
[windowMenu release];
+
+
+ /* Add the fullscreen view toggle menu option, if supported */
+ if ([NSApp respondsToSelector:@selector(setPresentationOptions:)]) {
+ /* Create the view menu */
+ viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
+
+ /* Add menu items */
+ menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
+ [menuItem setKeyEquivalentModifierMask:NSControlKeyMask | NSCommandKeyMask];
+
+ /* Put menu into the menubar */
+ menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
+ [menuItem setSubmenu:viewMenu];
+ [[NSApp mainMenu] addItem:menuItem];
+ [menuItem release];
+
+ [viewMenu release];
+ }
}
void
diff --git a/src/video/cocoa/SDL_cocoawindow.h b/src/video/cocoa/SDL_cocoawindow.h
index c08c55c..1ec3d3c 100644
--- a/src/video/cocoa/SDL_cocoawindow.h
+++ b/src/video/cocoa/SDL_cocoawindow.h
@@ -32,11 +32,14 @@ typedef struct SDL_WindowData SDL_WindowData;
BOOL observingVisible;
BOOL wasCtrlLeft;
BOOL wasVisible;
+ BOOL isFullscreen;
+ BOOL inFullscreenTransition;
}
-(void) listen:(SDL_WindowData *) data;
-(void) pauseVisibleObservation;
-(void) resumeVisibleObservation;
+-(BOOL) isToggledFullscreen;
-(void) close;
/* Window delegate functionality */
@@ -48,6 +51,10 @@ typedef struct SDL_WindowData SDL_WindowData;
-(void) windowDidDeminiaturize:(NSNotification *) aNotification;
-(void) windowDidBecomeKey:(NSNotification *) aNotification;
-(void) windowDidResignKey:(NSNotification *) aNotification;
+-(void) windowWillEnterFullScreen:(NSNotification *) aNotification;
+-(void) windowDidEnterFullScreen:(NSNotification *) aNotification;
+-(void) windowWillExitFullScreen:(NSNotification *) aNotification;
+-(void) windowDidExitFullScreen:(NSNotification *) aNotification;
/* Window event handling */
-(void) mouseDown:(NSEvent *) theEvent;
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 00ee08f..91cc7d1 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -50,7 +50,8 @@ static void ConvertNSRect(NSRect *r)
r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
}
-static void ScheduleContextUpdates(SDL_WindowData *data)
+static void
+ScheduleContextUpdates(SDL_WindowData *data)
{
NSMutableArray *contexts = data->nscontexts;
@synchronized (contexts) {
@@ -60,12 +61,34 @@ static void ScheduleContextUpdates(SDL_WindowData *data)
}
}
-static int GetHintCtrlClickEmulateRightClick()
+static int
+GetHintCtrlClickEmulateRightClick()
{
const char *hint = SDL_GetHint( SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK );
return hint != NULL && *hint != '0';
}
+static unsigned int
+GetWindowStyle(SDL_Window * window)
+{
+ unsigned int style;
+
+ if (window->flags & SDL_WINDOW_FULLSCREEN) {
+ style = NSBorderlessWindowMask;
+ } else {
+ if (window->flags & SDL_WINDOW_BORDERLESS) {
+ style = NSBorderlessWindowMask;
+ } else {
+ style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
+ }
+ if (window->flags & SDL_WINDOW_RESIZABLE) {
+ style |= NSResizableWindowMask;
+ }
+ }
+ return style;
+}
+
+
@implementation Cocoa_WindowListener
- (void)listen:(SDL_WindowData *)data
@@ -78,6 +101,8 @@ static int GetHintCtrlClickEmulateRightClick()
observingVisible = YES;
wasCtrlLeft = NO;
wasVisible = [window isVisible];
+ isFullscreen = NO;
+ inFullscreenTransition = NO;
center = [NSNotificationCenter defaultCenter];
@@ -89,6 +114,10 @@ static int GetHintCtrlClickEmulateRightClick()
[center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
[center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
[center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
+ [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
+ [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
+ [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
+ [center addObserver:self selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:window];
} else {
[window setDelegate:self];
}
@@ -152,6 +181,11 @@ static int GetHintCtrlClickEmulateRightClick()
}
}
+- (BOOL) isToggledFullscreen
+{
+ return isFullscreen;
+}
+
- (void)close
{
NSNotificationCenter *center;
@@ -169,6 +203,10 @@ static int GetHintCtrlClickEmulateRightClick()
[center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
[center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
[center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
+ [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
+ [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
+ [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
+ [center removeObserver:self name:NSWindowDidExitFullScreenNotification object:window];
} else {
[window setDelegate:nil];
}
@@ -250,8 +288,15 @@ static int GetHintCtrlClickEmulateRightClick()
y = (int)rect.origin.y;
w = (int)rect.size.width;
h = (int)rect.size.height;
- if (SDL_IsShapedWindow(_data->window))
+
+ if (inFullscreenTransition) {
+ /* We'll take care of this at the end of the transition */
+ return;
+ }
+
+ if (SDL_IsShapedWindow(_data->window)) {
Cocoa_ResizeWindowShape(_data->window);
+ }
ScheduleContextUpdates(_data);
@@ -317,6 +362,46 @@ static int GetHintCtrlClickEmulateRightClick()
}
}
+- (void)windowWillEnterFullScreen:(NSNotification *)aNotification
+{
+ SDL_Window *window = _data->window;
+ NSWindow *nswindow = _data->nswindow;
+
+ if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
+ if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
+ [nswindow setStyleMask:(NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask)];
+ } else {
+ [nswindow setStyleMask:NSBorderlessWindowMask];
+ }
+ }
+ isFullscreen = YES;
+ inFullscreenTransition = YES;
+}
+
+- (void)windowDidEnterFullScreen:(NSNotification *)aNotification
+{
+ inFullscreenTransition = NO;
+ [self windowDidResize:aNotification];
+}
+
+- (void)windowWillExitFullScreen:(NSNotification *)aNotification
+{
+ inFullscreenTransition = YES;
+}
+
+- (void)windowDidExitFullScreen:(NSNotification *)aNotification
+{
+ SDL_Window *window = _data->window;
+ NSWindow *nswindow = _data->nswindow;
+
+ if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
+ [nswindow setStyleMask:GetWindowStyle(window)];
+ }
+ isFullscreen = NO;
+ inFullscreenTransition = NO;
+ [self windowDidResize:aNotification];
+}
+
/* We'll respond to key events by doing nothing so we don't beep.
* We could handle key messages here, but we lose some in the NSApp dispatch,
* where they get converted to action messages, etc.
@@ -606,26 +691,6 @@ static int GetHintCtrlClickEmulateRightClick()
}
@end
-static unsigned int
-GetWindowStyle(SDL_Window * window)
-{
- unsigned int style;
-
- if (window->flags & SDL_WINDOW_FULLSCREEN) {
- style = NSBorderlessWindowMask;
- } else {
- if (window->flags & SDL_WINDOW_BORDERLESS) {
- style = NSBorderlessWindowMask;
- } else {
- style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
- }
- if (window->flags & SDL_WINDOW_RESIZABLE) {
- style |= NSResizableWindowMask;
- }
- }
- return style;
-}
-
static int
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
{
@@ -758,6 +823,7 @@ Cocoa_CreateWindow(_THIS, SDL_Window * window)
return -1;
}
[nswindow setBackgroundColor:[NSColor blackColor]];
+ [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
/* Create a default view for this window */
rect = [nswindow contentRectForFrameRect:[nswindow frame]];
@@ -1020,10 +1086,45 @@ Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
[pool release];
}
-void
-Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+static SDL_bool
+Cocoa_CanToggleFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+{
+ SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+ NSWindow *nswindow = data->nswindow;
+
+ if (![nswindow respondsToSelector: @selector(toggleFullScreen:)]) {
+ return SDL_FALSE;
+ }
+
+ /* We can enter new style fullscreen mode for "fullscreen desktop" */
+ if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
+ return SDL_TRUE;
+ }
+
+ /* We can always leave new style fullscreen mode */
+ if (!fullscreen && [data->listener isToggledFullscreen]) {
+ return SDL_TRUE;
+ }
+
+ /* Requesting a mode switched fullscreen mode */
+ return SDL_FALSE;
+}
+
+static void
+Cocoa_SetWindowFullscreen_NewStyle(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+{
+ SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+ NSWindow *nswindow = data->nswindow;
+
+ if (fullscreen != [data->listener isToggledFullscreen]) {
+ [nswindow toggleFullScreen:nil];
+ }
+ ScheduleContextUpdates(data);
+}
+
+static void
+Cocoa_SetWindowFullscreen_OldStyle(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
NSWindow *nswindow = data->nswindow;
NSRect rect;
@@ -1097,6 +1198,18 @@ Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
}
ScheduleContextUpdates(data);
+}
+
+void
+Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
+{
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ if (Cocoa_CanToggleFullscreen(_this, window, display, fullscreen)) {
+ Cocoa_SetWindowFullscreen_NewStyle(_this, window, display, fullscreen);
+ } else {
+ Cocoa_SetWindowFullscreen_OldStyle(_this, window, display, fullscreen);
+ }
[pool release];
}