src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Oct 2013 02:37:03 -0700
changeset 7873 e88af17231bd
parent 7771 4434498bf4b9
child 7915 86ad156a82ab
permissions -rw-r--r--
Fixed bug 2073 - Mac: window moves unexpectedly when exiting SDL_WINDOW_FULLSCREEN_DESKTOP mode

Alex Szpakowski

In Mac OS X, when SDL_SetWindowFullscreen(window, 0) is called on a window which was in SDL_WINDOW_FULLSCREEN_DESKTOP mode, its original size is restored but its position is moved to the bottom of the screen.

I tracked down the issue to these two lines: http://hg.libsdl.org/SDL/file/66b5b8446275/src/video/cocoa/SDL_cocoawindow.m#l1034

I believe [nswindow setFrameOrigin:rect.origin] implicitly calls [nswindow constrainFrameRect:rect toScreen:screen], which will attempt to constrain the window to the screen, but at that point the window size is still full-screen rather than the restored window size, so the constrainFrameRect function operates on the wrong window size.

https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/constrainFrameRect:toScreen:

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