src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 Mar 2014 12:45:51 -0500
changeset 8284 a2910aa6c056
parent 8261 841b66e4397a
child 8288 4e3afa79b8d8
permissions -rw-r--r--
Reworked fullscreen policy on Mac OS X.

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