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