src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Mon, 03 Mar 2014 21:25:16 -0500
changeset 8295 56f7e38ff0bc
parent 8294 7371078ee876
child 8618 06ee748d59b2
permissions -rw-r--r--
Mac: Added a hint to opt-out of new Spaces code.
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@7963
   590
    if (pendingWindowOperation == PENDING_OPERATION_ENTER_FULLSCREEN) {
slouken@7963
   591
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7967
   592
        [self setFullscreenSpace:YES];
slouken@7963
   593
    } else if (pendingWindowOperation == PENDING_OPERATION_MINIMIZE) {
slouken@7963
   594
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7963
   595
        [nswindow miniaturize:nil];
slouken@7961
   596
    } else {
icculus@8291
   597
        /* Adjust the fullscreen toggle button and readd menu now that we're here. */
icculus@8291
   598
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8291
   599
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
icculus@8291
   600
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
icculus@8291
   601
        } else {
icculus@8284
   602
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorManaged];
icculus@8284
   603
        }
icculus@8291
   604
        [NSMenu setMenuBarVisible:YES];
icculus@8284
   605
slouken@7963
   606
        pendingWindowOperation = PENDING_OPERATION_NONE;
slouken@7965
   607
        /* Force the size change event in case it was delivered earlier
slouken@7965
   608
           while the window was still animating into place.
slouken@7965
   609
         */
slouken@7965
   610
        window->w = 0;
slouken@7965
   611
        window->h = 0;
slouken@7961
   612
        [self windowDidResize:aNotification];
slouken@7961
   613
    }
slouken@7952
   614
}
slouken@7952
   615
icculus@8291
   616
-(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
icculus@8291
   617
{
icculus@8291
   618
    if ((_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
icculus@8291
   619
        return NSApplicationPresentationFullScreen | NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
icculus@8291
   620
    } else {
icculus@8291
   621
        return proposedOptions;
icculus@8291
   622
    }
icculus@8291
   623
}
icculus@8291
   624
icculus@8291
   625
slouken@7191
   626
/* We'll respond to key events by doing nothing so we don't beep.
slouken@7191
   627
 * We could handle key messages here, but we lose some in the NSApp dispatch,
slouken@7191
   628
 * where they get converted to action messages, etc.
slouken@7191
   629
 */
slouken@6514
   630
- (void)flagsChanged:(NSEvent *)theEvent
slouken@6514
   631
{
slouken@7191
   632
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   633
}
slouken@6514
   634
- (void)keyDown:(NSEvent *)theEvent
slouken@6514
   635
{
slouken@7191
   636
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   637
}
slouken@6514
   638
- (void)keyUp:(NSEvent *)theEvent
slouken@6514
   639
{
slouken@7191
   640
    /*Cocoa_HandleKeyEvent(SDL_GetVideoDevice(), theEvent);*/
slouken@6514
   641
}
slouken@6514
   642
slouken@7191
   643
/* We'll respond to selectors by doing nothing so we don't beep.
slouken@7191
   644
 * The escape key gets converted to a "cancel" selector, etc.
slouken@7191
   645
 */
slouken@6514
   646
- (void)doCommandBySelector:(SEL)aSelector
slouken@6514
   647
{
slouken@7191
   648
    /*NSLog(@"doCommandBySelector: %@\n", NSStringFromSelector(aSelector));*/
slouken@6514
   649
}
slouken@6514
   650
slouken@1933
   651
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   652
{
slouken@1959
   653
    int button;
slouken@1933
   654
slouken@1959
   655
    switch ([theEvent buttonNumber]) {
slouken@1959
   656
    case 0:
slouken@7915
   657
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   658
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   659
            wasCtrlLeft = YES;
slouken@7740
   660
            button = SDL_BUTTON_RIGHT;
slouken@7740
   661
        } else {
slouken@7740
   662
            wasCtrlLeft = NO;
slouken@7740
   663
            button = SDL_BUTTON_LEFT;
slouken@7740
   664
        }
slouken@1959
   665
        break;
slouken@1959
   666
    case 1:
slouken@1959
   667
        button = SDL_BUTTON_RIGHT;
slouken@1959
   668
        break;
slouken@1959
   669
    case 2:
slouken@1959
   670
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   671
        break;
slouken@1959
   672
    default:
slouken@5061
   673
        button = [theEvent buttonNumber] + 1;
slouken@1959
   674
        break;
slouken@1959
   675
    }
slouken@6950
   676
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   677
}
slouken@1933
   678
slouken@1933
   679
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   680
{
slouken@1959
   681
    [self mouseDown:theEvent];
slouken@1933
   682
}
slouken@1933
   683
slouken@1933
   684
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   685
{
slouken@1959
   686
    [self mouseDown:theEvent];
slouken@1933
   687
}
slouken@1933
   688
slouken@1933
   689
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   690
{
slouken@1959
   691
    int button;
slouken@1933
   692
slouken@1959
   693
    switch ([theEvent buttonNumber]) {
slouken@1959
   694
    case 0:
slouken@7740
   695
        if (wasCtrlLeft) {
slouken@7740
   696
            button = SDL_BUTTON_RIGHT;
slouken@7740
   697
            wasCtrlLeft = NO;
slouken@7740
   698
        } else {
slouken@7740
   699
            button = SDL_BUTTON_LEFT;
slouken@7740
   700
        }
slouken@1959
   701
        break;
slouken@1959
   702
    case 1:
slouken@1959
   703
        button = SDL_BUTTON_RIGHT;
slouken@1959
   704
        break;
slouken@1959
   705
    case 2:
slouken@1959
   706
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   707
        break;
slouken@1959
   708
    default:
slouken@5061
   709
        button = [theEvent buttonNumber] + 1;
slouken@1959
   710
        break;
slouken@1959
   711
    }
slouken@6950
   712
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   713
}
slouken@1933
   714
slouken@1933
   715
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   716
{
slouken@1959
   717
    [self mouseUp:theEvent];
slouken@1933
   718
}
slouken@1933
   719
slouken@1933
   720
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   721
{
slouken@1959
   722
    [self mouseUp:theEvent];
slouken@1933
   723
}
slouken@1933
   724
slouken@1933
   725
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   726
{
slouken@5406
   727
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   728
    SDL_Window *window = _data->window;
slouken@5396
   729
    NSPoint point;
slouken@5396
   730
    int x, y;
slouken@1933
   731
slouken@5406
   732
    if (mouse->relative_mode) {
gzjjgod@5059
   733
        return;
slouken@5371
   734
    }
gzjjgod@5059
   735
slouken@5396
   736
    point = [theEvent locationInWindow];
slouken@5396
   737
    x = (int)point.x;
slouken@5396
   738
    y = (int)(window->h - point.y);
slouken@5371
   739
slouken@5396
   740
    if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   741
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@6666
   742
            if (x < 0) {
slouken@6666
   743
                x = 0;
slouken@6666
   744
            } else if (x >= window->w) {
slouken@6666
   745
                x = window->w - 1;
slouken@6666
   746
            }
slouken@6666
   747
            if (y < 0) {
slouken@6666
   748
                y = 0;
slouken@6666
   749
            } else if (y >= window->h) {
slouken@6666
   750
                y = window->h - 1;
slouken@6666
   751
            }
slouken@6666
   752
jorgen@7593
   753
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   754
            CGPoint cgpoint;
jorgen@8260
   755
jorgen@7593
   756
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   757
             * SDL_cocoamousetap.m.
jorgen@7593
   758
             */
jorgen@7593
   759
slouken@6666
   760
            cgpoint.x = window->x + x;
slouken@6666
   761
            cgpoint.y = window->y + y;
jorgen@7098
   762
jorgen@7113
   763
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   764
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   765
             * sure how we'd procure the right one for this event.
jorgen@7098
   766
             */
jorgen@7113
   767
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   768
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   769
            CGSetLocalEventsSuppressionInterval(0.25);
jorgen@8261
   770
jorgen@8261
   771
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   772
#endif
slouken@5396
   773
        }
slouken@2059
   774
    }
slouken@6950
   775
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   776
}
slouken@1933
   777
slouken@1957
   778
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   779
{
slouken@1957
   780
    [self mouseMoved:theEvent];
slouken@1957
   781
}
slouken@1957
   782
slouken@1958
   783
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   784
{
slouken@1958
   785
    [self mouseMoved:theEvent];
slouken@1958
   786
}
slouken@1958
   787
slouken@1958
   788
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   789
{
slouken@1958
   790
    [self mouseMoved:theEvent];
slouken@1958
   791
}
slouken@1958
   792
slouken@1933
   793
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   794
{
gzjjgod@5057
   795
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
   796
}
slouken@3688
   797
slouken@4673
   798
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   799
{
slouken@4673
   800
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   801
}
slouken@4673
   802
slouken@4673
   803
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   804
{
slouken@4673
   805
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   806
}
slouken@4673
   807
slouken@4673
   808
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   809
{
slouken@4673
   810
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   811
}
slouken@4673
   812
slouken@4673
   813
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   814
{
slouken@4673
   815
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   816
}
slouken@4673
   817
slouken@4673
   818
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   819
{
slouken@4680
   820
    NSSet *touches = 0;
slouken@4680
   821
    NSEnumerator *enumerator;
slouken@4680
   822
    NSTouch *touch;
slouken@4673
   823
slouken@4680
   824
    switch (type) {
slouken@4680
   825
        case COCOA_TOUCH_DOWN:
slouken@4680
   826
            touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4680
   827
            break;
slouken@4680
   828
        case COCOA_TOUCH_UP:
jorgen@8106
   829
            touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
jorgen@8106
   830
            break;
slouken@4680
   831
        case COCOA_TOUCH_CANCELLED:
jorgen@8106
   832
            touches = [event touchesMatchingPhase:NSTouchPhaseCancelled inView:nil];
slouken@4680
   833
            break;
slouken@4680
   834
        case COCOA_TOUCH_MOVE:
slouken@4680
   835
            touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
slouken@4680
   836
            break;
slouken@4680
   837
    }
slouken@4680
   838
slouken@4680
   839
    enumerator = [touches objectEnumerator];
slouken@4680
   840
    touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   841
    while (touch) {
slouken@6953
   842
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@4673
   843
        if (!SDL_GetTouch(touchId)) {
slouken@6951
   844
            if (SDL_AddTouch(touchId, "") < 0) {
slouken@4680
   845
                return;
slouken@4673
   846
            }
slouken@7191
   847
        }
slouken@4687
   848
slouken@6953
   849
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
   850
        float x = [touch normalizedPosition].x;
slouken@4673
   851
        float y = [touch normalizedPosition].y;
slouken@5261
   852
        /* Make the origin the upper left instead of the lower left */
slouken@5261
   853
        y = 1.0f - y;
slouken@4687
   854
slouken@4673
   855
        switch (type) {
slouken@4673
   856
        case COCOA_TOUCH_DOWN:
slouken@6951
   857
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
   858
            break;
slouken@4673
   859
        case COCOA_TOUCH_UP:
slouken@4673
   860
        case COCOA_TOUCH_CANCELLED:
slouken@6951
   861
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
   862
            break;
slouken@4673
   863
        case COCOA_TOUCH_MOVE:
slouken@6951
   864
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
   865
            break;
slouken@4673
   866
        }
slouken@6951
   867
slouken@4673
   868
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   869
    }
slouken@1933
   870
}
slouken@1933
   871
slouken@1933
   872
@end
slouken@1933
   873
slouken@5379
   874
@interface SDLView : NSView
jorgen@7158
   875
slouken@5379
   876
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
   877
- (void)rightMouseDown:(NSEvent *)theEvent;
gzjjgod@4915
   878
@end
gzjjgod@4915
   879
gzjjgod@4915
   880
@implementation SDLView
gzjjgod@4915
   881
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
   882
{
slouken@5371
   883
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
   884
}
jorgen@7158
   885
jorgen@7158
   886
- (void)resetCursorRects
jorgen@7158
   887
{
jorgen@7158
   888
    [super resetCursorRects];
jorgen@7158
   889
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
   890
jorgen@7270
   891
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
   892
        [self addCursorRect:[self bounds]
jorgen@7158
   893
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
   894
    } else {
jorgen@7158
   895
        [self addCursorRect:[self bounds]
jorgen@7158
   896
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
   897
    }
jorgen@7158
   898
}
gzjjgod@4915
   899
@end
gzjjgod@4915
   900
slouken@1933
   901
static int
slouken@1951
   902
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   903
{
slouken@6848
   904
    NSAutoreleasePool *pool;
slouken@1951
   905
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
   906
    SDL_WindowData *data;
slouken@1933
   907
slouken@1933
   908
    /* Allocate the window data */
slouken@5249
   909
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
   910
    if (!data) {
icculus@7037
   911
        return SDL_OutOfMemory();
slouken@1933
   912
    }
slouken@3685
   913
    data->window = window;
slouken@3688
   914
    data->nswindow = nswindow;
slouken@1933
   915
    data->created = created;
slouken@1951
   916
    data->videodata = videodata;
jorgen@7595
   917
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
   918
slouken@6848
   919
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   920
slouken@6848
   921
    /* Create an event listener for the window */
slouken@6848
   922
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
   923
slouken@6848
   924
    /* Fill in the SDL window with the window data */
slouken@6848
   925
    {
slouken@6848
   926
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
   927
        ConvertNSRect(&rect);
slouken@6848
   928
        window->x = (int)rect.origin.x;
slouken@6848
   929
        window->y = (int)rect.origin.y;
slouken@6848
   930
        window->w = (int)rect.size.width;
slouken@6848
   931
        window->h = (int)rect.size.height;
slouken@6848
   932
    }
slouken@6848
   933
slouken@6848
   934
    /* Set up the listener after we create the view */
slouken@6848
   935
    [data->listener listen:data];
slouken@6848
   936
slouken@6848
   937
    if ([nswindow isVisible]) {
slouken@6848
   938
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
   939
    } else {
slouken@6848
   940
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
   941
    }
jorgen@7084
   942
slouken@6848
   943
    {
slouken@6848
   944
        unsigned int style = [nswindow styleMask];
slouken@6848
   945
slouken@6848
   946
        if (style == NSBorderlessWindowMask) {
slouken@6848
   947
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
   948
        } else {
slouken@6848
   949
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
   950
        }
slouken@6848
   951
        if (style & NSResizableWindowMask) {
slouken@6848
   952
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
   953
        } else {
slouken@6848
   954
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
   955
        }
slouken@6848
   956
    }
jorgen@7084
   957
slouken@6848
   958
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
   959
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
   960
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
   961
    } else {
slouken@6848
   962
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
   963
    }
jorgen@7084
   964
slouken@6848
   965
    if ([nswindow isMiniaturized]) {
slouken@6848
   966
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
   967
    } else {
slouken@6848
   968
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
   969
    }
jorgen@7084
   970
slouken@6848
   971
    if ([nswindow isKeyWindow]) {
slouken@6848
   972
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
   973
        SDL_SetKeyboardFocus(data->window);
slouken@6848
   974
    }
slouken@1933
   975
jorgen@7085
   976
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
   977
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
   978
     */
jorgen@7085
   979
    [nswindow setOneShot:NO];
jorgen@7085
   980
slouken@6848
   981
    /* All done! */
slouken@6848
   982
    [pool release];
slouken@6848
   983
    window->driverdata = data;
slouken@6848
   984
    return 0;
slouken@1933
   985
}
slouken@1933
   986
slouken@1933
   987
int
slouken@1933
   988
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   989
{
icculus@8295
   990
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
   991
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   992
    NSWindow *nswindow;
slouken@6848
   993
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
   994
    NSRect rect;
slouken@6848
   995
    SDL_Rect bounds;
slouken@6848
   996
    unsigned int style;
slouken@1933
   997
slouken@6848
   998
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
   999
    rect.origin.x = window->x;
slouken@6848
  1000
    rect.origin.y = window->y;
slouken@6848
  1001
    rect.size.width = window->w;
slouken@6848
  1002
    rect.size.height = window->h;
slouken@6848
  1003
    ConvertNSRect(&rect);
slouken@1933
  1004
slouken@6848
  1005
    style = GetWindowStyle(window);
slouken@1933
  1006
slouken@6848
  1007
    /* Figure out which screen to place this window */
slouken@6848
  1008
    NSArray *screens = [NSScreen screens];
slouken@6848
  1009
    NSScreen *screen = nil;
slouken@6848
  1010
    NSScreen *candidate;
slouken@6848
  1011
    int i, count = [screens count];
slouken@6848
  1012
    for (i = 0; i < count; ++i) {
slouken@6848
  1013
        candidate = [screens objectAtIndex:i];
slouken@6848
  1014
        NSRect screenRect = [candidate frame];
slouken@6848
  1015
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1016
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1017
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1018
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1019
            screen = candidate;
slouken@6848
  1020
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1021
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1022
        }
slouken@6848
  1023
    }
slouken@7946
  1024
slouken@7946
  1025
    @try {
slouken@7946
  1026
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1027
    }
slouken@7946
  1028
    @catch (NSException *e) {
slouken@7946
  1029
        SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7948
  1030
        [pool release];
slouken@7946
  1031
        return -1;
slouken@7946
  1032
    }
icculus@7205
  1033
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1034
icculus@8295
  1035
    if (videodata->allow_spaces) {
icculus@8295
  1036
        SDL_assert(videodata->osversion >= 0x1070);
icculus@8295
  1037
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1038
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1039
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1040
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1041
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1042
        }
slouken@7955
  1043
    }
alexey@6832
  1044
slouken@7191
  1045
    /* Create a default view for this window */
slouken@6848
  1046
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@6848
  1047
    NSView *contentView = [[SDLView alloc] initWithFrame:rect];
urkle@7746
  1048
slouken@7955
  1049
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1050
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1051
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1052
        }
urkle@7746
  1053
    }
urkle@7746
  1054
slouken@6848
  1055
    [nswindow setContentView: contentView];
slouken@6848
  1056
    [contentView release];
alexey@6832
  1057
slouken@6848
  1058
    [pool release];
slouken@6848
  1059
slouken@6848
  1060
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1061
        [nswindow release];
slouken@6848
  1062
        return -1;
slouken@3506
  1063
    }
slouken@6848
  1064
    return 0;
slouken@1933
  1065
}
slouken@1933
  1066
slouken@1933
  1067
int
slouken@1933
  1068
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
  1069
{
slouken@6848
  1070
    NSAutoreleasePool *pool;
slouken@1933
  1071
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1072
    NSString *title;
slouken@1933
  1073
slouken@6848
  1074
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1075
slouken@6848
  1076
    /* Query the title from the existing window */
slouken@6848
  1077
    title = [nswindow title];
slouken@6848
  1078
    if (title) {
slouken@6848
  1079
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1080
    }
slouken@1933
  1081
slouken@6848
  1082
    [pool release];
slouken@6848
  1083
slouken@1951
  1084
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
  1085
}
slouken@1933
  1086
slouken@1933
  1087
void
slouken@1933
  1088
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
  1089
{
slouken@6848
  1090
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1091
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1092
    NSString *string;
slouken@1933
  1093
slouken@6848
  1094
    if(window->title) {
slouken@6848
  1095
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@6848
  1096
    } else {
slouken@6848
  1097
        string = [[NSString alloc] init];
slouken@1956
  1098
    }
slouken@6848
  1099
    [nswindow setTitle:string];
slouken@6848
  1100
    [string release];
slouken@6848
  1101
slouken@6848
  1102
    [pool release];
slouken@1933
  1103
}
slouken@1933
  1104
slouken@1933
  1105
void
slouken@5375
  1106
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@5375
  1107
{
slouken@6848
  1108
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1109
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1110
slouken@6848
  1111
    if (nsimage) {
slouken@6848
  1112
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1113
    }
slouken@6848
  1114
slouken@6848
  1115
    [pool release];
slouken@5375
  1116
}
slouken@5375
  1117
slouken@5375
  1118
void
slouken@1933
  1119
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
  1120
{
slouken@6848
  1121
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7594
  1122
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1123
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1124
    NSRect rect;
slouken@6848
  1125
    Uint32 moveHack;
slouken@1933
  1126
slouken@6848
  1127
    rect.origin.x = window->x;
slouken@6848
  1128
    rect.origin.y = window->y;
slouken@6848
  1129
    rect.size.width = window->w;
slouken@6848
  1130
    rect.size.height = window->h;
slouken@6848
  1131
    ConvertNSRect(&rect);
slouken@5478
  1132
slouken@6848
  1133
    moveHack = s_moveHack;
slouken@6848
  1134
    s_moveHack = 0;
slouken@6848
  1135
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1136
    s_moveHack = moveHack;
slouken@5478
  1137
jorgen@7595
  1138
    ScheduleContextUpdates(windata);
slouken@6848
  1139
slouken@6848
  1140
    [pool release];
slouken@1933
  1141
}
slouken@1933
  1142
slouken@1933
  1143
void
slouken@1933
  1144
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
  1145
{
slouken@6848
  1146
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1147
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1148
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1149
    NSSize size;
slouken@1933
  1150
slouken@6848
  1151
    size.width = window->w;
slouken@6848
  1152
    size.height = window->h;
slouken@6848
  1153
    [nswindow setContentSize:size];
icculus@5564
  1154
jorgen@7595
  1155
    ScheduleContextUpdates(windata);
slouken@6848
  1156
slouken@6848
  1157
    [pool release];
slouken@1933
  1158
}
slouken@1933
  1159
slouken@1933
  1160
void
stopiccot@6681
  1161
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
stopiccot@6681
  1162
{
slouken@6848
  1163
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1164
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1165
slouken@6848
  1166
    NSSize minSize;
slouken@6848
  1167
    minSize.width = window->min_w;
slouken@6848
  1168
    minSize.height = window->min_h;
slouken@7191
  1169
slouken@6848
  1170
    [windata->nswindow setContentMinSize:minSize];
slouken@7191
  1171
slouken@6848
  1172
    [pool release];
slouken@6788
  1173
}
slouken@6788
  1174
slouken@6788
  1175
void
slouken@6788
  1176
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@6788
  1177
{
slouken@6848
  1178
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1179
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1180
slouken@6848
  1181
    NSSize maxSize;
slouken@6848
  1182
    maxSize.width = window->max_w;
slouken@6848
  1183
    maxSize.height = window->max_h;
slouken@7191
  1184
slouken@6848
  1185
    [windata->nswindow setContentMaxSize:maxSize];
slouken@7191
  1186
slouken@6848
  1187
    [pool release];
stopiccot@6681
  1188
}
stopiccot@6681
  1189
stopiccot@6681
  1190
void
slouken@1933
  1191
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
  1192
{
slouken@6848
  1193
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
  1194
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1195
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1196
slouken@6848
  1197
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1198
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1199
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1200
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1201
    }
slouken@6848
  1202
    [pool release];
slouken@1933
  1203
}
slouken@1933
  1204
slouken@1933
  1205
void
slouken@1933
  1206
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
  1207
{
slouken@6848
  1208
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1209
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1210
slouken@6848
  1211
    [nswindow orderOut:nil];
slouken@6848
  1212
    [pool release];
slouken@1933
  1213
}
slouken@1933
  1214
slouken@1933
  1215
void
slouken@1933
  1216
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
  1217
{
slouken@6848
  1218
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7087
  1219
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1220
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1221
slouken@7771
  1222
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1223
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1224
     */
jorgen@7087
  1225
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1226
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
jorgen@7469
  1227
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1228
    }
jorgen@7087
  1229
    [windowData->listener resumeVisibleObservation];
jorgen@7087
  1230
slouken@6848
  1231
    [pool release];
slouken@1933
  1232
}
slouken@1933
  1233
slouken@1933
  1234
void
slouken@1933
  1235
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
  1236
{
slouken@6848
  1237
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
jorgen@7594
  1238
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1239
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1240
slouken@6848
  1241
    [nswindow zoom:nil];
slouken@6848
  1242
jorgen@7595
  1243
    ScheduleContextUpdates(windata);
slouken@6848
  1244
slouken@6848
  1245
    [pool release];
slouken@1933
  1246
}
slouken@1933
  1247
slouken@1933
  1248
void
slouken@1933
  1249
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
  1250
{
slouken@6848
  1251
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7963
  1252
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1253
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1254
slouken@7968
  1255
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1256
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1257
    } else {
slouken@7963
  1258
        [nswindow miniaturize:nil];
slouken@7963
  1259
    }
slouken@6848
  1260
    [pool release];
slouken@1933
  1261
}
slouken@1933
  1262
slouken@1933
  1263
void
slouken@1933
  1264
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
  1265
{
slouken@6848
  1266
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1267
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1268
slouken@6848
  1269
    if ([nswindow isMiniaturized]) {
slouken@6848
  1270
        [nswindow deminiaturize:nil];
slouken@6848
  1271
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1272
        [nswindow zoom:nil];
slouken@1956
  1273
    }
slouken@6848
  1274
    [pool release];
slouken@1933
  1275
}
slouken@1933
  1276
slouken@5400
  1277
static NSWindow *
slouken@5400
  1278
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
  1279
{
slouken@5400
  1280
    if (!data->created) {
slouken@5400
  1281
        /* Don't mess with other people's windows... */
slouken@5400
  1282
        return nswindow;
slouken@5400
  1283
    }
slouken@5400
  1284
slouken@5400
  1285
    [data->listener close];
jorgen@7085
  1286
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
  1287
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@7085
  1288
    /* See comment in SetupWindowData. */
jorgen@7085
  1289
    [data->nswindow setOneShot:NO];
slouken@5400
  1290
    [data->listener listen:data];
slouken@5400
  1291
slouken@5400
  1292
    [nswindow close];
slouken@5400
  1293
slouken@5400
  1294
    return data->nswindow;
slouken@5400
  1295
}
slouken@5400
  1296
slouken@1933
  1297
void
icculus@6422
  1298
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
  1299
{
slouken@6848
  1300
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7990
  1301
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@6848
  1302
        if (bordered) {
slouken@7191
  1303
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1304
        }
icculus@6422
  1305
    }
slouken@6848
  1306
    [pool release];
icculus@6422
  1307
}
icculus@6422
  1308
slouken@7952
  1309
slouken@7968
  1310
void
slouken@7968
  1311
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@7952
  1312
{
slouken@7968
  1313
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1314
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1315
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1316
    NSRect rect;
slouken@5249
  1317
slouken@6848
  1318
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1319
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1320
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1321
    }
slouken@6848
  1322
slouken@6848
  1323
    if (fullscreen) {
slouken@6848
  1324
        SDL_Rect bounds;
slouken@6848
  1325
slouken@6848
  1326
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1327
        rect.origin.x = bounds.x;
slouken@6848
  1328
        rect.origin.y = bounds.y;
slouken@6848
  1329
        rect.size.width = bounds.w;
slouken@6848
  1330
        rect.size.height = bounds.h;
slouken@6848
  1331
        ConvertNSRect(&rect);
slouken@6848
  1332
slouken@6848
  1333
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1334
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1335
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1336
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1337
        }
slouken@5401
  1338
slouken@6848
  1339
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1340
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
  1341
        } else {
slouken@6848
  1342
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
  1343
        }
slouken@6848
  1344
    } else {
slouken@6848
  1345
        rect.origin.x = window->windowed.x;
slouken@6848
  1346
        rect.origin.y = window->windowed.y;
slouken@6848
  1347
        rect.size.width = window->windowed.w;
slouken@6848
  1348
        rect.size.height = window->windowed.h;
slouken@6848
  1349
        ConvertNSRect(&rect);
alexey@6832
  1350
slouken@6848
  1351
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1352
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@5400
  1353
        } else {
slouken@6848
  1354
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1355
        }
slouken@5398
  1356
    }
slouken@6848
  1357
slouken@6848
  1358
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1359
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1360
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1361
    }
slouken@6848
  1362
slouken@6848
  1363
    s_moveHack = 0;
slouken@7873
  1364
    [nswindow setContentSize:rect.size];
slouken@6848
  1365
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1366
    s_moveHack = SDL_GetTicks();
slouken@6848
  1367
slouken@6848
  1368
    /* When the window style changes the title is cleared */
slouken@6848
  1369
    if (!fullscreen) {
slouken@6848
  1370
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1371
    }
slouken@6848
  1372
slouken@6848
  1373
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1374
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1375
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1376
    } else {
slouken@6848
  1377
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1378
    }
jorgen@7087
  1379
jorgen@7636
  1380
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1381
        [data->listener pauseVisibleObservation];
jorgen@7636
  1382
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1383
        [data->listener resumeVisibleObservation];
jorgen@7636
  1384
    }
slouken@6848
  1385
jorgen@7595
  1386
    ScheduleContextUpdates(data);
slouken@6848
  1387
slouken@6848
  1388
    [pool release];
slouken@5249
  1389
}
slouken@5249
  1390
slouken@5466
  1391
int
slouken@5466
  1392
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1393
{
slouken@5466
  1394
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1395
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1396
    const uint32_t tableSize = 256;
slouken@5466
  1397
    CGGammaValue redTable[tableSize];
slouken@5466
  1398
    CGGammaValue greenTable[tableSize];
slouken@5466
  1399
    CGGammaValue blueTable[tableSize];
slouken@5466
  1400
    uint32_t i;
slouken@5466
  1401
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1402
slouken@5466
  1403
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1404
    for (i = 0; i < 256; i++) {
slouken@5466
  1405
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1406
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1407
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1408
    }
slouken@5466
  1409
slouken@5466
  1410
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1411
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1412
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1413
    }
slouken@5466
  1414
    return 0;
slouken@5466
  1415
}
slouken@5466
  1416
slouken@5466
  1417
int
slouken@5466
  1418
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1419
{
slouken@5466
  1420
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1421
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1422
    const uint32_t tableSize = 256;
slouken@5466
  1423
    CGGammaValue redTable[tableSize];
slouken@5466
  1424
    CGGammaValue greenTable[tableSize];
slouken@5466
  1425
    CGGammaValue blueTable[tableSize];
slouken@5466
  1426
    uint32_t i, tableCopied;
slouken@5466
  1427
slouken@5466
  1428
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1429
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1430
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1431
    }
slouken@5466
  1432
slouken@5466
  1433
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1434
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1435
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1436
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1437
    }
slouken@5466
  1438
    return 0;
slouken@5466
  1439
}
slouken@5466
  1440
slouken@5249
  1441
void
slouken@6662
  1442
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1443
{
slouken@5371
  1444
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1445
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1446
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1447
        int x, y;
slouken@5371
  1448
        CGPoint cgpoint;
slouken@5371
  1449
slouken@5371
  1450
        SDL_GetMouseState(&x, &y);
slouken@5371
  1451
        cgpoint.x = window->x + x;
slouken@5371
  1452
        cgpoint.y = window->y + y;
jorgen@8261
  1453
jorgen@8261
  1454
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1455
jorgen@8261
  1456
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1457
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1458
    }
slouken@7191
  1459
slouken@6784
  1460
    if ( window->flags & SDL_WINDOW_FULLSCREEN ) {
slouken@7191
  1461
        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6755
  1462
slouken@7191
  1463
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
  1464
            /* OpenGL is rendering to the window, so make it visible! */
slouken@7191
  1465
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1466
        } else {
slouken@7191
  1467
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1468
        }
slouken@7191
  1469
    }
slouken@1933
  1470
}
slouken@1933
  1471
slouken@1933
  1472
void
slouken@1933
  1473
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
  1474
{
slouken@6848
  1475
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
  1476
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1477
slouken@6848
  1478
    if (data) {
slouken@6848
  1479
        [data->listener close];
slouken@6848
  1480
        [data->listener release];
slouken@6848
  1481
        if (data->created) {
slouken@6848
  1482
            [data->nswindow close];
slouken@1933
  1483
        }
jorgen@7595
  1484
jorgen@7595
  1485
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1486
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1487
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1488
            [context setWindow:NULL];
jorgen@7595
  1489
        }
jorgen@7595
  1490
        [data->nscontexts release];
jorgen@7595
  1491
slouken@6848
  1492
        SDL_free(data);
slouken@1933
  1493
    }
slouken@6848
  1494
    [pool release];
slouken@1933
  1495
}
slouken@1933
  1496
slouken@1933
  1497
SDL_bool
slouken@1933
  1498
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1499
{
slouken@4900
  1500
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1501
slouken@1933
  1502
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1503
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1504
        info->info.cocoa.window = nswindow;
slouken@1933
  1505
        return SDL_TRUE;
slouken@1933
  1506
    } else {
slouken@1933
  1507
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1508
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1509
        return SDL_FALSE;
slouken@1933
  1510
    }
slouken@1933
  1511
}
slouken@1933
  1512
slouken@7968
  1513
SDL_bool
slouken@7969
  1514
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1515
{
slouken@7969
  1516
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1517
slouken@7969
  1518
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1519
        return SDL_TRUE;
slouken@7969
  1520
    } else {
slouken@7969
  1521
        return SDL_FALSE;
slouken@7969
  1522
    }
slouken@7969
  1523
}
slouken@7969
  1524
slouken@7969
  1525
SDL_bool
slouken@7968
  1526
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slouken@7968
  1527
{
slouken@7968
  1528
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1529
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@7968
  1530
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1531
slouken@7968
  1532
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@7968
  1533
        succeeded = SDL_TRUE;
slouken@7968
  1534
    }
slouken@7968
  1535
slouken@7968
  1536
    [pool release];
slouken@7968
  1537
slouken@7968
  1538
    return succeeded;
slouken@7968
  1539
}
slouken@7968
  1540
slouken@6044
  1541
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1542
slouken@1933
  1543
/* vi: set ts=4 sw=4 expandtab: */