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