src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Sat, 03 Aug 2013 02:20:00 -0400
changeset 7566 b14d00c30095
parent 7535 c515121a3df7
child 7593 20298a0d8631
permissions -rw-r--r--
Some fixes for SDL_MaximizeWindow().

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