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