src/video/cocoa/SDL_cocoawindow.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 26 Feb 2014 11:35:02 -0800
changeset 8261 841b66e4397a
parent 8260 028ed8da2524
child 8284 a2910aa6c056
permissions -rw-r--r--
Mac: Redo cursor warp handling.

This fixes bugs related to getting unnaturally large xrel/yrel for
SDL_MOUSEMOTION after warps and enabling / disabling relative mode.

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