src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Nov 2015 08:54:42 -0800
changeset 9903 2bbb11de1e60
parent 9897 b9b4c1bbb778
child 9904 e9b49510e51b
permissions -rw-r--r--
add hacky support for failed fullscreen transitions. SDL doesn't have the concept of a fullscreen transition that failed. if the user is actively changing spaces while the app goes fullscreen, it fails to go fullscreen; now it will just try again instead of hanging around with the wrong window styles.
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@9903
   644
    
slouken@9903
   645
    SetWindowStyle(window, GetWindowStyle(window));
slouken@9903
   646
slouken@9903
   647
    isFullscreenSpace = NO;
slouken@9903
   648
    inFullscreenTransition = NO;
slouken@9903
   649
slouken@9903
   650
    /* Try again? Not sure what else to do, the application wants to be fullscreen. */
slouken@9903
   651
    [self setFullscreenSpace:YES];
slouken@9903
   652
}
slouken@9903
   653
slouken@7952
   654
- (void)windowDidEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   655
{
slouken@7965
   656
    SDL_Window *window = _data->window;
slouken@7965
   657
slouken@7952
   658
    inFullscreenTransition = NO;
slouken@7961
   659
slouken@7963
   660
    if (pendingWindowOperation == PENDING_OPERATION_LEAVE_FULLSCREEN) {
slouken@7963
   661
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   662
        [self setFullscreenSpace:NO];
slouken@7961
   663
    } else {
icculus@8284
   664
        if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8284
   665
            [NSMenu setMenuBarVisible:NO];
icculus@8284
   666
        }
icculus@8284
   667
slouken@7963
   668
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   669
        /* Force the size change event in case it was delivered earlier
slouken@7965
   670
           while the window was still animating into place.
slouken@7965
   671
         */
slouken@7965
   672
        window->w = 0;
slouken@7965
   673
        window->h = 0;
slouken@7961
   674
        [self windowDidResize:aNotification];
slouken@7961
   675
    }
slouken@7952
   676
}
slouken@7952
   677
slouken@7952
   678
- (void)windowWillExitFullScreen:(NSNotification *)aNotification
slouken@7952
   679
{
slouken@7964
   680
    SDL_Window *window = _data->window;
slouken@7964
   681
slouken@7990
   682
    SetWindowStyle(window, GetWindowStyle(window));
slouken@7964
   683
slouken@7967
   684
    isFullscreenSpace = NO;
slouken@7952
   685
    inFullscreenTransition = YES;
slouken@7952
   686
}
slouken@7952
   687
slouken@9903
   688
- (void)windowDidFailToExitFullScreen:(NSNotification *)aNotification
slouken@9903
   689
{
slouken@9903
   690
    SDL_Window *window = _data->window;
slouken@9903
   691
    
slouken@9903
   692
    SetWindowStyle(window, (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask));
slouken@9903
   693
    
slouken@9903
   694
    isFullscreenSpace = YES;
slouken@9903
   695
    inFullscreenTransition = NO;
slouken@9903
   696
slouken@9903
   697
    /* Try again? Not sure what else to do, the application wants to be non-fullscreen. */
slouken@9903
   698
    [self setFullscreenSpace:NO];
slouken@9903
   699
}
slouken@9903
   700
slouken@7952
   701
- (void)windowDidExitFullScreen:(NSNotification *)aNotification
slouken@7952
   702
{
slouken@7965
   703
    SDL_Window *window = _data->window;
slouken@7952
   704
    NSWindow *nswindow = _data->nswindow;
slouken@7952
   705
slouken@7952
   706
    inFullscreenTransition = NO;
slouken@7961
   707
slouken@8618
   708
    [nswindow setLevel:kCGNormalWindowLevel];
slouken@8618
   709
slouken@7963
   710
    if (pendingWindowOperation == PENDING_OPERATION_ENTER_FULLSCREEN) {
slouken@7963
   711
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   712
        [self setFullscreenSpace:YES];
slouken@7963
   713
    } else if (pendingWindowOperation == PENDING_OPERATION_MINIMIZE) {
slouken@7963
   714
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7963
   715
        [nswindow miniaturize:nil];
slouken@7961
   716
    } else {
icculus@8291
   717
        /* Adjust the fullscreen toggle button and readd menu now that we're here. */
icculus@8291
   718
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8291
   719
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
icculus@8291
   720
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
icculus@8291
   721
        } else {
icculus@8284
   722
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
icculus@8284
   723
        }
icculus@8291
   724
        [NSMenu setMenuBarVisible:YES];
icculus@8284
   725
slouken@7963
   726
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   727
        /* Force the size change event in case it was delivered earlier
slouken@7965
   728
           while the window was still animating into place.
slouken@7965
   729
         */
slouken@7965
   730
        window->w = 0;
slouken@7965
   731
        window->h = 0;
slouken@7961
   732
        [self windowDidResize:aNotification];
icculus@8622
   733
slouken@8623
   734
        /* FIXME: Why does the window get hidden? */
slouken@8623
   735
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@8623
   736
            Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
slouken@8623
   737
        }
slouken@7961
   738
    }
slouken@7952
   739
}
slouken@7952
   740
icculus@8291
   741
-(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
icculus@8291
   742
{
icculus@8291
   743
    if ((_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8291
   744
        return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
icculus@8291
   745
    } else {
icculus@8291
   746
        return proposedOptions;
icculus@8291
   747
    }
icculus@8291
   748
}
icculus@8291
   749
icculus@8291
   750
slouken@7191
   751
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   752
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   753
 * where they get converted to action messages, etc.
slouken@7191
   754
 */
slouken@6514
   755
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   756
{
slouken@7191
   757
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   758
}
slouken@6514
   759
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   760
{
slouken@7191
   761
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   762
}
slouken@6514
   763
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   764
{
slouken@7191
   765
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   766
}
slouken@6514
   767
slouken@7191
   768
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   769
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   770
 */
slouken@6514
   771
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   772
{
slouken@7191
   773
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   774
}
slouken@6514
   775
icculus@8935
   776
- (BOOL)processHitTest:(NSEvent *)theEvent
icculus@8931
   777
{
icculus@8935
   778
    SDL_assert(isDragAreaRunning == [_data->nswindow isMovableByWindowBackground]);
icculus@8931
   779
icculus@8935
   780
    if (_data->window->hit_test) {  /* if no hit-test, skip this. */
icculus@8931
   781
        const NSPoint location = [theEvent locationInWindow];
icculus@8931
   782
        const SDL_Point point = { (int) location.x, _data->window->h - (((int) location.y)-1) };
icculus@8935
   783
        const SDL_HitTestResult rc = _data->window->hit_test(_data->window, &point, _data->window->hit_test_data);
icculus@8935
   784
        if (rc == SDL_HITTEST_DRAGGABLE) {
icculus@8935
   785
            if (!isDragAreaRunning) {
icculus@8935
   786
                isDragAreaRunning = YES;
icculus@8935
   787
                [_data->nswindow setMovableByWindowBackground:YES];
icculus@8931
   788
            }
icculus@8935
   789
            return YES;  /* dragging! */
icculus@8931
   790
        }
icculus@8931
   791
    }
icculus@8931
   792
icculus@8931
   793
    if (isDragAreaRunning) {
icculus@8931
   794
        isDragAreaRunning = NO;
icculus@8931
   795
        [_data->nswindow setMovableByWindowBackground:NO];
icculus@8931
   796
        return YES;  /* was dragging, drop event. */
icculus@8931
   797
    }
icculus@8931
   798
icculus@8935
   799
    return NO;  /* not a special area, carry on. */
icculus@8931
   800
}
icculus@8931
   801
slouken@1933
   802
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   803
{
slouken@1959
   804
    int button;
slouken@1933
   805
icculus@9685
   806
    /* Ignore events that aren't inside the client area (i.e. title bar.) */
icculus@9685
   807
    if ([theEvent window]) {
icculus@9685
   808
        const NSRect windowRect = [[[theEvent window] contentView] frame];
icculus@9685
   809
        if (!NSPointInRect([theEvent locationInWindow], windowRect)) {
icculus@9685
   810
            return;
icculus@9685
   811
        }
icculus@9685
   812
    }
icculus@9685
   813
icculus@8935
   814
    if ([self processHitTest:theEvent]) {
icculus@8931
   815
        return;  /* dragging, drop event. */
icculus@8931
   816
    }
icculus@8931
   817
slouken@1959
   818
    switch ([theEvent buttonNumber]) {
slouken@1959
   819
    case 0:
slouken@7915
   820
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   821
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   822
            wasCtrlLeft = YES;
slouken@7740
   823
            button = SDL_BUTTON_RIGHT;
slouken@7740
   824
        } else {
slouken@7740
   825
            wasCtrlLeft = NO;
slouken@7740
   826
            button = SDL_BUTTON_LEFT;
slouken@7740
   827
        }
slouken@1959
   828
        break;
slouken@1959
   829
    case 1:
slouken@1959
   830
        button = SDL_BUTTON_RIGHT;
slouken@1959
   831
        break;
slouken@1959
   832
    case 2:
slouken@1959
   833
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   834
        break;
slouken@1959
   835
    default:
slouken@5061
   836
        button = [theEvent buttonNumber] + 1;
slouken@1959
   837
        break;
slouken@1959
   838
    }
slouken@6950
   839
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   840
}
slouken@1933
   841
slouken@1933
   842
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   843
{
slouken@1959
   844
    [self mouseDown:theEvent];
slouken@1933
   845
}
slouken@1933
   846
slouken@1933
   847
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   848
{
slouken@1959
   849
    [self mouseDown:theEvent];
slouken@1933
   850
}
slouken@1933
   851
slouken@1933
   852
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   853
{
slouken@1959
   854
    int button;
slouken@1933
   855
icculus@8935
   856
    if ([self processHitTest:theEvent]) {
icculus@8931
   857
        return;  /* stopped dragging, drop event. */
icculus@8931
   858
    }
icculus@8931
   859
slouken@1959
   860
    switch ([theEvent buttonNumber]) {
slouken@1959
   861
    case 0:
slouken@7740
   862
        if (wasCtrlLeft) {
slouken@7740
   863
            button = SDL_BUTTON_RIGHT;
slouken@7740
   864
            wasCtrlLeft = NO;
slouken@7740
   865
        } else {
slouken@7740
   866
            button = SDL_BUTTON_LEFT;
slouken@7740
   867
        }
slouken@1959
   868
        break;
slouken@1959
   869
    case 1:
slouken@1959
   870
        button = SDL_BUTTON_RIGHT;
slouken@1959
   871
        break;
slouken@1959
   872
    case 2:
slouken@1959
   873
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   874
        break;
slouken@1959
   875
    default:
slouken@5061
   876
        button = [theEvent buttonNumber] + 1;
slouken@1959
   877
        break;
slouken@1959
   878
    }
slouken@6950
   879
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   880
}
slouken@1933
   881
slouken@1933
   882
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   883
{
slouken@1959
   884
    [self mouseUp:theEvent];
slouken@1933
   885
}
slouken@1933
   886
slouken@1933
   887
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   888
{
slouken@1959
   889
    [self mouseUp:theEvent];
slouken@1933
   890
}
slouken@1933
   891
slouken@1933
   892
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   893
{
slouken@5406
   894
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   895
    SDL_Window *window = _data->window;
slouken@5396
   896
    NSPoint point;
slouken@5396
   897
    int x, y;
slouken@1933
   898
icculus@8935
   899
    if ([self processHitTest:theEvent]) {
icculus@8931
   900
        return;  /* dragging, drop event. */
icculus@8931
   901
    }
icculus@8931
   902
slouken@5406
   903
    if (mouse->relative_mode) {
gzjjgod@5059
   904
        return;
slouken@5371
   905
    }
gzjjgod@5059
   906
slouken@5396
   907
    point = [theEvent locationInWindow];
slouken@5396
   908
    x = (int)point.x;
slouken@5396
   909
    y = (int)(window->h - point.y);
slouken@5371
   910
icculus@8929
   911
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@8929
   912
        if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   913
            if (x < 0) {
slouken@6666
   914
                x = 0;
slouken@6666
   915
            } else if (x >= window->w) {
slouken@6666
   916
                x = window->w - 1;
slouken@6666
   917
            }
slouken@6666
   918
            if (y < 0) {
slouken@6666
   919
                y = 0;
slouken@6666
   920
            } else if (y >= window->h) {
slouken@6666
   921
                y = window->h - 1;
slouken@6666
   922
            }
slouken@6666
   923
jorgen@7593
   924
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   925
            CGPoint cgpoint;
jorgen@8260
   926
jorgen@7593
   927
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   928
             * SDL_cocoamousetap.m.
jorgen@7593
   929
             */
jorgen@7593
   930
slouken@6666
   931
            cgpoint.x = window->x + x;
slouken@6666
   932
            cgpoint.y = window->y + y;
jorgen@7098
   933
jorgen@7113
   934
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   935
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   936
             * sure how we'd procure the right one for this event.
jorgen@7098
   937
             */
jorgen@7113
   938
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   939
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   940
            CGSetLocalEventsSuppressionInterval(0.25);
jorgen@8261
   941
jorgen@8261
   942
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   943
#endif
slouken@5396
   944
        }
slouken@2059
   945
    }
slouken@6950
   946
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   947
}
slouken@1933
   948
slouken@1957
   949
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   950
{
slouken@1957
   951
    [self mouseMoved:theEvent];
slouken@1957
   952
}
slouken@1957
   953
slouken@1958
   954
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   955
{
slouken@1958
   956
    [self mouseMoved:theEvent];
slouken@1958
   957
}
slouken@1958
   958
slouken@1958
   959
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   960
{
slouken@1958
   961
    [self mouseMoved:theEvent];
slouken@1958
   962
}
slouken@1958
   963
slouken@1933
   964
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   965
{
gzjjgod@5057
   966
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   967
}
slouken@3688
   968
slouken@4673
   969
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   970
{
urkle@9236
   971
    NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
urkle@9236
   972
    int existingTouchCount = 0;
urkle@9236
   973
urkle@9236
   974
    for (NSTouch* touch in touches) {
urkle@9236
   975
        if ([touch phase] != NSTouchPhaseBegan) {
urkle@9236
   976
            existingTouchCount++;
urkle@9236
   977
        }
urkle@9236
   978
    }
urkle@9236
   979
    if (existingTouchCount == 0) {
urkle@9236
   980
        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
urkle@9236
   981
        int numFingers = SDL_GetNumTouchFingers(touchID);
urkle@9236
   982
        DLog("Reset Lost Fingers: %d", numFingers);
urkle@9236
   983
        for (--numFingers; numFingers >= 0; --numFingers) {
urkle@9236
   984
            SDL_Finger* finger = SDL_GetTouchFinger(touchID, numFingers);
urkle@9236
   985
            SDL_SendTouch(touchID, finger->id, SDL_FALSE, 0, 0, 0);
urkle@9236
   986
        }
urkle@9236
   987
    }
urkle@9236
   988
urkle@9236
   989
    DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
slouken@8986
   990
    [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
slouken@4673
   991
}
slouken@4673
   992
slouken@4673
   993
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   994
{
slouken@8986
   995
    [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
slouken@4673
   996
}
slouken@4673
   997
slouken@4673
   998
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   999
{
slouken@8986
  1000
    [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
slouken@4673
  1001
}
slouken@4673
  1002
slouken@4673
  1003
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
  1004
{
slouken@8986
  1005
    [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
slouken@4673
  1006
}
slouken@4673
  1007
slouken@8986
  1008
- (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
slouken@4673
  1009
{
slouken@8986
  1010
    NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
slouken@4673
  1011
slouken@8986
  1012
    for (NSTouch *touch in touches) {
slouken@6953
  1013
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@9679
  1014
        if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
  1015
            return;
slouken@7191
  1016
        }
slouken@4687
  1017
slouken@6953
  1018
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
  1019
        float x = [touch normalizedPosition].x;
slouken@4673
  1020
        float y = [touch normalizedPosition].y;
slouken@5261
  1021
        /* Make the origin the upper left instead of the lower left */
slouken@5261
  1022
        y = 1.0f - y;
slouken@4687
  1023
slouken@8986
  1024
        switch (phase) {
slouken@8986
  1025
        case NSTouchPhaseBegan:
slouken@6951
  1026
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
  1027
            break;
slouken@8986
  1028
        case NSTouchPhaseEnded:
slouken@8986
  1029
        case NSTouchPhaseCancelled:
slouken@6951
  1030
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
  1031
            break;
slouken@8986
  1032
        case NSTouchPhaseMoved:
slouken@6951
  1033
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
  1034
            break;
slouken@8986
  1035
        default:
slouken@8986
  1036
            break;
slouken@4673
  1037
        }
slouken@4673
  1038
    }
slouken@1933
  1039
}
slouken@1933
  1040
slouken@1933
  1041
@end
slouken@1933
  1042
icculus@9623
  1043
@interface SDLView : NSView {
icculus@9623
  1044
    SDL_Window *_sdlWindow;
icculus@9623
  1045
}
icculus@9623
  1046
icculus@9623
  1047
- (void)setSDLWindow:(SDL_Window*)window;
jorgen@7158
  1048
slouken@5379
  1049
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
  1050
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
  1051
- (BOOL)mouseDownCanMoveWindow;
icculus@9623
  1052
- (void)drawRect:(NSRect)dirtyRect;
gzjjgod@4915
  1053
@end
gzjjgod@4915
  1054
gzjjgod@4915
  1055
@implementation SDLView
icculus@9623
  1056
- (void)setSDLWindow:(SDL_Window*)window
icculus@9623
  1057
{
icculus@9623
  1058
    _sdlWindow = window;
icculus@9623
  1059
}
icculus@9623
  1060
icculus@9623
  1061
- (void)drawRect:(NSRect)dirtyRect
icculus@9623
  1062
{
icculus@9623
  1063
    SDL_SendWindowEvent(_sdlWindow, SDL_WINDOWEVENT_EXPOSED, 0, 0);
icculus@9623
  1064
}
icculus@9623
  1065
gzjjgod@4915
  1066
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
  1067
{
slouken@5371
  1068
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
  1069
}
jorgen@7158
  1070
icculus@8931
  1071
- (BOOL)mouseDownCanMoveWindow
icculus@8931
  1072
{
icculus@8931
  1073
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
  1074
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
  1075
       during mouse events when we're using a drag area. */
icculus@8931
  1076
    return YES;
icculus@8931
  1077
}
icculus@8931
  1078
jorgen@7158
  1079
- (void)resetCursorRects
jorgen@7158
  1080
{
jorgen@7158
  1081
    [super resetCursorRects];
jorgen@7158
  1082
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
  1083
jorgen@7270
  1084
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
  1085
        [self addCursorRect:[self bounds]
jorgen@7158
  1086
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
  1087
    } else {
jorgen@7158
  1088
        [self addCursorRect:[self bounds]
jorgen@7158
  1089
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
  1090
    }
jorgen@7158
  1091
}
gzjjgod@4915
  1092
@end
gzjjgod@4915
  1093
slouken@1933
  1094
static int
slouken@1951
  1095
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1096
{ @autoreleasepool
slouken@1933
  1097
{
slouken@1951
  1098
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1099
    SDL_WindowData *data;
slouken@1933
  1100
slouken@1933
  1101
    /* Allocate the window data */
slouken@8926
  1102
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1103
    if (!data) {
icculus@7037
  1104
        return SDL_OutOfMemory();
slouken@1933
  1105
    }
slouken@3685
  1106
    data->window = window;
slouken@3688
  1107
    data->nswindow = nswindow;
slouken@1933
  1108
    data->created = created;
slouken@1951
  1109
    data->videodata = videodata;
jorgen@7595
  1110
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1111
slouken@6848
  1112
    /* Create an event listener for the window */
slouken@6848
  1113
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1114
slouken@6848
  1115
    /* Fill in the SDL window with the window data */
slouken@6848
  1116
    {
slouken@6848
  1117
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1118
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1119
        window->x = (int)rect.origin.x;
slouken@6848
  1120
        window->y = (int)rect.origin.y;
slouken@6848
  1121
        window->w = (int)rect.size.width;
slouken@6848
  1122
        window->h = (int)rect.size.height;
slouken@6848
  1123
    }
slouken@6848
  1124
slouken@6848
  1125
    /* Set up the listener after we create the view */
slouken@6848
  1126
    [data->listener listen:data];
slouken@6848
  1127
slouken@6848
  1128
    if ([nswindow isVisible]) {
slouken@6848
  1129
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1130
    } else {
slouken@6848
  1131
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1132
    }
jorgen@7084
  1133
slouken@6848
  1134
    {
slouken@6848
  1135
        unsigned int style = [nswindow styleMask];
slouken@6848
  1136
slouken@6848
  1137
        if (style == NSBorderlessWindowMask) {
slouken@6848
  1138
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1139
        } else {
slouken@6848
  1140
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1141
        }
slouken@6848
  1142
        if (style & NSResizableWindowMask) {
slouken@6848
  1143
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1144
        } else {
slouken@6848
  1145
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1146
        }
slouken@6848
  1147
    }
jorgen@7084
  1148
slouken@6848
  1149
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1150
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1151
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1152
    } else {
slouken@6848
  1153
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1154
    }
jorgen@7084
  1155
slouken@6848
  1156
    if ([nswindow isMiniaturized]) {
slouken@6848
  1157
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1158
    } else {
slouken@6848
  1159
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1160
    }
jorgen@7084
  1161
slouken@6848
  1162
    if ([nswindow isKeyWindow]) {
slouken@6848
  1163
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1164
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1165
    }
slouken@1933
  1166
jorgen@7085
  1167
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1168
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1169
     */
jorgen@7085
  1170
    [nswindow setOneShot:NO];
jorgen@7085
  1171
slouken@6848
  1172
    /* All done! */
slouken@6848
  1173
    window->driverdata = data;
slouken@6848
  1174
    return 0;
slime73@9587
  1175
}}
slouken@1933
  1176
slouken@1933
  1177
int
slouken@1933
  1178
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1179
{ @autoreleasepool
slouken@1933
  1180
{
icculus@8295
  1181
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1182
    NSWindow *nswindow;
slouken@6848
  1183
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1184
    NSRect rect;
slouken@6848
  1185
    SDL_Rect bounds;
slouken@6848
  1186
    unsigned int style;
slouken@8986
  1187
    NSArray *screens = [NSScreen screens];
slouken@1933
  1188
slouken@6848
  1189
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1190
    rect.origin.x = window->x;
slouken@6848
  1191
    rect.origin.y = window->y;
slouken@6848
  1192
    rect.size.width = window->w;
slouken@6848
  1193
    rect.size.height = window->h;
slouken@8986
  1194
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1195
slouken@6848
  1196
    style = GetWindowStyle(window);
slouken@1933
  1197
slouken@6848
  1198
    /* Figure out which screen to place this window */
slouken@6848
  1199
    NSScreen *screen = nil;
slouken@8986
  1200
    for (NSScreen *candidate in screens) {
slouken@6848
  1201
        NSRect screenRect = [candidate frame];
slouken@6848
  1202
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1203
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1204
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1205
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1206
            screen = candidate;
slouken@6848
  1207
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1208
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1209
        }
slouken@6848
  1210
    }
slouken@7946
  1211
slouken@7946
  1212
    @try {
slouken@7946
  1213
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1214
    }
slouken@7946
  1215
    @catch (NSException *e) {
slime73@9587
  1216
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1217
    }
icculus@7205
  1218
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1219
icculus@8295
  1220
    if (videodata->allow_spaces) {
slouken@8986
  1221
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1222
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1223
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1224
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1225
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1226
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1227
        }
slouken@7955
  1228
    }
alexey@6832
  1229
slouken@7191
  1230
    /* Create a default view for this window */
slouken@6848
  1231
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
icculus@9623
  1232
    SDLView *contentView = [[SDLView alloc] initWithFrame:rect];
icculus@9623
  1233
    [contentView setSDLWindow:window];
urkle@7746
  1234
slouken@7955
  1235
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1236
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1237
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1238
        }
urkle@7746
  1239
    }
urkle@7746
  1240
slouken@6848
  1241
    [nswindow setContentView: contentView];
slouken@6848
  1242
    [contentView release];
alexey@6832
  1243
jorgen@9237
  1244
    /* Allow files and folders to be dragged onto the window by users */
jorgen@9237
  1245
    [nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@9237
  1246
slouken@6848
  1247
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1248
        [nswindow release];
slouken@6848
  1249
        return -1;
slouken@3506
  1250
    }
slouken@6848
  1251
    return 0;
slime73@9587
  1252
}}
slouken@1933
  1253
slouken@1933
  1254
int
slouken@1933
  1255
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1256
{ @autoreleasepool
slouken@1933
  1257
{
slouken@1933
  1258
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1259
    NSString *title;
slouken@1933
  1260
slouken@6848
  1261
    /* Query the title from the existing window */
slouken@6848
  1262
    title = [nswindow title];
slouken@6848
  1263
    if (title) {
slouken@6848
  1264
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1265
    }
slouken@1933
  1266
slouken@1951
  1267
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1268
}}
slouken@1933
  1269
slouken@1933
  1270
void
slouken@1933
  1271
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1272
{ @autoreleasepool
slouken@1933
  1273
{
slouken@9797
  1274
    const char *title = window->title ? window->title : "";
slouken@6848
  1275
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@9797
  1276
    NSString *string = [[NSString alloc] initWithUTF8String:title];
slouken@6848
  1277
    [nswindow setTitle:string];
slouken@6848
  1278
    [string release];
slime73@9587
  1279
}}
slouken@1933
  1280
slouken@1933
  1281
void
slouken@5375
  1282
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1283
{ @autoreleasepool
slouken@5375
  1284
{
slouken@6848
  1285
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1286
slouken@6848
  1287
    if (nsimage) {
slouken@6848
  1288
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1289
    }
slime73@9587
  1290
}}
slouken@5375
  1291
slouken@5375
  1292
void
slouken@1933
  1293
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1294
{ @autoreleasepool
slouken@1933
  1295
{
jorgen@7594
  1296
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1297
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1298
    NSRect rect;
slouken@6848
  1299
    Uint32 moveHack;
slouken@1933
  1300
slouken@6848
  1301
    rect.origin.x = window->x;
slouken@6848
  1302
    rect.origin.y = window->y;
slouken@6848
  1303
    rect.size.width = window->w;
slouken@6848
  1304
    rect.size.height = window->h;
slouken@8801
  1305
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1306
slouken@6848
  1307
    moveHack = s_moveHack;
slouken@6848
  1308
    s_moveHack = 0;
slouken@6848
  1309
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1310
    s_moveHack = moveHack;
slouken@5478
  1311
jorgen@7595
  1312
    ScheduleContextUpdates(windata);
slime73@9587
  1313
}}
slouken@1933
  1314
slouken@1933
  1315
void
slouken@1933
  1316
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1317
{ @autoreleasepool
slouken@1933
  1318
{
slouken@6848
  1319
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1320
    NSWindow *nswindow = windata->nswindow;
slime73@9860
  1321
    NSRect rect;
slime73@9860
  1322
    Uint32 moveHack;
slouken@1933
  1323
slime73@9860
  1324
    /* Cocoa will resize the window from the bottom-left rather than the
slime73@9860
  1325
     * top-left when -[nswindow setContentSize:] is used, so we must set the
slime73@9860
  1326
     * entire frame based on the new size, in order to preserve the position.
slime73@9860
  1327
     */
slime73@9860
  1328
    rect.origin.x = window->x;
slime73@9860
  1329
    rect.origin.y = window->y;
slime73@9860
  1330
    rect.size.width = window->w;
slime73@9860
  1331
    rect.size.height = window->h;
slime73@9860
  1332
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
icculus@9841
  1333
slime73@9860
  1334
    moveHack = s_moveHack;
slime73@9860
  1335
    s_moveHack = 0;
slime73@9860
  1336
    [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
slime73@9860
  1337
    s_moveHack = moveHack;
icculus@5564
  1338
jorgen@7595
  1339
    ScheduleContextUpdates(windata);
slime73@9587
  1340
}}
slouken@1933
  1341
slouken@1933
  1342
void
stopiccot@6681
  1343
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1344
{ @autoreleasepool
stopiccot@6681
  1345
{
slouken@6848
  1346
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1347
slouken@6848
  1348
    NSSize minSize;
slouken@6848
  1349
    minSize.width = window->min_w;
slouken@6848
  1350
    minSize.height = window->min_h;
slouken@7191
  1351
slouken@6848
  1352
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1353
}}
slouken@6788
  1354
slouken@6788
  1355
void
slouken@6788
  1356
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1357
{ @autoreleasepool
slouken@6788
  1358
{
slouken@6848
  1359
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1360
slouken@6848
  1361
    NSSize maxSize;
slouken@6848
  1362
    maxSize.width = window->max_w;
slouken@6848
  1363
    maxSize.height = window->max_h;
slouken@7191
  1364
slouken@6848
  1365
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1366
}}
stopiccot@6681
  1367
stopiccot@6681
  1368
void
slouken@1933
  1369
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1370
{ @autoreleasepool
slouken@1933
  1371
{
jorgen@7087
  1372
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1373
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1374
slouken@6848
  1375
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1376
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1377
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1378
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1379
    }
slime73@9587
  1380
}}
slouken@1933
  1381
slouken@1933
  1382
void
slouken@1933
  1383
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1384
{ @autoreleasepool
slouken@1933
  1385
{
slouken@6848
  1386
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1387
slouken@6848
  1388
    [nswindow orderOut:nil];
slime73@9587
  1389
}}
slouken@1933
  1390
slouken@1933
  1391
void
slouken@1933
  1392
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1393
{ @autoreleasepool
slouken@1933
  1394
{
jorgen@7087
  1395
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1396
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1397
slouken@7771
  1398
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1399
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1400
     */
jorgen@7087
  1401
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1402
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1403
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1404
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1405
    }
jorgen@7087
  1406
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1407
}}
slouken@1933
  1408
slouken@1933
  1409
void
slouken@1933
  1410
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1411
{ @autoreleasepool
slouken@1933
  1412
{
jorgen@7594
  1413
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1414
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1415
slouken@6848
  1416
    [nswindow zoom:nil];
slouken@6848
  1417
jorgen@7595
  1418
    ScheduleContextUpdates(windata);
slime73@9587
  1419
}}
slouken@1933
  1420
slouken@1933
  1421
void
slouken@1933
  1422
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1423
{ @autoreleasepool
slouken@1933
  1424
{
slouken@7963
  1425
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1426
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1427
slouken@7968
  1428
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1429
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1430
    } else {
slouken@7963
  1431
        [nswindow miniaturize:nil];
slouken@7963
  1432
    }
slime73@9587
  1433
}}
slouken@1933
  1434
slouken@1933
  1435
void
slouken@1933
  1436
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1437
{ @autoreleasepool
slouken@1933
  1438
{
slouken@6848
  1439
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1440
slouken@6848
  1441
    if ([nswindow isMiniaturized]) {
slouken@6848
  1442
        [nswindow deminiaturize:nil];
slouken@6848
  1443
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1444
        [nswindow zoom:nil];
slouken@1956
  1445
    }
slime73@9587
  1446
}}
slouken@1933
  1447
slouken@5400
  1448
static NSWindow *
slouken@5400
  1449
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
  1450
{
slouken@5400
  1451
    if (!data->created) {
slouken@5400
  1452
        /* Don't mess with other people's windows... */
slouken@5400
  1453
        return nswindow;
slouken@5400
  1454
    }
slouken@5400
  1455
slouken@5400
  1456
    [data->listener close];
jorgen@7085
  1457
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
  1458
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@9237
  1459
    [data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@7085
  1460
    /* See comment in SetupWindowData. */
jorgen@7085
  1461
    [data->nswindow setOneShot:NO];
slouken@5400
  1462
    [data->listener listen:data];
slouken@5400
  1463
slouken@5400
  1464
    [nswindow close];
slouken@5400
  1465
slouken@5400
  1466
    return data->nswindow;
slouken@5400
  1467
}
slouken@5400
  1468
slouken@1933
  1469
void
icculus@6422
  1470
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1471
{ @autoreleasepool
icculus@6422
  1472
{
slouken@7990
  1473
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@9797
  1474
        if (bordered) {
slouken@7191
  1475
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1476
        }
icculus@6422
  1477
    }
slime73@9587
  1478
}}
icculus@6422
  1479
slouken@7952
  1480
slouken@7968
  1481
void
slouken@7968
  1482
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1483
{ @autoreleasepool
slouken@7952
  1484
{
slouken@6848
  1485
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1486
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1487
    NSRect rect;
slouken@5249
  1488
slouken@6848
  1489
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1490
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1491
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1492
    }
slouken@6848
  1493
slouken@6848
  1494
    if (fullscreen) {
slouken@6848
  1495
        SDL_Rect bounds;
slouken@6848
  1496
slouken@6848
  1497
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1498
        rect.origin.x = bounds.x;
slouken@6848
  1499
        rect.origin.y = bounds.y;
slouken@6848
  1500
        rect.size.width = bounds.w;
slouken@6848
  1501
        rect.size.height = bounds.h;
slouken@8801
  1502
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1503
slouken@6848
  1504
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1505
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1506
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1507
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1508
        }
slouken@5401
  1509
slouken@6848
  1510
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1511
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
  1512
        } else {
slouken@6848
  1513
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
  1514
        }
slouken@6848
  1515
    } else {
slouken@6848
  1516
        rect.origin.x = window->windowed.x;
slouken@6848
  1517
        rect.origin.y = window->windowed.y;
slouken@6848
  1518
        rect.size.width = window->windowed.w;
slouken@6848
  1519
        rect.size.height = window->windowed.h;
slouken@8801
  1520
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1521
slouken@6848
  1522
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1523
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@9790
  1524
slouken@9790
  1525
            /* Hack to restore window decorations on Mac OS X 10.10 */
slouken@9790
  1526
            NSRect frameRect = [nswindow frame];
slouken@9790
  1527
            [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
slouken@9790
  1528
            [nswindow setFrame:frameRect display:NO];
slouken@5400
  1529
        } else {
slouken@6848
  1530
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1531
        }
slouken@5398
  1532
    }
slouken@6848
  1533
slouken@6848
  1534
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1535
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1536
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1537
    }
slouken@6848
  1538
slouken@6848
  1539
    s_moveHack = 0;
slouken@7873
  1540
    [nswindow setContentSize:rect.size];
slouken@6848
  1541
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1542
    s_moveHack = SDL_GetTicks();
slouken@6848
  1543
slouken@6848
  1544
    /* When the window style changes the title is cleared */
slouken@9797
  1545
    if (!fullscreen) {
slouken@6848
  1546
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1547
    }
slouken@6848
  1548
slouken@6848
  1549
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1550
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1551
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1552
    } else {
slouken@6848
  1553
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1554
    }
jorgen@7087
  1555
jorgen@7636
  1556
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1557
        [data->listener pauseVisibleObservation];
jorgen@7636
  1558
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1559
        [data->listener resumeVisibleObservation];
jorgen@7636
  1560
    }
slouken@6848
  1561
jorgen@7595
  1562
    ScheduleContextUpdates(data);
slime73@9587
  1563
}}
slouken@5249
  1564
slouken@5466
  1565
int
slouken@5466
  1566
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1567
{
slouken@5466
  1568
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1569
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1570
    const uint32_t tableSize = 256;
slouken@5466
  1571
    CGGammaValue redTable[tableSize];
slouken@5466
  1572
    CGGammaValue greenTable[tableSize];
slouken@5466
  1573
    CGGammaValue blueTable[tableSize];
slouken@5466
  1574
    uint32_t i;
slouken@5466
  1575
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1576
slouken@5466
  1577
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1578
    for (i = 0; i < 256; i++) {
slouken@5466
  1579
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1580
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1581
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1582
    }
slouken@5466
  1583
slouken@5466
  1584
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1585
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1586
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1587
    }
slouken@5466
  1588
    return 0;
slouken@5466
  1589
}
slouken@5466
  1590
slouken@5466
  1591
int
slouken@5466
  1592
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, 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, tableCopied;
slouken@5466
  1601
slouken@5466
  1602
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1603
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1604
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1605
    }
slouken@5466
  1606
slouken@5466
  1607
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1608
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1609
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1610
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1611
    }
slouken@5466
  1612
    return 0;
slouken@5466
  1613
}
slouken@5466
  1614
slouken@5249
  1615
void
slouken@6662
  1616
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1617
{
slouken@5371
  1618
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1619
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1620
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1621
        int x, y;
slouken@5371
  1622
        CGPoint cgpoint;
slouken@5371
  1623
slouken@5371
  1624
        SDL_GetMouseState(&x, &y);
slouken@5371
  1625
        cgpoint.x = window->x + x;
slouken@5371
  1626
        cgpoint.y = window->y + y;
jorgen@8261
  1627
jorgen@8261
  1628
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1629
jorgen@8261
  1630
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1631
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1632
    }
slouken@7191
  1633
icculus@8652
  1634
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slime73@9897
  1635
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)
slime73@9897
  1636
            && ![data->listener isInFullscreenSpace]) {
slouken@7191
  1637
            /* OpenGL is rendering to the window, so make it visible! */
slime73@9897
  1638
            /* Doing this in 10.11 while in a Space breaks things (bug #3152) */
slouken@7191
  1639
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1640
        } else {
slouken@7191
  1641
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1642
        }
slouken@7191
  1643
    }
slouken@1933
  1644
}
slouken@1933
  1645
slouken@1933
  1646
void
slouken@1933
  1647
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1648
{ @autoreleasepool
slouken@1933
  1649
{
slouken@6848
  1650
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1651
slouken@6848
  1652
    if (data) {
slouken@6848
  1653
        [data->listener close];
slouken@6848
  1654
        [data->listener release];
slouken@6848
  1655
        if (data->created) {
slouken@6848
  1656
            [data->nswindow close];
slouken@1933
  1657
        }
jorgen@7595
  1658
jorgen@7595
  1659
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1660
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1661
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1662
            [context setWindow:NULL];
jorgen@7595
  1663
        }
jorgen@7595
  1664
        [data->nscontexts release];
jorgen@7595
  1665
slouken@6848
  1666
        SDL_free(data);
slouken@1933
  1667
    }
slouken@8978
  1668
    window->driverdata = NULL;
slime73@9587
  1669
}}
slouken@1933
  1670
slouken@1933
  1671
SDL_bool
slouken@1933
  1672
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1673
{
slouken@4900
  1674
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1675
slouken@1933
  1676
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1677
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1678
        info->info.cocoa.window = nswindow;
slouken@1933
  1679
        return SDL_TRUE;
slouken@1933
  1680
    } else {
slouken@1933
  1681
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1682
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1683
        return SDL_FALSE;
slouken@1933
  1684
    }
slouken@1933
  1685
}
slouken@1933
  1686
slouken@7968
  1687
SDL_bool
slouken@7969
  1688
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1689
{
slouken@7969
  1690
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1691
slouken@7969
  1692
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1693
        return SDL_TRUE;
slouken@7969
  1694
    } else {
slouken@7969
  1695
        return SDL_FALSE;
slouken@7969
  1696
    }
slouken@7969
  1697
}
slouken@7969
  1698
slouken@7969
  1699
SDL_bool
slouken@7968
  1700
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1701
{ @autoreleasepool
slouken@7968
  1702
{
slouken@7968
  1703
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1704
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1705
slouken@7968
  1706
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@7968
  1707
        succeeded = SDL_TRUE;
slouken@8810
  1708
slouken@8810
  1709
        /* Wait for the transition to complete, so application changes
slouken@8810
  1710
           take effect properly (e.g. setting the window size, etc.)
slouken@8810
  1711
         */
slouken@8810
  1712
        const int limit = 10000;
slouken@8810
  1713
        int count = 0;
slouken@8810
  1714
        while ([data->listener isInFullscreenSpaceTransition]) {
slouken@8810
  1715
            if ( ++count == limit ) {
slouken@8810
  1716
                /* Uh oh, transition isn't completing. Should we assert? */
slouken@8810
  1717
                break;
slouken@8810
  1718
            }
slouken@8810
  1719
            SDL_Delay(1);
slouken@8810
  1720
            SDL_PumpEvents();
slouken@8810
  1721
        }
slouken@7968
  1722
    }
slouken@7968
  1723
slouken@7968
  1724
    return succeeded;
slime73@9587
  1725
}}
slouken@7968
  1726
icculus@8931
  1727
int
icculus@8935
  1728
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1729
{
icculus@8931
  1730
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1731
}
icculus@8931
  1732
slouken@6044
  1733
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1734
slouken@1933
  1735
/* vi: set ts=4 sw=4 expandtab: */