src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Thu, 20 Mar 2014 11:22:57 -0400
changeset 8652 8514fc57f124
parent 8623 b4eee818b6fd
child 8729 94af945dbb57
permissions -rw-r--r--
Static analysis fix: dereference of a NULL pointer.
slouken@1933
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1933
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@8083
    25
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
slouken@8083
    26
# error SDL for Mac OS X must be built with a 10.7 SDK or above.
slouken@8083
    27
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1070 */
slouken@8083
    28
slouken@1933
    29
#include "SDL_syswm.h"
slouken@5398
    30
#include "SDL_timer.h"  /* For SDL_GetTicks() */
slouken@7915
    31
#include "SDL_hints.h"
slouken@1933
    32
#include "../SDL_sysvideo.h"
slouken@1933
    33
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    34
#include "../../events/SDL_mouse_c.h"
slouken@4673
    35
#include "../../events/SDL_touch_c.h"
slouken@1933
    36
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    37
#include "SDL_cocoavideo.h"
eligottlieb@4811
    38
#include "SDL_cocoashape.h"
gzjjgod@5057
    39
#include "SDL_cocoamouse.h"
jorgen@7594
    40
#include "SDL_cocoaopengl.h"
icculus@8295
    41
#include "SDL_assert.h"
slouken@1933
    42
jorgen@8261
    43
/* #define DEBUG_COCOAWINDOW */
jorgen@8261
    44
jorgen@8261
    45
#ifdef DEBUG_COCOAWINDOW
jorgen@8261
    46
#define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
jorgen@8261
    47
#else
jorgen@8261
    48
#define DLog(...) do { } while (0)
jorgen@8261
    49
#endif
jorgen@8261
    50
jorgen@8261
    51
jorgen@8260
    52
@interface SDLWindow : NSWindow
jorgen@8260
    53
/* These are needed for borderless/fullscreen windows */
jorgen@8260
    54
- (BOOL)canBecomeKeyWindow;
jorgen@8260
    55
- (BOOL)canBecomeMainWindow;
jorgen@8260
    56
- (void)sendEvent:(NSEvent *)event;
jorgen@8260
    57
@end
jorgen@8260
    58
jorgen@8260
    59
@implementation SDLWindow
jorgen@8260
    60
- (BOOL)canBecomeKeyWindow
jorgen@8260
    61
{
jorgen@8260
    62
    return YES;
jorgen@8260
    63
}
jorgen@8260
    64
jorgen@8260
    65
- (BOOL)canBecomeMainWindow
jorgen@8260
    66
{
jorgen@8260
    67
    return YES;
jorgen@8260
    68
}
jorgen@8260
    69
jorgen@8260
    70
- (void)sendEvent:(NSEvent *)event
jorgen@8260
    71
{
jorgen@8260
    72
  [super sendEvent:event];
jorgen@8260
    73
jorgen@8260
    74
  if ([event type] != NSLeftMouseUp) {
jorgen@8260
    75
      return;
jorgen@8260
    76
  }
jorgen@8260
    77
jorgen@8260
    78
  id delegate = [self delegate];
jorgen@8260
    79
  if (![delegate isKindOfClass:[Cocoa_WindowListener class]]) {
jorgen@8260
    80
      return;
jorgen@8260
    81
  }
jorgen@8260
    82
jorgen@8260
    83
  if ([delegate isMoving]) {
jorgen@8260
    84
      [delegate windowDidFinishMoving];
jorgen@8260
    85
  }
jorgen@8260
    86
}
jorgen@8260
    87
@end
jorgen@8260
    88
jorgen@8260
    89
slouken@5398
    90
static Uint32 s_moveHack;
slouken@5398
    91
slouken@7771
    92
static void ConvertNSRect(NSRect *r)
slouken@1933
    93
{
slouken@1933
    94
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    95
}
slouken@1933
    96
slouken@7952
    97
static void
slouken@7952
    98
ScheduleContextUpdates(SDL_WindowData *data)
jorgen@7595
    99
{
jorgen@8258
   100
    NSOpenGLContext *currentContext = [NSOpenGLContext currentContext];
jorgen@7595
   101
    NSMutableArray *contexts = data->nscontexts;
jorgen@7595
   102
    @synchronized (contexts) {
jorgen@7595
   103
        for (SDLOpenGLContext *context in contexts) {
jorgen@8258
   104
            if (context == currentContext) {
jorgen@8258
   105
                [context update];
jorgen@8258
   106
            } else {
jorgen@8258
   107
                [context scheduleUpdate];
jorgen@8258
   108
            }
jorgen@7595
   109
        }
jorgen@7595
   110
    }
jorgen@7595
   111
}
jorgen@7595
   112
slouken@7952
   113
static int
slouken@7952
   114
GetHintCtrlClickEmulateRightClick()
slouken@7915
   115
{
slouken@7915
   116
	const char *hint = SDL_GetHint( SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK );
slouken@7915
   117
	return hint != NULL && *hint != '0';
slouken@7915
   118
}
slouken@7915
   119
slouken@7952
   120
static unsigned int
slouken@7952
   121
GetWindowStyle(SDL_Window * window)
slouken@7952
   122
{
slouken@7952
   123
    unsigned int style;
slouken@7952
   124
icculus@8295
   125
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7952
   126
        style = NSBorderlessWindowMask;
slouken@7952
   127
    } else {
slouken@7952
   128
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@7952
   129
            style = NSBorderlessWindowMask;
slouken@7952
   130
        } else {
slouken@7952
   131
            style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@7952
   132
        }
slouken@7952
   133
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@7952
   134
            style |= NSResizableWindowMask;
slouken@7952
   135
        }
slouken@7952
   136
    }
slouken@7952
   137
    return style;
slouken@7952
   138
}
slouken@7952
   139
slouken@7990
   140
static SDL_bool
slouken@7990
   141
SetWindowStyle(SDL_Window * window, unsigned int style)
slouken@7990
   142
{
slouken@7990
   143
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7990
   144
    NSWindow *nswindow = data->nswindow;
slouken@7990
   145
slouken@7990
   146
    if (![nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@7990
   147
        return SDL_FALSE;
slouken@7990
   148
    }
slouken@7990
   149
slouken@7990
   150
    /* The view responder chain gets messed with during setStyleMask */
slouken@7990
   151
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@7990
   152
        [[nswindow contentView] setNextResponder:nil];
slouken@7990
   153
    }
slouken@7990
   154
slouken@7990
   155
    [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)style];
slouken@7990
   156
slouken@7990
   157
    /* The view responder chain gets messed with during setStyleMask */
slouken@7990
   158
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@7990
   159
        [[nswindow contentView] setNextResponder:data->listener];
slouken@7990
   160
    }
slouken@7990
   161
slouken@7990
   162
    return SDL_TRUE;
slouken@7990
   163
}
slouken@7990
   164
slouken@7952
   165
slouken@1933
   166
@implementation Cocoa_WindowListener
slouken@1933
   167
slouken@1933
   168
- (void)listen:(SDL_WindowData *)data
slouken@1933
   169
{
slouken@1933
   170
    NSNotificationCenter *center;
slouken@5371
   171
    NSWindow *window = data->nswindow;
slouken@5371
   172
    NSView *view = [window contentView];
slouken@1933
   173
slouken@1933
   174
    _data = data;
jorgen@7087
   175
    observingVisible = YES;
slouken@7740
   176
    wasCtrlLeft = NO;
jorgen@7087
   177
    wasVisible = [window isVisible];
slouken@7967
   178
    isFullscreenSpace = NO;
slouken@7952
   179
    inFullscreenTransition = NO;
slouken@7963
   180
    pendingWindowOperation = PENDING_OPERATION_NONE;
jorgen@8260
   181
    isMoving = NO;
slouken@1933
   182
slouken@1933
   183
    center = [NSNotificationCenter defaultCenter];
slouken@1933
   184
slouken@5374
   185
    if ([window delegate] != nil) {
slouken@5374
   186
        [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
slouken@5374
   187
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
slouken@5374
   188
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
slouken@5374
   189
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
   190
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
   191
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
   192
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
slouken@7952
   193
        [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
slouken@7952
   194
        [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
slouken@7952
   195
        [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
slouken@7952
   196
        [center addObserver:self selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:window];
slouken@5374
   197
    } else {
slouken@5374
   198
        [window setDelegate:self];
slouken@5374
   199
    }
slouken@1933
   200
slouken@7191
   201
    /* Haven't found a delegate / notification that triggers when the window is
slouken@7191
   202
     * ordered out (is not visible any more). You can be ordered out without
slouken@7191
   203
     * minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
slouken@7191
   204
     */
jorgen@7084
   205
    [window addObserver:self
jorgen@7084
   206
             forKeyPath:@"visible"
jorgen@7084
   207
                options:NSKeyValueObservingOptionNew
jorgen@7084
   208
                context:NULL];
jorgen@7084
   209
slouken@5371
   210
    [window setNextResponder:self];
slouken@5371
   211
    [window setAcceptsMouseMovedEvents:YES];
slouken@5371
   212
slouken@5371
   213
    [view setNextResponder:self];
icculus@6108
   214
icculus@6108
   215
    if ([view respondsToSelector:@selector(setAcceptsTouchEvents:)]) {
icculus@6108
   216
        [view setAcceptsTouchEvents:YES];
icculus@6108
   217
    }
slouken@1933
   218
}
slouken@1933
   219
jorgen@7084
   220
- (void)observeValueForKeyPath:(NSString *)keyPath
jorgen@7084
   221
                      ofObject:(id)object
jorgen@7084
   222
                        change:(NSDictionary *)change
jorgen@7084
   223
                       context:(void *)context
jorgen@7084
   224
{
jorgen@7087
   225
    if (!observingVisible) {
jorgen@7087
   226
        return;
jorgen@7087
   227
    }
jorgen@7087
   228
jorgen@7084
   229
    if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
jorgen@7084
   230
        int newVisibility = [[change objectForKey:@"new"] intValue];
jorgen@7084
   231
        if (newVisibility) {
jorgen@7084
   232
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7084
   233
        } else {
jorgen@7084
   234
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7084
   235
        }
jorgen@7084
   236
    }
jorgen@7084
   237
}
jorgen@7084
   238
jorgen@7087
   239
-(void) pauseVisibleObservation
jorgen@7087
   240
{
jorgen@7087
   241
    observingVisible = NO;
jorgen@7087
   242
    wasVisible = [_data->nswindow isVisible];
jorgen@7087
   243
}
jorgen@7087
   244
jorgen@7087
   245
-(void) resumeVisibleObservation
jorgen@7087
   246
{
jorgen@7087
   247
    BOOL isVisible = [_data->nswindow isVisible];
jorgen@7087
   248
    observingVisible = YES;
jorgen@7087
   249
    if (wasVisible != isVisible) {
jorgen@7087
   250
        if (isVisible) {
jorgen@7087
   251
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
jorgen@7087
   252
        } else {
jorgen@7087
   253
            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
jorgen@7087
   254
        }
jorgen@7087
   255
jorgen@7087
   256
        wasVisible = isVisible;
jorgen@7087
   257
    }
jorgen@7087
   258
}
jorgen@7087
   259
icculus@8284
   260
-(BOOL) setFullscreenSpace:(BOOL) state
slouken@7961
   261
{
slouken@7961
   262
    SDL_Window *window = _data->window;
slouken@7961
   263
    NSWindow *nswindow = _data->nswindow;
icculus@8295
   264
    SDL_VideoData *videodata = ((SDL_WindowData *) window->driverdata)->videodata;
slouken@7961
   265
icculus@8295
   266
    if (!videodata->allow_spaces) {
icculus@8295
   267
        return NO;  /* Spaces are forcibly disabled. */
icculus@8295
   268
    } else if (state && ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP)) {
icculus@8292
   269
        return NO;  /* we only allow you to make a Space on FULLSCREEN_DESKTOP windows. */
icculus@8284
   270
    } else if (state == isFullscreenSpace) {
icculus@8284
   271
        return YES;  /* already there. */
slouken@7961
   272
    }
slouken@7961
   273
slouken@7961
   274
    if (inFullscreenTransition) {
slouken@7961
   275
        if (state) {
slouken@7963
   276
            [self addPendingWindowOperation:PENDING_OPERATION_ENTER_FULLSCREEN];
slouken@7961
   277
        } else {
slouken@7963
   278
            [self addPendingWindowOperation:PENDING_OPERATION_LEAVE_FULLSCREEN];
slouken@7961
   279
        }
slouken@7961
   280
        return YES;
slouken@7961
   281
    }
slouken@7968
   282
    inFullscreenTransition = YES;
slouken@7968
   283
icculus@8295
   284
    /* you need to be FullScreenPrimary, or toggleFullScreen doesn't work. Unset it again in windowDidExitFullScreen. */
icculus@8284
   285
    [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7965
   286
    [nswindow performSelectorOnMainThread: @selector(toggleFullScreen:) withObject:nswindow waitUntilDone:NO];
slouken@7961
   287
    return YES;
slouken@7952
   288
}
slouken@7952
   289
slouken@7968
   290
-(BOOL) isInFullscreenSpace
slouken@7968
   291
{
slouken@7968
   292
    return isFullscreenSpace;
slouken@7968
   293
}
slouken@7968
   294
slouken@7968
   295
-(BOOL) isInFullscreenSpaceTransition
slouken@7963
   296
{
slouken@7963
   297
    return inFullscreenTransition;
slouken@7963
   298
}
slouken@7963
   299
slouken@7963
   300
-(void) addPendingWindowOperation:(PendingWindowOperation) operation
slouken@7963
   301
{
slouken@7963
   302
    pendingWindowOperation = operation;
slouken@7963
   303
}
slouken@7963
   304
slouken@1933
   305
- (void)close
slouken@1933
   306
{
slouken@1933
   307
    NSNotificationCenter *center;
slouken@5371
   308
    NSWindow *window = _data->nswindow;
slouken@5371
   309
    NSView *view = [window contentView];
icculus@7534
   310
    NSArray *windows = nil;
slouken@1933
   311
slouken@1933
   312
    center = [NSNotificationCenter defaultCenter];
slouken@1933
   313
slouken@5374
   314
    if ([window delegate] != self) {
slouken@5374
   315
        [center removeObserver:self name:NSWindowDidExposeNotification object:window];
slouken@5374
   316
        [center removeObserver:self name:NSWindowDidMoveNotification object:window];
slouken@5374
   317
        [center removeObserver:self name:NSWindowDidResizeNotification object:window];
slouken@5374
   318
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
slouken@5374
   319
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
slouken@5374
   320
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
slouken@5374
   321
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
slouken@7952
   322
        [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
slouken@7952
   323
        [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
slouken@7952
   324
        [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
slouken@7952
   325
        [center removeObserver:self name:NSWindowDidExitFullScreenNotification object:window];
slouken@5374
   326
    } else {
slouken@5374
   327
        [window setDelegate:nil];
slouken@5374
   328
    }
slouken@5371
   329
slouken@7961
   330
    [window removeObserver:self forKeyPath:@"visible"];
jorgen@7084
   331
slouken@5374
   332
    if ([window nextResponder] == self) {
slouken@5374
   333
        [window setNextResponder:nil];
slouken@5374
   334
    }
slouken@5374
   335
    if ([view nextResponder] == self) {
slouken@5374
   336
        [view setNextResponder:nil];
slouken@5374
   337
    }
icculus@7534
   338
icculus@7534
   339
    /* Make the next window in the z-order Key. If we weren't the foreground
icculus@7535
   340
       when closed, this is a no-op.
icculus@7535
   341
       !!! FIXME: Note that this is a hack, and there are corner cases where
icculus@7535
   342
       !!! FIXME:  this fails (such as the About box). The typical nib+RunLoop
icculus@7535
   343
       !!! FIXME:  handles this for Cocoa apps, but we bypass all that in SDL.
icculus@7535
   344
       !!! FIXME:  We should remove this code when we find a better way to
icculus@7535
   345
       !!! FIXME:  have the system do this for us. See discussion in
icculus@7535
   346
       !!! FIXME:   http://bugzilla.libsdl.org/show_bug.cgi?id=1825
icculus@7535
   347
    */
icculus@7534
   348
    windows = [NSApp orderedWindows];
jorgen@8111
   349
    for (NSWindow *win in windows)
jorgen@8111
   350
    {
jorgen@8111
   351
        if (win == window) {
jorgen@8111
   352
            continue;
jorgen@8111
   353
        }
jorgen@8111
   354
icculus@7534
   355
        [win makeKeyAndOrderFront:self];
jorgen@8111
   356
        break;
icculus@7534
   357
    }
slouken@1933
   358
}
slouken@1933
   359
jorgen@8260
   360
- (BOOL)isMoving
jorgen@8260
   361
{
jorgen@8260
   362
    return isMoving;
jorgen@8260
   363
}
jorgen@8260
   364
jorgen@8260
   365
-(void) setPendingMoveX:(int)x Y:(int)y
jorgen@8260
   366
{
jorgen@8260
   367
    pendingWindowWarpX = x;
jorgen@8260
   368
    pendingWindowWarpY = y;
jorgen@8260
   369
}
jorgen@8260
   370
jorgen@8260
   371
- (void)windowDidFinishMoving
jorgen@8260
   372
{
jorgen@8260
   373
    if ([self isMoving])
jorgen@8260
   374
    {
jorgen@8260
   375
        isMoving = NO;
jorgen@8260
   376
jorgen@8260
   377
        SDL_Mouse *mouse = SDL_GetMouse();
jorgen@8260
   378
        if (pendingWindowWarpX >= 0 && pendingWindowWarpY >= 0) {
jorgen@8260
   379
            mouse->WarpMouse(_data->window, pendingWindowWarpX, pendingWindowWarpY);
jorgen@8260
   380
            pendingWindowWarpX = pendingWindowWarpY = -1;
jorgen@8260
   381
        }
jorgen@8260
   382
        if (mouse->relative_mode && SDL_GetMouseFocus() == _data->window) {
jorgen@8260
   383
            mouse->SetRelativeMouseMode(SDL_TRUE);
jorgen@8260
   384
        }
jorgen@8260
   385
    }
jorgen@8260
   386
}
jorgen@8260
   387
slouken@1933
   388
- (BOOL)windowShouldClose:(id)sender
slouken@1933
   389
{
slouken@3685
   390
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
   391
    return NO;
slouken@1933
   392
}
slouken@1933
   393
slouken@1933
   394
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
   395
{
slouken@3685
   396
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
   397
}
slouken@1933
   398
jorgen@8260
   399
- (void)windowWillMove:(NSNotification *)aNotification
jorgen@8260
   400
{
jorgen@8260
   401
    if ([_data->nswindow isKindOfClass:[SDLWindow class]]) {
jorgen@8260
   402
        pendingWindowWarpX = pendingWindowWarpY = -1;
jorgen@8260
   403
        isMoving = YES;
jorgen@8260
   404
    }
jorgen@8260
   405
}
jorgen@8260
   406
slouken@1933
   407
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   408
{
slouken@1933
   409
    int x, y;
slouken@5398
   410
    SDL_Window *window = _data->window;
slouken@5398
   411
    NSWindow *nswindow = _data->nswindow;
slouken@5398
   412
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   413
    ConvertNSRect(&rect);
slouken@5398
   414
slouken@5398
   415
    if (s_moveHack) {
slouken@5398
   416
        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
slouken@5398
   417
slouken@5398
   418
        s_moveHack = 0;
slouken@5398
   419
slouken@5398
   420
        if (blockMove) {
slouken@5398
   421
            /* Cocoa is adjusting the window in response to a mode change */
slouken@5398
   422
            rect.origin.x = window->x;
slouken@5398
   423
            rect.origin.y = window->y;
slouken@5398
   424
            ConvertNSRect(&rect);
slouken@5398
   425
            [nswindow setFrameOrigin:rect.origin];
slouken@5398
   426
            return;
slouken@5398
   427
        }
slouken@5398
   428
    }
slouken@5398
   429
slouken@3507
   430
    x = (int)rect.origin.x;
slouken@3507
   431
    y = (int)rect.origin.y;
icculus@5564
   432
jorgen@7595
   433
    ScheduleContextUpdates(_data);
icculus@5564
   434
slouken@5398
   435
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   436
}
slouken@1933
   437
slouken@1933
   438
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   439
{
icculus@8284
   440
    if (inFullscreenTransition) {
icculus@8284
   441
        /* We'll take care of this at the end of the transition */
icculus@8284
   442
        return;
icculus@8284
   443
    }
icculus@8284
   444
slouken@7963
   445
    SDL_Window *window = _data->window;
slouken@7963
   446
    NSWindow *nswindow = _data->nswindow;
slouken@6231
   447
    int x, y, w, h;
slouken@7963
   448
    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6231
   449
    ConvertNSRect(&rect);
slouken@6231
   450
    x = (int)rect.origin.x;
slouken@6231
   451
    y = (int)rect.origin.y;
slouken@1933
   452
    w = (int)rect.size.width;
slouken@1933
   453
    h = (int)rect.size.height;
slouken@7952
   454
slouken@7963
   455
    if (SDL_IsShapedWindow(window)) {
slouken@7963
   456
        Cocoa_ResizeWindowShape(window);
slouken@7952
   457
    }
icculus@5564
   458
jorgen@7595
   459
    ScheduleContextUpdates(_data);
icculus@5564
   460
slouken@6231
   461
    /* The window can move during a resize event, such as when maximizing
slouken@6231
   462
       or resizing from a corner */
slouken@7963
   463
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@7963
   464
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
icculus@7566
   465
slouken@7963
   466
    const BOOL zoomed = [nswindow isZoomed];
icculus@7566
   467
    if (!zoomed) {
slouken@7963
   468
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@7566
   469
    } else if (zoomed) {
slouken@7963
   470
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
icculus@7566
   471
    }
slouken@1933
   472
}
slouken@1933
   473
slouken@1933
   474
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   475
{
slouken@3685
   476
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   477
}
slouken@1933
   478
slouken@1933
   479
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   480
{
slouken@3685
   481
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   482
}
slouken@1933
   483
slouken@1933
   484
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   485
{
slouken@5367
   486
    SDL_Window *window = _data->window;
jorgen@7271
   487
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@8260
   488
    if (mouse->relative_mode && ![self isMoving]) {
jorgen@8260
   489
        mouse->SetRelativeMouseMode(SDL_TRUE);
jorgen@8260
   490
    }
slouken@5367
   491
slouken@3688
   492
    /* We're going to get keyboard events, since we're key. */
slouken@5367
   493
    SDL_SetKeyboardFocus(window);
slouken@5367
   494
slouken@5367
   495
    /* If we just gained focus we need the updated mouse position */
jorgen@7271
   496
    if (!mouse->relative_mode) {
slouken@5367
   497
        NSPoint point;
slouken@5396
   498
        int x, y;
slouken@5396
   499
slouken@5396
   500
        point = [_data->nswindow mouseLocationOutsideOfEventStream];
slouken@5396
   501
        x = (int)point.x;
slouken@5396
   502
        y = (int)(window->h - point.y);
slouken@5396
   503
slouken@5396
   504
        if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
slouken@6950
   505
            SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@5396
   506
        }
slouken@5367
   507
    }
slouken@1962
   508
slouken@4503
   509
    /* Check to see if someone updated the clipboard */
slouken@4503
   510
    Cocoa_CheckClipboardUpdate(_data->videodata);
icculus@8288
   511
icculus@8293
   512
    if ((isFullscreenSpace) && ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)) {
icculus@8288
   513
        [NSMenu setMenuBarVisible:NO];
icculus@8288
   514
    }
slouken@1933
   515
}
slouken@1933
   516
slouken@1933
   517
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   518
{
jorgen@8260
   519
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@8260
   520
    if (mouse->relative_mode) {
jorgen@8260
   521
        mouse->SetRelativeMouseMode(SDL_FALSE);
jorgen@8260
   522
    }
jorgen@8260
   523
slouken@2059
   524
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   525
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   526
        SDL_SetMouseFocus(NULL);
slouken@2059
   527
    }
slouken@2059
   528
slouken@2059
   529
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   530
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   531
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   532
    }
icculus@8288
   533
icculus@8288
   534
    if (isFullscreenSpace) {
icculus@8288
   535
        [NSMenu setMenuBarVisible:YES];
icculus@8288
   536
    }
slouken@1933
   537
}
slouken@1933
   538
slouken@7952
   539
- (void)windowWillEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   540
{
slouken@7952
   541
    SDL_Window *window = _data->window;
slouken@7952
   542
slouken@7990
   543
    SetWindowStyle(window, (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask));
slouken@7965
   544
slouken@7967
   545
    isFullscreenSpace = YES;
slouken@7952
   546
    inFullscreenTransition = YES;
slouken@7952
   547
}
slouken@7952
   548
slouken@7952
   549
- (void)windowDidEnterFullScreen:(NSNotification *)aNotification
slouken@7952
   550
{
slouken@7965
   551
    SDL_Window *window = _data->window;
slouken@7965
   552
slouken@7952
   553
    inFullscreenTransition = NO;
slouken@7961
   554
slouken@7963
   555
    if (pendingWindowOperation == PENDING_OPERATION_LEAVE_FULLSCREEN) {
slouken@7963
   556
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   557
        [self setFullscreenSpace:NO];
slouken@7961
   558
    } else {
icculus@8284
   559
        if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8284
   560
            [NSMenu setMenuBarVisible:NO];
icculus@8284
   561
        }
icculus@8284
   562
slouken@7963
   563
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   564
        /* Force the size change event in case it was delivered earlier
slouken@7965
   565
           while the window was still animating into place.
slouken@7965
   566
         */
slouken@7965
   567
        window->w = 0;
slouken@7965
   568
        window->h = 0;
slouken@7961
   569
        [self windowDidResize:aNotification];
slouken@7961
   570
    }
slouken@7952
   571
}
slouken@7952
   572
slouken@7952
   573
- (void)windowWillExitFullScreen:(NSNotification *)aNotification
slouken@7952
   574
{
slouken@7964
   575
    SDL_Window *window = _data->window;
slouken@7964
   576
slouken@7990
   577
    SetWindowStyle(window, GetWindowStyle(window));
slouken@7964
   578
slouken@7967
   579
    isFullscreenSpace = NO;
slouken@7952
   580
    inFullscreenTransition = YES;
slouken@7952
   581
}
slouken@7952
   582
slouken@7952
   583
- (void)windowDidExitFullScreen:(NSNotification *)aNotification
slouken@7952
   584
{
slouken@7965
   585
    SDL_Window *window = _data->window;
slouken@7952
   586
    NSWindow *nswindow = _data->nswindow;
slouken@7952
   587
slouken@7952
   588
    inFullscreenTransition = NO;
slouken@7961
   589
slouken@8618
   590
    [nswindow setLevel:kCGNormalWindowLevel];
slouken@8618
   591
slouken@7963
   592
    if (pendingWindowOperation == PENDING_OPERATION_ENTER_FULLSCREEN) {
slouken@7963
   593
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   594
        [self setFullscreenSpace:YES];
slouken@7963
   595
    } else if (pendingWindowOperation == PENDING_OPERATION_MINIMIZE) {
slouken@7963
   596
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7963
   597
        [nswindow miniaturize:nil];
slouken@7961
   598
    } else {
icculus@8291
   599
        /* Adjust the fullscreen toggle button and readd menu now that we're here. */
icculus@8291
   600
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8291
   601
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
icculus@8291
   602
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
icculus@8291
   603
        } else {
icculus@8284
   604
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
icculus@8284
   605
        }
icculus@8291
   606
        [NSMenu setMenuBarVisible:YES];
icculus@8284
   607
slouken@7963
   608
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   609
        /* Force the size change event in case it was delivered earlier
slouken@7965
   610
           while the window was still animating into place.
slouken@7965
   611
         */
slouken@7965
   612
        window->w = 0;
slouken@7965
   613
        window->h = 0;
slouken@7961
   614
        [self windowDidResize:aNotification];
icculus@8622
   615
slouken@8623
   616
        /* FIXME: Why does the window get hidden? */
slouken@8623
   617
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@8623
   618
            Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
slouken@8623
   619
        }
slouken@7961
   620
    }
slouken@7952
   621
}
slouken@7952
   622
icculus@8291
   623
-(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
icculus@8291
   624
{
icculus@8291
   625
    if ((_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8291
   626
        return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
icculus@8291
   627
    } else {
icculus@8291
   628
        return proposedOptions;
icculus@8291
   629
    }
icculus@8291
   630
}
icculus@8291
   631
icculus@8291
   632
slouken@7191
   633
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   634
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   635
 * where they get converted to action messages, etc.
slouken@7191
   636
 */
slouken@6514
   637
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   638
{
slouken@7191
   639
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   640
}
slouken@6514
   641
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   642
{
slouken@7191
   643
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   644
}
slouken@6514
   645
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   646
{
slouken@7191
   647
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   648
}
slouken@6514
   649
slouken@7191
   650
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   651
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   652
 */
slouken@6514
   653
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   654
{
slouken@7191
   655
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   656
}
slouken@6514
   657
slouken@1933
   658
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   659
{
slouken@1959
   660
    int button;
slouken@1933
   661
slouken@1959
   662
    switch ([theEvent buttonNumber]) {
slouken@1959
   663
    case 0:
slouken@7915
   664
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   665
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   666
            wasCtrlLeft = YES;
slouken@7740
   667
            button = SDL_BUTTON_RIGHT;
slouken@7740
   668
        } else {
slouken@7740
   669
            wasCtrlLeft = NO;
slouken@7740
   670
            button = SDL_BUTTON_LEFT;
slouken@7740
   671
        }
slouken@1959
   672
        break;
slouken@1959
   673
    case 1:
slouken@1959
   674
        button = SDL_BUTTON_RIGHT;
slouken@1959
   675
        break;
slouken@1959
   676
    case 2:
slouken@1959
   677
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   678
        break;
slouken@1959
   679
    default:
slouken@5061
   680
        button = [theEvent buttonNumber] + 1;
slouken@1959
   681
        break;
slouken@1959
   682
    }
slouken@6950
   683
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   684
}
slouken@1933
   685
slouken@1933
   686
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   687
{
slouken@1959
   688
    [self mouseDown:theEvent];
slouken@1933
   689
}
slouken@1933
   690
slouken@1933
   691
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   692
{
slouken@1959
   693
    [self mouseDown:theEvent];
slouken@1933
   694
}
slouken@1933
   695
slouken@1933
   696
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   697
{
slouken@1959
   698
    int button;
slouken@1933
   699
slouken@1959
   700
    switch ([theEvent buttonNumber]) {
slouken@1959
   701
    case 0:
slouken@7740
   702
        if (wasCtrlLeft) {
slouken@7740
   703
            button = SDL_BUTTON_RIGHT;
slouken@7740
   704
            wasCtrlLeft = NO;
slouken@7740
   705
        } else {
slouken@7740
   706
            button = SDL_BUTTON_LEFT;
slouken@7740
   707
        }
slouken@1959
   708
        break;
slouken@1959
   709
    case 1:
slouken@1959
   710
        button = SDL_BUTTON_RIGHT;
slouken@1959
   711
        break;
slouken@1959
   712
    case 2:
slouken@1959
   713
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   714
        break;
slouken@1959
   715
    default:
slouken@5061
   716
        button = [theEvent buttonNumber] + 1;
slouken@1959
   717
        break;
slouken@1959
   718
    }
slouken@6950
   719
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   720
}
slouken@1933
   721
slouken@1933
   722
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   723
{
slouken@1959
   724
    [self mouseUp:theEvent];
slouken@1933
   725
}
slouken@1933
   726
slouken@1933
   727
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   728
{
slouken@1959
   729
    [self mouseUp:theEvent];
slouken@1933
   730
}
slouken@1933
   731
slouken@1933
   732
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   733
{
slouken@5406
   734
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   735
    SDL_Window *window = _data->window;
slouken@5396
   736
    NSPoint point;
slouken@5396
   737
    int x, y;
slouken@1933
   738
slouken@5406
   739
    if (mouse->relative_mode) {
gzjjgod@5059
   740
        return;
slouken@5371
   741
    }
gzjjgod@5059
   742
slouken@5396
   743
    point = [theEvent locationInWindow];
slouken@5396
   744
    x = (int)point.x;
slouken@5396
   745
    y = (int)(window->h - point.y);
slouken@5371
   746
slouken@5396
   747
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   748
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@6666
   749
            if (x < 0) {
slouken@6666
   750
                x = 0;
slouken@6666
   751
            } else if (x >= window->w) {
slouken@6666
   752
                x = window->w - 1;
slouken@6666
   753
            }
slouken@6666
   754
            if (y < 0) {
slouken@6666
   755
                y = 0;
slouken@6666
   756
            } else if (y >= window->h) {
slouken@6666
   757
                y = window->h - 1;
slouken@6666
   758
            }
slouken@6666
   759
jorgen@7593
   760
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   761
            CGPoint cgpoint;
jorgen@8260
   762
jorgen@7593
   763
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   764
             * SDL_cocoamousetap.m.
jorgen@7593
   765
             */
jorgen@7593
   766
slouken@6666
   767
            cgpoint.x = window->x + x;
slouken@6666
   768
            cgpoint.y = window->y + y;
jorgen@7098
   769
jorgen@7113
   770
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   771
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   772
             * sure how we'd procure the right one for this event.
jorgen@7098
   773
             */
jorgen@7113
   774
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   775
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   776
            CGSetLocalEventsSuppressionInterval(0.25);
jorgen@8261
   777
jorgen@8261
   778
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   779
#endif
slouken@5396
   780
        }
slouken@2059
   781
    }
slouken@6950
   782
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   783
}
slouken@1933
   784
slouken@1957
   785
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   786
{
slouken@1957
   787
    [self mouseMoved:theEvent];
slouken@1957
   788
}
slouken@1957
   789
slouken@1958
   790
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   791
{
slouken@1958
   792
    [self mouseMoved:theEvent];
slouken@1958
   793
}
slouken@1958
   794
slouken@1958
   795
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   796
{
slouken@1958
   797
    [self mouseMoved:theEvent];
slouken@1958
   798
}
slouken@1958
   799
slouken@1933
   800
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   801
{
gzjjgod@5057
   802
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   803
}
slouken@3688
   804
slouken@4673
   805
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   806
{
slouken@4673
   807
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   808
}
slouken@4673
   809
slouken@4673
   810
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   811
{
slouken@4673
   812
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   813
}
slouken@4673
   814
slouken@4673
   815
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   816
{
slouken@4673
   817
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   818
}
slouken@4673
   819
slouken@4673
   820
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   821
{
slouken@4673
   822
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   823
}
slouken@4673
   824
slouken@4673
   825
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   826
{
slouken@4680
   827
    NSSet *touches = 0;
slouken@4680
   828
    NSEnumerator *enumerator;
slouken@4680
   829
    NSTouch *touch;
slouken@4673
   830
slouken@4680
   831
    switch (type) {
slouken@4680
   832
        case COCOA_TOUCH_DOWN:
slouken@4680
   833
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   834
            break;
slouken@4680
   835
        case COCOA_TOUCH_UP:
jorgen@8106
   836
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
jorgen@8106
   837
            break;
slouken@4680
   838
        case COCOA_TOUCH_CANCELLED:
jorgen@8106
   839
            touches = [event touchesMatchingPhase:NSTouchPhaseCancelled inView:nil];
slouken@4680
   840
            break;
slouken@4680
   841
        case COCOA_TOUCH_MOVE:
slouken@4680
   842
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   843
            break;
slouken@4680
   844
    }
slouken@4680
   845
slouken@4680
   846
    enumerator = [touches objectEnumerator];
slouken@4680
   847
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   848
    while (touch) {
slouken@6953
   849
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@4673
   850
        if (!SDL_GetTouch(touchId)) {
slouken@6951
   851
            if (SDL_AddTouch(touchId, "") < 0) {
slouken@4680
   852
                return;
slouken@4673
   853
            }
slouken@7191
   854
        }
slouken@4687
   855
slouken@6953
   856
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
   857
        float x = [touch normalizedPosition].x;
slouken@4673
   858
        float y = [touch normalizedPosition].y;
slouken@5261
   859
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   860
        y = 1.0f - y;
slouken@4687
   861
slouken@4673
   862
        switch (type) {
slouken@4673
   863
        case COCOA_TOUCH_DOWN:
slouken@6951
   864
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
   865
            break;
slouken@4673
   866
        case COCOA_TOUCH_UP:
slouken@4673
   867
        case COCOA_TOUCH_CANCELLED:
slouken@6951
   868
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
   869
            break;
slouken@4673
   870
        case COCOA_TOUCH_MOVE:
slouken@6951
   871
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
   872
            break;
slouken@4673
   873
        }
slouken@6951
   874
slouken@4673
   875
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   876
    }
slouken@1933
   877
}
slouken@1933
   878
slouken@1933
   879
@end
slouken@1933
   880
slouken@5379
   881
@interface SDLView : NSView
jorgen@7158
   882
slouken@5379
   883
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   884
- (void)rightMouseDown:(NSEvent *)theEvent;
gzjjgod@4915
   885
@end
gzjjgod@4915
   886
gzjjgod@4915
   887
@implementation SDLView
gzjjgod@4915
   888
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   889
{
slouken@5371
   890
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   891
}
jorgen@7158
   892
jorgen@7158
   893
- (void)resetCursorRects
jorgen@7158
   894
{
jorgen@7158
   895
    [super resetCursorRects];
jorgen@7158
   896
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
   897
jorgen@7270
   898
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
   899
        [self addCursorRect:[self bounds]
jorgen@7158
   900
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
   901
    } else {
jorgen@7158
   902
        [self addCursorRect:[self bounds]
jorgen@7158
   903
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
   904
    }
jorgen@7158
   905
}
gzjjgod@4915
   906
@end
gzjjgod@4915
   907
slouken@1933
   908
static int
slouken@1951
   909
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   910
{
slouken@6848
   911
    NSAutoreleasePool *pool;
slouken@1951
   912
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   913
    SDL_WindowData *data;
slouken@1933
   914
slouken@1933
   915
    /* Allocate the window data */
slouken@5249
   916
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   917
    if (!data) {
icculus@7037
   918
        return SDL_OutOfMemory();
slouken@1933
   919
    }
slouken@3685
   920
    data->window = window;
slouken@3688
   921
    data->nswindow = nswindow;
slouken@1933
   922
    data->created = created;
slouken@1951
   923
    data->videodata = videodata;
jorgen@7595
   924
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
   925
slouken@6848
   926
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   927
slouken@6848
   928
    /* Create an event listener for the window */
slouken@6848
   929
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
   930
slouken@6848
   931
    /* Fill in the SDL window with the window data */
slouken@6848
   932
    {
slouken@6848
   933
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   934
        ConvertNSRect(&rect);
slouken@6848
   935
        window->x = (int)rect.origin.x;
slouken@6848
   936
        window->y = (int)rect.origin.y;
slouken@6848
   937
        window->w = (int)rect.size.width;
slouken@6848
   938
        window->h = (int)rect.size.height;
slouken@6848
   939
    }
slouken@6848
   940
slouken@6848
   941
    /* Set up the listener after we create the view */
slouken@6848
   942
    [data->listener listen:data];
slouken@6848
   943
slouken@6848
   944
    if ([nswindow isVisible]) {
slouken@6848
   945
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
   946
    } else {
slouken@6848
   947
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
   948
    }
jorgen@7084
   949
slouken@6848
   950
    {
slouken@6848
   951
        unsigned int style = [nswindow styleMask];
slouken@6848
   952
slouken@6848
   953
        if (style == NSBorderlessWindowMask) {
slouken@6848
   954
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
   955
        } else {
slouken@6848
   956
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
   957
        }
slouken@6848
   958
        if (style & NSResizableWindowMask) {
slouken@6848
   959
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
   960
        } else {
slouken@6848
   961
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
   962
        }
slouken@6848
   963
    }
jorgen@7084
   964
slouken@6848
   965
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
   966
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   967
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
   968
    } else {
slouken@6848
   969
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
   970
    }
jorgen@7084
   971
slouken@6848
   972
    if ([nswindow isMiniaturized]) {
slouken@6848
   973
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
   974
    } else {
slouken@6848
   975
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
   976
    }
jorgen@7084
   977
slouken@6848
   978
    if ([nswindow isKeyWindow]) {
slouken@6848
   979
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
   980
        SDL_SetKeyboardFocus(data->window);
slouken@6848
   981
    }
slouken@1933
   982
jorgen@7085
   983
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
   984
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
   985
     */
jorgen@7085
   986
    [nswindow setOneShot:NO];
jorgen@7085
   987
slouken@6848
   988
    /* All done! */
slouken@6848
   989
    [pool release];
slouken@6848
   990
    window->driverdata = data;
slouken@6848
   991
    return 0;
slouken@1933
   992
}
slouken@1933
   993
slouken@1933
   994
int
slouken@1933
   995
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   996
{
icculus@8295
   997
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
   998
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   999
    NSWindow *nswindow;
slouken@6848
  1000
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1001
    NSRect rect;
slouken@6848
  1002
    SDL_Rect bounds;
slouken@6848
  1003
    unsigned int style;
slouken@1933
  1004
slouken@6848
  1005
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1006
    rect.origin.x = window->x;
slouken@6848
  1007
    rect.origin.y = window->y;
slouken@6848
  1008
    rect.size.width = window->w;
slouken@6848
  1009
    rect.size.height = window->h;
slouken@6848
  1010
    ConvertNSRect(&rect);
slouken@1933
  1011
slouken@6848
  1012
    style = GetWindowStyle(window);
slouken@1933
  1013
slouken@6848
  1014
    /* Figure out which screen to place this window */
slouken@6848
  1015
    NSArray *screens = [NSScreen screens];
slouken@6848
  1016
    NSScreen *screen = nil;
slouken@6848
  1017
    NSScreen *candidate;
slouken@6848
  1018
    int i, count = [screens count];
slouken@6848
  1019
    for (i = 0; i < count; ++i) {
slouken@6848
  1020
        candidate = [screens objectAtIndex:i];
slouken@6848
  1021
        NSRect screenRect = [candidate frame];
slouken@6848
  1022
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1023
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1024
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1025
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1026
            screen = candidate;
slouken@6848
  1027
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1028
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1029
        }
slouken@6848
  1030
    }
slouken@7946
  1031
slouken@7946
  1032
    @try {
slouken@7946
  1033
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1034
    }
slouken@7946
  1035
    @catch (NSException *e) {
slouken@7946
  1036
        SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7948
  1037
        [pool release];
slouken@7946
  1038
        return -1;
slouken@7946
  1039
    }
icculus@7205
  1040
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1041
icculus@8295
  1042
    if (videodata->allow_spaces) {
icculus@8295
  1043
        SDL_assert(videodata->osversion >= 0x1070);
icculus@8295
  1044
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1045
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1046
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1047
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1048
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1049
        }
slouken@7955
  1050
    }
alexey@6832
  1051
slouken@7191
  1052
    /* Create a default view for this window */
slouken@6848
  1053
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
  1054
    NSView *contentView = [[SDLView alloc] initWithFrame:rect];
urkle@7746
  1055
slouken@7955
  1056
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1057
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1058
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1059
        }
urkle@7746
  1060
    }
urkle@7746
  1061
slouken@6848
  1062
    [nswindow setContentView: contentView];
slouken@6848
  1063
    [contentView release];
alexey@6832
  1064
slouken@6848
  1065
    [pool release];
slouken@6848
  1066
slouken@6848
  1067
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1068
        [nswindow release];
slouken@6848
  1069
        return -1;
slouken@3506
  1070
    }
slouken@6848
  1071
    return 0;
slouken@1933
  1072
}
slouken@1933
  1073
slouken@1933
  1074
int
slouken@1933
  1075
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
  1076
{
slouken@6848
  1077
    NSAutoreleasePool *pool;
slouken@1933
  1078
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1079
    NSString *title;
slouken@1933
  1080
slouken@6848
  1081
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1082
slouken@6848
  1083
    /* Query the title from the existing window */
slouken@6848
  1084
    title = [nswindow title];
slouken@6848
  1085
    if (title) {
slouken@6848
  1086
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1087
    }
slouken@1933
  1088
slouken@6848
  1089
    [pool release];
slouken@6848
  1090
slouken@1951
  1091
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
  1092
}
slouken@1933
  1093
slouken@1933
  1094
void
slouken@1933
  1095
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
  1096
{
slouken@6848
  1097
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1098
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1099
    NSString *string;
slouken@1933
  1100
slouken@6848
  1101
    if(window->title) {
slouken@6848
  1102
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@6848
  1103
    } else {
slouken@6848
  1104
        string = [[NSString alloc] init];
slouken@1956
  1105
    }
slouken@6848
  1106
    [nswindow setTitle:string];
slouken@6848
  1107
    [string release];
slouken@6848
  1108
slouken@6848
  1109
    [pool release];
slouken@1933
  1110
}
slouken@1933
  1111
slouken@1933
  1112
void
slouken@5375
  1113
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@5375
  1114
{
slouken@6848
  1115
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1116
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1117
slouken@6848
  1118
    if (nsimage) {
slouken@6848
  1119
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1120
    }
slouken@6848
  1121
slouken@6848
  1122
    [pool release];
slouken@5375
  1123
}
slouken@5375
  1124
slouken@5375
  1125
void
slouken@1933
  1126
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
  1127
{
slouken@6848
  1128
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7594
  1129
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1130
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1131
    NSRect rect;
slouken@6848
  1132
    Uint32 moveHack;
slouken@1933
  1133
slouken@6848
  1134
    rect.origin.x = window->x;
slouken@6848
  1135
    rect.origin.y = window->y;
slouken@6848
  1136
    rect.size.width = window->w;
slouken@6848
  1137
    rect.size.height = window->h;
slouken@6848
  1138
    ConvertNSRect(&rect);
slouken@5478
  1139
slouken@6848
  1140
    moveHack = s_moveHack;
slouken@6848
  1141
    s_moveHack = 0;
slouken@6848
  1142
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1143
    s_moveHack = moveHack;
slouken@5478
  1144
jorgen@7595
  1145
    ScheduleContextUpdates(windata);
slouken@6848
  1146
slouken@6848
  1147
    [pool release];
slouken@1933
  1148
}
slouken@1933
  1149
slouken@1933
  1150
void
slouken@1933
  1151
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
  1152
{
slouken@6848
  1153
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1154
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1155
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1156
    NSSize size;
slouken@1933
  1157
slouken@6848
  1158
    size.width = window->w;
slouken@6848
  1159
    size.height = window->h;
slouken@6848
  1160
    [nswindow setContentSize:size];
icculus@5564
  1161
jorgen@7595
  1162
    ScheduleContextUpdates(windata);
slouken@6848
  1163
slouken@6848
  1164
    [pool release];
slouken@1933
  1165
}
slouken@1933
  1166
slouken@1933
  1167
void
stopiccot@6681
  1168
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
stopiccot@6681
  1169
{
slouken@6848
  1170
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1171
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1172
slouken@6848
  1173
    NSSize minSize;
slouken@6848
  1174
    minSize.width = window->min_w;
slouken@6848
  1175
    minSize.height = window->min_h;
slouken@7191
  1176
slouken@6848
  1177
    [windata->nswindow setContentMinSize:minSize];
slouken@7191
  1178
slouken@6848
  1179
    [pool release];
slouken@6788
  1180
}
slouken@6788
  1181
slouken@6788
  1182
void
slouken@6788
  1183
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@6788
  1184
{
slouken@6848
  1185
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1186
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1187
slouken@6848
  1188
    NSSize maxSize;
slouken@6848
  1189
    maxSize.width = window->max_w;
slouken@6848
  1190
    maxSize.height = window->max_h;
slouken@7191
  1191
slouken@6848
  1192
    [windata->nswindow setContentMaxSize:maxSize];
slouken@7191
  1193
slouken@6848
  1194
    [pool release];
stopiccot@6681
  1195
}
stopiccot@6681
  1196
stopiccot@6681
  1197
void
slouken@1933
  1198
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
  1199
{
slouken@6848
  1200
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
  1201
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1202
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1203
slouken@6848
  1204
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1205
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1206
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1207
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1208
    }
slouken@6848
  1209
    [pool release];
slouken@1933
  1210
}
slouken@1933
  1211
slouken@1933
  1212
void
slouken@1933
  1213
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
  1214
{
slouken@6848
  1215
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1216
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1217
slouken@6848
  1218
    [nswindow orderOut:nil];
slouken@6848
  1219
    [pool release];
slouken@1933
  1220
}
slouken@1933
  1221
slouken@1933
  1222
void
slouken@1933
  1223
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
  1224
{
slouken@6848
  1225
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
  1226
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1227
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1228
slouken@7771
  1229
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1230
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1231
     */
jorgen@7087
  1232
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1233
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
jorgen@7469
  1234
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1235
    }
jorgen@7087
  1236
    [windowData->listener resumeVisibleObservation];
jorgen@7087
  1237
slouken@6848
  1238
    [pool release];
slouken@1933
  1239
}
slouken@1933
  1240
slouken@1933
  1241
void
slouken@1933
  1242
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
  1243
{
slouken@6848
  1244
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7594
  1245
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1246
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1247
slouken@6848
  1248
    [nswindow zoom:nil];
slouken@6848
  1249
jorgen@7595
  1250
    ScheduleContextUpdates(windata);
slouken@6848
  1251
slouken@6848
  1252
    [pool release];
slouken@1933
  1253
}
slouken@1933
  1254
slouken@1933
  1255
void
slouken@1933
  1256
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
  1257
{
slouken@6848
  1258
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7963
  1259
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1260
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1261
slouken@7968
  1262
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1263
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1264
    } else {
slouken@7963
  1265
        [nswindow miniaturize:nil];
slouken@7963
  1266
    }
slouken@6848
  1267
    [pool release];
slouken@1933
  1268
}
slouken@1933
  1269
slouken@1933
  1270
void
slouken@1933
  1271
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
  1272
{
slouken@6848
  1273
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1274
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1275
slouken@6848
  1276
    if ([nswindow isMiniaturized]) {
slouken@6848
  1277
        [nswindow deminiaturize:nil];
slouken@6848
  1278
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1279
        [nswindow zoom:nil];
slouken@1956
  1280
    }
slouken@6848
  1281
    [pool release];
slouken@1933
  1282
}
slouken@1933
  1283
slouken@5400
  1284
static NSWindow *
slouken@5400
  1285
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
  1286
{
slouken@5400
  1287
    if (!data->created) {
slouken@5400
  1288
        /* Don't mess with other people's windows... */
slouken@5400
  1289
        return nswindow;
slouken@5400
  1290
    }
slouken@5400
  1291
slouken@5400
  1292
    [data->listener close];
jorgen@7085
  1293
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
  1294
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@7085
  1295
    /* See comment in SetupWindowData. */
jorgen@7085
  1296
    [data->nswindow setOneShot:NO];
slouken@5400
  1297
    [data->listener listen:data];
slouken@5400
  1298
slouken@5400
  1299
    [nswindow close];
slouken@5400
  1300
slouken@5400
  1301
    return data->nswindow;
slouken@5400
  1302
}
slouken@5400
  1303
slouken@1933
  1304
void
icculus@6422
  1305
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
  1306
{
slouken@6848
  1307
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7990
  1308
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@6848
  1309
        if (bordered) {
slouken@7191
  1310
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1311
        }
icculus@6422
  1312
    }
slouken@6848
  1313
    [pool release];
icculus@6422
  1314
}
icculus@6422
  1315
slouken@7952
  1316
slouken@7968
  1317
void
slouken@7968
  1318
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@7952
  1319
{
slouken@7968
  1320
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1321
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1322
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1323
    NSRect rect;
slouken@5249
  1324
slouken@6848
  1325
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1326
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1327
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1328
    }
slouken@6848
  1329
slouken@6848
  1330
    if (fullscreen) {
slouken@6848
  1331
        SDL_Rect bounds;
slouken@6848
  1332
slouken@6848
  1333
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1334
        rect.origin.x = bounds.x;
slouken@6848
  1335
        rect.origin.y = bounds.y;
slouken@6848
  1336
        rect.size.width = bounds.w;
slouken@6848
  1337
        rect.size.height = bounds.h;
slouken@6848
  1338
        ConvertNSRect(&rect);
slouken@6848
  1339
slouken@6848
  1340
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1341
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1342
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1343
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1344
        }
slouken@5401
  1345
slouken@6848
  1346
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1347
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
  1348
        } else {
slouken@6848
  1349
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
  1350
        }
slouken@6848
  1351
    } else {
slouken@6848
  1352
        rect.origin.x = window->windowed.x;
slouken@6848
  1353
        rect.origin.y = window->windowed.y;
slouken@6848
  1354
        rect.size.width = window->windowed.w;
slouken@6848
  1355
        rect.size.height = window->windowed.h;
slouken@6848
  1356
        ConvertNSRect(&rect);
alexey@6832
  1357
slouken@6848
  1358
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1359
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
  1360
        } else {
slouken@6848
  1361
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1362
        }
slouken@5398
  1363
    }
slouken@6848
  1364
slouken@6848
  1365
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1366
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1367
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1368
    }
slouken@6848
  1369
slouken@6848
  1370
    s_moveHack = 0;
slouken@7873
  1371
    [nswindow setContentSize:rect.size];
slouken@6848
  1372
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1373
    s_moveHack = SDL_GetTicks();
slouken@6848
  1374
slouken@6848
  1375
    /* When the window style changes the title is cleared */
slouken@6848
  1376
    if (!fullscreen) {
slouken@6848
  1377
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1378
    }
slouken@6848
  1379
slouken@6848
  1380
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1381
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1382
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1383
    } else {
slouken@6848
  1384
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1385
    }
jorgen@7087
  1386
jorgen@7636
  1387
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1388
        [data->listener pauseVisibleObservation];
jorgen@7636
  1389
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1390
        [data->listener resumeVisibleObservation];
jorgen@7636
  1391
    }
slouken@6848
  1392
jorgen@7595
  1393
    ScheduleContextUpdates(data);
slouken@6848
  1394
slouken@6848
  1395
    [pool release];
slouken@5249
  1396
}
slouken@5249
  1397
slouken@5466
  1398
int
slouken@5466
  1399
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1400
{
slouken@5466
  1401
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1402
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1403
    const uint32_t tableSize = 256;
slouken@5466
  1404
    CGGammaValue redTable[tableSize];
slouken@5466
  1405
    CGGammaValue greenTable[tableSize];
slouken@5466
  1406
    CGGammaValue blueTable[tableSize];
slouken@5466
  1407
    uint32_t i;
slouken@5466
  1408
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1409
slouken@5466
  1410
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1411
    for (i = 0; i < 256; i++) {
slouken@5466
  1412
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1413
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1414
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1415
    }
slouken@5466
  1416
slouken@5466
  1417
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1418
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1419
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1420
    }
slouken@5466
  1421
    return 0;
slouken@5466
  1422
}
slouken@5466
  1423
slouken@5466
  1424
int
slouken@5466
  1425
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1426
{
slouken@5466
  1427
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1428
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1429
    const uint32_t tableSize = 256;
slouken@5466
  1430
    CGGammaValue redTable[tableSize];
slouken@5466
  1431
    CGGammaValue greenTable[tableSize];
slouken@5466
  1432
    CGGammaValue blueTable[tableSize];
slouken@5466
  1433
    uint32_t i, tableCopied;
slouken@5466
  1434
slouken@5466
  1435
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1436
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1437
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1438
    }
slouken@5466
  1439
slouken@5466
  1440
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1441
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1442
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1443
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1444
    }
slouken@5466
  1445
    return 0;
slouken@5466
  1446
}
slouken@5466
  1447
slouken@5249
  1448
void
slouken@6662
  1449
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1450
{
slouken@5371
  1451
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1452
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1453
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1454
        int x, y;
slouken@5371
  1455
        CGPoint cgpoint;
slouken@5371
  1456
slouken@5371
  1457
        SDL_GetMouseState(&x, &y);
slouken@5371
  1458
        cgpoint.x = window->x + x;
slouken@5371
  1459
        cgpoint.y = window->y + y;
jorgen@8261
  1460
jorgen@8261
  1461
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1462
jorgen@8261
  1463
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1464
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1465
    }
slouken@7191
  1466
icculus@8652
  1467
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slouken@7191
  1468
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
  1469
            /* OpenGL is rendering to the window, so make it visible! */
slouken@7191
  1470
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1471
        } else {
slouken@7191
  1472
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1473
        }
slouken@7191
  1474
    }
slouken@1933
  1475
}
slouken@1933
  1476
slouken@1933
  1477
void
slouken@1933
  1478
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
  1479
{
slouken@6848
  1480
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1481
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1482
slouken@6848
  1483
    if (data) {
slouken@6848
  1484
        [data->listener close];
slouken@6848
  1485
        [data->listener release];
slouken@6848
  1486
        if (data->created) {
slouken@6848
  1487
            [data->nswindow close];
slouken@1933
  1488
        }
jorgen@7595
  1489
jorgen@7595
  1490
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1491
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1492
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1493
            [context setWindow:NULL];
jorgen@7595
  1494
        }
jorgen@7595
  1495
        [data->nscontexts release];
jorgen@7595
  1496
slouken@6848
  1497
        SDL_free(data);
slouken@1933
  1498
    }
slouken@6848
  1499
    [pool release];
slouken@1933
  1500
}
slouken@1933
  1501
slouken@1933
  1502
SDL_bool
slouken@1933
  1503
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1504
{
slouken@4900
  1505
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1506
slouken@1933
  1507
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1508
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1509
        info->info.cocoa.window = nswindow;
slouken@1933
  1510
        return SDL_TRUE;
slouken@1933
  1511
    } else {
slouken@1933
  1512
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1513
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1514
        return SDL_FALSE;
slouken@1933
  1515
    }
slouken@1933
  1516
}
slouken@1933
  1517
slouken@7968
  1518
SDL_bool
slouken@7969
  1519
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1520
{
slouken@7969
  1521
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1522
slouken@7969
  1523
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1524
        return SDL_TRUE;
slouken@7969
  1525
    } else {
slouken@7969
  1526
        return SDL_FALSE;
slouken@7969
  1527
    }
slouken@7969
  1528
}
slouken@7969
  1529
slouken@7969
  1530
SDL_bool
slouken@7968
  1531
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slouken@7968
  1532
{
slouken@7968
  1533
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1534
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7968
  1535
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1536
slouken@7968
  1537
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@7968
  1538
        succeeded = SDL_TRUE;
slouken@7968
  1539
    }
slouken@7968
  1540
slouken@7968
  1541
    [pool release];
slouken@7968
  1542
slouken@7968
  1543
    return succeeded;
slouken@7968
  1544
}
slouken@7968
  1545
slouken@6044
  1546
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1547
slouken@1933
  1548
/* vi: set ts=4 sw=4 expandtab: */