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