src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 07 May 2013 16:52:39 -0700
changeset 7158 ff52fba70795
parent 7113 7b4b596b3cfb
child 7191 75360622e65f
permissions -rw-r--r--
Mac: Use cursor rects instead of NSCursor hide/unhide.

This should hopefully fix a class of problems around cursor hiding not behaving correctly on Mac.

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