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