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