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