src/video/cocoa/SDL_cocoawindow.m
author David Ludwig <dludwig@pobox.com>
Sun, 03 Sep 2017 17:33:49 -0400
changeset 11447 f76299105635
parent 11436 37b2fcd8fc8c
child 11482 05aa6d232dca
permissions -rw-r--r--
macOS: bug-fix for #3793, "fullscreen toggle does not maintain SDL_Renderer's logical size"

This also seems to fix the follow-up issue in bug #3719, whereby the initial fix caused the SDL window to move, after transitioning from fullscreen to windowed-mode
slouken@1933
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1933
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1933
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1933
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1933
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1933
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@8083
    25
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
slouken@8083
    26
# error SDL for Mac OS X must be built with a 10.7 SDK or above.
slouken@8083
    27
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1070 */
slouken@8083
    28
slouken@1933
    29
#include "SDL_syswm.h"
slouken@5398
    30
#include "SDL_timer.h"  /* For SDL_GetTicks() */
slouken@7915
    31
#include "SDL_hints.h"
slouken@1933
    32
#include "../SDL_sysvideo.h"
slouken@1933
    33
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    34
#include "../../events/SDL_mouse_c.h"
slouken@4673
    35
#include "../../events/SDL_touch_c.h"
slouken@1933
    36
#include "../../events/SDL_windowevents_c.h"
jorgen@9237
    37
#include "../../events/SDL_dropevents_c.h"
slouken@1933
    38
#include "SDL_cocoavideo.h"
eligottlieb@4811
    39
#include "SDL_cocoashape.h"
gzjjgod@5057
    40
#include "SDL_cocoamouse.h"
slouken@10653
    41
#include "SDL_cocoamousetap.h"
jorgen@7594
    42
#include "SDL_cocoaopengl.h"
icculus@8295
    43
#include "SDL_assert.h"
slouken@1933
    44
jorgen@8261
    45
/* #define DEBUG_COCOAWINDOW */
jorgen@8261
    46
jorgen@8261
    47
#ifdef DEBUG_COCOAWINDOW
jorgen@8261
    48
#define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
jorgen@8261
    49
#else
jorgen@8261
    50
#define DLog(...) do { } while (0)
jorgen@8261
    51
#endif
jorgen@8261
    52
jorgen@8261
    53
slouken@8801
    54
#define FULLSCREEN_MASK (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN)
slouken@8801
    55
slouken@8801
    56
jorgen@9237
    57
@interface SDLWindow : NSWindow <NSDraggingDestination>
jorgen@8260
    58
/* These are needed for borderless/fullscreen windows */
jorgen@8260
    59
- (BOOL)canBecomeKeyWindow;
jorgen@8260
    60
- (BOOL)canBecomeMainWindow;
jorgen@8260
    61
- (void)sendEvent:(NSEvent *)event;
slouken@8809
    62
- (void)doCommandBySelector:(SEL)aSelector;
jorgen@9237
    63
jorgen@9237
    64
/* Handle drag-and-drop of files onto the SDL window. */
jorgen@9237
    65
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
jorgen@9237
    66
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
jorgen@9237
    67
- (BOOL)wantsPeriodicDraggingUpdates;
slime73@11434
    68
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
slime73@11434
    69
slime73@11434
    70
- (SDL_Window*)findSDLWindow;
jorgen@8260
    71
@end
jorgen@8260
    72
jorgen@8260
    73
@implementation SDLWindow
jorgen@9237
    74
slime73@11434
    75
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
slime73@11434
    76
{
slime73@11434
    77
    /* Only allow using the macOS native fullscreen toggle menubar item if the
slime73@11434
    78
     * window is resizable and not in a SDL fullscreen mode.
slime73@11434
    79
     */
slime73@11434
    80
    if ([menuItem action] == @selector(toggleFullScreen:)) {
slime73@11434
    81
        SDL_Window *window = [self findSDLWindow];
slime73@11434
    82
        if (window == NULL) {
slime73@11436
    83
            return NO;
slime73@11434
    84
        } else if ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_FULLSCREEN_DESKTOP)) != 0) {
slime73@11434
    85
            return NO;
slime73@11434
    86
        } else if ((window->flags & SDL_WINDOW_RESIZABLE) == 0) {
slime73@11434
    87
            return NO;
slime73@11434
    88
        }
slime73@11434
    89
    }
slime73@11436
    90
    return [super validateMenuItem:menuItem];
slime73@11434
    91
}
slime73@11434
    92
jorgen@8260
    93
- (BOOL)canBecomeKeyWindow
jorgen@8260
    94
{
jorgen@8260
    95
    return YES;
jorgen@8260
    96
}
jorgen@8260
    97
jorgen@8260
    98
- (BOOL)canBecomeMainWindow
jorgen@8260
    99
{
jorgen@8260
   100
    return YES;
jorgen@8260
   101
}
jorgen@8260
   102
jorgen@8260
   103
- (void)sendEvent:(NSEvent *)event
jorgen@8260
   104
{
slime73@10140
   105
    [super sendEvent:event];
jorgen@8260
   106
slime73@11144
   107
    if ([event type] != NSEventTypeLeftMouseUp) {
slime73@10140
   108
        return;
slime73@10140
   109
    }
jorgen@8260
   110
slime73@10140
   111
    id delegate = [self delegate];
slime73@10140
   112
    if (![delegate isKindOfClass:[Cocoa_WindowListener class]]) {
slime73@10140
   113
        return;
slime73@10140
   114
    }
jorgen@8260
   115
slime73@10140
   116
    if ([delegate isMoving]) {
slime73@10140
   117
        [delegate windowDidFinishMoving];
slime73@10140
   118
    }
jorgen@8260
   119
}
slouken@8809
   120
slouken@8809
   121
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@8809
   122
 * The escape key gets converted to a "cancel" selector, etc.
slouken@8809
   123
 */
slouken@8809
   124
- (void)doCommandBySelector:(SEL)aSelector
slouken@8809
   125
{
slouken@8809
   126
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@8809
   127
}
jorgen@9237
   128
jorgen@9237
   129
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
jorgen@9237
   130
{
icculus@9795
   131
    if (([sender draggingSourceOperationMask] & NSDragOperationGeneric) == NSDragOperationGeneric) {
icculus@9795
   132
        return NSDragOperationGeneric;
icculus@9795
   133
    }
icculus@9795
   134
icculus@9795
   135
    return NSDragOperationNone; /* no idea what to do with this, reject it. */
jorgen@9237
   136
}
jorgen@9237
   137
jorgen@9237
   138
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
icculus@9795
   139
{ @autoreleasepool
jorgen@9237
   140
{
icculus@9795
   141
    NSPasteboard *pasteboard = [sender draggingPasteboard];
icculus@9795
   142
    NSArray *types = [NSArray arrayWithObject:NSFilenamesPboardType];
icculus@9795
   143
    NSString *desiredType = [pasteboard availableTypeFromArray:types];
slime73@11434
   144
    SDL_Window *sdlwindow = [self findSDLWindow];
icculus@10022
   145
icculus@9795
   146
    if (desiredType == nil) {
icculus@9795
   147
        return NO;  /* can't accept anything that's being dropped here. */
icculus@9795
   148
    }
jorgen@9237
   149
icculus@9795
   150
    NSData *data = [pasteboard dataForType:desiredType];
icculus@9795
   151
    if (data == nil) {
jorgen@9237
   152
        return NO;
jorgen@9237
   153
    }
jorgen@9237
   154
icculus@9795
   155
    SDL_assert([desiredType isEqualToString:NSFilenamesPboardType]);
icculus@9795
   156
    NSArray *array = [pasteboard propertyListForType:@"NSFilenamesPboardType"];
jorgen@9237
   157
icculus@9795
   158
    for (NSString *path in array) {
icculus@10289
   159
        NSURL *fileURL = [NSURL fileURLWithPath:path];
icculus@9795
   160
        NSNumber *isAlias = nil;
jorgen@9237
   161
slime73@10176
   162
        [fileURL getResourceValue:&isAlias forKey:NSURLIsAliasFileKey error:nil];
icculus@9795
   163
icculus@9795
   164
        /* If the URL is an alias, resolve it. */
icculus@9795
   165
        if ([isAlias boolValue]) {
icculus@9795
   166
            NSURLBookmarkResolutionOptions opts = NSURLBookmarkResolutionWithoutMounting | NSURLBookmarkResolutionWithoutUI;
icculus@9795
   167
            NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL:fileURL error:nil];
icculus@9795
   168
            if (bookmark != nil) {
icculus@9795
   169
                NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData:bookmark
icculus@9795
   170
                                                               options:opts
icculus@9795
   171
                                                         relativeToURL:nil
icculus@9795
   172
                                                   bookmarkDataIsStale:nil
icculus@9795
   173
                                                                 error:nil];
icculus@9795
   174
icculus@9795
   175
                if (resolvedURL != nil) {
icculus@9795
   176
                    fileURL = resolvedURL;
icculus@9795
   177
                }
jorgen@9237
   178
            }
jorgen@9237
   179
        }
icculus@9795
   180
icculus@10022
   181
        if (!SDL_SendDropFile(sdlwindow, [[fileURL path] UTF8String])) {
icculus@9795
   182
            return NO;
icculus@9795
   183
        }
jorgen@9237
   184
    }
jorgen@9237
   185
icculus@10022
   186
    SDL_SendDropComplete(sdlwindow);
icculus@9795
   187
    return YES;
icculus@9795
   188
}}
jorgen@9237
   189
jorgen@9237
   190
- (BOOL)wantsPeriodicDraggingUpdates
jorgen@9237
   191
{
jorgen@9237
   192
    return NO;
jorgen@9237
   193
}
jorgen@9237
   194
slime73@11434
   195
- (SDL_Window*)findSDLWindow
slime73@11434
   196
{
slime73@11434
   197
    SDL_Window *sdlwindow = NULL;
slime73@11434
   198
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slime73@11434
   199
slime73@11434
   200
    /* !!! FIXME: is there a better way to do this? */
slime73@11434
   201
    if (_this) {
slime73@11434
   202
        for (sdlwindow = _this->windows; sdlwindow; sdlwindow = sdlwindow->next) {
slime73@11434
   203
            NSWindow *nswindow = ((SDL_WindowData *) sdlwindow->driverdata)->nswindow;
slime73@11434
   204
            if (nswindow == self) {
slime73@11434
   205
                break;
slime73@11434
   206
            }
slime73@11434
   207
        }
slime73@11434
   208
    }
slime73@11434
   209
slime73@11434
   210
    return sdlwindow;
slime73@11434
   211
}
slime73@11434
   212
jorgen@8260
   213
@end
jorgen@8260
   214
jorgen@8260
   215
slouken@5398
   216
static Uint32 s_moveHack;
slouken@5398
   217
slouken@8801
   218
static void ConvertNSRect(NSScreen *screen, BOOL fullscreen, NSRect *r)
slouken@1933
   219
{
slouken@9086
   220
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
   221
}
slouken@1933
   222
slouken@7952
   223
static void
slouken@7952
   224
ScheduleContextUpdates(SDL_WindowData *data)
jorgen@7595
   225
{
jorgen@8258
   226
    NSOpenGLContext *currentContext = [NSOpenGLContext currentContext];
jorgen@7595
   227
    NSMutableArray *contexts = data->nscontexts;
jorgen@7595
   228
    @synchronized (contexts) {
jorgen@7595
   229
        for (SDLOpenGLContext *context in contexts) {
jorgen@8258
   230
            if (context == currentContext) {
jorgen@8258
   231
                [context update];
jorgen@8258
   232
            } else {
jorgen@8258
   233
                [context scheduleUpdate];
jorgen@8258
   234
            }
jorgen@7595
   235
        }
jorgen@7595
   236
    }
jorgen@7595
   237
}
jorgen@7595
   238
icculus@10010
   239
/* !!! FIXME: this should use a hint callback. */
slouken@7952
   240
static int
slouken@7952
   241
GetHintCtrlClickEmulateRightClick()
slouken@7915
   242
{
slouken@10499
   243
	return SDL_GetHintBoolean(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, SDL_FALSE);
slouken@7915
   244
}
slouken@7915
   245
slime73@10176
   246
static NSUInteger
slouken@7952
   247
GetWindowStyle(SDL_Window * window)
slouken@7952
   248
{
slime73@10176
   249
    NSUInteger style = 0;
slouken@7952
   250
icculus@8295
   251
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slime73@11144
   252
        style = NSWindowStyleMaskBorderless;
slouken@7952
   253
    } else {
slouken@7952
   254
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slime73@11144
   255
            style = NSWindowStyleMaskBorderless;
slouken@7952
   256
        } else {
slime73@11144
   257
            style = (NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable);
slouken@7952
   258
        }
slouken@7952
   259
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slime73@11144
   260
            style |= NSWindowStyleMaskResizable;
slouken@7952
   261
        }
slouken@7952
   262
    }
slouken@7952
   263
    return style;
slouken@7952
   264
}
slouken@7952
   265
slouken@7990
   266
static SDL_bool
slime73@10176
   267
SetWindowStyle(SDL_Window * window, NSUInteger style)
slouken@7990
   268
{
slouken@7990
   269
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7990
   270
    NSWindow *nswindow = data->nswindow;
slouken@7990
   271
slouken@7990
   272
    /* The view responder chain gets messed with during setStyleMask */
slouken@7990
   273
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@7990
   274
        [[nswindow contentView] setNextResponder:nil];
slouken@7990
   275
    }
slouken@7990
   276
slime73@10176
   277
    [nswindow setStyleMask:style];
slouken@7990
   278
slouken@7990
   279
    /* The view responder chain gets messed with during setStyleMask */
slouken@7990
   280
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@7990
   281
        [[nswindow contentView] setNextResponder:data->listener];
slouken@7990
   282
    }
slouken@7990
   283
slouken@7990
   284
    return SDL_TRUE;
slouken@7990
   285
}
slouken@7990
   286
slouken@7952
   287
slouken@1933
   288
@implementation Cocoa_WindowListener
slouken@1933
   289
slouken@1933
   290
- (void)listen:(SDL_WindowData *)data
slouken@1933
   291
{
slouken@1933
   292
    NSNotificationCenter *center;
slouken@5371
   293
    NSWindow *window = data->nswindow;
slouken@5371
   294
    NSView *view = [window contentView];
slouken@1933
   295
slouken@1933
   296
    _data = data;
jorgen@7087
   297
    observingVisible = YES;
slouken@7740
   298
    wasCtrlLeft = NO;
jorgen@7087
   299
    wasVisible = [window isVisible];
slouken@7967
   300
    isFullscreenSpace = NO;
slouken@7952
   301
    inFullscreenTransition = NO;
slouken@7963
   302
    pendingWindowOperation = PENDING_OPERATION_NONE;
jorgen@8260
   303
    isMoving = NO;
icculus@8931
   304
    isDragAreaRunning = NO;
slouken@1933
   305
slouken@1933
   306
    center = [NSNotificationCenter defaultCenter];
slouken@1933
   307
slouken@5374
   308
    if ([window delegate] != nil) {
slouken@5374
   309
        [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
slouken@5374
   310
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
slouken@5374
   311
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
slouken@5374
   312
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
   313
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
   314
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
   315
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
slime73@9643
   316
        [center addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window];
slouken@7952
   317
        [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
slouken@7952
   318
        [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
slouken@7952
   319
        [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
slouken@7952
   320
        [center addObserver:self selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:window];
slouken@9903
   321
        [center addObserver:self selector:@selector(windowDidFailToEnterFullScreen:) name:@"NSWindowDidFailToEnterFullScreenNotification" object:window];
slouken@9903
   322
        [center addObserver:self selector:@selector(windowDidFailToExitFullScreen:) name:@"NSWindowDidFailToExitFullScreenNotification" object:window];
slouken@5374
   323
    } else {
slouken@5374
   324
        [window setDelegate:self];
slouken@5374
   325
    }
slouken@1933
   326
slouken@7191
   327
    /* Haven't found a delegate / notification that triggers when the window is
slouken@7191
   328
     * ordered out (is not visible any more). You can be ordered out without
slouken@7191
   329
     * minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
slouken@7191
   330
     */
jorgen@7084
   331
    [window addObserver:self
jorgen@7084
   332
             forKeyPath:@"visible"
jorgen@7084
   333
                options:NSKeyValueObservingOptionNew
jorgen@7084
   334
                context:NULL];
jorgen@7084
   335
slouken@5371
   336
    [window setNextResponder:self];
slouken@5371
   337
    [window setAcceptsMouseMovedEvents:YES];
slouken@5371
   338
slouken@5371
   339
    [view setNextResponder:self];
icculus@6108
   340
slime73@10176
   341
    [view setAcceptsTouchEvents:YES];
slouken@1933
   342
}
slouken@1933
   343
jorgen@7084
   344
- (void)observeValueForKeyPath:(NSString *)keyPath
jorgen@7084
   345
                      ofObject:(id)object
jorgen@7084
   346
                        change:(NSDictionary *)change
jorgen@7084
   347
                       context:(void *)context
jorgen@7084
   348
{
jorgen@7087
   349
    if (!observingVisible) {
jorgen@7087
   350
        return;
jorgen@7087
   351
    }
jorgen@7087
   352
jorgen@7084
   353
    if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
jorgen@7084
   354
        int newVisibility = [[change objectForKey:@"new"] intValue];
jorgen@7084
   355
        if (newVisibility) {
jorgen@7084
   356
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7084
   357
        } else {
jorgen@7084
   358
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7084
   359
        }
jorgen@7084
   360
    }
jorgen@7084
   361
}
jorgen@7084
   362
jorgen@7087
   363
-(void) pauseVisibleObservation
jorgen@7087
   364
{
jorgen@7087
   365
    observingVisible = NO;
jorgen@7087
   366
    wasVisible = [_data->nswindow isVisible];
jorgen@7087
   367
}
jorgen@7087
   368
jorgen@7087
   369
-(void) resumeVisibleObservation
jorgen@7087
   370
{
jorgen@7087
   371
    BOOL isVisible = [_data->nswindow isVisible];
jorgen@7087
   372
    observingVisible = YES;
jorgen@7087
   373
    if (wasVisible != isVisible) {
jorgen@7087
   374
        if (isVisible) {
jorgen@7087
   375
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7087
   376
        } else {
jorgen@7087
   377
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7087
   378
        }
jorgen@7087
   379
jorgen@7087
   380
        wasVisible = isVisible;
jorgen@7087
   381
    }
jorgen@7087
   382
}
jorgen@7087
   383
icculus@8284
   384
-(BOOL) setFullscreenSpace:(BOOL) state
slouken@7961
   385
{
slouken@7961
   386
    SDL_Window *window = _data->window;
slouken@7961
   387
    NSWindow *nswindow = _data->nswindow;
icculus@8295
   388
    SDL_VideoData *videodata = ((SDL_WindowData *) window->driverdata)->videodata;
slouken@7961
   389
icculus@8295
   390
    if (!videodata->allow_spaces) {
icculus@8295
   391
        return NO;  /* Spaces are forcibly disabled. */
icculus@8295
   392
    } else if (state && ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP)) {
icculus@8292
   393
        return NO;  /* we only allow you to make a Space on FULLSCREEN_DESKTOP windows. */
slouken@8798
   394
    } else if (!state && ((window->last_fullscreen_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP)) {
slouken@8798
   395
        return NO;  /* we only handle leaving the Space on windows that were previously FULLSCREEN_DESKTOP. */
icculus@8284
   396
    } else if (state == isFullscreenSpace) {
icculus@8284
   397
        return YES;  /* already there. */
slouken@7961
   398
    }
slouken@7961
   399
slouken@7961
   400
    if (inFullscreenTransition) {
slouken@7961
   401
        if (state) {
slouken@7963
   402
            [self addPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
slouken@7961
   403
        } else {
slouken@7963
   404
            [self addPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
slouken@7961
   405
        }
slouken@7961
   406
        return YES;
slouken@7961
   407
    }
slouken@7968
   408
    inFullscreenTransition = YES;
slouken@7968
   409
icculus@8295
   410
    /* you need to be FullScreenPrimary, or toggleFullScreen doesn't work. Unset it again in windowDidExitFullScreen. */
icculus@8284
   411
    [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7965
   412
    [nswindow performSelectorOnMainThread: @selector(toggleFullScreen:) withObject:nswindow waitUntilDone:NO];
slouken@7961
   413
    return YES;
slouken@7952
   414
}
slouken@7952
   415
slouken@7968
   416
-(BOOL) isInFullscreenSpace
slouken@7968
   417
{
slouken@7968
   418
    return isFullscreenSpace;
slouken@7968
   419
}
slouken@7968
   420
slouken@7968
   421
-(BOOL) isInFullscreenSpaceTransition
slouken@7963
   422
{
slouken@7963
   423
    return inFullscreenTransition;
slouken@7963
   424
}
slouken@7963
   425
slouken@7963
   426
-(void) addPendingWindowOperation:(PendingWindowOperation) operation
slouken@7963
   427
{
slouken@7963
   428
    pendingWindowOperation = operation;
slouken@7963
   429
}
slouken@7963
   430
slouken@1933
   431
- (void)close
slouken@1933
   432
{
slouken@1933
   433
    NSNotificationCenter *center;
slouken@5371
   434
    NSWindow *window = _data->nswindow;
slouken@5371
   435
    NSView *view = [window contentView];
slouken@1933
   436
slouken@1933
   437
    center = [NSNotificationCenter defaultCenter];
slouken@1933
   438
slouken@5374
   439
    if ([window delegate] != self) {
slouken@5374
   440
        [center removeObserver:self name:NSWindowDidExposeNotification object:window];
slouken@5374
   441
        [center removeObserver:self name:NSWindowDidMoveNotification object:window];
slouken@5374
   442
        [center removeObserver:self name:NSWindowDidResizeNotification object:window];
slouken@5374
   443
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
   444
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
   445
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
   446
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
slime73@9643
   447
        [center removeObserver:self name:NSWindowDidChangeBackingPropertiesNotification object:window];
slouken@7952
   448
        [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
slouken@7952
   449
        [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
slouken@7952
   450
        [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
slouken@7952
   451
        [center removeObserver:self name:NSWindowDidExitFullScreenNotification object:window];
slouken@9903
   452
        [center removeObserver:self name:@"NSWindowDidFailToEnterFullScreenNotification" object:window];
slouken@9903
   453
        [center removeObserver:self name:@"NSWindowDidFailToExitFullScreenNotification" object:window];
slouken@5374
   454
    } else {
slouken@5374
   455
        [window setDelegate:nil];
slouken@5374
   456
    }
slouken@5371
   457
slouken@7961
   458
    [window removeObserver:self forKeyPath:@"visible"];
jorgen@7084
   459
slouken@5374
   460
    if ([window nextResponder] == self) {
slouken@5374
   461
        [window setNextResponder:nil];
slouken@5374
   462
    }
slouken@5374
   463
    if ([view nextResponder] == self) {
slouken@5374
   464
        [view setNextResponder:nil];
slouken@5374
   465
    }
slouken@1933
   466
}
slouken@1933
   467
jorgen@8260
   468
- (BOOL)isMoving
jorgen@8260
   469
{
jorgen@8260
   470
    return isMoving;
jorgen@8260
   471
}
jorgen@8260
   472
jorgen@8260
   473
-(void) setPendingMoveX:(int)x Y:(int)y
jorgen@8260
   474
{
jorgen@8260
   475
    pendingWindowWarpX = x;
jorgen@8260
   476
    pendingWindowWarpY = y;
jorgen@8260
   477
}
jorgen@8260
   478
jorgen@8260
   479
- (void)windowDidFinishMoving
jorgen@8260
   480
{
slouken@8986
   481
    if ([self isMoving]) {
jorgen@8260
   482
        isMoving = NO;
jorgen@8260
   483
jorgen@8260
   484
        SDL_Mouse *mouse = SDL_GetMouse();
slouken@9086
   485
        if (pendingWindowWarpX != INT_MAX && pendingWindowWarpY != INT_MAX) {
slouken@9086
   486
            mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY);
slouken@9086
   487
            pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
jorgen@8260
   488
        }
slouken@9086
   489
        if (mouse->relative_mode && !mouse->relative_mode_warp && mouse->focus == _data->window) {
jorgen@8260
   490
            mouse->SetRelativeMouseMode(SDL_TRUE);
jorgen@8260
   491
        }
jorgen@8260
   492
    }
jorgen@8260
   493
}
jorgen@8260
   494
slouken@1933
   495
- (BOOL)windowShouldClose:(id)sender
slouken@1933
   496
{
slouken@3685
   497
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
   498
    return NO;
slouken@1933
   499
}
slouken@1933
   500
slouken@1933
   501
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
   502
{
slouken@3685
   503
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
   504
}
slouken@1933
   505
jorgen@8260
   506
- (void)windowWillMove:(NSNotification *)aNotification
jorgen@8260
   507
{
jorgen@8260
   508
    if ([_data->nswindow isKindOfClass:[SDLWindow class]]) {
slouken@9086
   509
        pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
jorgen@8260
   510
        isMoving = YES;
jorgen@8260
   511
    }
jorgen@8260
   512
}
jorgen@8260
   513
slouken@1933
   514
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   515
{
slouken@1933
   516
    int x, y;
slouken@5398
   517
    SDL_Window *window = _data->window;
slouken@5398
   518
    NSWindow *nswindow = _data->nswindow;
slouken@8801
   519
    BOOL fullscreen = window->flags & FULLSCREEN_MASK;
slouken@5398
   520
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
   521
    ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@5398
   522
slouken@5398
   523
    if (s_moveHack) {
slouken@5398
   524
        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
slouken@5398
   525
slouken@5398
   526
        s_moveHack = 0;
slouken@5398
   527
slouken@5398
   528
        if (blockMove) {
slouken@5398
   529
            /* Cocoa is adjusting the window in response to a mode change */
slouken@5398
   530
            rect.origin.x = window->x;
slouken@5398
   531
            rect.origin.y = window->y;
slouken@8801
   532
            ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@5398
   533
            [nswindow setFrameOrigin:rect.origin];
slouken@5398
   534
            return;
slouken@5398
   535
        }
slouken@5398
   536
    }
slouken@5398
   537
slouken@3507
   538
    x = (int)rect.origin.x;
slouken@3507
   539
    y = (int)rect.origin.y;
icculus@5564
   540
jorgen@7595
   541
    ScheduleContextUpdates(_data);
icculus@5564
   542
slouken@5398
   543
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   544
}
slouken@1933
   545
slouken@1933
   546
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   547
{
icculus@8284
   548
    if (inFullscreenTransition) {
icculus@8284
   549
        /* We'll take care of this at the end of the transition */
icculus@8284
   550
        return;
icculus@8284
   551
    }
icculus@8284
   552
slouken@7963
   553
    SDL_Window *window = _data->window;
slouken@7963
   554
    NSWindow *nswindow = _data->nswindow;
slouken@6231
   555
    int x, y, w, h;
slouken@7963
   556
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
   557
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6231
   558
    x = (int)rect.origin.x;
slouken@6231
   559
    y = (int)rect.origin.y;
slouken@1933
   560
    w = (int)rect.size.width;
slouken@1933
   561
    h = (int)rect.size.height;
slouken@7952
   562
slouken@7963
   563
    if (SDL_IsShapedWindow(window)) {
slouken@7963
   564
        Cocoa_ResizeWindowShape(window);
slouken@7952
   565
    }
icculus@5564
   566
jorgen@7595
   567
    ScheduleContextUpdates(_data);
icculus@5564
   568
slouken@6231
   569
    /* The window can move during a resize event, such as when maximizing
slouken@6231
   570
       or resizing from a corner */
slouken@7963
   571
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@7963
   572
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
icculus@7566
   573
slouken@7963
   574
    const BOOL zoomed = [nswindow isZoomed];
icculus@7566
   575
    if (!zoomed) {
slouken@7963
   576
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@7566
   577
    } else if (zoomed) {
slouken@7963
   578
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
icculus@7566
   579
    }
slouken@1933
   580
}
slouken@1933
   581
slouken@1933
   582
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   583
{
slouken@3685
   584
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   585
}
slouken@1933
   586
slouken@1933
   587
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   588
{
slouken@3685
   589
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   590
}
slouken@1933
   591
slouken@1933
   592
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   593
{
slouken@5367
   594
    SDL_Window *window = _data->window;
jorgen@7271
   595
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@9628
   596
icculus@9628
   597
    /* We're going to get keyboard events, since we're key. */
icculus@9628
   598
    /* This needs to be done before restoring the relative mouse mode. */
icculus@9628
   599
    SDL_SetKeyboardFocus(window);
icculus@9628
   600
slouken@9086
   601
    if (mouse->relative_mode && !mouse->relative_mode_warp && ![self isMoving]) {
jorgen@8260
   602
        mouse->SetRelativeMouseMode(SDL_TRUE);
jorgen@8260
   603
    }
slouken@5367
   604
slouken@5367
   605
    /* If we just gained focus we need the updated mouse position */
jorgen@7271
   606
    if (!mouse->relative_mode) {
slouken@5367
   607
        NSPoint point;
slouken@5396
   608
        int x, y;
slouken@5396
   609
slouken@5396
   610
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
slouken@5396
   611
        x = (int)point.x;
slouken@5396
   612
        y = (int)(window->h - point.y);
slouken@5396
   613
slouken@5396
   614
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
slouken@6950
   615
            SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@5396
   616
        }
slouken@5367
   617
    }
slouken@1962
   618
slouken@4503
   619
    /* Check to see if someone updated the clipboard */
slouken@4503
   620
    Cocoa_CheckClipboardUpdate(_data->videodata);
icculus@8288
   621
icculus@8293
   622
    if ((isFullscreenSpace) && ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)) {
icculus@8288
   623
        [NSMenu setMenuBarVisible:NO];
icculus@8288
   624
    }
icculus@9972
   625
slime73@11144
   626
    const unsigned int newflags = [NSEvent modifierFlags] & NSEventModifierFlagCapsLock;
slime73@11144
   627
    _data->videodata->modifierFlags = (_data->videodata->modifierFlags & ~NSEventModifierFlagCapsLock) | newflags;
slime73@10176
   628
    SDL_ToggleModState(KMOD_CAPS, newflags != 0);
slouken@1933
   629
}
slouken@1933
   630
slouken@1933
   631
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   632
{
jorgen@8260
   633
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@9086
   634
    if (mouse->relative_mode && !mouse->relative_mode_warp) {
jorgen@8260
   635
        mouse->SetRelativeMouseMode(SDL_FALSE);
jorgen@8260
   636
    }
jorgen@8260
   637
slouken@2059
   638
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   639
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   640
        SDL_SetMouseFocus(NULL);
slouken@2059
   641
    }
slouken@2059
   642
slouken@2059
   643
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   644
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   645
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   646
    }
icculus@8288
   647
icculus@8288
   648
    if (isFullscreenSpace) {
icculus@8288
   649
        [NSMenu setMenuBarVisible:YES];
icculus@8288
   650
    }
slouken@1933
   651
}
slouken@1933
   652
slime73@9643
   653
- (void)windowDidChangeBackingProperties:(NSNotification *)aNotification
slime73@9643
   654
{
slime73@9643
   655
    NSNumber *oldscale = [[aNotification userInfo] objectForKey:NSBackingPropertyOldScaleFactorKey];
slime73@9643
   656
slime73@9643
   657
    if (inFullscreenTransition) {
slime73@9643
   658
        return;
slime73@9643
   659
    }
slime73@9643
   660
slime73@9643
   661
    if ([oldscale doubleValue] != [_data->nswindow backingScaleFactor]) {
slime73@9643
   662
        /* Force a resize event when the backing scale factor changes. */
slime73@9643
   663
        _data->window->w = 0;
slime73@9643
   664
        _data->window->h = 0;
slime73@9643
   665
        [self windowDidResize:aNotification];
slime73@9643
   666
    }
slime73@9643
   667
}
slime73@9643
   668
slouken@7952
   669
- (void)windowWillEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   670
{
slouken@7952
   671
    SDL_Window *window = _data->window;
slouken@7952
   672
slime73@11144
   673
    SetWindowStyle(window, (NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable));
slouken@7965
   674
slouken@7967
   675
    isFullscreenSpace = YES;
slouken@7952
   676
    inFullscreenTransition = YES;
slouken@7952
   677
}
slouken@7952
   678
slouken@9903
   679
- (void)windowDidFailToEnterFullScreen:(NSNotification *)aNotification
slouken@9903
   680
{
slouken@9903
   681
    SDL_Window *window = _data->window;
slouken@9907
   682
slouken@9907
   683
    if (window->is_destroying) {
slouken@9907
   684
        return;
slouken@9907
   685
    }
slouken@9907
   686
slouken@9903
   687
    SetWindowStyle(window, GetWindowStyle(window));
slouken@9903
   688
slouken@9903
   689
    isFullscreenSpace = NO;
slouken@9903
   690
    inFullscreenTransition = NO;
slouken@9905
   691
    
slouken@9905
   692
    [self windowDidExitFullScreen:nil];
slouken@9903
   693
}
slouken@9903
   694
slouken@7952
   695
- (void)windowDidEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   696
{
slouken@7965
   697
    SDL_Window *window = _data->window;
slouken@11173
   698
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@11173
   699
    NSWindow *nswindow = data->nswindow;
slouken@7965
   700
slouken@7952
   701
    inFullscreenTransition = NO;
slouken@7961
   702
slouken@7963
   703
    if (pendingWindowOperation == PENDING_OPERATION_LEAVE_FULLSCREEN) {
slouken@7963
   704
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   705
        [self setFullscreenSpace:NO];
slouken@7961
   706
    } else {
slouken@11173
   707
        /* Unset the resizable flag. 
slouken@11173
   708
           This is a workaround for https://bugzilla.libsdl.org/show_bug.cgi?id=3697
slouken@11173
   709
         */
slouken@11173
   710
        SetWindowStyle(window, [nswindow styleMask] & (~NSWindowStyleMaskResizable));
slouken@11173
   711
icculus@8284
   712
        if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8284
   713
            [NSMenu setMenuBarVisible:NO];
icculus@8284
   714
        }
icculus@8284
   715
slouken@7963
   716
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   717
        /* Force the size change event in case it was delivered earlier
slouken@7965
   718
           while the window was still animating into place.
slouken@7965
   719
         */
slouken@7965
   720
        window->w = 0;
slouken@7965
   721
        window->h = 0;
slouken@7961
   722
        [self windowDidResize:aNotification];
slouken@7961
   723
    }
slouken@7952
   724
}
slouken@7952
   725
slouken@7952
   726
- (void)windowWillExitFullScreen:(NSNotification *)aNotification
slouken@7952
   727
{
slouken@7964
   728
    SDL_Window *window = _data->window;
slouken@7964
   729
slime73@9932
   730
    /* As of OS X 10.11, the window seems to need to be resizable when exiting
slime73@9932
   731
       a Space, in order for it to resize back to its windowed-mode size.
slime73@9932
   732
     */
slime73@11144
   733
    SetWindowStyle(window, GetWindowStyle(window) | NSWindowStyleMaskResizable);
slouken@7964
   734
slouken@7967
   735
    isFullscreenSpace = NO;
slouken@7952
   736
    inFullscreenTransition = YES;
slouken@7952
   737
}
slouken@7952
   738
slouken@9903
   739
- (void)windowDidFailToExitFullScreen:(NSNotification *)aNotification
slouken@9903
   740
{
slouken@9903
   741
    SDL_Window *window = _data->window;
slouken@9903
   742
    
slouken@9907
   743
    if (window->is_destroying) {
slouken@9907
   744
        return;
slouken@9907
   745
    }
slouken@9907
   746
slime73@11144
   747
    SetWindowStyle(window, (NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable));
slouken@9903
   748
    
slouken@9903
   749
    isFullscreenSpace = YES;
slouken@9903
   750
    inFullscreenTransition = NO;
slouken@9905
   751
    
slouken@9905
   752
    [self windowDidEnterFullScreen:nil];
slouken@9903
   753
}
slouken@9903
   754
slouken@7952
   755
- (void)windowDidExitFullScreen:(NSNotification *)aNotification
slouken@7952
   756
{
slouken@7965
   757
    SDL_Window *window = _data->window;
slouken@7952
   758
    NSWindow *nswindow = _data->nswindow;
slouken@7952
   759
slouken@7952
   760
    inFullscreenTransition = NO;
slouken@7961
   761
slime73@9932
   762
    SetWindowStyle(window, GetWindowStyle(window));
slime73@9932
   763
slouken@8618
   764
    [nswindow setLevel:kCGNormalWindowLevel];
slouken@8618
   765
slouken@7963
   766
    if (pendingWindowOperation == PENDING_OPERATION_ENTER_FULLSCREEN) {
slouken@7963
   767
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   768
        [self setFullscreenSpace:YES];
slouken@7963
   769
    } else if (pendingWindowOperation == PENDING_OPERATION_MINIMIZE) {
slouken@7963
   770
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7963
   771
        [nswindow miniaturize:nil];
slouken@7961
   772
    } else {
icculus@8291
   773
        /* Adjust the fullscreen toggle button and readd menu now that we're here. */
icculus@8291
   774
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8291
   775
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
icculus@8291
   776
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
icculus@8291
   777
        } else {
icculus@8284
   778
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
icculus@8284
   779
        }
icculus@8291
   780
        [NSMenu setMenuBarVisible:YES];
icculus@8284
   781
slouken@7963
   782
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@11421
   783
slouken@11421
   784
        /* Restore windowed size and position in case it changed while fullscreen */
slouken@11421
   785
        {
slouken@11421
   786
            NSRect rect;
slouken@11421
   787
            rect.origin.x = window->windowed.x;
slouken@11421
   788
            rect.origin.y = window->windowed.y;
slouken@11421
   789
            rect.size.width = window->windowed.w;
slouken@11421
   790
            rect.size.height = window->windowed.h;
slouken@11421
   791
            ConvertNSRect([nswindow screen], NO, &rect);
slouken@11421
   792
slouken@11421
   793
            s_moveHack = 0;
slouken@11421
   794
            [nswindow setContentSize:rect.size];
slouken@11421
   795
            [nswindow setFrameOrigin:rect.origin];
slouken@11421
   796
            s_moveHack = SDL_GetTicks();
slouken@11421
   797
        }
icculus@8622
   798
dludwig@11447
   799
        /* Force the size change event in case it was delivered earlier
dludwig@11447
   800
           while the window was still animating into place.
dludwig@11447
   801
         */
dludwig@11447
   802
        window->w = 0;
dludwig@11447
   803
        window->h = 0;
dludwig@11447
   804
        [self windowDidResize:aNotification];
dludwig@11447
   805
slouken@8623
   806
        /* FIXME: Why does the window get hidden? */
slouken@8623
   807
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@8623
   808
            Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
slouken@8623
   809
        }
slouken@7961
   810
    }
slouken@7952
   811
}
slouken@7952
   812
icculus@8291
   813
-(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
icculus@8291
   814
{
icculus@8291
   815
    if ((_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8291
   816
        return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
icculus@8291
   817
    } else {
icculus@8291
   818
        return proposedOptions;
icculus@8291
   819
    }
icculus@8291
   820
}
icculus@8291
   821
icculus@8291
   822
slouken@7191
   823
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   824
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   825
 * where they get converted to action messages, etc.
slouken@7191
   826
 */
slouken@6514
   827
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   828
{
slouken@7191
   829
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   830
}
slouken@6514
   831
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   832
{
slouken@7191
   833
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   834
}
slouken@6514
   835
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   836
{
slouken@7191
   837
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   838
}
slouken@6514
   839
slouken@7191
   840
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   841
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   842
 */
slouken@6514
   843
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   844
{
slouken@7191
   845
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   846
}
slouken@6514
   847
icculus@8935
   848
- (BOOL)processHitTest:(NSEvent *)theEvent
icculus@8931
   849
{
icculus@8935
   850
    SDL_assert(isDragAreaRunning == [_data->nswindow isMovableByWindowBackground]);
icculus@8931
   851
icculus@8935
   852
    if (_data->window->hit_test) {  /* if no hit-test, skip this. */
icculus@8931
   853
        const NSPoint location = [theEvent locationInWindow];
icculus@8931
   854
        const SDL_Point point = { (int) location.x, _data->window->h - (((int) location.y)-1) };
icculus@8935
   855
        const SDL_HitTestResult rc = _data->window->hit_test(_data->window, &point, _data->window->hit_test_data);
icculus@8935
   856
        if (rc == SDL_HITTEST_DRAGGABLE) {
icculus@8935
   857
            if (!isDragAreaRunning) {
icculus@8935
   858
                isDragAreaRunning = YES;
icculus@8935
   859
                [_data->nswindow setMovableByWindowBackground:YES];
icculus@8931
   860
            }
icculus@8935
   861
            return YES;  /* dragging! */
icculus@8931
   862
        }
icculus@8931
   863
    }
icculus@8931
   864
icculus@8931
   865
    if (isDragAreaRunning) {
icculus@8931
   866
        isDragAreaRunning = NO;
icculus@8931
   867
        [_data->nswindow setMovableByWindowBackground:NO];
icculus@8931
   868
        return YES;  /* was dragging, drop event. */
icculus@8931
   869
    }
icculus@8931
   870
icculus@8935
   871
    return NO;  /* not a special area, carry on. */
icculus@8931
   872
}
icculus@8931
   873
slouken@1933
   874
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   875
{
slouken@1959
   876
    int button;
slime73@10366
   877
    int clicks;
slouken@1933
   878
icculus@9685
   879
    /* Ignore events that aren't inside the client area (i.e. title bar.) */
icculus@9685
   880
    if ([theEvent window]) {
icculus@9983
   881
        NSRect windowRect = [[[theEvent window] contentView] frame];
slime73@10159
   882
        if (!NSMouseInRect([theEvent locationInWindow], windowRect, NO)) {
icculus@9685
   883
            return;
icculus@9685
   884
        }
icculus@9685
   885
    }
icculus@9685
   886
icculus@8935
   887
    if ([self processHitTest:theEvent]) {
icculus@10028
   888
        SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   889
        return;  /* dragging, drop event. */
icculus@8931
   890
    }
icculus@8931
   891
slouken@1959
   892
    switch ([theEvent buttonNumber]) {
slouken@1959
   893
    case 0:
slime73@11144
   894
        if (([theEvent modifierFlags] & NSEventModifierFlagControl) &&
slouken@7915
   895
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   896
            wasCtrlLeft = YES;
slouken@7740
   897
            button = SDL_BUTTON_RIGHT;
slouken@7740
   898
        } else {
slouken@7740
   899
            wasCtrlLeft = NO;
slouken@7740
   900
            button = SDL_BUTTON_LEFT;
slouken@7740
   901
        }
slouken@1959
   902
        break;
slouken@1959
   903
    case 1:
slouken@1959
   904
        button = SDL_BUTTON_RIGHT;
slouken@1959
   905
        break;
slouken@1959
   906
    case 2:
slouken@1959
   907
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   908
        break;
slouken@1959
   909
    default:
slime73@10339
   910
        button = (int) [theEvent buttonNumber] + 1;
slouken@1959
   911
        break;
slouken@1959
   912
    }
slime73@10366
   913
slime73@10366
   914
    clicks = (int) [theEvent clickCount];
slime73@10366
   915
    SDL_SendMouseButtonClicks(_data->window, 0, SDL_PRESSED, button, clicks);
slouken@1933
   916
}
slouken@1933
   917
slouken@1933
   918
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   919
{
slouken@1959
   920
    [self mouseDown:theEvent];
slouken@1933
   921
}
slouken@1933
   922
slouken@1933
   923
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   924
{
slouken@1959
   925
    [self mouseDown:theEvent];
slouken@1933
   926
}
slouken@1933
   927
slouken@1933
   928
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   929
{
slouken@1959
   930
    int button;
slime73@10366
   931
    int clicks;
slouken@1933
   932
icculus@8935
   933
    if ([self processHitTest:theEvent]) {
icculus@10028
   934
        SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   935
        return;  /* stopped dragging, drop event. */
icculus@8931
   936
    }
icculus@8931
   937
slouken@1959
   938
    switch ([theEvent buttonNumber]) {
slouken@1959
   939
    case 0:
slouken@7740
   940
        if (wasCtrlLeft) {
slouken@7740
   941
            button = SDL_BUTTON_RIGHT;
slouken@7740
   942
            wasCtrlLeft = NO;
slouken@7740
   943
        } else {
slouken@7740
   944
            button = SDL_BUTTON_LEFT;
slouken@7740
   945
        }
slouken@1959
   946
        break;
slouken@1959
   947
    case 1:
slouken@1959
   948
        button = SDL_BUTTON_RIGHT;
slouken@1959
   949
        break;
slouken@1959
   950
    case 2:
slouken@1959
   951
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   952
        break;
slouken@1959
   953
    default:
slime73@10339
   954
        button = (int) [theEvent buttonNumber] + 1;
slouken@1959
   955
        break;
slouken@1959
   956
    }
slime73@10366
   957
slime73@10366
   958
    clicks = (int) [theEvent clickCount];
slime73@10366
   959
    SDL_SendMouseButtonClicks(_data->window, 0, SDL_RELEASED, button, clicks);
slouken@1933
   960
}
slouken@1933
   961
slouken@1933
   962
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   963
{
slouken@1959
   964
    [self mouseUp:theEvent];
slouken@1933
   965
}
slouken@1933
   966
slouken@1933
   967
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   968
{
slouken@1959
   969
    [self mouseUp:theEvent];
slouken@1933
   970
}
slouken@1933
   971
slouken@1933
   972
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   973
{
slouken@5406
   974
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   975
    SDL_Window *window = _data->window;
slouken@5396
   976
    NSPoint point;
slouken@5396
   977
    int x, y;
slouken@1933
   978
icculus@8935
   979
    if ([self processHitTest:theEvent]) {
icculus@10028
   980
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   981
        return;  /* dragging, drop event. */
icculus@8931
   982
    }
icculus@8931
   983
slouken@5406
   984
    if (mouse->relative_mode) {
gzjjgod@5059
   985
        return;
slouken@5371
   986
    }
gzjjgod@5059
   987
slouken@5396
   988
    point = [theEvent locationInWindow];
slouken@5396
   989
    x = (int)point.x;
slouken@5396
   990
    y = (int)(window->h - point.y);
slouken@5371
   991
icculus@8929
   992
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@8929
   993
        if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   994
            if (x < 0) {
slouken@6666
   995
                x = 0;
slouken@6666
   996
            } else if (x >= window->w) {
slouken@6666
   997
                x = window->w - 1;
slouken@6666
   998
            }
slouken@6666
   999
            if (y < 0) {
slouken@6666
  1000
                y = 0;
slouken@6666
  1001
            } else if (y >= window->h) {
slouken@6666
  1002
                y = window->h - 1;
slouken@6666
  1003
            }
slouken@6666
  1004
jorgen@7593
  1005
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
  1006
            CGPoint cgpoint;
jorgen@8260
  1007
jorgen@7593
  1008
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
  1009
             * SDL_cocoamousetap.m.
jorgen@7593
  1010
             */
jorgen@7593
  1011
slouken@6666
  1012
            cgpoint.x = window->x + x;
slouken@6666
  1013
            cgpoint.y = window->y + y;
jorgen@7098
  1014
slouken@6666
  1015
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slime73@10158
  1016
            CGAssociateMouseAndMouseCursorPosition(YES);
jorgen@8261
  1017
jorgen@8261
  1018
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
  1019
#endif
slouken@5396
  1020
        }
slouken@2059
  1021
    }
slouken@6950
  1022
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
  1023
}
slouken@1933
  1024
slouken@1957
  1025
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
  1026
{
slouken@1957
  1027
    [self mouseMoved:theEvent];
slouken@1957
  1028
}
slouken@1957
  1029
slouken@1958
  1030
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
  1031
{
slouken@1958
  1032
    [self mouseMoved:theEvent];
slouken@1958
  1033
}
slouken@1958
  1034
slouken@1958
  1035
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
  1036
{
slouken@1958
  1037
    [self mouseMoved:theEvent];
slouken@1958
  1038
}
slouken@1958
  1039
slouken@1933
  1040
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
  1041
{
gzjjgod@5057
  1042
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
  1043
}
slouken@3688
  1044
slouken@4673
  1045
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
  1046
{
urkle@9236
  1047
    NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
urkle@9236
  1048
    int existingTouchCount = 0;
urkle@9236
  1049
urkle@9236
  1050
    for (NSTouch* touch in touches) {
urkle@9236
  1051
        if ([touch phase] != NSTouchPhaseBegan) {
urkle@9236
  1052
            existingTouchCount++;
urkle@9236
  1053
        }
urkle@9236
  1054
    }
urkle@9236
  1055
    if (existingTouchCount == 0) {
urkle@9236
  1056
        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
urkle@9236
  1057
        int numFingers = SDL_GetNumTouchFingers(touchID);
urkle@9236
  1058
        DLog("Reset Lost Fingers: %d", numFingers);
urkle@9236
  1059
        for (--numFingers; numFingers >= 0; --numFingers) {
urkle@9236
  1060
            SDL_Finger* finger = SDL_GetTouchFinger(touchID, numFingers);
urkle@9236
  1061
            SDL_SendTouch(touchID, finger->id, SDL_FALSE, 0, 0, 0);
urkle@9236
  1062
        }
urkle@9236
  1063
    }
urkle@9236
  1064
urkle@9236
  1065
    DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
slouken@8986
  1066
    [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
slouken@4673
  1067
}
slouken@4673
  1068
slouken@4673
  1069
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
  1070
{
slouken@8986
  1071
    [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
slouken@4673
  1072
}
slouken@4673
  1073
slouken@4673
  1074
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
  1075
{
slouken@8986
  1076
    [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
slouken@4673
  1077
}
slouken@4673
  1078
slouken@4673
  1079
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
  1080
{
slouken@8986
  1081
    [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
slouken@4673
  1082
}
slouken@4673
  1083
slouken@8986
  1084
- (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
slouken@4673
  1085
{
slouken@8986
  1086
    NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
slouken@4673
  1087
slouken@8986
  1088
    for (NSTouch *touch in touches) {
slouken@6953
  1089
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@9679
  1090
        if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
  1091
            return;
slouken@7191
  1092
        }
slouken@4687
  1093
slouken@6953
  1094
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
  1095
        float x = [touch normalizedPosition].x;
slouken@4673
  1096
        float y = [touch normalizedPosition].y;
slouken@5261
  1097
        /* Make the origin the upper left instead of the lower left */
slouken@5261
  1098
        y = 1.0f - y;
slouken@4687
  1099
slouken@8986
  1100
        switch (phase) {
slouken@8986
  1101
        case NSTouchPhaseBegan:
slouken@6951
  1102
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
  1103
            break;
slouken@8986
  1104
        case NSTouchPhaseEnded:
slouken@8986
  1105
        case NSTouchPhaseCancelled:
slouken@6951
  1106
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
  1107
            break;
slouken@8986
  1108
        case NSTouchPhaseMoved:
slouken@6951
  1109
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
  1110
            break;
slouken@8986
  1111
        default:
slouken@8986
  1112
            break;
slouken@4673
  1113
        }
slouken@4673
  1114
    }
slouken@1933
  1115
}
slouken@1933
  1116
slouken@1933
  1117
@end
slouken@1933
  1118
icculus@9623
  1119
@interface SDLView : NSView {
icculus@9623
  1120
    SDL_Window *_sdlWindow;
icculus@9623
  1121
}
icculus@9623
  1122
icculus@9623
  1123
- (void)setSDLWindow:(SDL_Window*)window;
jorgen@7158
  1124
slouken@5379
  1125
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
  1126
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
  1127
- (BOOL)mouseDownCanMoveWindow;
icculus@9623
  1128
- (void)drawRect:(NSRect)dirtyRect;
slime73@10140
  1129
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
gzjjgod@4915
  1130
@end
gzjjgod@4915
  1131
gzjjgod@4915
  1132
@implementation SDLView
icculus@9623
  1133
- (void)setSDLWindow:(SDL_Window*)window
icculus@9623
  1134
{
icculus@9623
  1135
    _sdlWindow = window;
icculus@9623
  1136
}
icculus@9623
  1137
icculus@9623
  1138
- (void)drawRect:(NSRect)dirtyRect
icculus@9623
  1139
{
icculus@9623
  1140
    SDL_SendWindowEvent(_sdlWindow, SDL_WINDOWEVENT_EXPOSED, 0, 0);
icculus@9623
  1141
}
icculus@9623
  1142
gzjjgod@4915
  1143
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
  1144
{
slouken@5371
  1145
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
  1146
}
jorgen@7158
  1147
icculus@8931
  1148
- (BOOL)mouseDownCanMoveWindow
icculus@8931
  1149
{
icculus@8931
  1150
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
  1151
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
  1152
       during mouse events when we're using a drag area. */
icculus@8931
  1153
    return YES;
icculus@8931
  1154
}
icculus@8931
  1155
jorgen@7158
  1156
- (void)resetCursorRects
jorgen@7158
  1157
{
jorgen@7158
  1158
    [super resetCursorRects];
jorgen@7158
  1159
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
  1160
jorgen@7270
  1161
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
  1162
        [self addCursorRect:[self bounds]
jorgen@7158
  1163
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
  1164
    } else {
jorgen@7158
  1165
        [self addCursorRect:[self bounds]
jorgen@7158
  1166
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
  1167
    }
jorgen@7158
  1168
}
slime73@10140
  1169
slime73@10140
  1170
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
slime73@10140
  1171
{
slouken@10499
  1172
    if (SDL_GetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH)) {
slouken@10499
  1173
        return SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, SDL_FALSE);
slouken@10499
  1174
    } else {
slouken@10499
  1175
        return SDL_GetHintBoolean("SDL_MAC_MOUSE_FOCUS_CLICKTHROUGH", SDL_FALSE);
slouken@10378
  1176
    }
slime73@10140
  1177
}
gzjjgod@4915
  1178
@end
gzjjgod@4915
  1179
slouken@1933
  1180
static int
slouken@1951
  1181
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1182
{ @autoreleasepool
slouken@1933
  1183
{
slouken@1951
  1184
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1185
    SDL_WindowData *data;
slouken@1933
  1186
slouken@1933
  1187
    /* Allocate the window data */
slouken@8926
  1188
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1189
    if (!data) {
icculus@7037
  1190
        return SDL_OutOfMemory();
slouken@1933
  1191
    }
slouken@3685
  1192
    data->window = window;
slouken@3688
  1193
    data->nswindow = nswindow;
slouken@1933
  1194
    data->created = created;
slouken@1951
  1195
    data->videodata = videodata;
jorgen@7595
  1196
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1197
slouken@6848
  1198
    /* Create an event listener for the window */
slouken@6848
  1199
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1200
slouken@6848
  1201
    /* Fill in the SDL window with the window data */
slouken@6848
  1202
    {
slouken@6848
  1203
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1204
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1205
        window->x = (int)rect.origin.x;
slouken@6848
  1206
        window->y = (int)rect.origin.y;
slouken@6848
  1207
        window->w = (int)rect.size.width;
slouken@6848
  1208
        window->h = (int)rect.size.height;
slouken@6848
  1209
    }
slouken@6848
  1210
slouken@6848
  1211
    /* Set up the listener after we create the view */
slouken@6848
  1212
    [data->listener listen:data];
slouken@6848
  1213
slouken@6848
  1214
    if ([nswindow isVisible]) {
slouken@6848
  1215
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1216
    } else {
slouken@6848
  1217
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1218
    }
jorgen@7084
  1219
slouken@6848
  1220
    {
slouken@10353
  1221
        unsigned long style = [nswindow styleMask];
slouken@6848
  1222
slime73@11144
  1223
        if (style == NSWindowStyleMaskBorderless) {
slouken@6848
  1224
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1225
        } else {
slouken@6848
  1226
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1227
        }
slime73@11144
  1228
        if (style & NSWindowStyleMaskResizable) {
slouken@6848
  1229
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1230
        } else {
slouken@6848
  1231
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1232
        }
slouken@6848
  1233
    }
jorgen@7084
  1234
slouken@6848
  1235
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1236
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1237
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1238
    } else {
slouken@6848
  1239
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1240
    }
jorgen@7084
  1241
slouken@6848
  1242
    if ([nswindow isMiniaturized]) {
slouken@6848
  1243
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1244
    } else {
slouken@6848
  1245
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1246
    }
jorgen@7084
  1247
slouken@6848
  1248
    if ([nswindow isKeyWindow]) {
slouken@6848
  1249
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1250
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1251
    }
slouken@1933
  1252
jorgen@7085
  1253
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1254
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1255
     */
jorgen@7085
  1256
    [nswindow setOneShot:NO];
jorgen@7085
  1257
slouken@6848
  1258
    /* All done! */
slouken@6848
  1259
    window->driverdata = data;
slouken@6848
  1260
    return 0;
slime73@9587
  1261
}}
slouken@1933
  1262
slouken@1933
  1263
int
slouken@1933
  1264
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1265
{ @autoreleasepool
slouken@1933
  1266
{
icculus@8295
  1267
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1268
    NSWindow *nswindow;
slouken@6848
  1269
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1270
    NSRect rect;
slouken@6848
  1271
    SDL_Rect bounds;
slime73@10339
  1272
    NSUInteger style;
slouken@8986
  1273
    NSArray *screens = [NSScreen screens];
slouken@1933
  1274
slouken@6848
  1275
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1276
    rect.origin.x = window->x;
slouken@6848
  1277
    rect.origin.y = window->y;
slouken@6848
  1278
    rect.size.width = window->w;
slouken@6848
  1279
    rect.size.height = window->h;
slouken@8986
  1280
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1281
slouken@6848
  1282
    style = GetWindowStyle(window);
slouken@1933
  1283
slouken@6848
  1284
    /* Figure out which screen to place this window */
slouken@6848
  1285
    NSScreen *screen = nil;
slouken@8986
  1286
    for (NSScreen *candidate in screens) {
slouken@6848
  1287
        NSRect screenRect = [candidate frame];
slouken@6848
  1288
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1289
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1290
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1291
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1292
            screen = candidate;
slouken@6848
  1293
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1294
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1295
        }
slouken@6848
  1296
    }
slouken@7946
  1297
slouken@7946
  1298
    @try {
slouken@7946
  1299
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1300
    }
slouken@7946
  1301
    @catch (NSException *e) {
slime73@9587
  1302
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1303
    }
icculus@7205
  1304
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1305
icculus@8295
  1306
    if (videodata->allow_spaces) {
slouken@8986
  1307
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1308
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1309
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1310
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1311
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1312
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1313
        }
slouken@7955
  1314
    }
alexey@6832
  1315
slouken@7191
  1316
    /* Create a default view for this window */
slouken@6848
  1317
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
icculus@9623
  1318
    SDLView *contentView = [[SDLView alloc] initWithFrame:rect];
icculus@9623
  1319
    [contentView setSDLWindow:window];
urkle@7746
  1320
slouken@7955
  1321
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1322
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1323
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1324
        }
urkle@7746
  1325
    }
urkle@7746
  1326
slime73@10366
  1327
    [nswindow setContentView:contentView];
slouken@6848
  1328
    [contentView release];
alexey@6832
  1329
jorgen@9237
  1330
    /* Allow files and folders to be dragged onto the window by users */
jorgen@9237
  1331
    [nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@9237
  1332
slouken@6848
  1333
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1334
        [nswindow release];
slouken@6848
  1335
        return -1;
slouken@3506
  1336
    }
slouken@6848
  1337
    return 0;
slime73@9587
  1338
}}
slouken@1933
  1339
slouken@1933
  1340
int
slouken@1933
  1341
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1342
{ @autoreleasepool
slouken@1933
  1343
{
slouken@1933
  1344
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1345
    NSString *title;
slouken@1933
  1346
slouken@6848
  1347
    /* Query the title from the existing window */
slouken@6848
  1348
    title = [nswindow title];
slouken@6848
  1349
    if (title) {
slouken@6848
  1350
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1351
    }
slouken@1933
  1352
slouken@1951
  1353
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1354
}}
slouken@1933
  1355
slouken@1933
  1356
void
slouken@1933
  1357
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1358
{ @autoreleasepool
slouken@1933
  1359
{
slouken@9797
  1360
    const char *title = window->title ? window->title : "";
slouken@6848
  1361
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@9797
  1362
    NSString *string = [[NSString alloc] initWithUTF8String:title];
slouken@6848
  1363
    [nswindow setTitle:string];
slouken@6848
  1364
    [string release];
slime73@9587
  1365
}}
slouken@1933
  1366
slouken@1933
  1367
void
slouken@5375
  1368
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1369
{ @autoreleasepool
slouken@5375
  1370
{
slouken@6848
  1371
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1372
slouken@6848
  1373
    if (nsimage) {
slouken@6848
  1374
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1375
    }
slime73@9587
  1376
}}
slouken@5375
  1377
slouken@5375
  1378
void
slouken@1933
  1379
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1380
{ @autoreleasepool
slouken@1933
  1381
{
jorgen@7594
  1382
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1383
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1384
    NSRect rect;
slouken@6848
  1385
    Uint32 moveHack;
slouken@1933
  1386
slouken@6848
  1387
    rect.origin.x = window->x;
slouken@6848
  1388
    rect.origin.y = window->y;
slouken@6848
  1389
    rect.size.width = window->w;
slouken@6848
  1390
    rect.size.height = window->h;
slouken@8801
  1391
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1392
slouken@6848
  1393
    moveHack = s_moveHack;
slouken@6848
  1394
    s_moveHack = 0;
slouken@6848
  1395
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1396
    s_moveHack = moveHack;
slouken@5478
  1397
jorgen@7595
  1398
    ScheduleContextUpdates(windata);
slime73@9587
  1399
}}
slouken@1933
  1400
slouken@1933
  1401
void
slouken@1933
  1402
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1403
{ @autoreleasepool
slouken@1933
  1404
{
slouken@6848
  1405
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1406
    NSWindow *nswindow = windata->nswindow;
slime73@9860
  1407
    NSRect rect;
slime73@9860
  1408
    Uint32 moveHack;
slouken@1933
  1409
slime73@9860
  1410
    /* Cocoa will resize the window from the bottom-left rather than the
slime73@9860
  1411
     * top-left when -[nswindow setContentSize:] is used, so we must set the
slime73@9860
  1412
     * entire frame based on the new size, in order to preserve the position.
slime73@9860
  1413
     */
slime73@9860
  1414
    rect.origin.x = window->x;
slime73@9860
  1415
    rect.origin.y = window->y;
slime73@9860
  1416
    rect.size.width = window->w;
slime73@9860
  1417
    rect.size.height = window->h;
slime73@9860
  1418
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
icculus@9841
  1419
slime73@9860
  1420
    moveHack = s_moveHack;
slime73@9860
  1421
    s_moveHack = 0;
slime73@9860
  1422
    [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
slime73@9860
  1423
    s_moveHack = moveHack;
icculus@5564
  1424
jorgen@7595
  1425
    ScheduleContextUpdates(windata);
slime73@9587
  1426
}}
slouken@1933
  1427
slouken@1933
  1428
void
stopiccot@6681
  1429
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1430
{ @autoreleasepool
stopiccot@6681
  1431
{
slouken@6848
  1432
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1433
slouken@6848
  1434
    NSSize minSize;
slouken@6848
  1435
    minSize.width = window->min_w;
slouken@6848
  1436
    minSize.height = window->min_h;
slouken@7191
  1437
slouken@6848
  1438
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1439
}}
slouken@6788
  1440
slouken@6788
  1441
void
slouken@6788
  1442
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1443
{ @autoreleasepool
slouken@6788
  1444
{
slouken@6848
  1445
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1446
slouken@6848
  1447
    NSSize maxSize;
slouken@6848
  1448
    maxSize.width = window->max_w;
slouken@6848
  1449
    maxSize.height = window->max_h;
slouken@7191
  1450
slouken@6848
  1451
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1452
}}
stopiccot@6681
  1453
stopiccot@6681
  1454
void
slouken@1933
  1455
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1456
{ @autoreleasepool
slouken@1933
  1457
{
jorgen@7087
  1458
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1459
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1460
slouken@6848
  1461
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1462
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1463
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1464
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1465
    }
slime73@9587
  1466
}}
slouken@1933
  1467
slouken@1933
  1468
void
slouken@1933
  1469
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1470
{ @autoreleasepool
slouken@1933
  1471
{
slouken@6848
  1472
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1473
slouken@6848
  1474
    [nswindow orderOut:nil];
slime73@9587
  1475
}}
slouken@1933
  1476
slouken@1933
  1477
void
slouken@1933
  1478
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1479
{ @autoreleasepool
slouken@1933
  1480
{
jorgen@7087
  1481
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1482
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1483
slouken@7771
  1484
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1485
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1486
     */
jorgen@7087
  1487
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1488
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1489
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1490
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1491
    }
jorgen@7087
  1492
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1493
}}
slouken@1933
  1494
slouken@1933
  1495
void
slouken@1933
  1496
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1497
{ @autoreleasepool
slouken@1933
  1498
{
jorgen@7594
  1499
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1500
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1501
slouken@6848
  1502
    [nswindow zoom:nil];
slouken@6848
  1503
jorgen@7595
  1504
    ScheduleContextUpdates(windata);
slime73@9587
  1505
}}
slouken@1933
  1506
slouken@1933
  1507
void
slouken@1933
  1508
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1509
{ @autoreleasepool
slouken@1933
  1510
{
slouken@7963
  1511
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1512
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1513
slouken@7968
  1514
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1515
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1516
    } else {
slouken@7963
  1517
        [nswindow miniaturize:nil];
slouken@7963
  1518
    }
slime73@9587
  1519
}}
slouken@1933
  1520
slouken@1933
  1521
void
slouken@1933
  1522
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1523
{ @autoreleasepool
slouken@1933
  1524
{
slouken@6848
  1525
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1526
slouken@6848
  1527
    if ([nswindow isMiniaturized]) {
slouken@6848
  1528
        [nswindow deminiaturize:nil];
slouken@6848
  1529
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1530
        [nswindow zoom:nil];
slouken@1956
  1531
    }
slime73@9587
  1532
}}
slouken@1933
  1533
slouken@1933
  1534
void
icculus@6422
  1535
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1536
{ @autoreleasepool
icculus@6422
  1537
{
slouken@7990
  1538
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@9797
  1539
        if (bordered) {
slouken@7191
  1540
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1541
        }
icculus@6422
  1542
    }
slime73@9587
  1543
}}
icculus@6422
  1544
icculus@10385
  1545
void
icculus@10385
  1546
Cocoa_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
icculus@10385
  1547
{ @autoreleasepool
icculus@10385
  1548
{
icculus@10385
  1549
    /* Don't set this if we're in a space!
icculus@10385
  1550
     * The window will get permanently stuck if resizable is false.
icculus@10385
  1551
     * -flibit
icculus@10385
  1552
     */
icculus@10385
  1553
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10385
  1554
    Cocoa_WindowListener *listener = data->listener;
icculus@10385
  1555
    if (![listener isInFullscreenSpace]) {
icculus@10385
  1556
        SetWindowStyle(window, GetWindowStyle(window));
icculus@10385
  1557
    }
icculus@10385
  1558
}}
slouken@7952
  1559
slouken@7968
  1560
void
slouken@7968
  1561
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1562
{ @autoreleasepool
slouken@7952
  1563
{
slouken@6848
  1564
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1565
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1566
    NSRect rect;
slouken@5249
  1567
slouken@6848
  1568
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1569
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1570
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1571
    }
slouken@6848
  1572
slouken@6848
  1573
    if (fullscreen) {
slouken@6848
  1574
        SDL_Rect bounds;
slouken@6848
  1575
slouken@6848
  1576
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1577
        rect.origin.x = bounds.x;
slouken@6848
  1578
        rect.origin.y = bounds.y;
slouken@6848
  1579
        rect.size.width = bounds.w;
slouken@6848
  1580
        rect.size.height = bounds.h;
slouken@8801
  1581
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1582
slouken@6848
  1583
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1584
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1585
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1586
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1587
        }
slouken@5401
  1588
slime73@11144
  1589
        [nswindow setStyleMask:NSWindowStyleMaskBorderless];
slouken@6848
  1590
    } else {
slouken@6848
  1591
        rect.origin.x = window->windowed.x;
slouken@6848
  1592
        rect.origin.y = window->windowed.y;
slouken@6848
  1593
        rect.size.width = window->windowed.w;
slouken@6848
  1594
        rect.size.height = window->windowed.h;
slouken@8801
  1595
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1596
slime73@10176
  1597
        [nswindow setStyleMask:GetWindowStyle(window)];
slouken@9790
  1598
slime73@10176
  1599
        /* Hack to restore window decorations on Mac OS X 10.10 */
slime73@10176
  1600
        NSRect frameRect = [nswindow frame];
slime73@10176
  1601
        [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
slime73@10176
  1602
        [nswindow setFrame:frameRect display:NO];
slouken@5398
  1603
    }
slouken@6848
  1604
slouken@6848
  1605
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1606
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1607
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1608
    }
slouken@6848
  1609
slouken@6848
  1610
    s_moveHack = 0;
slouken@7873
  1611
    [nswindow setContentSize:rect.size];
slouken@6848
  1612
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1613
    s_moveHack = SDL_GetTicks();
slouken@6848
  1614
slouken@6848
  1615
    /* When the window style changes the title is cleared */
slouken@9797
  1616
    if (!fullscreen) {
slouken@6848
  1617
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1618
    }
slouken@6848
  1619
slouken@6848
  1620
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1621
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1622
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1623
    } else {
slouken@6848
  1624
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1625
    }
jorgen@7087
  1626
jorgen@7636
  1627
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1628
        [data->listener pauseVisibleObservation];
jorgen@7636
  1629
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1630
        [data->listener resumeVisibleObservation];
jorgen@7636
  1631
    }
slouken@6848
  1632
jorgen@7595
  1633
    ScheduleContextUpdates(data);
slime73@9587
  1634
}}
slouken@5249
  1635
slouken@5466
  1636
int
slouken@5466
  1637
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1638
{
slouken@5466
  1639
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1640
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1641
    const uint32_t tableSize = 256;
slouken@5466
  1642
    CGGammaValue redTable[tableSize];
slouken@5466
  1643
    CGGammaValue greenTable[tableSize];
slouken@5466
  1644
    CGGammaValue blueTable[tableSize];
slouken@5466
  1645
    uint32_t i;
slouken@5466
  1646
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1647
slouken@5466
  1648
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1649
    for (i = 0; i < 256; i++) {
slouken@5466
  1650
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1651
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1652
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1653
    }
slouken@5466
  1654
slouken@5466
  1655
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1656
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1657
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1658
    }
slouken@5466
  1659
    return 0;
slouken@5466
  1660
}
slouken@5466
  1661
slouken@5466
  1662
int
slouken@5466
  1663
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1664
{
slouken@5466
  1665
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1666
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1667
    const uint32_t tableSize = 256;
slouken@5466
  1668
    CGGammaValue redTable[tableSize];
slouken@5466
  1669
    CGGammaValue greenTable[tableSize];
slouken@5466
  1670
    CGGammaValue blueTable[tableSize];
slouken@5466
  1671
    uint32_t i, tableCopied;
slouken@5466
  1672
slouken@5466
  1673
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1674
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1675
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1676
    }
slouken@5466
  1677
slouken@5466
  1678
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1679
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1680
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1681
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1682
    }
slouken@5466
  1683
    return 0;
slouken@5466
  1684
}
slouken@5466
  1685
slouken@5249
  1686
void
slouken@6662
  1687
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1688
{
slouken@10653
  1689
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@10653
  1690
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@10653
  1691
slouken@10653
  1692
    /* Enable or disable the event tap as necessary */
slouken@10653
  1693
    Cocoa_EnableMouseEventTap(mouse->driverdata, grabbed);
slouken@10653
  1694
slouken@5371
  1695
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1696
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1697
        int x, y;
slouken@5371
  1698
        CGPoint cgpoint;
slouken@5371
  1699
slouken@5371
  1700
        SDL_GetMouseState(&x, &y);
slouken@5371
  1701
        cgpoint.x = window->x + x;
slouken@5371
  1702
        cgpoint.y = window->y + y;
jorgen@8261
  1703
jorgen@8261
  1704
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1705
jorgen@8261
  1706
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1707
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1708
    }
slouken@7191
  1709
icculus@8652
  1710
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slime73@9897
  1711
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)
slime73@9897
  1712
            && ![data->listener isInFullscreenSpace]) {
slouken@7191
  1713
            /* OpenGL is rendering to the window, so make it visible! */
slime73@9897
  1714
            /* Doing this in 10.11 while in a Space breaks things (bug #3152) */
slouken@7191
  1715
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1716
        } else {
slouken@7191
  1717
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1718
        }
slouken@7191
  1719
    }
slouken@1933
  1720
}
slouken@1933
  1721
slouken@1933
  1722
void
slouken@1933
  1723
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1724
{ @autoreleasepool
slouken@1933
  1725
{
slouken@6848
  1726
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1727
slouken@6848
  1728
    if (data) {
slouken@9907
  1729
        if ([data->listener isInFullscreenSpace]) {
slouken@9907
  1730
            [NSMenu setMenuBarVisible:YES];
slouken@9907
  1731
        }
slouken@6848
  1732
        [data->listener close];
slouken@6848
  1733
        [data->listener release];
slouken@6848
  1734
        if (data->created) {
slouken@6848
  1735
            [data->nswindow close];
slouken@1933
  1736
        }
jorgen@7595
  1737
jorgen@7595
  1738
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1739
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1740
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1741
            [context setWindow:NULL];
jorgen@7595
  1742
        }
jorgen@7595
  1743
        [data->nscontexts release];
jorgen@7595
  1744
slouken@6848
  1745
        SDL_free(data);
slouken@1933
  1746
    }
slouken@8978
  1747
    window->driverdata = NULL;
slime73@9587
  1748
}}
slouken@1933
  1749
slouken@1933
  1750
SDL_bool
slouken@1933
  1751
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1752
{
slouken@4900
  1753
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1754
slouken@1933
  1755
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1756
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1757
        info->info.cocoa.window = nswindow;
slouken@1933
  1758
        return SDL_TRUE;
slouken@1933
  1759
    } else {
philipp@10945
  1760
        SDL_SetError("Application not compiled with SDL %d.%d",
slouken@1933
  1761
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1762
        return SDL_FALSE;
slouken@1933
  1763
    }
slouken@1933
  1764
}
slouken@1933
  1765
slouken@7968
  1766
SDL_bool
slouken@7969
  1767
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1768
{
slouken@7969
  1769
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1770
slouken@7969
  1771
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1772
        return SDL_TRUE;
slouken@7969
  1773
    } else {
slouken@7969
  1774
        return SDL_FALSE;
slouken@7969
  1775
    }
slouken@7969
  1776
}
slouken@7969
  1777
slouken@7969
  1778
SDL_bool
slouken@7968
  1779
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1780
{ @autoreleasepool
slouken@7968
  1781
{
slouken@7968
  1782
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1783
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1784
slouken@7968
  1785
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@9904
  1786
        const int maxattempts = 3;
slouken@9904
  1787
        int attempt = 0;
slouken@9904
  1788
        while (++attempt <= maxattempts) {
slouken@9904
  1789
            /* Wait for the transition to complete, so application changes
slouken@9904
  1790
             take effect properly (e.g. setting the window size, etc.)
slouken@9904
  1791
             */
slouken@9904
  1792
            const int limit = 10000;
slouken@9904
  1793
            int count = 0;
slouken@9904
  1794
            while ([data->listener isInFullscreenSpaceTransition]) {
slouken@9904
  1795
                if ( ++count == limit ) {
slouken@9904
  1796
                    /* Uh oh, transition isn't completing. Should we assert? */
slouken@9904
  1797
                    break;
slouken@9904
  1798
                }
slouken@9904
  1799
                SDL_Delay(1);
slouken@9904
  1800
                SDL_PumpEvents();
slouken@9904
  1801
            }
slouken@9904
  1802
            if ([data->listener isInFullscreenSpace] == (state ? YES : NO))
slouken@9904
  1803
                break;
slouken@9904
  1804
            /* Try again, the last attempt was interrupted by user gestures */
slouken@9904
  1805
            if (![data->listener setFullscreenSpace:(state ? YES : NO)])
slouken@9904
  1806
                break; /* ??? */
slouken@9904
  1807
        }
slouken@9904
  1808
        /* Return TRUE to prevent non-space fullscreen logic from running */
slouken@7968
  1809
        succeeded = SDL_TRUE;
slouken@7968
  1810
    }
slouken@7968
  1811
slouken@7968
  1812
    return succeeded;
slime73@9587
  1813
}}
slouken@7968
  1814
icculus@8931
  1815
int
icculus@8935
  1816
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1817
{
icculus@8931
  1818
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1819
}
icculus@8931
  1820
icculus@10025
  1821
int
icculus@10025
  1822
Cocoa_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
  1823
{
icculus@10025
  1824
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
  1825
    [data->nswindow setAlphaValue:opacity];
icculus@10025
  1826
    return 0;
icculus@10025
  1827
}
icculus@10025
  1828
slouken@6044
  1829
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1830
slouken@1933
  1831
/* vi: set ts=4 sw=4 expandtab: */