src/video/cocoa/SDL_cocoawindow.m
author Alex Szpakowski <slime73@gmail.com>
Sat, 10 Nov 2018 20:56:23 -0400
changeset 12405 5a95fbfd3617
parent 12404 eb60e952b13f
child 12424 6b3a68e3dd06
permissions -rw-r--r--
cocoa: fix building with the macOS 10.7 SDK (thanks Riccardo!)

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