src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 22 Apr 2013 18:14:26 -0700
changeset 7087 5639ac726076
parent 7085 152cc7ddfa57
child 7098 f4b2c6fb0258
permissions -rw-r--r--
Fix Mac crash when creating fullscreen window introduced in 9d43403e9fc5.

makeKeyAndOrderFront: was sending three KVO transitions for isVisible,
for false -> true, true -> false, and then false -> true. This was
causing an infinite recursion.

We now suspend monitoring of the KVO before makeKeyAndOrderFront, then
resume afterwards and send any changes in isVisible's state.
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@6666
   257
            SDL_SetCursor(NULL);
slouken@5396
   258
        }
slouken@5367
   259
    }
slouken@1962
   260
slouken@4503
   261
    /* Check to see if someone updated the clipboard */
slouken@4503
   262
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   263
}
slouken@1933
   264
slouken@1933
   265
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   266
{
slouken@2059
   267
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   268
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   269
        SDL_SetMouseFocus(NULL);
slouken@2059
   270
    }
slouken@2059
   271
slouken@2059
   272
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   273
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   274
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   275
    }
slouken@1933
   276
}
slouken@1933
   277
slouken@6514
   278
// We'll respond to key events by doing nothing so we don't beep.
slouken@6514
   279
// We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@6514
   280
// where they get converted to action messages, etc.
slouken@6514
   281
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   282
{
slouken@6514
   283
    //Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
slouken@6514
   284
}
slouken@6514
   285
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   286
{
slouken@6514
   287
    //Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
slouken@6514
   288
}
slouken@6514
   289
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   290
{
slouken@6514
   291
    //Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);
slouken@6514
   292
}
slouken@6514
   293
slouken@6514
   294
// We'll respond to selectors by doing nothing so we don't beep.
slouken@6514
   295
// The escape key gets converted to a "cancel" selector, etc.
slouken@6514
   296
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   297
{
slouken@6514
   298
    //NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));
slouken@6514
   299
}
slouken@6514
   300
slouken@1933
   301
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   302
{
slouken@1959
   303
    int button;
slouken@1933
   304
slouken@1959
   305
    switch ([theEvent buttonNumber]) {
slouken@1959
   306
    case 0:
slouken@1959
   307
        button = SDL_BUTTON_LEFT;
slouken@1959
   308
        break;
slouken@1959
   309
    case 1:
slouken@1959
   310
        button = SDL_BUTTON_RIGHT;
slouken@1959
   311
        break;
slouken@1959
   312
    case 2:
slouken@1959
   313
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   314
        break;
slouken@1959
   315
    default:
slouken@5061
   316
        button = [theEvent buttonNumber] + 1;
slouken@1959
   317
        break;
slouken@1959
   318
    }
slouken@6950
   319
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   320
}
slouken@1933
   321
slouken@1933
   322
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   323
{
slouken@1959
   324
    [self mouseDown:theEvent];
slouken@1933
   325
}
slouken@1933
   326
slouken@1933
   327
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   328
{
slouken@1959
   329
    [self mouseDown:theEvent];
slouken@1933
   330
}
slouken@1933
   331
slouken@1933
   332
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   333
{
slouken@1959
   334
    int button;
slouken@1933
   335
slouken@1959
   336
    switch ([theEvent buttonNumber]) {
slouken@1959
   337
    case 0:
slouken@1959
   338
        button = SDL_BUTTON_LEFT;
slouken@1959
   339
        break;
slouken@1959
   340
    case 1:
slouken@1959
   341
        button = SDL_BUTTON_RIGHT;
slouken@1959
   342
        break;
slouken@1959
   343
    case 2:
slouken@1959
   344
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   345
        break;
slouken@1959
   346
    default:
slouken@5061
   347
        button = [theEvent buttonNumber] + 1;
slouken@1959
   348
        break;
slouken@1959
   349
    }
slouken@6950
   350
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   351
}
slouken@1933
   352
slouken@1933
   353
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   354
{
slouken@1959
   355
    [self mouseUp:theEvent];
slouken@1933
   356
}
slouken@1933
   357
slouken@1933
   358
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   359
{
slouken@1959
   360
    [self mouseUp:theEvent];
slouken@1933
   361
}
slouken@1933
   362
slouken@1933
   363
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   364
{
slouken@5406
   365
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   366
    SDL_Window *window = _data->window;
slouken@5396
   367
    NSPoint point;
slouken@5396
   368
    int x, y;
slouken@1933
   369
slouken@5406
   370
    if (mouse->relative_mode) {
gzjjgod@5059
   371
        return;
slouken@5371
   372
    }
gzjjgod@5059
   373
slouken@5396
   374
    point = [theEvent locationInWindow];
slouken@5396
   375
    x = (int)point.x;
slouken@5396
   376
    y = (int)(window->h - point.y);
slouken@5371
   377
slouken@5396
   378
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   379
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@6666
   380
            CGPoint cgpoint;
slouken@6666
   381
slouken@6666
   382
            if (x < 0) {
slouken@6666
   383
                x = 0;
slouken@6666
   384
            } else if (x >= window->w) {
slouken@6666
   385
                x = window->w - 1;
slouken@6666
   386
            }
slouken@6666
   387
            if (y < 0) {
slouken@6666
   388
                y = 0;
slouken@6666
   389
            } else if (y >= window->h) {
slouken@6666
   390
                y = window->h - 1;
slouken@6666
   391
            }
slouken@6666
   392
slouken@6666
   393
            cgpoint.x = window->x + x;
slouken@6666
   394
            cgpoint.y = window->y + y;
slouken@6666
   395
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5396
   396
        }
slouken@2059
   397
    }
slouken@6950
   398
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   399
}
slouken@1933
   400
slouken@1957
   401
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   402
{
slouken@1957
   403
    [self mouseMoved:theEvent];
slouken@1957
   404
}
slouken@1957
   405
slouken@1958
   406
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   407
{
slouken@1958
   408
    [self mouseMoved:theEvent];
slouken@1958
   409
}
slouken@1958
   410
slouken@1958
   411
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   412
{
slouken@1958
   413
    [self mouseMoved:theEvent];
slouken@1958
   414
}
slouken@1958
   415
slouken@1933
   416
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   417
{
gzjjgod@5057
   418
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   419
}
slouken@3688
   420
slouken@4673
   421
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   422
{
slouken@4673
   423
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   424
}
slouken@4673
   425
slouken@4673
   426
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   427
{
slouken@4673
   428
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   429
}
slouken@4673
   430
slouken@4673
   431
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   432
{
slouken@4673
   433
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   434
}
slouken@4673
   435
slouken@4673
   436
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   437
{
slouken@4673
   438
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   439
}
slouken@4673
   440
slouken@4673
   441
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   442
{
icculus@5646
   443
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
slouken@4680
   444
    NSSet *touches = 0;
slouken@4680
   445
    NSEnumerator *enumerator;
slouken@4680
   446
    NSTouch *touch;
slouken@4673
   447
slouken@4680
   448
    switch (type) {
slouken@4680
   449
        case COCOA_TOUCH_DOWN:
slouken@4680
   450
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   451
            break;
slouken@4680
   452
        case COCOA_TOUCH_UP:
slouken@4680
   453
        case COCOA_TOUCH_CANCELLED:
slouken@4680
   454
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
slouken@4680
   455
            break;
slouken@4680
   456
        case COCOA_TOUCH_MOVE:
slouken@4680
   457
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   458
            break;
slouken@4680
   459
    }
slouken@4680
   460
slouken@4680
   461
    enumerator = [touches objectEnumerator];
slouken@4680
   462
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   463
    while (touch) {
slouken@6953
   464
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@4673
   465
        if (!SDL_GetTouch(touchId)) {
slouken@6951
   466
            if (SDL_AddTouch(touchId, "") < 0) {
slouken@4680
   467
                return;
slouken@4673
   468
            }
slouken@4673
   469
        } 
slouken@4687
   470
slouken@6953
   471
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
   472
        float x = [touch normalizedPosition].x;
slouken@4673
   473
        float y = [touch normalizedPosition].y;
slouken@5261
   474
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   475
        y = 1.0f - y;
slouken@4687
   476
slouken@4673
   477
        switch (type) {
slouken@4673
   478
        case COCOA_TOUCH_DOWN:
slouken@6951
   479
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
   480
            break;
slouken@4673
   481
        case COCOA_TOUCH_UP:
slouken@4673
   482
        case COCOA_TOUCH_CANCELLED:
slouken@6951
   483
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
   484
            break;
slouken@4673
   485
        case COCOA_TOUCH_MOVE:
slouken@6951
   486
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
   487
            break;
slouken@4673
   488
        }
slouken@6951
   489
slouken@4673
   490
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   491
    }
icculus@5646
   492
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 */
slouken@1933
   493
}
slouken@1933
   494
slouken@1933
   495
@end
slouken@1933
   496
slouken@1973
   497
@interface SDLWindow : NSWindow
slouken@1973
   498
/* These are needed for borderless/fullscreen windows */
slouken@1973
   499
- (BOOL)canBecomeKeyWindow;
slouken@1973
   500
- (BOOL)canBecomeMainWindow;
slouken@1973
   501
@end
slouken@1973
   502
slouken@1973
   503
@implementation SDLWindow
slouken@1973
   504
- (BOOL)canBecomeKeyWindow
slouken@1973
   505
{
slouken@1973
   506
    return YES;
slouken@1973
   507
}
slouken@1973
   508
slouken@1973
   509
- (BOOL)canBecomeMainWindow
slouken@1973
   510
{
slouken@1973
   511
    return YES;
slouken@1973
   512
}
slouken@1973
   513
@end
slouken@1973
   514
slouken@5379
   515
@interface SDLView : NSView
slouken@5379
   516
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   517
- (void)rightMouseDown:(NSEvent *)theEvent;
gzjjgod@4915
   518
@end
gzjjgod@4915
   519
gzjjgod@4915
   520
@implementation SDLView
gzjjgod@4915
   521
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   522
{
slouken@5371
   523
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   524
}
gzjjgod@4915
   525
@end
gzjjgod@4915
   526
slouken@5249
   527
static unsigned int
slouken@5305
   528
GetWindowStyle(SDL_Window * window)
slouken@5249
   529
{
slouken@5249
   530
    unsigned int style;
slouken@5249
   531
slouken@5305
   532
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5249
   533
        style = NSBorderlessWindowMask;
slouken@5305
   534
	} else {
slouken@5305
   535
		if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
   536
			style = NSBorderlessWindowMask;
slouken@5305
   537
		} else {
slouken@5305
   538
			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@5305
   539
		}
slouken@5305
   540
		if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
   541
			style |= NSResizableWindowMask;
slouken@5305
   542
		}
slouken@5305
   543
	}
slouken@5249
   544
    return style;
slouken@5249
   545
}
slouken@5249
   546
slouken@1933
   547
static int
slouken@1951
   548
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   549
{
slouken@6848
   550
    NSAutoreleasePool *pool;
slouken@1951
   551
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   552
    SDL_WindowData *data;
slouken@1933
   553
slouken@1933
   554
    /* Allocate the window data */
slouken@5249
   555
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   556
    if (!data) {
icculus@7037
   557
        return SDL_OutOfMemory();
slouken@1933
   558
    }
slouken@3685
   559
    data->window = window;
slouken@3688
   560
    data->nswindow = nswindow;
slouken@1933
   561
    data->created = created;
slouken@1951
   562
    data->videodata = videodata;
slouken@1933
   563
slouken@6848
   564
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   565
slouken@6848
   566
    /* Create an event listener for the window */
slouken@6848
   567
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
   568
slouken@6848
   569
    /* Fill in the SDL window with the window data */
slouken@6848
   570
    {
slouken@6848
   571
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   572
        ConvertNSRect(&rect);
slouken@6848
   573
        window->x = (int)rect.origin.x;
slouken@6848
   574
        window->y = (int)rect.origin.y;
slouken@6848
   575
        window->w = (int)rect.size.width;
slouken@6848
   576
        window->h = (int)rect.size.height;
slouken@6848
   577
    }
slouken@6848
   578
slouken@6848
   579
    /* Set up the listener after we create the view */
slouken@6848
   580
    [data->listener listen:data];
slouken@6848
   581
slouken@6848
   582
    if ([nswindow isVisible]) {
slouken@6848
   583
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
   584
    } else {
slouken@6848
   585
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
   586
    }
jorgen@7084
   587
slouken@6848
   588
    {
slouken@6848
   589
        unsigned int style = [nswindow styleMask];
slouken@6848
   590
slouken@6848
   591
        if (style == NSBorderlessWindowMask) {
slouken@6848
   592
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
   593
        } else {
slouken@6848
   594
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
   595
        }
slouken@6848
   596
        if (style & NSResizableWindowMask) {
slouken@6848
   597
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
   598
        } else {
slouken@6848
   599
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
   600
        }
slouken@6848
   601
    }
jorgen@7084
   602
slouken@6848
   603
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
   604
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   605
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
   606
    } else {
slouken@6848
   607
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
   608
    }
jorgen@7084
   609
slouken@6848
   610
    if ([nswindow isMiniaturized]) {
slouken@6848
   611
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
   612
    } else {
slouken@6848
   613
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
   614
    }
jorgen@7084
   615
slouken@6848
   616
    if ([nswindow isKeyWindow]) {
slouken@6848
   617
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
   618
        SDL_SetKeyboardFocus(data->window);
slouken@6848
   619
    }
slouken@1933
   620
jorgen@7085
   621
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
   622
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
   623
     */
jorgen@7085
   624
    [nswindow setOneShot:NO];
jorgen@7085
   625
slouken@6848
   626
    /* All done! */
slouken@6848
   627
    [pool release];
slouken@6848
   628
    window->driverdata = data;
slouken@6848
   629
    return 0;
slouken@1933
   630
}
slouken@1933
   631
slouken@1933
   632
int
slouken@1933
   633
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   634
{
slouken@6848
   635
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   636
    NSWindow *nswindow;
slouken@6848
   637
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
   638
    NSRect rect;
slouken@6848
   639
    SDL_Rect bounds;
slouken@6848
   640
    unsigned int style;
slouken@1933
   641
slouken@6848
   642
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
   643
    rect.origin.x = window->x;
slouken@6848
   644
    rect.origin.y = window->y;
slouken@6848
   645
    rect.size.width = window->w;
slouken@6848
   646
    rect.size.height = window->h;
slouken@6848
   647
    ConvertNSRect(&rect);
slouken@1933
   648
slouken@6848
   649
    style = GetWindowStyle(window);
slouken@1933
   650
slouken@6848
   651
    /* Figure out which screen to place this window */
slouken@6848
   652
    NSArray *screens = [NSScreen screens];
slouken@6848
   653
    NSScreen *screen = nil;
slouken@6848
   654
    NSScreen *candidate;
slouken@6848
   655
    int i, count = [screens count];
slouken@6848
   656
    for (i = 0; i < count; ++i) {
slouken@6848
   657
        candidate = [screens objectAtIndex:i];
slouken@6848
   658
        NSRect screenRect = [candidate frame];
slouken@6848
   659
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
   660
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
   661
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
   662
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
   663
            screen = candidate;
slouken@6848
   664
            rect.origin.x -= screenRect.origin.x;
slouken@6848
   665
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   666
        }
slouken@6848
   667
    }
jorgen@7085
   668
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
alexey@6832
   669
slouken@6848
   670
    // Create a default view for this window
slouken@6848
   671
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   672
    NSView *contentView = [[SDLView alloc] initWithFrame:rect];
slouken@6848
   673
    [nswindow setContentView: contentView];
slouken@6848
   674
    [contentView release];
alexey@6832
   675
slouken@6848
   676
    [pool release];
slouken@6848
   677
slouken@6848
   678
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
   679
        [nswindow release];
slouken@6848
   680
        return -1;
slouken@3506
   681
    }
slouken@6848
   682
    return 0;
slouken@1933
   683
}
slouken@1933
   684
slouken@1933
   685
int
slouken@1933
   686
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   687
{
slouken@6848
   688
    NSAutoreleasePool *pool;
slouken@1933
   689
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   690
    NSString *title;
slouken@1933
   691
slouken@6848
   692
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   693
slouken@6848
   694
    /* Query the title from the existing window */
slouken@6848
   695
    title = [nswindow title];
slouken@6848
   696
    if (title) {
slouken@6848
   697
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   698
    }
slouken@1933
   699
slouken@6848
   700
    [pool release];
slouken@6848
   701
slouken@1951
   702
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   703
}
slouken@1933
   704
slouken@1933
   705
void
slouken@1933
   706
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   707
{
slouken@6848
   708
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   709
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   710
    NSString *string;
slouken@1933
   711
slouken@6848
   712
    if(window->title) {
slouken@6848
   713
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@6848
   714
    } else {
slouken@6848
   715
        string = [[NSString alloc] init];
slouken@1956
   716
    }
slouken@6848
   717
    [nswindow setTitle:string];
slouken@6848
   718
    [string release];
slouken@6848
   719
slouken@6848
   720
    [pool release];
slouken@1933
   721
}
slouken@1933
   722
slouken@1933
   723
void
slouken@5375
   724
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@5375
   725
{
slouken@6848
   726
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   727
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
   728
slouken@6848
   729
    if (nsimage) {
slouken@6848
   730
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
   731
    }
slouken@6848
   732
slouken@6848
   733
    [pool release];
slouken@5375
   734
}
slouken@5375
   735
slouken@5375
   736
void
slouken@1933
   737
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   738
{
slouken@6848
   739
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   740
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   741
    NSRect rect;
slouken@6848
   742
    Uint32 moveHack;
slouken@1933
   743
slouken@6848
   744
    rect.origin.x = window->x;
slouken@6848
   745
    rect.origin.y = window->y;
slouken@6848
   746
    rect.size.width = window->w;
slouken@6848
   747
    rect.size.height = window->h;
slouken@6848
   748
    ConvertNSRect(&rect);
slouken@5478
   749
slouken@6848
   750
    moveHack = s_moveHack;
slouken@6848
   751
    s_moveHack = 0;
slouken@6848
   752
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
   753
    s_moveHack = moveHack;
slouken@5478
   754
slouken@6848
   755
    if (window == _this->current_glwin) {
slouken@6848
   756
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   757
    }
slouken@6848
   758
slouken@6848
   759
    [pool release];
slouken@1933
   760
}
slouken@1933
   761
slouken@1933
   762
void
slouken@1933
   763
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   764
{
slouken@6848
   765
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   766
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
   767
    NSWindow *nswindow = windata->nswindow;
slouken@6848
   768
    NSSize size;
slouken@1933
   769
slouken@6848
   770
    size.width = window->w;
slouken@6848
   771
    size.height = window->h;
slouken@6848
   772
    [nswindow setContentSize:size];
icculus@5564
   773
slouken@6848
   774
    if (window == _this->current_glwin) {
slouken@6848
   775
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   776
    }
slouken@6848
   777
slouken@6848
   778
    [pool release];
slouken@1933
   779
}
slouken@1933
   780
slouken@1933
   781
void
stopiccot@6681
   782
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
stopiccot@6681
   783
{
slouken@6848
   784
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   785
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
   786
        
slouken@6848
   787
    NSSize minSize;
slouken@6848
   788
    minSize.width = window->min_w;
slouken@6848
   789
    minSize.height = window->min_h;
slouken@6848
   790
        
slouken@6848
   791
    [windata->nswindow setContentMinSize:minSize];
slouken@6848
   792
    
slouken@6848
   793
    [pool release];
slouken@6788
   794
}
slouken@6788
   795
slouken@6788
   796
void
slouken@6788
   797
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@6788
   798
{
slouken@6848
   799
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   800
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
   801
        
slouken@6848
   802
    NSSize maxSize;
slouken@6848
   803
    maxSize.width = window->max_w;
slouken@6848
   804
    maxSize.height = window->max_h;
slouken@6848
   805
        
slouken@6848
   806
    [windata->nswindow setContentMaxSize:maxSize];
slouken@6848
   807
    
slouken@6848
   808
    [pool release];
stopiccot@6681
   809
}
stopiccot@6681
   810
stopiccot@6681
   811
void
slouken@1933
   812
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   813
{
slouken@6848
   814
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
   815
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
   816
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
   817
slouken@6848
   818
    if (![nswindow isMiniaturized]) {
jorgen@7087
   819
        [windowData->listener pauseVisibleObservation];
slouken@6848
   820
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
   821
        [windowData->listener resumeVisibleObservation];
slouken@1956
   822
    }
slouken@6848
   823
    [pool release];
slouken@1933
   824
}
slouken@1933
   825
slouken@1933
   826
void
slouken@1933
   827
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   828
{
slouken@6848
   829
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   830
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   831
slouken@6848
   832
    [nswindow orderOut:nil];
slouken@6848
   833
    [pool release];
slouken@1933
   834
}
slouken@1933
   835
slouken@1933
   836
void
slouken@1933
   837
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   838
{
slouken@6848
   839
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
   840
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
   841
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
   842
jorgen@7087
   843
    [windowData->listener pauseVisibleObservation];
slouken@6848
   844
    [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
   845
    [windowData->listener resumeVisibleObservation];
jorgen@7087
   846
slouken@6848
   847
    [pool release];
slouken@1933
   848
}
slouken@1933
   849
slouken@1933
   850
void
slouken@1933
   851
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   852
{
slouken@6848
   853
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   854
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   855
slouken@6848
   856
    [nswindow zoom:nil];
slouken@6848
   857
slouken@6848
   858
    if (window == _this->current_glwin) {
slouken@6848
   859
        [((NSOpenGLContext *) _this->current_glctx) update];
icculus@5564
   860
    }
slouken@6848
   861
slouken@6848
   862
    [pool release];
slouken@1933
   863
}
slouken@1933
   864
slouken@1933
   865
void
slouken@1933
   866
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   867
{
slouken@6848
   868
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   869
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   870
slouken@6848
   871
    [nswindow miniaturize:nil];
slouken@6848
   872
    [pool release];
slouken@1933
   873
}
slouken@1933
   874
slouken@1933
   875
void
slouken@1933
   876
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   877
{
slouken@6848
   878
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   879
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   880
slouken@6848
   881
    if ([nswindow isMiniaturized]) {
slouken@6848
   882
        [nswindow deminiaturize:nil];
slouken@6848
   883
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   884
        [nswindow zoom:nil];
slouken@1956
   885
    }
slouken@6848
   886
    [pool release];
slouken@1933
   887
}
slouken@1933
   888
slouken@5400
   889
static NSWindow *
slouken@5400
   890
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
   891
{
slouken@5400
   892
    if (!data->created) {
slouken@5400
   893
        /* Don't mess with other people's windows... */
slouken@5400
   894
        return nswindow;
slouken@5400
   895
    }
slouken@5400
   896
slouken@5400
   897
    [data->listener close];
jorgen@7085
   898
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
   899
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@7085
   900
    /* See comment in SetupWindowData. */
jorgen@7085
   901
    [data->nswindow setOneShot:NO];
slouken@5400
   902
    [data->listener listen:data];
slouken@5400
   903
slouken@5400
   904
    [nswindow close];
slouken@5400
   905
slouken@5400
   906
    return data->nswindow;
slouken@5400
   907
}
slouken@5400
   908
slouken@1933
   909
void
icculus@6422
   910
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   911
{
icculus@6422
   912
    /* this message arrived in 10.6. You're out of luck on older OSes. */
icculus@6422
   913
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
slouken@6848
   914
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   915
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
   916
    if ([nswindow respondsToSelector:@selector(setStyleMask:)]) {
slouken@6848
   917
        [nswindow setStyleMask:GetWindowStyle(window)];
slouken@6848
   918
        if (bordered) {
slouken@6848
   919
            Cocoa_SetWindowTitle(_this, window);  // this got blanked out.
icculus@6426
   920
        }
icculus@6422
   921
    }
slouken@6848
   922
    [pool release];
icculus@6422
   923
#endif
icculus@6422
   924
}
icculus@6422
   925
icculus@6422
   926
void
slouken@5305
   927
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5249
   928
{
slouken@6848
   929
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   930
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
   931
    NSWindow *nswindow = data->nswindow;
slouken@6848
   932
    NSRect rect;
slouken@5249
   933
slouken@6848
   934
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
   935
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
   936
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
   937
    }
slouken@6848
   938
slouken@6848
   939
    if (fullscreen) {
slouken@6848
   940
        SDL_Rect bounds;
slouken@6848
   941
slouken@6848
   942
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
   943
        rect.origin.x = bounds.x;
slouken@6848
   944
        rect.origin.y = bounds.y;
slouken@6848
   945
        rect.size.width = bounds.w;
slouken@6848
   946
        rect.size.height = bounds.h;
slouken@6848
   947
        ConvertNSRect(&rect);
slouken@6848
   948
slouken@6848
   949
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
   950
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
   951
        if (screenRect.size.height >= 1.0f) {
slouken@6848
   952
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
   953
        }
slouken@5401
   954
slouken@6848
   955
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
   956
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
   957
        } else {
slouken@6848
   958
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
   959
        }
slouken@6848
   960
    } else {
slouken@6848
   961
        rect.origin.x = window->windowed.x;
slouken@6848
   962
        rect.origin.y = window->windowed.y;
slouken@6848
   963
        rect.size.width = window->windowed.w;
slouken@6848
   964
        rect.size.height = window->windowed.h;
slouken@6848
   965
        ConvertNSRect(&rect);
alexey@6832
   966
slouken@6848
   967
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
   968
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
   969
        } else {
slouken@6848
   970
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
   971
        }
slouken@5398
   972
    }
slouken@6848
   973
slouken@6848
   974
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
   975
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
   976
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
   977
    }
slouken@6848
   978
slouken@6848
   979
    s_moveHack = 0;
slouken@6848
   980
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
   981
    [nswindow setContentSize:rect.size];
slouken@6848
   982
    s_moveHack = SDL_GetTicks();
slouken@6848
   983
slouken@6848
   984
    /* When the window style changes the title is cleared */
slouken@6848
   985
    if (!fullscreen) {
slouken@6848
   986
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
   987
    }
slouken@6848
   988
slouken@6848
   989
#ifdef FULLSCREEN_TOGGLEABLE
slouken@6848
   990
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
   991
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
   992
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
   993
    } else {
slouken@6848
   994
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
   995
    }
slouken@6848
   996
#endif
jorgen@7087
   997
jorgen@7087
   998
    [data->listener pauseVisibleObservation];
slouken@6848
   999
    [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1000
    [data->listener resumeVisibleObservation];
slouken@6848
  1001
slouken@6848
  1002
    if (window == _this->current_glwin) {
slouken@6848
  1003
        [((NSOpenGLContext *) _this->current_glctx) update];
slouken@6848
  1004
    }
slouken@6848
  1005
slouken@6848
  1006
    [pool release];
slouken@5249
  1007
}
slouken@5249
  1008
slouken@5466
  1009
int
slouken@5466
  1010
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1011
{
slouken@5466
  1012
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1013
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1014
    const uint32_t tableSize = 256;
slouken@5466
  1015
    CGGammaValue redTable[tableSize];
slouken@5466
  1016
    CGGammaValue greenTable[tableSize];
slouken@5466
  1017
    CGGammaValue blueTable[tableSize];
slouken@5466
  1018
    uint32_t i;
slouken@5466
  1019
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1020
slouken@5466
  1021
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1022
    for (i = 0; i < 256; i++) {
slouken@5466
  1023
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1024
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1025
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1026
    }
slouken@5466
  1027
slouken@5466
  1028
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1029
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1030
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1031
    }
slouken@5466
  1032
    return 0;
slouken@5466
  1033
}
slouken@5466
  1034
slouken@5466
  1035
int
slouken@5466
  1036
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1037
{
slouken@5466
  1038
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1039
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1040
    const uint32_t tableSize = 256;
slouken@5466
  1041
    CGGammaValue redTable[tableSize];
slouken@5466
  1042
    CGGammaValue greenTable[tableSize];
slouken@5466
  1043
    CGGammaValue blueTable[tableSize];
slouken@5466
  1044
    uint32_t i, tableCopied;
slouken@5466
  1045
slouken@5466
  1046
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1047
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1048
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1049
    }
slouken@5466
  1050
slouken@5466
  1051
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1052
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1053
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1054
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1055
    }
slouken@5466
  1056
    return 0;
slouken@5466
  1057
}
slouken@5466
  1058
slouken@5249
  1059
void
slouken@6662
  1060
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1061
{
slouken@5371
  1062
    /* Move the cursor to the nearest point in the window */
slouken@6662
  1063
    if (grabbed) {
slouken@5371
  1064
        int x, y;
slouken@5371
  1065
        CGPoint cgpoint;
slouken@5371
  1066
slouken@5371
  1067
        SDL_GetMouseState(&x, &y);
slouken@5371
  1068
        cgpoint.x = window->x + x;
slouken@5371
  1069
        cgpoint.y = window->y + y;
slouken@5371
  1070
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1071
    }
slouken@6755
  1072
	
slouken@6784
  1073
    if ( window->flags & SDL_WINDOW_FULLSCREEN ) {
slouken@6755
  1074
		SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6755
  1075
slouken@6784
  1076
		if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@6755
  1077
			/* OpenGL is rendering to the window, so make it visible! */
slouken@6755
  1078
			[data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@6784
  1079
		} else {
slouken@6755
  1080
			[data->nswindow setLevel:kCGNormalWindowLevel];
slouken@6755
  1081
		}
slouken@6755
  1082
	}
slouken@1933
  1083
}
slouken@1933
  1084
slouken@1933
  1085
void
slouken@1933
  1086
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
  1087
{
slouken@6848
  1088
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1089
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1090
slouken@6848
  1091
    if (data) {
slouken@6848
  1092
        [data->listener close];
slouken@6848
  1093
        [data->listener release];
slouken@6848
  1094
        if (data->created) {
slouken@6848
  1095
            [data->nswindow close];
slouken@1933
  1096
        }
slouken@6848
  1097
        SDL_free(data);
slouken@1933
  1098
    }
slouken@6848
  1099
    [pool release];
slouken@1933
  1100
}
slouken@1933
  1101
slouken@1933
  1102
SDL_bool
slouken@1933
  1103
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1104
{
slouken@4900
  1105
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1106
slouken@1933
  1107
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1108
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1109
        info->info.cocoa.window = nswindow;
slouken@1933
  1110
        return SDL_TRUE;
slouken@1933
  1111
    } else {
slouken@1933
  1112
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1113
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1114
        return SDL_FALSE;
slouken@1933
  1115
    }
slouken@1933
  1116
}
slouken@1933
  1117
slouken@6044
  1118
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1119
slouken@1933
  1120
/* vi: set ts=4 sw=4 expandtab: */