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