src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6953 b83579d7d19a
child 7084 9d43403e9fc5
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


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