src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Jul 2014 10:33:32 -0700
changeset 8978 7753e4fd3d1d
parent 8953 dc80dc0bd22e
child 8986 1c81316ac642
permissions -rw-r--r--
Fixed bug 2629 - Mac: crash when calling SDL_DestroyWindow with an active OpenGL context

Alex Szpakowski

Since this commit https://hg.libsdl.org/SDL/rev/1519c462cee6 , calling SDL_DestroyWindow will crash the program if the window has an active OpenGL context.

This is because the Cocoa_DestroyWindow code sets the window's driverdata to NULL and then calls [context setWindow:NULL], which tries to access the window's driverdata, resulting in a null pointer dereference.

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