src/video/cocoa/SDL_cocoawindow.m
author Alex Szpakowski <slime73@gmail.com>
Sat, 24 Sep 2016 18:46:34 -0300
changeset 10366 e1b52e69578b
parent 10353 c0f4fe10d1b3
child 10378 cb7f88c92a2e
permissions -rw-r--r--
Use OS-provided click counts on macOS and iOS for mouse press and release events.
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@10289
   138
        NSURL *fileURL = [NSURL fileURLWithPath:path];
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;
slime73@10366
   826
    int clicks;
slouken@1933
   827
icculus@9685
   828
    /* Ignore events that aren't inside the client area (i.e. title bar.) */
icculus@9685
   829
    if ([theEvent window]) {
icculus@9983
   830
        NSRect windowRect = [[[theEvent window] contentView] frame];
slime73@10159
   831
        if (!NSMouseInRect([theEvent locationInWindow], windowRect, NO)) {
icculus@9685
   832
            return;
icculus@9685
   833
        }
icculus@9685
   834
    }
icculus@9685
   835
icculus@8935
   836
    if ([self processHitTest:theEvent]) {
icculus@10028
   837
        SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   838
        return;  /* dragging, drop event. */
icculus@8931
   839
    }
icculus@8931
   840
slouken@1959
   841
    switch ([theEvent buttonNumber]) {
slouken@1959
   842
    case 0:
slouken@7915
   843
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   844
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   845
            wasCtrlLeft = YES;
slouken@7740
   846
            button = SDL_BUTTON_RIGHT;
slouken@7740
   847
        } else {
slouken@7740
   848
            wasCtrlLeft = NO;
slouken@7740
   849
            button = SDL_BUTTON_LEFT;
slouken@7740
   850
        }
slouken@1959
   851
        break;
slouken@1959
   852
    case 1:
slouken@1959
   853
        button = SDL_BUTTON_RIGHT;
slouken@1959
   854
        break;
slouken@1959
   855
    case 2:
slouken@1959
   856
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   857
        break;
slouken@1959
   858
    default:
slime73@10339
   859
        button = (int) [theEvent buttonNumber] + 1;
slouken@1959
   860
        break;
slouken@1959
   861
    }
slime73@10366
   862
slime73@10366
   863
    clicks = (int) [theEvent clickCount];
slime73@10366
   864
    SDL_SendMouseButtonClicks(_data->window, 0, SDL_PRESSED, button, clicks);
slouken@1933
   865
}
slouken@1933
   866
slouken@1933
   867
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   868
{
slouken@1959
   869
    [self mouseDown:theEvent];
slouken@1933
   870
}
slouken@1933
   871
slouken@1933
   872
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   873
{
slouken@1959
   874
    [self mouseDown:theEvent];
slouken@1933
   875
}
slouken@1933
   876
slouken@1933
   877
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   878
{
slouken@1959
   879
    int button;
slime73@10366
   880
    int clicks;
slouken@1933
   881
icculus@8935
   882
    if ([self processHitTest:theEvent]) {
icculus@10028
   883
        SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   884
        return;  /* stopped dragging, drop event. */
icculus@8931
   885
    }
icculus@8931
   886
slouken@1959
   887
    switch ([theEvent buttonNumber]) {
slouken@1959
   888
    case 0:
slouken@7740
   889
        if (wasCtrlLeft) {
slouken@7740
   890
            button = SDL_BUTTON_RIGHT;
slouken@7740
   891
            wasCtrlLeft = NO;
slouken@7740
   892
        } else {
slouken@7740
   893
            button = SDL_BUTTON_LEFT;
slouken@7740
   894
        }
slouken@1959
   895
        break;
slouken@1959
   896
    case 1:
slouken@1959
   897
        button = SDL_BUTTON_RIGHT;
slouken@1959
   898
        break;
slouken@1959
   899
    case 2:
slouken@1959
   900
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   901
        break;
slouken@1959
   902
    default:
slime73@10339
   903
        button = (int) [theEvent buttonNumber] + 1;
slouken@1959
   904
        break;
slouken@1959
   905
    }
slime73@10366
   906
slime73@10366
   907
    clicks = (int) [theEvent clickCount];
slime73@10366
   908
    SDL_SendMouseButtonClicks(_data->window, 0, SDL_RELEASED, button, clicks);
slouken@1933
   909
}
slouken@1933
   910
slouken@1933
   911
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   912
{
slouken@1959
   913
    [self mouseUp:theEvent];
slouken@1933
   914
}
slouken@1933
   915
slouken@1933
   916
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   917
{
slouken@1959
   918
    [self mouseUp:theEvent];
slouken@1933
   919
}
slouken@1933
   920
slouken@1933
   921
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   922
{
slouken@5406
   923
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   924
    SDL_Window *window = _data->window;
slouken@5396
   925
    NSPoint point;
slouken@5396
   926
    int x, y;
slouken@1933
   927
icculus@8935
   928
    if ([self processHitTest:theEvent]) {
icculus@10028
   929
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
icculus@8931
   930
        return;  /* dragging, drop event. */
icculus@8931
   931
    }
icculus@8931
   932
slouken@5406
   933
    if (mouse->relative_mode) {
gzjjgod@5059
   934
        return;
slouken@5371
   935
    }
gzjjgod@5059
   936
slouken@5396
   937
    point = [theEvent locationInWindow];
slouken@5396
   938
    x = (int)point.x;
slouken@5396
   939
    y = (int)(window->h - point.y);
slouken@5371
   940
icculus@8929
   941
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@8929
   942
        if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   943
            if (x < 0) {
slouken@6666
   944
                x = 0;
slouken@6666
   945
            } else if (x >= window->w) {
slouken@6666
   946
                x = window->w - 1;
slouken@6666
   947
            }
slouken@6666
   948
            if (y < 0) {
slouken@6666
   949
                y = 0;
slouken@6666
   950
            } else if (y >= window->h) {
slouken@6666
   951
                y = window->h - 1;
slouken@6666
   952
            }
slouken@6666
   953
jorgen@7593
   954
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   955
            CGPoint cgpoint;
jorgen@8260
   956
jorgen@7593
   957
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   958
             * SDL_cocoamousetap.m.
jorgen@7593
   959
             */
jorgen@7593
   960
slouken@6666
   961
            cgpoint.x = window->x + x;
slouken@6666
   962
            cgpoint.y = window->y + y;
jorgen@7098
   963
slouken@6666
   964
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slime73@10158
   965
            CGAssociateMouseAndMouseCursorPosition(YES);
jorgen@8261
   966
jorgen@8261
   967
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   968
#endif
slouken@5396
   969
        }
slouken@2059
   970
    }
slouken@6950
   971
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   972
}
slouken@1933
   973
slouken@1957
   974
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   975
{
slouken@1957
   976
    [self mouseMoved:theEvent];
slouken@1957
   977
}
slouken@1957
   978
slouken@1958
   979
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   980
{
slouken@1958
   981
    [self mouseMoved:theEvent];
slouken@1958
   982
}
slouken@1958
   983
slouken@1958
   984
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   985
{
slouken@1958
   986
    [self mouseMoved:theEvent];
slouken@1958
   987
}
slouken@1958
   988
slouken@1933
   989
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   990
{
gzjjgod@5057
   991
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   992
}
slouken@3688
   993
slouken@4673
   994
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   995
{
urkle@9236
   996
    NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
urkle@9236
   997
    int existingTouchCount = 0;
urkle@9236
   998
urkle@9236
   999
    for (NSTouch* touch in touches) {
urkle@9236
  1000
        if ([touch phase] != NSTouchPhaseBegan) {
urkle@9236
  1001
            existingTouchCount++;
urkle@9236
  1002
        }
urkle@9236
  1003
    }
urkle@9236
  1004
    if (existingTouchCount == 0) {
urkle@9236
  1005
        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
urkle@9236
  1006
        int numFingers = SDL_GetNumTouchFingers(touchID);
urkle@9236
  1007
        DLog("Reset Lost Fingers: %d", numFingers);
urkle@9236
  1008
        for (--numFingers; numFingers >= 0; --numFingers) {
urkle@9236
  1009
            SDL_Finger* finger = SDL_GetTouchFinger(touchID, numFingers);
urkle@9236
  1010
            SDL_SendTouch(touchID, finger->id, SDL_FALSE, 0, 0, 0);
urkle@9236
  1011
        }
urkle@9236
  1012
    }
urkle@9236
  1013
urkle@9236
  1014
    DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
slouken@8986
  1015
    [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
slouken@4673
  1016
}
slouken@4673
  1017
slouken@4673
  1018
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
  1019
{
slouken@8986
  1020
    [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
slouken@4673
  1021
}
slouken@4673
  1022
slouken@4673
  1023
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
  1024
{
slouken@8986
  1025
    [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
slouken@4673
  1026
}
slouken@4673
  1027
slouken@4673
  1028
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
  1029
{
slouken@8986
  1030
    [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
slouken@4673
  1031
}
slouken@4673
  1032
slouken@8986
  1033
- (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
slouken@4673
  1034
{
slouken@8986
  1035
    NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
slouken@4673
  1036
slouken@8986
  1037
    for (NSTouch *touch in touches) {
slouken@6953
  1038
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@9679
  1039
        if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
  1040
            return;
slouken@7191
  1041
        }
slouken@4687
  1042
slouken@6953
  1043
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
  1044
        float x = [touch normalizedPosition].x;
slouken@4673
  1045
        float y = [touch normalizedPosition].y;
slouken@5261
  1046
        /* Make the origin the upper left instead of the lower left */
slouken@5261
  1047
        y = 1.0f - y;
slouken@4687
  1048
slouken@8986
  1049
        switch (phase) {
slouken@8986
  1050
        case NSTouchPhaseBegan:
slouken@6951
  1051
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
  1052
            break;
slouken@8986
  1053
        case NSTouchPhaseEnded:
slouken@8986
  1054
        case NSTouchPhaseCancelled:
slouken@6951
  1055
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
  1056
            break;
slouken@8986
  1057
        case NSTouchPhaseMoved:
slouken@6951
  1058
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
  1059
            break;
slouken@8986
  1060
        default:
slouken@8986
  1061
            break;
slouken@4673
  1062
        }
slouken@4673
  1063
    }
slouken@1933
  1064
}
slouken@1933
  1065
slouken@1933
  1066
@end
slouken@1933
  1067
icculus@9623
  1068
@interface SDLView : NSView {
icculus@9623
  1069
    SDL_Window *_sdlWindow;
icculus@9623
  1070
}
icculus@9623
  1071
icculus@9623
  1072
- (void)setSDLWindow:(SDL_Window*)window;
jorgen@7158
  1073
slouken@5379
  1074
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
  1075
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
  1076
- (BOOL)mouseDownCanMoveWindow;
icculus@9623
  1077
- (void)drawRect:(NSRect)dirtyRect;
slime73@10140
  1078
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
gzjjgod@4915
  1079
@end
gzjjgod@4915
  1080
gzjjgod@4915
  1081
@implementation SDLView
icculus@9623
  1082
- (void)setSDLWindow:(SDL_Window*)window
icculus@9623
  1083
{
icculus@9623
  1084
    _sdlWindow = window;
icculus@9623
  1085
}
icculus@9623
  1086
icculus@9623
  1087
- (void)drawRect:(NSRect)dirtyRect
icculus@9623
  1088
{
icculus@9623
  1089
    SDL_SendWindowEvent(_sdlWindow, SDL_WINDOWEVENT_EXPOSED, 0, 0);
icculus@9623
  1090
}
icculus@9623
  1091
gzjjgod@4915
  1092
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
  1093
{
slouken@5371
  1094
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
  1095
}
jorgen@7158
  1096
icculus@8931
  1097
- (BOOL)mouseDownCanMoveWindow
icculus@8931
  1098
{
icculus@8931
  1099
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
  1100
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
  1101
       during mouse events when we're using a drag area. */
icculus@8931
  1102
    return YES;
icculus@8931
  1103
}
icculus@8931
  1104
jorgen@7158
  1105
- (void)resetCursorRects
jorgen@7158
  1106
{
jorgen@7158
  1107
    [super resetCursorRects];
jorgen@7158
  1108
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
  1109
jorgen@7270
  1110
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
  1111
        [self addCursorRect:[self bounds]
jorgen@7158
  1112
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
  1113
    } else {
jorgen@7158
  1114
        [self addCursorRect:[self bounds]
jorgen@7158
  1115
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
  1116
    }
jorgen@7158
  1117
}
slime73@10140
  1118
slime73@10140
  1119
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
slime73@10140
  1120
{
slime73@10140
  1121
    const char *hint = SDL_GetHint(SDL_HINT_MAC_MOUSE_FOCUS_CLICKTHROUGH);
slime73@10140
  1122
    return hint && *hint != '0';
slime73@10140
  1123
}
gzjjgod@4915
  1124
@end
gzjjgod@4915
  1125
slouken@1933
  1126
static int
slouken@1951
  1127
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1128
{ @autoreleasepool
slouken@1933
  1129
{
slouken@1951
  1130
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1131
    SDL_WindowData *data;
slouken@1933
  1132
slouken@1933
  1133
    /* Allocate the window data */
slouken@8926
  1134
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1135
    if (!data) {
icculus@7037
  1136
        return SDL_OutOfMemory();
slouken@1933
  1137
    }
slouken@3685
  1138
    data->window = window;
slouken@3688
  1139
    data->nswindow = nswindow;
slouken@1933
  1140
    data->created = created;
slouken@1951
  1141
    data->videodata = videodata;
jorgen@7595
  1142
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1143
slouken@6848
  1144
    /* Create an event listener for the window */
slouken@6848
  1145
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1146
slouken@6848
  1147
    /* Fill in the SDL window with the window data */
slouken@6848
  1148
    {
slouken@6848
  1149
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1150
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1151
        window->x = (int)rect.origin.x;
slouken@6848
  1152
        window->y = (int)rect.origin.y;
slouken@6848
  1153
        window->w = (int)rect.size.width;
slouken@6848
  1154
        window->h = (int)rect.size.height;
slouken@6848
  1155
    }
slouken@6848
  1156
slouken@6848
  1157
    /* Set up the listener after we create the view */
slouken@6848
  1158
    [data->listener listen:data];
slouken@6848
  1159
slouken@6848
  1160
    if ([nswindow isVisible]) {
slouken@6848
  1161
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1162
    } else {
slouken@6848
  1163
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1164
    }
jorgen@7084
  1165
slouken@6848
  1166
    {
slouken@10353
  1167
        unsigned long style = [nswindow styleMask];
slouken@6848
  1168
slouken@6848
  1169
        if (style == NSBorderlessWindowMask) {
slouken@6848
  1170
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1171
        } else {
slouken@6848
  1172
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1173
        }
slouken@6848
  1174
        if (style & NSResizableWindowMask) {
slouken@6848
  1175
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1176
        } else {
slouken@6848
  1177
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1178
        }
slouken@6848
  1179
    }
jorgen@7084
  1180
slouken@6848
  1181
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1182
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1183
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1184
    } else {
slouken@6848
  1185
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1186
    }
jorgen@7084
  1187
slouken@6848
  1188
    if ([nswindow isMiniaturized]) {
slouken@6848
  1189
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1190
    } else {
slouken@6848
  1191
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1192
    }
jorgen@7084
  1193
slouken@6848
  1194
    if ([nswindow isKeyWindow]) {
slouken@6848
  1195
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1196
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1197
    }
slouken@1933
  1198
jorgen@7085
  1199
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1200
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1201
     */
jorgen@7085
  1202
    [nswindow setOneShot:NO];
jorgen@7085
  1203
slouken@6848
  1204
    /* All done! */
slouken@6848
  1205
    window->driverdata = data;
slouken@6848
  1206
    return 0;
slime73@9587
  1207
}}
slouken@1933
  1208
slouken@1933
  1209
int
slouken@1933
  1210
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1211
{ @autoreleasepool
slouken@1933
  1212
{
icculus@8295
  1213
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1214
    NSWindow *nswindow;
slouken@6848
  1215
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1216
    NSRect rect;
slouken@6848
  1217
    SDL_Rect bounds;
slime73@10339
  1218
    NSUInteger style;
slouken@8986
  1219
    NSArray *screens = [NSScreen screens];
slouken@1933
  1220
slouken@6848
  1221
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1222
    rect.origin.x = window->x;
slouken@6848
  1223
    rect.origin.y = window->y;
slouken@6848
  1224
    rect.size.width = window->w;
slouken@6848
  1225
    rect.size.height = window->h;
slouken@8986
  1226
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1227
slouken@6848
  1228
    style = GetWindowStyle(window);
slouken@1933
  1229
slouken@6848
  1230
    /* Figure out which screen to place this window */
slouken@6848
  1231
    NSScreen *screen = nil;
slouken@8986
  1232
    for (NSScreen *candidate in screens) {
slouken@6848
  1233
        NSRect screenRect = [candidate frame];
slouken@6848
  1234
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1235
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1236
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1237
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1238
            screen = candidate;
slouken@6848
  1239
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1240
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1241
        }
slouken@6848
  1242
    }
slouken@7946
  1243
slouken@7946
  1244
    @try {
slouken@7946
  1245
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1246
    }
slouken@7946
  1247
    @catch (NSException *e) {
slime73@9587
  1248
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1249
    }
icculus@7205
  1250
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1251
icculus@8295
  1252
    if (videodata->allow_spaces) {
slouken@8986
  1253
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1254
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1255
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1256
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1257
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1258
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1259
        }
slouken@7955
  1260
    }
alexey@6832
  1261
slouken@7191
  1262
    /* Create a default view for this window */
slouken@6848
  1263
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
icculus@9623
  1264
    SDLView *contentView = [[SDLView alloc] initWithFrame:rect];
icculus@9623
  1265
    [contentView setSDLWindow:window];
urkle@7746
  1266
slouken@7955
  1267
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1268
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1269
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1270
        }
urkle@7746
  1271
    }
urkle@7746
  1272
slime73@10366
  1273
    [nswindow setContentView:contentView];
slouken@6848
  1274
    [contentView release];
alexey@6832
  1275
jorgen@9237
  1276
    /* Allow files and folders to be dragged onto the window by users */
jorgen@9237
  1277
    [nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@9237
  1278
slouken@6848
  1279
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1280
        [nswindow release];
slouken@6848
  1281
        return -1;
slouken@3506
  1282
    }
slouken@6848
  1283
    return 0;
slime73@9587
  1284
}}
slouken@1933
  1285
slouken@1933
  1286
int
slouken@1933
  1287
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1288
{ @autoreleasepool
slouken@1933
  1289
{
slouken@1933
  1290
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1291
    NSString *title;
slouken@1933
  1292
slouken@6848
  1293
    /* Query the title from the existing window */
slouken@6848
  1294
    title = [nswindow title];
slouken@6848
  1295
    if (title) {
slouken@6848
  1296
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1297
    }
slouken@1933
  1298
slouken@1951
  1299
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1300
}}
slouken@1933
  1301
slouken@1933
  1302
void
slouken@1933
  1303
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1304
{ @autoreleasepool
slouken@1933
  1305
{
slouken@9797
  1306
    const char *title = window->title ? window->title : "";
slouken@6848
  1307
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@9797
  1308
    NSString *string = [[NSString alloc] initWithUTF8String:title];
slouken@6848
  1309
    [nswindow setTitle:string];
slouken@6848
  1310
    [string release];
slime73@9587
  1311
}}
slouken@1933
  1312
slouken@1933
  1313
void
slouken@5375
  1314
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1315
{ @autoreleasepool
slouken@5375
  1316
{
slouken@6848
  1317
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1318
slouken@6848
  1319
    if (nsimage) {
slouken@6848
  1320
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1321
    }
slime73@9587
  1322
}}
slouken@5375
  1323
slouken@5375
  1324
void
slouken@1933
  1325
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1326
{ @autoreleasepool
slouken@1933
  1327
{
jorgen@7594
  1328
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1329
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1330
    NSRect rect;
slouken@6848
  1331
    Uint32 moveHack;
slouken@1933
  1332
slouken@6848
  1333
    rect.origin.x = window->x;
slouken@6848
  1334
    rect.origin.y = window->y;
slouken@6848
  1335
    rect.size.width = window->w;
slouken@6848
  1336
    rect.size.height = window->h;
slouken@8801
  1337
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1338
slouken@6848
  1339
    moveHack = s_moveHack;
slouken@6848
  1340
    s_moveHack = 0;
slouken@6848
  1341
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1342
    s_moveHack = moveHack;
slouken@5478
  1343
jorgen@7595
  1344
    ScheduleContextUpdates(windata);
slime73@9587
  1345
}}
slouken@1933
  1346
slouken@1933
  1347
void
slouken@1933
  1348
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1349
{ @autoreleasepool
slouken@1933
  1350
{
slouken@6848
  1351
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1352
    NSWindow *nswindow = windata->nswindow;
slime73@9860
  1353
    NSRect rect;
slime73@9860
  1354
    Uint32 moveHack;
slouken@1933
  1355
slime73@9860
  1356
    /* Cocoa will resize the window from the bottom-left rather than the
slime73@9860
  1357
     * top-left when -[nswindow setContentSize:] is used, so we must set the
slime73@9860
  1358
     * entire frame based on the new size, in order to preserve the position.
slime73@9860
  1359
     */
slime73@9860
  1360
    rect.origin.x = window->x;
slime73@9860
  1361
    rect.origin.y = window->y;
slime73@9860
  1362
    rect.size.width = window->w;
slime73@9860
  1363
    rect.size.height = window->h;
slime73@9860
  1364
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
icculus@9841
  1365
slime73@9860
  1366
    moveHack = s_moveHack;
slime73@9860
  1367
    s_moveHack = 0;
slime73@9860
  1368
    [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
slime73@9860
  1369
    s_moveHack = moveHack;
icculus@5564
  1370
jorgen@7595
  1371
    ScheduleContextUpdates(windata);
slime73@9587
  1372
}}
slouken@1933
  1373
slouken@1933
  1374
void
stopiccot@6681
  1375
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1376
{ @autoreleasepool
stopiccot@6681
  1377
{
slouken@6848
  1378
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1379
slouken@6848
  1380
    NSSize minSize;
slouken@6848
  1381
    minSize.width = window->min_w;
slouken@6848
  1382
    minSize.height = window->min_h;
slouken@7191
  1383
slouken@6848
  1384
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1385
}}
slouken@6788
  1386
slouken@6788
  1387
void
slouken@6788
  1388
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1389
{ @autoreleasepool
slouken@6788
  1390
{
slouken@6848
  1391
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1392
slouken@6848
  1393
    NSSize maxSize;
slouken@6848
  1394
    maxSize.width = window->max_w;
slouken@6848
  1395
    maxSize.height = window->max_h;
slouken@7191
  1396
slouken@6848
  1397
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1398
}}
stopiccot@6681
  1399
stopiccot@6681
  1400
void
slouken@1933
  1401
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1402
{ @autoreleasepool
slouken@1933
  1403
{
jorgen@7087
  1404
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1405
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1406
slouken@6848
  1407
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1408
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1409
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1410
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1411
    }
slime73@9587
  1412
}}
slouken@1933
  1413
slouken@1933
  1414
void
slouken@1933
  1415
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1416
{ @autoreleasepool
slouken@1933
  1417
{
slouken@6848
  1418
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1419
slouken@6848
  1420
    [nswindow orderOut:nil];
slime73@9587
  1421
}}
slouken@1933
  1422
slouken@1933
  1423
void
slouken@1933
  1424
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1425
{ @autoreleasepool
slouken@1933
  1426
{
jorgen@7087
  1427
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1428
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1429
slouken@7771
  1430
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1431
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1432
     */
jorgen@7087
  1433
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1434
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1435
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1436
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1437
    }
jorgen@7087
  1438
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1439
}}
slouken@1933
  1440
slouken@1933
  1441
void
slouken@1933
  1442
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1443
{ @autoreleasepool
slouken@1933
  1444
{
jorgen@7594
  1445
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1446
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1447
slouken@6848
  1448
    [nswindow zoom:nil];
slouken@6848
  1449
jorgen@7595
  1450
    ScheduleContextUpdates(windata);
slime73@9587
  1451
}}
slouken@1933
  1452
slouken@1933
  1453
void
slouken@1933
  1454
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1455
{ @autoreleasepool
slouken@1933
  1456
{
slouken@7963
  1457
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1458
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1459
slouken@7968
  1460
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1461
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1462
    } else {
slouken@7963
  1463
        [nswindow miniaturize:nil];
slouken@7963
  1464
    }
slime73@9587
  1465
}}
slouken@1933
  1466
slouken@1933
  1467
void
slouken@1933
  1468
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1469
{ @autoreleasepool
slouken@1933
  1470
{
slouken@6848
  1471
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1472
slouken@6848
  1473
    if ([nswindow isMiniaturized]) {
slouken@6848
  1474
        [nswindow deminiaturize:nil];
slouken@6848
  1475
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1476
        [nswindow zoom:nil];
slouken@1956
  1477
    }
slime73@9587
  1478
}}
slouken@1933
  1479
slouken@1933
  1480
void
icculus@6422
  1481
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1482
{ @autoreleasepool
icculus@6422
  1483
{
slouken@7990
  1484
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@9797
  1485
        if (bordered) {
slouken@7191
  1486
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1487
        }
icculus@6422
  1488
    }
slime73@9587
  1489
}}
icculus@6422
  1490
slouken@7952
  1491
slouken@7968
  1492
void
slouken@7968
  1493
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1494
{ @autoreleasepool
slouken@7952
  1495
{
slouken@6848
  1496
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1497
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1498
    NSRect rect;
slouken@5249
  1499
slouken@6848
  1500
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1501
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1502
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1503
    }
slouken@6848
  1504
slouken@6848
  1505
    if (fullscreen) {
slouken@6848
  1506
        SDL_Rect bounds;
slouken@6848
  1507
slouken@6848
  1508
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1509
        rect.origin.x = bounds.x;
slouken@6848
  1510
        rect.origin.y = bounds.y;
slouken@6848
  1511
        rect.size.width = bounds.w;
slouken@6848
  1512
        rect.size.height = bounds.h;
slouken@8801
  1513
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1514
slouken@6848
  1515
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1516
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1517
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1518
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1519
        }
slouken@5401
  1520
slime73@10176
  1521
        [nswindow setStyleMask:NSBorderlessWindowMask];
slouken@6848
  1522
    } else {
slouken@6848
  1523
        rect.origin.x = window->windowed.x;
slouken@6848
  1524
        rect.origin.y = window->windowed.y;
slouken@6848
  1525
        rect.size.width = window->windowed.w;
slouken@6848
  1526
        rect.size.height = window->windowed.h;
slouken@8801
  1527
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1528
slime73@10176
  1529
        [nswindow setStyleMask:GetWindowStyle(window)];
slouken@9790
  1530
slime73@10176
  1531
        /* Hack to restore window decorations on Mac OS X 10.10 */
slime73@10176
  1532
        NSRect frameRect = [nswindow frame];
slime73@10176
  1533
        [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
slime73@10176
  1534
        [nswindow setFrame:frameRect display:NO];
slouken@5398
  1535
    }
slouken@6848
  1536
slouken@6848
  1537
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1538
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1539
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1540
    }
slouken@6848
  1541
slouken@6848
  1542
    s_moveHack = 0;
slouken@7873
  1543
    [nswindow setContentSize:rect.size];
slouken@6848
  1544
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1545
    s_moveHack = SDL_GetTicks();
slouken@6848
  1546
slouken@6848
  1547
    /* When the window style changes the title is cleared */
slouken@9797
  1548
    if (!fullscreen) {
slouken@6848
  1549
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1550
    }
slouken@6848
  1551
slouken@6848
  1552
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1553
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1554
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1555
    } else {
slouken@6848
  1556
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1557
    }
jorgen@7087
  1558
jorgen@7636
  1559
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1560
        [data->listener pauseVisibleObservation];
jorgen@7636
  1561
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1562
        [data->listener resumeVisibleObservation];
jorgen@7636
  1563
    }
slouken@6848
  1564
jorgen@7595
  1565
    ScheduleContextUpdates(data);
slime73@9587
  1566
}}
slouken@5249
  1567
slouken@5466
  1568
int
slouken@5466
  1569
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1570
{
slouken@5466
  1571
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1572
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1573
    const uint32_t tableSize = 256;
slouken@5466
  1574
    CGGammaValue redTable[tableSize];
slouken@5466
  1575
    CGGammaValue greenTable[tableSize];
slouken@5466
  1576
    CGGammaValue blueTable[tableSize];
slouken@5466
  1577
    uint32_t i;
slouken@5466
  1578
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1579
slouken@5466
  1580
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1581
    for (i = 0; i < 256; i++) {
slouken@5466
  1582
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1583
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1584
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1585
    }
slouken@5466
  1586
slouken@5466
  1587
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1588
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1589
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1590
    }
slouken@5466
  1591
    return 0;
slouken@5466
  1592
}
slouken@5466
  1593
slouken@5466
  1594
int
slouken@5466
  1595
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1596
{
slouken@5466
  1597
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1598
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1599
    const uint32_t tableSize = 256;
slouken@5466
  1600
    CGGammaValue redTable[tableSize];
slouken@5466
  1601
    CGGammaValue greenTable[tableSize];
slouken@5466
  1602
    CGGammaValue blueTable[tableSize];
slouken@5466
  1603
    uint32_t i, tableCopied;
slouken@5466
  1604
slouken@5466
  1605
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1606
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1607
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1608
    }
slouken@5466
  1609
slouken@5466
  1610
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1611
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1612
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1613
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1614
    }
slouken@5466
  1615
    return 0;
slouken@5466
  1616
}
slouken@5466
  1617
slouken@5249
  1618
void
slouken@6662
  1619
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1620
{
slouken@5371
  1621
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1622
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1623
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1624
        int x, y;
slouken@5371
  1625
        CGPoint cgpoint;
slouken@5371
  1626
slouken@5371
  1627
        SDL_GetMouseState(&x, &y);
slouken@5371
  1628
        cgpoint.x = window->x + x;
slouken@5371
  1629
        cgpoint.y = window->y + y;
jorgen@8261
  1630
jorgen@8261
  1631
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1632
jorgen@8261
  1633
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1634
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1635
    }
slouken@7191
  1636
icculus@8652
  1637
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slime73@9897
  1638
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)
slime73@9897
  1639
            && ![data->listener isInFullscreenSpace]) {
slouken@7191
  1640
            /* OpenGL is rendering to the window, so make it visible! */
slime73@9897
  1641
            /* Doing this in 10.11 while in a Space breaks things (bug #3152) */
slouken@7191
  1642
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1643
        } else {
slouken@7191
  1644
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1645
        }
slouken@7191
  1646
    }
slouken@1933
  1647
}
slouken@1933
  1648
slouken@1933
  1649
void
slouken@1933
  1650
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1651
{ @autoreleasepool
slouken@1933
  1652
{
slouken@6848
  1653
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1654
slouken@6848
  1655
    if (data) {
slouken@9907
  1656
        if ([data->listener isInFullscreenSpace]) {
slouken@9907
  1657
            [NSMenu setMenuBarVisible:YES];
slouken@9907
  1658
        }
slouken@6848
  1659
        [data->listener close];
slouken@6848
  1660
        [data->listener release];
slouken@6848
  1661
        if (data->created) {
slouken@6848
  1662
            [data->nswindow close];
slouken@1933
  1663
        }
jorgen@7595
  1664
jorgen@7595
  1665
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1666
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1667
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1668
            [context setWindow:NULL];
jorgen@7595
  1669
        }
jorgen@7595
  1670
        [data->nscontexts release];
jorgen@7595
  1671
slouken@6848
  1672
        SDL_free(data);
slouken@1933
  1673
    }
slouken@8978
  1674
    window->driverdata = NULL;
slime73@9587
  1675
}}
slouken@1933
  1676
slouken@1933
  1677
SDL_bool
slouken@1933
  1678
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1679
{
slouken@4900
  1680
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1681
slouken@1933
  1682
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1683
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1684
        info->info.cocoa.window = nswindow;
slouken@1933
  1685
        return SDL_TRUE;
slouken@1933
  1686
    } else {
slouken@1933
  1687
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1688
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1689
        return SDL_FALSE;
slouken@1933
  1690
    }
slouken@1933
  1691
}
slouken@1933
  1692
slouken@7968
  1693
SDL_bool
slouken@7969
  1694
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1695
{
slouken@7969
  1696
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1697
slouken@7969
  1698
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1699
        return SDL_TRUE;
slouken@7969
  1700
    } else {
slouken@7969
  1701
        return SDL_FALSE;
slouken@7969
  1702
    }
slouken@7969
  1703
}
slouken@7969
  1704
slouken@7969
  1705
SDL_bool
slouken@7968
  1706
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1707
{ @autoreleasepool
slouken@7968
  1708
{
slouken@7968
  1709
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1710
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1711
slouken@7968
  1712
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@9904
  1713
        const int maxattempts = 3;
slouken@9904
  1714
        int attempt = 0;
slouken@9904
  1715
        while (++attempt <= maxattempts) {
slouken@9904
  1716
            /* Wait for the transition to complete, so application changes
slouken@9904
  1717
             take effect properly (e.g. setting the window size, etc.)
slouken@9904
  1718
             */
slouken@9904
  1719
            const int limit = 10000;
slouken@9904
  1720
            int count = 0;
slouken@9904
  1721
            while ([data->listener isInFullscreenSpaceTransition]) {
slouken@9904
  1722
                if ( ++count == limit ) {
slouken@9904
  1723
                    /* Uh oh, transition isn't completing. Should we assert? */
slouken@9904
  1724
                    break;
slouken@9904
  1725
                }
slouken@9904
  1726
                SDL_Delay(1);
slouken@9904
  1727
                SDL_PumpEvents();
slouken@9904
  1728
            }
slouken@9904
  1729
            if ([data->listener isInFullscreenSpace] == (state ? YES : NO))
slouken@9904
  1730
                break;
slouken@9904
  1731
            /* Try again, the last attempt was interrupted by user gestures */
slouken@9904
  1732
            if (![data->listener setFullscreenSpace:(state ? YES : NO)])
slouken@9904
  1733
                break; /* ??? */
slouken@9904
  1734
        }
slouken@9904
  1735
        /* Return TRUE to prevent non-space fullscreen logic from running */
slouken@7968
  1736
        succeeded = SDL_TRUE;
slouken@7968
  1737
    }
slouken@7968
  1738
slouken@7968
  1739
    return succeeded;
slime73@9587
  1740
}}
slouken@7968
  1741
icculus@8931
  1742
int
icculus@8935
  1743
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1744
{
icculus@8931
  1745
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1746
}
icculus@8931
  1747
icculus@10025
  1748
int
icculus@10025
  1749
Cocoa_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
  1750
{
icculus@10025
  1751
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
  1752
    [data->nswindow setAlphaValue:opacity];
icculus@10025
  1753
    return 0;
icculus@10025
  1754
}
icculus@10025
  1755
slouken@6044
  1756
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1757
slouken@1933
  1758
/* vi: set ts=4 sw=4 expandtab: */