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