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