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