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