src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 08:49:20 -0800
changeset 5466 006883d5fa51
parent 5406 1517da4ab6b1
child 5478 6b65ff3cee62
permissions -rw-r--r--
Gamma support is back!

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