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