src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Thu, 25 Apr 2013 18:40:22 -0700
changeset 7113 7b4b596b3cfb
parent 7098 f4b2c6fb0258
child 7158 ff52fba70795
permissions -rw-r--r--
Mac: Don't supress mousemoves after warp.

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