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