src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Tue, 21 Apr 2015 10:10:59 -0400
changeset 10028 da668b612d10
parent 10025 bf4f8cde1c54
child 10140 6dd5f49e4a77
permissions -rw-r--r--
Added SDL_WINDOWEVENT_HIT_TEST.

This lets windows know when they are dropping a mouse event because their
hit test reported something other than SDL_HITTEST_NORMAL. It lets them know
exactly where in the event queue this happened.

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