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