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