src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 07 Aug 2013 16:29:15 -0700
changeset 7593 20298a0d8631
parent 7566 b14d00c30095
child 7594 6abcf951af68
permissions -rw-r--r--
Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.

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