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