src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Sep 2018 11:49:25 -0700
changeset 12201 8bdc4d340419
parent 12070 1d65571b57dd
child 12239 55489adbb75c
permissions -rw-r--r--
Fixed whitespace
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];
slouken@9679
  1106
        if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
  1107
            return;
slouken@7191
  1108
        }
slouken@4687
  1109
slouken@6953
  1110
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
  1111
        float x = [touch normalizedPosition].x;
slouken@4673
  1112
        float y = [touch normalizedPosition].y;
slouken@5261
  1113
        /* Make the origin the upper left instead of the lower left */
slouken@5261
  1114
        y = 1.0f - y;
slouken@4687
  1115
slouken@8986
  1116
        switch (phase) {
slouken@8986
  1117
        case NSTouchPhaseBegan:
slouken@6951
  1118
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
  1119
            break;
slouken@8986
  1120
        case NSTouchPhaseEnded:
slouken@8986
  1121
        case NSTouchPhaseCancelled:
slouken@6951
  1122
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
  1123
            break;
slouken@8986
  1124
        case NSTouchPhaseMoved:
slouken@6951
  1125
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
  1126
            break;
slouken@8986
  1127
        default:
slouken@8986
  1128
            break;
slouken@4673
  1129
        }
slouken@4673
  1130
    }
slouken@1933
  1131
}
slouken@1933
  1132
slouken@1933
  1133
@end
slouken@1933
  1134
icculus@9623
  1135
@interface SDLView : NSView {
icculus@9623
  1136
    SDL_Window *_sdlWindow;
icculus@9623
  1137
}
icculus@9623
  1138
icculus@9623
  1139
- (void)setSDLWindow:(SDL_Window*)window;
jorgen@7158
  1140
slouken@5379
  1141
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
  1142
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
  1143
- (BOOL)mouseDownCanMoveWindow;
icculus@9623
  1144
- (void)drawRect:(NSRect)dirtyRect;
slime73@10140
  1145
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
gzjjgod@4915
  1146
@end
gzjjgod@4915
  1147
gzjjgod@4915
  1148
@implementation SDLView
icculus@9623
  1149
- (void)setSDLWindow:(SDL_Window*)window
icculus@9623
  1150
{
icculus@9623
  1151
    _sdlWindow = window;
icculus@9623
  1152
}
icculus@9623
  1153
icculus@9623
  1154
- (void)drawRect:(NSRect)dirtyRect
icculus@9623
  1155
{
icculus@11654
  1156
    /* Force the graphics context to clear to black so we don't get a flash of
icculus@11654
  1157
       white until the app is ready to draw. In practice on modern macOS, this
icculus@11654
  1158
       only gets called for window creation and other extraordinary events. */
icculus@11654
  1159
    [[NSColor blackColor] setFill];
icculus@11654
  1160
    NSRectFill(dirtyRect);
icculus@9623
  1161
    SDL_SendWindowEvent(_sdlWindow, SDL_WINDOWEVENT_EXPOSED, 0, 0);
icculus@9623
  1162
}
icculus@9623
  1163
gzjjgod@4915
  1164
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
  1165
{
slouken@5371
  1166
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
  1167
}
jorgen@7158
  1168
icculus@8931
  1169
- (BOOL)mouseDownCanMoveWindow
icculus@8931
  1170
{
icculus@8931
  1171
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
  1172
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
  1173
       during mouse events when we're using a drag area. */
icculus@8931
  1174
    return YES;
icculus@8931
  1175
}
icculus@8931
  1176
jorgen@7158
  1177
- (void)resetCursorRects
jorgen@7158
  1178
{
jorgen@7158
  1179
    [super resetCursorRects];
jorgen@7158
  1180
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
  1181
jorgen@7270
  1182
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
  1183
        [self addCursorRect:[self bounds]
jorgen@7158
  1184
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
  1185
    } else {
jorgen@7158
  1186
        [self addCursorRect:[self bounds]
jorgen@7158
  1187
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
  1188
    }
jorgen@7158
  1189
}
slime73@10140
  1190
slime73@10140
  1191
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
slime73@10140
  1192
{
slouken@10499
  1193
    if (SDL_GetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH)) {
slouken@10499
  1194
        return SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, SDL_FALSE);
slouken@10499
  1195
    } else {
slouken@10499
  1196
        return SDL_GetHintBoolean("SDL_MAC_MOUSE_FOCUS_CLICKTHROUGH", SDL_FALSE);
slouken@10378
  1197
    }
slime73@10140
  1198
}
gzjjgod@4915
  1199
@end
gzjjgod@4915
  1200
slouken@1933
  1201
static int
slouken@1951
  1202
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1203
{ @autoreleasepool
slouken@1933
  1204
{
slouken@1951
  1205
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1206
    SDL_WindowData *data;
slouken@1933
  1207
slouken@1933
  1208
    /* Allocate the window data */
slouken@8926
  1209
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1210
    if (!data) {
icculus@7037
  1211
        return SDL_OutOfMemory();
slouken@1933
  1212
    }
slouken@3685
  1213
    data->window = window;
slouken@3688
  1214
    data->nswindow = nswindow;
slouken@1933
  1215
    data->created = created;
slouken@1951
  1216
    data->videodata = videodata;
jorgen@7595
  1217
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1218
slouken@6848
  1219
    /* Create an event listener for the window */
slouken@6848
  1220
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1221
slouken@6848
  1222
    /* Fill in the SDL window with the window data */
slouken@6848
  1223
    {
slouken@6848
  1224
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1225
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1226
        window->x = (int)rect.origin.x;
slouken@6848
  1227
        window->y = (int)rect.origin.y;
slouken@6848
  1228
        window->w = (int)rect.size.width;
slouken@6848
  1229
        window->h = (int)rect.size.height;
slouken@6848
  1230
    }
slouken@6848
  1231
slouken@6848
  1232
    /* Set up the listener after we create the view */
slouken@6848
  1233
    [data->listener listen:data];
slouken@6848
  1234
slouken@6848
  1235
    if ([nswindow isVisible]) {
slouken@6848
  1236
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1237
    } else {
slouken@6848
  1238
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1239
    }
jorgen@7084
  1240
slouken@6848
  1241
    {
slouken@10353
  1242
        unsigned long style = [nswindow styleMask];
slouken@6848
  1243
slime73@11144
  1244
        if (style == NSWindowStyleMaskBorderless) {
slouken@6848
  1245
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1246
        } else {
slouken@6848
  1247
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1248
        }
slime73@11144
  1249
        if (style & NSWindowStyleMaskResizable) {
slouken@6848
  1250
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1251
        } else {
slouken@6848
  1252
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1253
        }
slouken@6848
  1254
    }
jorgen@7084
  1255
slouken@6848
  1256
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1257
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1258
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1259
    } else {
slouken@6848
  1260
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1261
    }
jorgen@7084
  1262
slouken@6848
  1263
    if ([nswindow isMiniaturized]) {
slouken@6848
  1264
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1265
    } else {
slouken@6848
  1266
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1267
    }
jorgen@7084
  1268
slouken@6848
  1269
    if ([nswindow isKeyWindow]) {
slouken@6848
  1270
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1271
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1272
    }
slouken@1933
  1273
jorgen@7085
  1274
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1275
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1276
     */
jorgen@7085
  1277
    [nswindow setOneShot:NO];
jorgen@7085
  1278
slouken@6848
  1279
    /* All done! */
slouken@6848
  1280
    window->driverdata = data;
slouken@6848
  1281
    return 0;
slime73@9587
  1282
}}
slouken@1933
  1283
slouken@1933
  1284
int
slouken@1933
  1285
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1286
{ @autoreleasepool
slouken@1933
  1287
{
icculus@8295
  1288
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1289
    NSWindow *nswindow;
slouken@6848
  1290
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1291
    NSRect rect;
slouken@6848
  1292
    SDL_Rect bounds;
slime73@10339
  1293
    NSUInteger style;
slouken@8986
  1294
    NSArray *screens = [NSScreen screens];
slouken@1933
  1295
slouken@6848
  1296
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1297
    rect.origin.x = window->x;
slouken@6848
  1298
    rect.origin.y = window->y;
slouken@6848
  1299
    rect.size.width = window->w;
slouken@6848
  1300
    rect.size.height = window->h;
slouken@8986
  1301
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1302
slouken@6848
  1303
    style = GetWindowStyle(window);
slouken@1933
  1304
slouken@6848
  1305
    /* Figure out which screen to place this window */
slouken@6848
  1306
    NSScreen *screen = nil;
slouken@8986
  1307
    for (NSScreen *candidate in screens) {
slouken@6848
  1308
        NSRect screenRect = [candidate frame];
slouken@6848
  1309
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1310
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1311
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1312
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1313
            screen = candidate;
slouken@6848
  1314
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1315
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1316
        }
slouken@6848
  1317
    }
slouken@7946
  1318
slouken@7946
  1319
    @try {
slouken@7946
  1320
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1321
    }
slouken@7946
  1322
    @catch (NSException *e) {
slime73@9587
  1323
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1324
    }
icculus@8284
  1325
icculus@8295
  1326
    if (videodata->allow_spaces) {
slouken@8986
  1327
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1328
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1329
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1330
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1331
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1332
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1333
        }
slouken@7955
  1334
    }
alexey@6832
  1335
slouken@7191
  1336
    /* Create a default view for this window */
slouken@6848
  1337
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
icculus@9623
  1338
    SDLView *contentView = [[SDLView alloc] initWithFrame:rect];
icculus@9623
  1339
    [contentView setSDLWindow:window];
urkle@7746
  1340
slouken@7955
  1341
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1342
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1343
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1344
        }
urkle@7746
  1345
    }
slouken@11723
  1346
#if SDL_VIDEO_OPENGL_ES2
slouken@11723
  1347
#if SDL_VIDEO_OPENGL_EGL
slouken@11723
  1348
    if ((window->flags & SDL_WINDOW_OPENGL) &&
slouken@11723
  1349
        _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
slouken@11723
  1350
        [contentView setWantsLayer:TRUE];
slouken@11723
  1351
    }
slouken@11723
  1352
#endif /* SDL_VIDEO_OPENGL_EGL */
slouken@11723
  1353
#endif /* SDL_VIDEO_OPENGL_ES2 */
slime73@10366
  1354
    [nswindow setContentView:contentView];
slouken@6848
  1355
    [contentView release];
alexey@6832
  1356
slouken@6848
  1357
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1358
        [nswindow release];
slouken@6848
  1359
        return -1;
slouken@3506
  1360
    }
slouken@11723
  1361
slouken@11723
  1362
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@11723
  1363
        return 0;
slouken@11723
  1364
    }
slouken@11723
  1365
    
slouken@11723
  1366
    /* The rest of this macro mess is for OpenGL or OpenGL ES windows */
slouken@11723
  1367
#if SDL_VIDEO_OPENGL_ES2
slouken@11723
  1368
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
slouken@11723
  1369
#if SDL_VIDEO_OPENGL_EGL
slouken@11723
  1370
        if (Cocoa_GLES_SetupWindow(_this, window) < 0) {
slouken@11723
  1371
            Cocoa_DestroyWindow(_this, window);
slouken@11723
  1372
            return -1;
slouken@11723
  1373
        }
slouken@11723
  1374
        return 0;
slouken@11723
  1375
#else
slouken@11723
  1376
        return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
slouken@11723
  1377
#endif /* SDL_VIDEO_OPENGL_EGL */
slouken@11723
  1378
    }
slouken@11723
  1379
#endif /* SDL_VIDEO_OPENGL_ES2 */
slouken@6848
  1380
    return 0;
slime73@9587
  1381
}}
slouken@1933
  1382
slouken@1933
  1383
int
slouken@1933
  1384
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1385
{ @autoreleasepool
slouken@1933
  1386
{
slouken@1933
  1387
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1388
    NSString *title;
slouken@1933
  1389
slouken@6848
  1390
    /* Query the title from the existing window */
slouken@6848
  1391
    title = [nswindow title];
slouken@6848
  1392
    if (title) {
slouken@6848
  1393
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1394
    }
slouken@1933
  1395
slouken@1951
  1396
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1397
}}
slouken@1933
  1398
slouken@1933
  1399
void
slouken@1933
  1400
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1401
{ @autoreleasepool
slouken@1933
  1402
{
slouken@9797
  1403
    const char *title = window->title ? window->title : "";
slouken@6848
  1404
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@9797
  1405
    NSString *string = [[NSString alloc] initWithUTF8String:title];
slouken@6848
  1406
    [nswindow setTitle:string];
slouken@6848
  1407
    [string release];
slime73@9587
  1408
}}
slouken@1933
  1409
slouken@1933
  1410
void
slouken@5375
  1411
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1412
{ @autoreleasepool
slouken@5375
  1413
{
slouken@6848
  1414
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1415
slouken@6848
  1416
    if (nsimage) {
slouken@6848
  1417
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1418
    }
slime73@9587
  1419
}}
slouken@5375
  1420
slouken@5375
  1421
void
slouken@1933
  1422
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1423
{ @autoreleasepool
slouken@1933
  1424
{
jorgen@7594
  1425
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1426
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1427
    NSRect rect;
slouken@6848
  1428
    Uint32 moveHack;
slouken@1933
  1429
slouken@6848
  1430
    rect.origin.x = window->x;
slouken@6848
  1431
    rect.origin.y = window->y;
slouken@6848
  1432
    rect.size.width = window->w;
slouken@6848
  1433
    rect.size.height = window->h;
slouken@8801
  1434
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1435
slouken@6848
  1436
    moveHack = s_moveHack;
slouken@6848
  1437
    s_moveHack = 0;
slouken@6848
  1438
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1439
    s_moveHack = moveHack;
slouken@5478
  1440
jorgen@7595
  1441
    ScheduleContextUpdates(windata);
slime73@9587
  1442
}}
slouken@1933
  1443
slouken@1933
  1444
void
slouken@1933
  1445
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1446
{ @autoreleasepool
slouken@1933
  1447
{
slouken@6848
  1448
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1449
    NSWindow *nswindow = windata->nswindow;
slime73@9860
  1450
    NSRect rect;
slime73@9860
  1451
    Uint32 moveHack;
slouken@1933
  1452
slime73@9860
  1453
    /* Cocoa will resize the window from the bottom-left rather than the
slime73@9860
  1454
     * top-left when -[nswindow setContentSize:] is used, so we must set the
slime73@9860
  1455
     * entire frame based on the new size, in order to preserve the position.
slime73@9860
  1456
     */
slime73@9860
  1457
    rect.origin.x = window->x;
slime73@9860
  1458
    rect.origin.y = window->y;
slime73@9860
  1459
    rect.size.width = window->w;
slime73@9860
  1460
    rect.size.height = window->h;
slime73@9860
  1461
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
icculus@9841
  1462
slime73@9860
  1463
    moveHack = s_moveHack;
slime73@9860
  1464
    s_moveHack = 0;
slime73@9860
  1465
    [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
slime73@9860
  1466
    s_moveHack = moveHack;
icculus@5564
  1467
jorgen@7595
  1468
    ScheduleContextUpdates(windata);
slime73@9587
  1469
}}
slouken@1933
  1470
slouken@1933
  1471
void
stopiccot@6681
  1472
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1473
{ @autoreleasepool
stopiccot@6681
  1474
{
slouken@6848
  1475
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1476
slouken@6848
  1477
    NSSize minSize;
slouken@6848
  1478
    minSize.width = window->min_w;
slouken@6848
  1479
    minSize.height = window->min_h;
slouken@7191
  1480
slouken@6848
  1481
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1482
}}
slouken@6788
  1483
slouken@6788
  1484
void
slouken@6788
  1485
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1486
{ @autoreleasepool
slouken@6788
  1487
{
slouken@6848
  1488
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1489
slouken@6848
  1490
    NSSize maxSize;
slouken@6848
  1491
    maxSize.width = window->max_w;
slouken@6848
  1492
    maxSize.height = window->max_h;
slouken@7191
  1493
slouken@6848
  1494
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1495
}}
stopiccot@6681
  1496
stopiccot@6681
  1497
void
slouken@1933
  1498
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1499
{ @autoreleasepool
slouken@1933
  1500
{
jorgen@7087
  1501
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1502
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1503
slouken@6848
  1504
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1505
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1506
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1507
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1508
    }
slime73@9587
  1509
}}
slouken@1933
  1510
slouken@1933
  1511
void
slouken@1933
  1512
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1513
{ @autoreleasepool
slouken@1933
  1514
{
slouken@6848
  1515
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1516
slouken@6848
  1517
    [nswindow orderOut:nil];
slime73@9587
  1518
}}
slouken@1933
  1519
slouken@1933
  1520
void
slouken@1933
  1521
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1522
{ @autoreleasepool
slouken@1933
  1523
{
jorgen@7087
  1524
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1525
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1526
slouken@7771
  1527
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1528
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1529
     */
jorgen@7087
  1530
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1531
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1532
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1533
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1534
    }
jorgen@7087
  1535
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1536
}}
slouken@1933
  1537
slouken@1933
  1538
void
slouken@1933
  1539
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1540
{ @autoreleasepool
slouken@1933
  1541
{
jorgen@7594
  1542
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1543
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1544
slouken@6848
  1545
    [nswindow zoom:nil];
slouken@6848
  1546
jorgen@7595
  1547
    ScheduleContextUpdates(windata);
slime73@9587
  1548
}}
slouken@1933
  1549
slouken@1933
  1550
void
slouken@1933
  1551
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1552
{ @autoreleasepool
slouken@1933
  1553
{
slouken@7963
  1554
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1555
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1556
slouken@7968
  1557
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1558
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1559
    } else {
slouken@7963
  1560
        [nswindow miniaturize:nil];
slouken@7963
  1561
    }
slime73@9587
  1562
}}
slouken@1933
  1563
slouken@1933
  1564
void
slouken@1933
  1565
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1566
{ @autoreleasepool
slouken@1933
  1567
{
slouken@6848
  1568
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1569
slouken@6848
  1570
    if ([nswindow isMiniaturized]) {
slouken@6848
  1571
        [nswindow deminiaturize:nil];
slouken@6848
  1572
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1573
        [nswindow zoom:nil];
slouken@1956
  1574
    }
slime73@9587
  1575
}}
slouken@1933
  1576
slouken@1933
  1577
void
icculus@6422
  1578
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1579
{ @autoreleasepool
icculus@6422
  1580
{
slouken@7990
  1581
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@9797
  1582
        if (bordered) {
slouken@7191
  1583
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1584
        }
icculus@6422
  1585
    }
slime73@9587
  1586
}}
icculus@6422
  1587
icculus@10385
  1588
void
icculus@10385
  1589
Cocoa_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
icculus@10385
  1590
{ @autoreleasepool
icculus@10385
  1591
{
icculus@10385
  1592
    /* Don't set this if we're in a space!
icculus@10385
  1593
     * The window will get permanently stuck if resizable is false.
icculus@10385
  1594
     * -flibit
icculus@10385
  1595
     */
icculus@10385
  1596
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10385
  1597
    Cocoa_WindowListener *listener = data->listener;
icculus@10385
  1598
    if (![listener isInFullscreenSpace]) {
icculus@10385
  1599
        SetWindowStyle(window, GetWindowStyle(window));
icculus@10385
  1600
    }
icculus@10385
  1601
}}
slouken@7952
  1602
slouken@7968
  1603
void
slouken@7968
  1604
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1605
{ @autoreleasepool
slouken@7952
  1606
{
slouken@6848
  1607
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1608
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1609
    NSRect rect;
slouken@5249
  1610
slouken@6848
  1611
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1612
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1613
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1614
    }
slouken@6848
  1615
slouken@6848
  1616
    if (fullscreen) {
slouken@6848
  1617
        SDL_Rect bounds;
slouken@6848
  1618
slouken@6848
  1619
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1620
        rect.origin.x = bounds.x;
slouken@6848
  1621
        rect.origin.y = bounds.y;
slouken@6848
  1622
        rect.size.width = bounds.w;
slouken@6848
  1623
        rect.size.height = bounds.h;
slouken@8801
  1624
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1625
slouken@6848
  1626
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1627
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1628
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1629
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1630
        }
slouken@5401
  1631
slime73@11144
  1632
        [nswindow setStyleMask:NSWindowStyleMaskBorderless];
slouken@6848
  1633
    } else {
slouken@6848
  1634
        rect.origin.x = window->windowed.x;
slouken@6848
  1635
        rect.origin.y = window->windowed.y;
slouken@6848
  1636
        rect.size.width = window->windowed.w;
slouken@6848
  1637
        rect.size.height = window->windowed.h;
slouken@8801
  1638
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1639
slime73@10176
  1640
        [nswindow setStyleMask:GetWindowStyle(window)];
slouken@9790
  1641
slime73@10176
  1642
        /* Hack to restore window decorations on Mac OS X 10.10 */
slime73@10176
  1643
        NSRect frameRect = [nswindow frame];
slime73@10176
  1644
        [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
slime73@10176
  1645
        [nswindow setFrame:frameRect display:NO];
slouken@5398
  1646
    }
slouken@6848
  1647
slouken@6848
  1648
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1649
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1650
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1651
    }
slouken@6848
  1652
slouken@6848
  1653
    s_moveHack = 0;
slouken@7873
  1654
    [nswindow setContentSize:rect.size];
slouken@6848
  1655
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1656
    s_moveHack = SDL_GetTicks();
slouken@6848
  1657
slouken@6848
  1658
    /* When the window style changes the title is cleared */
slouken@9797
  1659
    if (!fullscreen) {
slouken@6848
  1660
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1661
    }
slouken@6848
  1662
slouken@6848
  1663
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1664
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1665
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1666
    } else {
slouken@6848
  1667
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1668
    }
jorgen@7087
  1669
jorgen@7636
  1670
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1671
        [data->listener pauseVisibleObservation];
jorgen@7636
  1672
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1673
        [data->listener resumeVisibleObservation];
jorgen@7636
  1674
    }
slouken@6848
  1675
jorgen@7595
  1676
    ScheduleContextUpdates(data);
slime73@9587
  1677
}}
slouken@5249
  1678
slouken@5466
  1679
int
slouken@5466
  1680
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1681
{
slouken@5466
  1682
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1683
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1684
    const uint32_t tableSize = 256;
slouken@5466
  1685
    CGGammaValue redTable[tableSize];
slouken@5466
  1686
    CGGammaValue greenTable[tableSize];
slouken@5466
  1687
    CGGammaValue blueTable[tableSize];
slouken@5466
  1688
    uint32_t i;
slouken@5466
  1689
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1690
slouken@5466
  1691
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1692
    for (i = 0; i < 256; i++) {
slouken@5466
  1693
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1694
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1695
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1696
    }
slouken@5466
  1697
slouken@5466
  1698
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1699
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1700
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1701
    }
slouken@5466
  1702
    return 0;
slouken@5466
  1703
}
slouken@5466
  1704
slouken@5466
  1705
int
slouken@5466
  1706
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1707
{
slouken@5466
  1708
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1709
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1710
    const uint32_t tableSize = 256;
slouken@5466
  1711
    CGGammaValue redTable[tableSize];
slouken@5466
  1712
    CGGammaValue greenTable[tableSize];
slouken@5466
  1713
    CGGammaValue blueTable[tableSize];
slouken@5466
  1714
    uint32_t i, tableCopied;
slouken@5466
  1715
slouken@5466
  1716
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1717
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1718
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1719
    }
slouken@5466
  1720
slouken@5466
  1721
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1722
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1723
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1724
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1725
    }
slouken@5466
  1726
    return 0;
slouken@5466
  1727
}
slouken@5466
  1728
slouken@5249
  1729
void
slouken@6662
  1730
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1731
{
slouken@10653
  1732
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@10653
  1733
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@10653
  1734
slouken@10653
  1735
    /* Enable or disable the event tap as necessary */
slouken@10653
  1736
    Cocoa_EnableMouseEventTap(mouse->driverdata, grabbed);
slouken@10653
  1737
slouken@5371
  1738
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1739
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1740
        int x, y;
slouken@5371
  1741
        CGPoint cgpoint;
slouken@5371
  1742
slouken@5371
  1743
        SDL_GetMouseState(&x, &y);
slouken@5371
  1744
        cgpoint.x = window->x + x;
slouken@5371
  1745
        cgpoint.y = window->y + y;
jorgen@8261
  1746
jorgen@8261
  1747
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1748
jorgen@8261
  1749
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1750
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1751
    }
slouken@7191
  1752
icculus@8652
  1753
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slime73@9897
  1754
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)
slime73@9897
  1755
            && ![data->listener isInFullscreenSpace]) {
slouken@7191
  1756
            /* OpenGL is rendering to the window, so make it visible! */
slime73@9897
  1757
            /* Doing this in 10.11 while in a Space breaks things (bug #3152) */
slouken@7191
  1758
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1759
        } else {
slouken@7191
  1760
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1761
        }
slouken@7191
  1762
    }
slouken@1933
  1763
}
slouken@1933
  1764
slouken@1933
  1765
void
slouken@1933
  1766
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1767
{ @autoreleasepool
slouken@1933
  1768
{
slouken@6848
  1769
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1770
slouken@6848
  1771
    if (data) {
slouken@9907
  1772
        if ([data->listener isInFullscreenSpace]) {
slouken@9907
  1773
            [NSMenu setMenuBarVisible:YES];
slouken@9907
  1774
        }
slouken@6848
  1775
        [data->listener close];
slouken@6848
  1776
        [data->listener release];
slouken@6848
  1777
        if (data->created) {
slouken@6848
  1778
            [data->nswindow close];
slouken@1933
  1779
        }
jorgen@7595
  1780
jorgen@7595
  1781
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1782
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1783
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1784
            [context setWindow:NULL];
jorgen@7595
  1785
        }
jorgen@7595
  1786
        [data->nscontexts release];
jorgen@7595
  1787
slouken@6848
  1788
        SDL_free(data);
slouken@1933
  1789
    }
slouken@8978
  1790
    window->driverdata = NULL;
slime73@9587
  1791
}}
slouken@1933
  1792
slouken@1933
  1793
SDL_bool
slouken@1933
  1794
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1795
{
slouken@4900
  1796
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1797
slouken@1933
  1798
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1799
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1800
        info->info.cocoa.window = nswindow;
slouken@1933
  1801
        return SDL_TRUE;
slouken@1933
  1802
    } else {
philipp@10945
  1803
        SDL_SetError("Application not compiled with SDL %d.%d",
slouken@1933
  1804
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1805
        return SDL_FALSE;
slouken@1933
  1806
    }
slouken@1933
  1807
}
slouken@1933
  1808
slouken@7968
  1809
SDL_bool
slouken@7969
  1810
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1811
{
slouken@7969
  1812
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1813
slouken@7969
  1814
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1815
        return SDL_TRUE;
slouken@7969
  1816
    } else {
slouken@7969
  1817
        return SDL_FALSE;
slouken@7969
  1818
    }
slouken@7969
  1819
}
slouken@7969
  1820
slouken@7969
  1821
SDL_bool
slouken@7968
  1822
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1823
{ @autoreleasepool
slouken@7968
  1824
{
slouken@7968
  1825
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1826
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1827
slouken@7968
  1828
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@9904
  1829
        const int maxattempts = 3;
slouken@9904
  1830
        int attempt = 0;
slouken@9904
  1831
        while (++attempt <= maxattempts) {
slouken@9904
  1832
            /* Wait for the transition to complete, so application changes
slouken@9904
  1833
             take effect properly (e.g. setting the window size, etc.)
slouken@9904
  1834
             */
slouken@9904
  1835
            const int limit = 10000;
slouken@9904
  1836
            int count = 0;
slouken@9904
  1837
            while ([data->listener isInFullscreenSpaceTransition]) {
slouken@9904
  1838
                if ( ++count == limit ) {
slouken@9904
  1839
                    /* Uh oh, transition isn't completing. Should we assert? */
slouken@9904
  1840
                    break;
slouken@9904
  1841
                }
slouken@9904
  1842
                SDL_Delay(1);
slouken@9904
  1843
                SDL_PumpEvents();
slouken@9904
  1844
            }
slouken@9904
  1845
            if ([data->listener isInFullscreenSpace] == (state ? YES : NO))
slouken@9904
  1846
                break;
slouken@9904
  1847
            /* Try again, the last attempt was interrupted by user gestures */
slouken@9904
  1848
            if (![data->listener setFullscreenSpace:(state ? YES : NO)])
slouken@9904
  1849
                break; /* ??? */
slouken@9904
  1850
        }
slouken@9904
  1851
        /* Return TRUE to prevent non-space fullscreen logic from running */
slouken@7968
  1852
        succeeded = SDL_TRUE;
slouken@7968
  1853
    }
slouken@7968
  1854
slouken@7968
  1855
    return succeeded;
slime73@9587
  1856
}}
slouken@7968
  1857
icculus@8931
  1858
int
icculus@8935
  1859
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1860
{
icculus@8931
  1861
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1862
}
icculus@8931
  1863
icculus@12070
  1864
void
icculus@12070
  1865
Cocoa_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept)
icculus@12070
  1866
{
icculus@12070
  1867
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@12070
  1868
    if (accept) {
icculus@12070
  1869
        [data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
icculus@12070
  1870
    } else {
icculus@12070
  1871
        [data->nswindow unregisterDraggedTypes];
icculus@12070
  1872
    }
icculus@12070
  1873
}
icculus@12070
  1874
icculus@10025
  1875
int
icculus@10025
  1876
Cocoa_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
  1877
{
icculus@10025
  1878
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
  1879
    [data->nswindow setAlphaValue:opacity];
icculus@10025
  1880
    return 0;
icculus@10025
  1881
}
icculus@10025
  1882
slouken@6044
  1883
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1884
slouken@1933
  1885
/* vi: set ts=4 sw=4 expandtab: */