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