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