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