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