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