src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 27 Dec 2013 10:18:11 -0800
changeset 8083 3782a12331d6
parent 7990 a05a48e493c0
child 8093 b43765095a6f
permissions -rw-r--r--
Bump SDL to build with 10.7 SDK.

This also bumps the minimum requirement for building SDL to 10.7, and
removes some checking we no longer need.

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