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