src/video/cocoa/SDL_cocoawindow.m
author Ryan C. Gordon <icculus@icculus.org>
Tue, 05 Jan 2016 01:42:00 -0500
changeset 10022 30807689ca1b
parent 10010 cc07869515b2
child 10025 bf4f8cde1c54
permissions -rw-r--r--
Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.

This allows an app to know when a set of drops are coming in a grouping of
some sort (for example, a user selected multiple files and dropped them all
on the window with a single drag), and when that set is complete.

This also adds a window ID to the drop events, so the app can determine to
which window a given drop was delivered. For application-level drops (for
example, you launched an app by dropping a file on its icon), the window ID
will be zero.
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@8931
   855
        return;  /* dragging, drop event. */
icculus@8931
   856
    }
icculus@8931
   857
slouken@1959
   858
    switch ([theEvent buttonNumber]) {
slouken@1959
   859
    case 0:
slouken@7915
   860
        if (([theEvent modifierFlags] & NSControlKeyMask) &&
slouken@7915
   861
		    GetHintCtrlClickEmulateRightClick()) {
slouken@7740
   862
            wasCtrlLeft = YES;
slouken@7740
   863
            button = SDL_BUTTON_RIGHT;
slouken@7740
   864
        } else {
slouken@7740
   865
            wasCtrlLeft = NO;
slouken@7740
   866
            button = SDL_BUTTON_LEFT;
slouken@7740
   867
        }
slouken@1959
   868
        break;
slouken@1959
   869
    case 1:
slouken@1959
   870
        button = SDL_BUTTON_RIGHT;
slouken@1959
   871
        break;
slouken@1959
   872
    case 2:
slouken@1959
   873
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   874
        break;
slouken@1959
   875
    default:
slouken@5061
   876
        button = [theEvent buttonNumber] + 1;
slouken@1959
   877
        break;
slouken@1959
   878
    }
slouken@6950
   879
    SDL_SendMouseButton(_data->window, 0, SDL_PRESSED, button);
slouken@1933
   880
}
slouken@1933
   881
slouken@1933
   882
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   883
{
slouken@1959
   884
    [self mouseDown:theEvent];
slouken@1933
   885
}
slouken@1933
   886
slouken@1933
   887
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   888
{
slouken@1959
   889
    [self mouseDown:theEvent];
slouken@1933
   890
}
slouken@1933
   891
slouken@1933
   892
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   893
{
slouken@1959
   894
    int button;
slouken@1933
   895
icculus@8935
   896
    if ([self processHitTest:theEvent]) {
icculus@8931
   897
        return;  /* stopped dragging, drop event. */
icculus@8931
   898
    }
icculus@8931
   899
slouken@1959
   900
    switch ([theEvent buttonNumber]) {
slouken@1959
   901
    case 0:
slouken@7740
   902
        if (wasCtrlLeft) {
slouken@7740
   903
            button = SDL_BUTTON_RIGHT;
slouken@7740
   904
            wasCtrlLeft = NO;
slouken@7740
   905
        } else {
slouken@7740
   906
            button = SDL_BUTTON_LEFT;
slouken@7740
   907
        }
slouken@1959
   908
        break;
slouken@1959
   909
    case 1:
slouken@1959
   910
        button = SDL_BUTTON_RIGHT;
slouken@1959
   911
        break;
slouken@1959
   912
    case 2:
slouken@1959
   913
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   914
        break;
slouken@1959
   915
    default:
slouken@5061
   916
        button = [theEvent buttonNumber] + 1;
slouken@1959
   917
        break;
slouken@1959
   918
    }
slouken@6950
   919
    SDL_SendMouseButton(_data->window, 0, SDL_RELEASED, button);
slouken@1933
   920
}
slouken@1933
   921
slouken@1933
   922
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   923
{
slouken@1959
   924
    [self mouseUp:theEvent];
slouken@1933
   925
}
slouken@1933
   926
slouken@1933
   927
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   928
{
slouken@1959
   929
    [self mouseUp:theEvent];
slouken@1933
   930
}
slouken@1933
   931
slouken@1933
   932
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   933
{
slouken@5406
   934
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@3685
   935
    SDL_Window *window = _data->window;
slouken@5396
   936
    NSPoint point;
slouken@5396
   937
    int x, y;
slouken@1933
   938
icculus@8935
   939
    if ([self processHitTest:theEvent]) {
icculus@8931
   940
        return;  /* dragging, drop event. */
icculus@8931
   941
    }
icculus@8931
   942
slouken@5406
   943
    if (mouse->relative_mode) {
gzjjgod@5059
   944
        return;
slouken@5371
   945
    }
gzjjgod@5059
   946
slouken@5396
   947
    point = [theEvent locationInWindow];
slouken@5396
   948
    x = (int)point.x;
slouken@5396
   949
    y = (int)(window->h - point.y);
slouken@5371
   950
icculus@8929
   951
    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@8929
   952
        if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
slouken@6666
   953
            if (x < 0) {
slouken@6666
   954
                x = 0;
slouken@6666
   955
            } else if (x >= window->w) {
slouken@6666
   956
                x = window->w - 1;
slouken@6666
   957
            }
slouken@6666
   958
            if (y < 0) {
slouken@6666
   959
                y = 0;
slouken@6666
   960
            } else if (y >= window->h) {
slouken@6666
   961
                y = window->h - 1;
slouken@6666
   962
            }
slouken@6666
   963
jorgen@7593
   964
#if !SDL_MAC_NO_SANDBOX
jorgen@8260
   965
            CGPoint cgpoint;
jorgen@8260
   966
jorgen@7593
   967
            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
jorgen@7593
   968
             * SDL_cocoamousetap.m.
jorgen@7593
   969
             */
jorgen@7593
   970
slouken@6666
   971
            cgpoint.x = window->x + x;
slouken@6666
   972
            cgpoint.y = window->y + y;
jorgen@7098
   973
jorgen@7113
   974
            /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   975
             * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   976
             * sure how we'd procure the right one for this event.
jorgen@7098
   977
             */
jorgen@7113
   978
            CGSetLocalEventsSuppressionInterval(0.0);
slouken@6666
   979
            CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
jorgen@7113
   980
            CGSetLocalEventsSuppressionInterval(0.25);
jorgen@8261
   981
jorgen@8261
   982
            Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@7593
   983
#endif
slouken@5396
   984
        }
slouken@2059
   985
    }
slouken@6950
   986
    SDL_SendMouseMotion(window, 0, 0, x, y);
slouken@1933
   987
}
slouken@1933
   988
slouken@1957
   989
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   990
{
slouken@1957
   991
    [self mouseMoved:theEvent];
slouken@1957
   992
}
slouken@1957
   993
slouken@1958
   994
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   995
{
slouken@1958
   996
    [self mouseMoved:theEvent];
slouken@1958
   997
}
slouken@1958
   998
slouken@1958
   999
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
  1000
{
slouken@1958
  1001
    [self mouseMoved:theEvent];
slouken@1958
  1002
}
slouken@1958
  1003
slouken@1933
  1004
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
  1005
{
gzjjgod@5057
  1006
    Cocoa_HandleMouseWheel(_data->window, theEvent);
slouken@3688
  1007
}
slouken@3688
  1008
slouken@4673
  1009
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
  1010
{
urkle@9236
  1011
    NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
urkle@9236
  1012
    int existingTouchCount = 0;
urkle@9236
  1013
urkle@9236
  1014
    for (NSTouch* touch in touches) {
urkle@9236
  1015
        if ([touch phase] != NSTouchPhaseBegan) {
urkle@9236
  1016
            existingTouchCount++;
urkle@9236
  1017
        }
urkle@9236
  1018
    }
urkle@9236
  1019
    if (existingTouchCount == 0) {
urkle@9236
  1020
        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
urkle@9236
  1021
        int numFingers = SDL_GetNumTouchFingers(touchID);
urkle@9236
  1022
        DLog("Reset Lost Fingers: %d", numFingers);
urkle@9236
  1023
        for (--numFingers; numFingers >= 0; --numFingers) {
urkle@9236
  1024
            SDL_Finger* finger = SDL_GetTouchFinger(touchID, numFingers);
urkle@9236
  1025
            SDL_SendTouch(touchID, finger->id, SDL_FALSE, 0, 0, 0);
urkle@9236
  1026
        }
urkle@9236
  1027
    }
urkle@9236
  1028
urkle@9236
  1029
    DLog("Began Fingers: %lu .. existing: %d", (unsigned long)[touches count], existingTouchCount);
slouken@8986
  1030
    [self handleTouches:NSTouchPhaseBegan withEvent:theEvent];
slouken@4673
  1031
}
slouken@4673
  1032
slouken@4673
  1033
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
  1034
{
slouken@8986
  1035
    [self handleTouches:NSTouchPhaseMoved withEvent:theEvent];
slouken@4673
  1036
}
slouken@4673
  1037
slouken@4673
  1038
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
  1039
{
slouken@8986
  1040
    [self handleTouches:NSTouchPhaseEnded withEvent:theEvent];
slouken@4673
  1041
}
slouken@4673
  1042
slouken@4673
  1043
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
  1044
{
slouken@8986
  1045
    [self handleTouches:NSTouchPhaseCancelled withEvent:theEvent];
slouken@4673
  1046
}
slouken@4673
  1047
slouken@8986
  1048
- (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
slouken@4673
  1049
{
slouken@8986
  1050
    NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
slouken@4673
  1051
slouken@8986
  1052
    for (NSTouch *touch in touches) {
slouken@6953
  1053
        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
slouken@9679
  1054
        if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
  1055
            return;
slouken@7191
  1056
        }
slouken@4687
  1057
slouken@6953
  1058
        const SDL_FingerID fingerId = (SDL_FingerID)(intptr_t)[touch identity];
slouken@4673
  1059
        float x = [touch normalizedPosition].x;
slouken@4673
  1060
        float y = [touch normalizedPosition].y;
slouken@5261
  1061
        /* Make the origin the upper left instead of the lower left */
slouken@5261
  1062
        y = 1.0f - y;
slouken@4687
  1063
slouken@8986
  1064
        switch (phase) {
slouken@8986
  1065
        case NSTouchPhaseBegan:
slouken@6951
  1066
            SDL_SendTouch(touchId, fingerId, SDL_TRUE, x, y, 1.0f);
slouken@4673
  1067
            break;
slouken@8986
  1068
        case NSTouchPhaseEnded:
slouken@8986
  1069
        case NSTouchPhaseCancelled:
slouken@6951
  1070
            SDL_SendTouch(touchId, fingerId, SDL_FALSE, x, y, 1.0f);
slouken@4673
  1071
            break;
slouken@8986
  1072
        case NSTouchPhaseMoved:
slouken@6951
  1073
            SDL_SendTouchMotion(touchId, fingerId, x, y, 1.0f);
slouken@4673
  1074
            break;
slouken@8986
  1075
        default:
slouken@8986
  1076
            break;
slouken@4673
  1077
        }
slouken@4673
  1078
    }
slouken@1933
  1079
}
slouken@1933
  1080
slouken@1933
  1081
@end
slouken@1933
  1082
icculus@9623
  1083
@interface SDLView : NSView {
icculus@9623
  1084
    SDL_Window *_sdlWindow;
icculus@9623
  1085
}
icculus@9623
  1086
icculus@9623
  1087
- (void)setSDLWindow:(SDL_Window*)window;
jorgen@7158
  1088
slouken@5379
  1089
/* The default implementation doesn't pass rightMouseDown to responder chain */
slouken@5379
  1090
- (void)rightMouseDown:(NSEvent *)theEvent;
icculus@8931
  1091
- (BOOL)mouseDownCanMoveWindow;
icculus@9623
  1092
- (void)drawRect:(NSRect)dirtyRect;
gzjjgod@4915
  1093
@end
gzjjgod@4915
  1094
gzjjgod@4915
  1095
@implementation SDLView
icculus@9623
  1096
- (void)setSDLWindow:(SDL_Window*)window
icculus@9623
  1097
{
icculus@9623
  1098
    _sdlWindow = window;
icculus@9623
  1099
}
icculus@9623
  1100
icculus@9623
  1101
- (void)drawRect:(NSRect)dirtyRect
icculus@9623
  1102
{
icculus@9623
  1103
    SDL_SendWindowEvent(_sdlWindow, SDL_WINDOWEVENT_EXPOSED, 0, 0);
icculus@9623
  1104
}
icculus@9623
  1105
gzjjgod@4915
  1106
- (void)rightMouseDown:(NSEvent *)theEvent
gzjjgod@4915
  1107
{
slouken@5371
  1108
    [[self nextResponder] rightMouseDown:theEvent];
gzjjgod@4915
  1109
}
jorgen@7158
  1110
icculus@8931
  1111
- (BOOL)mouseDownCanMoveWindow
icculus@8931
  1112
{
icculus@8931
  1113
    /* Always say YES, but this doesn't do anything until we call
icculus@8931
  1114
       -[NSWindow setMovableByWindowBackground:YES], which we ninja-toggle
icculus@8931
  1115
       during mouse events when we're using a drag area. */
icculus@8931
  1116
    return YES;
icculus@8931
  1117
}
icculus@8931
  1118
jorgen@7158
  1119
- (void)resetCursorRects
jorgen@7158
  1120
{
jorgen@7158
  1121
    [super resetCursorRects];
jorgen@7158
  1122
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7158
  1123
jorgen@7270
  1124
    if (mouse->cursor_shown && mouse->cur_cursor && !mouse->relative_mode) {
jorgen@7158
  1125
        [self addCursorRect:[self bounds]
jorgen@7158
  1126
                     cursor:mouse->cur_cursor->driverdata];
jorgen@7158
  1127
    } else {
jorgen@7158
  1128
        [self addCursorRect:[self bounds]
jorgen@7158
  1129
                     cursor:[NSCursor invisibleCursor]];
jorgen@7158
  1130
    }
jorgen@7158
  1131
}
gzjjgod@4915
  1132
@end
gzjjgod@4915
  1133
slouken@1933
  1134
static int
slouken@1951
  1135
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slime73@9587
  1136
{ @autoreleasepool
slouken@1933
  1137
{
slouken@1951
  1138
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1933
  1139
    SDL_WindowData *data;
slouken@1933
  1140
slouken@1933
  1141
    /* Allocate the window data */
slouken@8926
  1142
    window->driverdata = data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1933
  1143
    if (!data) {
icculus@7037
  1144
        return SDL_OutOfMemory();
slouken@1933
  1145
    }
slouken@3685
  1146
    data->window = window;
slouken@3688
  1147
    data->nswindow = nswindow;
slouken@1933
  1148
    data->created = created;
slouken@1951
  1149
    data->videodata = videodata;
jorgen@7595
  1150
    data->nscontexts = [[NSMutableArray alloc] init];
slouken@1933
  1151
slouken@6848
  1152
    /* Create an event listener for the window */
slouken@6848
  1153
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@6848
  1154
slouken@6848
  1155
    /* Fill in the SDL window with the window data */
slouken@6848
  1156
    {
slouken@6848
  1157
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@8801
  1158
        ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@6848
  1159
        window->x = (int)rect.origin.x;
slouken@6848
  1160
        window->y = (int)rect.origin.y;
slouken@6848
  1161
        window->w = (int)rect.size.width;
slouken@6848
  1162
        window->h = (int)rect.size.height;
slouken@6848
  1163
    }
slouken@6848
  1164
slouken@6848
  1165
    /* Set up the listener after we create the view */
slouken@6848
  1166
    [data->listener listen:data];
slouken@6848
  1167
slouken@6848
  1168
    if ([nswindow isVisible]) {
slouken@6848
  1169
        window->flags |= SDL_WINDOW_SHOWN;
slouken@6848
  1170
    } else {
slouken@6848
  1171
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@6848
  1172
    }
jorgen@7084
  1173
slouken@6848
  1174
    {
slouken@6848
  1175
        unsigned int style = [nswindow styleMask];
slouken@6848
  1176
slouken@6848
  1177
        if (style == NSBorderlessWindowMask) {
slouken@6848
  1178
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@6848
  1179
        } else {
slouken@6848
  1180
            window->flags &= ~SDL_WINDOW_BORDERLESS;
alexey@6832
  1181
        }
slouken@6848
  1182
        if (style & NSResizableWindowMask) {
slouken@6848
  1183
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@6848
  1184
        } else {
slouken@6848
  1185
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@6848
  1186
        }
slouken@6848
  1187
    }
jorgen@7084
  1188
slouken@6848
  1189
    /* isZoomed always returns true if the window is not resizable */
slouken@6848
  1190
    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1191
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@6848
  1192
    } else {
slouken@6848
  1193
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@6848
  1194
    }
jorgen@7084
  1195
slouken@6848
  1196
    if ([nswindow isMiniaturized]) {
slouken@6848
  1197
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@6848
  1198
    } else {
slouken@6848
  1199
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@6848
  1200
    }
jorgen@7084
  1201
slouken@6848
  1202
    if ([nswindow isKeyWindow]) {
slouken@6848
  1203
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6848
  1204
        SDL_SetKeyboardFocus(data->window);
slouken@6848
  1205
    }
slouken@1933
  1206
jorgen@7085
  1207
    /* Prevents the window's "window device" from being destroyed when it is
jorgen@7085
  1208
     * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
jorgen@7085
  1209
     */
jorgen@7085
  1210
    [nswindow setOneShot:NO];
jorgen@7085
  1211
slouken@6848
  1212
    /* All done! */
slouken@6848
  1213
    window->driverdata = data;
slouken@6848
  1214
    return 0;
slime73@9587
  1215
}}
slouken@1933
  1216
slouken@1933
  1217
int
slouken@1933
  1218
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slime73@9587
  1219
{ @autoreleasepool
slouken@1933
  1220
{
icculus@8295
  1221
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6848
  1222
    NSWindow *nswindow;
slouken@6848
  1223
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6848
  1224
    NSRect rect;
slouken@6848
  1225
    SDL_Rect bounds;
slouken@6848
  1226
    unsigned int style;
slouken@8986
  1227
    NSArray *screens = [NSScreen screens];
slouken@1933
  1228
slouken@6848
  1229
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1230
    rect.origin.x = window->x;
slouken@6848
  1231
    rect.origin.y = window->y;
slouken@6848
  1232
    rect.size.width = window->w;
slouken@6848
  1233
    rect.size.height = window->h;
slouken@8986
  1234
    ConvertNSRect([screens objectAtIndex:0], (window->flags & FULLSCREEN_MASK), &rect);
slouken@1933
  1235
slouken@6848
  1236
    style = GetWindowStyle(window);
slouken@1933
  1237
slouken@6848
  1238
    /* Figure out which screen to place this window */
slouken@6848
  1239
    NSScreen *screen = nil;
slouken@8986
  1240
    for (NSScreen *candidate in screens) {
slouken@6848
  1241
        NSRect screenRect = [candidate frame];
slouken@6848
  1242
        if (rect.origin.x >= screenRect.origin.x &&
slouken@6848
  1243
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@6848
  1244
            rect.origin.y >= screenRect.origin.y &&
slouken@6848
  1245
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@6848
  1246
            screen = candidate;
slouken@6848
  1247
            rect.origin.x -= screenRect.origin.x;
slouken@6848
  1248
            rect.origin.y -= screenRect.origin.y;
slouken@3506
  1249
        }
slouken@6848
  1250
    }
slouken@7946
  1251
slouken@7946
  1252
    @try {
slouken@7946
  1253
        nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen];
slouken@7946
  1254
    }
slouken@7946
  1255
    @catch (NSException *e) {
slime73@9587
  1256
        return SDL_SetError("%s", [[e reason] UTF8String]);
slouken@7946
  1257
    }
icculus@7205
  1258
    [nswindow setBackgroundColor:[NSColor blackColor]];
icculus@8284
  1259
icculus@8295
  1260
    if (videodata->allow_spaces) {
slouken@8986
  1261
        SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
icculus@8295
  1262
        SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);
icculus@8284
  1263
        /* we put FULLSCREEN_DESKTOP windows in their own Space, without a toggle button or menubar, later */
icculus@8284
  1264
        if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@8284
  1265
            /* resizable windows are Spaces-friendly: they get the "go fullscreen" toggle button on their titlebar. */
slouken@7968
  1266
            [nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
slouken@7968
  1267
        }
slouken@7955
  1268
    }
alexey@6832
  1269
slouken@7191
  1270
    /* Create a default view for this window */
slouken@6848
  1271
    rect = [nswindow contentRectForFrameRect:[nswindow frame]];
icculus@9623
  1272
    SDLView *contentView = [[SDLView alloc] initWithFrame:rect];
icculus@9623
  1273
    [contentView setSDLWindow:window];
urkle@7746
  1274
slouken@7955
  1275
    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
urkle@7746
  1276
        if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
urkle@7746
  1277
            [contentView setWantsBestResolutionOpenGLSurface:YES];
urkle@7746
  1278
        }
urkle@7746
  1279
    }
urkle@7746
  1280
slouken@6848
  1281
    [nswindow setContentView: contentView];
slouken@6848
  1282
    [contentView release];
alexey@6832
  1283
jorgen@9237
  1284
    /* Allow files and folders to be dragged onto the window by users */
jorgen@9237
  1285
    [nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@9237
  1286
slouken@6848
  1287
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@6848
  1288
        [nswindow release];
slouken@6848
  1289
        return -1;
slouken@3506
  1290
    }
slouken@6848
  1291
    return 0;
slime73@9587
  1292
}}
slouken@1933
  1293
slouken@1933
  1294
int
slouken@1933
  1295
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slime73@9587
  1296
{ @autoreleasepool
slouken@1933
  1297
{
slouken@1933
  1298
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
  1299
    NSString *title;
slouken@1933
  1300
slouken@6848
  1301
    /* Query the title from the existing window */
slouken@6848
  1302
    title = [nswindow title];
slouken@6848
  1303
    if (title) {
slouken@6848
  1304
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
  1305
    }
slouken@1933
  1306
slouken@1951
  1307
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slime73@9587
  1308
}}
slouken@1933
  1309
slouken@1933
  1310
void
slouken@1933
  1311
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slime73@9587
  1312
{ @autoreleasepool
slouken@1933
  1313
{
slouken@9797
  1314
    const char *title = window->title ? window->title : "";
slouken@6848
  1315
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@9797
  1316
    NSString *string = [[NSString alloc] initWithUTF8String:title];
slouken@6848
  1317
    [nswindow setTitle:string];
slouken@6848
  1318
    [string release];
slime73@9587
  1319
}}
slouken@1933
  1320
slouken@1933
  1321
void
slouken@5375
  1322
Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slime73@9587
  1323
{ @autoreleasepool
slouken@5375
  1324
{
slouken@6848
  1325
    NSImage *nsimage = Cocoa_CreateImage(icon);
slouken@5375
  1326
slouken@6848
  1327
    if (nsimage) {
slouken@6848
  1328
        [NSApp setApplicationIconImage:nsimage];
slouken@5375
  1329
    }
slime73@9587
  1330
}}
slouken@5375
  1331
slouken@5375
  1332
void
slouken@1933
  1333
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slime73@9587
  1334
{ @autoreleasepool
slouken@1933
  1335
{
jorgen@7594
  1336
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1337
    NSWindow *nswindow = windata->nswindow;
slouken@6848
  1338
    NSRect rect;
slouken@6848
  1339
    Uint32 moveHack;
slouken@1933
  1340
slouken@6848
  1341
    rect.origin.x = window->x;
slouken@6848
  1342
    rect.origin.y = window->y;
slouken@6848
  1343
    rect.size.width = window->w;
slouken@6848
  1344
    rect.size.height = window->h;
slouken@8801
  1345
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
slouken@5478
  1346
slouken@6848
  1347
    moveHack = s_moveHack;
slouken@6848
  1348
    s_moveHack = 0;
slouken@6848
  1349
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1350
    s_moveHack = moveHack;
slouken@5478
  1351
jorgen@7595
  1352
    ScheduleContextUpdates(windata);
slime73@9587
  1353
}}
slouken@1933
  1354
slouken@1933
  1355
void
slouken@1933
  1356
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slime73@9587
  1357
{ @autoreleasepool
slouken@1933
  1358
{
slouken@6848
  1359
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@6848
  1360
    NSWindow *nswindow = windata->nswindow;
slime73@9860
  1361
    NSRect rect;
slime73@9860
  1362
    Uint32 moveHack;
slouken@1933
  1363
slime73@9860
  1364
    /* Cocoa will resize the window from the bottom-left rather than the
slime73@9860
  1365
     * top-left when -[nswindow setContentSize:] is used, so we must set the
slime73@9860
  1366
     * entire frame based on the new size, in order to preserve the position.
slime73@9860
  1367
     */
slime73@9860
  1368
    rect.origin.x = window->x;
slime73@9860
  1369
    rect.origin.y = window->y;
slime73@9860
  1370
    rect.size.width = window->w;
slime73@9860
  1371
    rect.size.height = window->h;
slime73@9860
  1372
    ConvertNSRect([nswindow screen], (window->flags & FULLSCREEN_MASK), &rect);
icculus@9841
  1373
slime73@9860
  1374
    moveHack = s_moveHack;
slime73@9860
  1375
    s_moveHack = 0;
slime73@9860
  1376
    [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
slime73@9860
  1377
    s_moveHack = moveHack;
icculus@5564
  1378
jorgen@7595
  1379
    ScheduleContextUpdates(windata);
slime73@9587
  1380
}}
slouken@1933
  1381
slouken@1933
  1382
void
stopiccot@6681
  1383
Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
slime73@9587
  1384
{ @autoreleasepool
stopiccot@6681
  1385
{
slouken@6848
  1386
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1387
slouken@6848
  1388
    NSSize minSize;
slouken@6848
  1389
    minSize.width = window->min_w;
slouken@6848
  1390
    minSize.height = window->min_h;
slouken@7191
  1391
slouken@6848
  1392
    [windata->nswindow setContentMinSize:minSize];
slime73@9587
  1393
}}
slouken@6788
  1394
slouken@6788
  1395
void
slouken@6788
  1396
Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
slime73@9587
  1397
{ @autoreleasepool
slouken@6788
  1398
{
slouken@6848
  1399
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
slouken@7191
  1400
slouken@6848
  1401
    NSSize maxSize;
slouken@6848
  1402
    maxSize.width = window->max_w;
slouken@6848
  1403
    maxSize.height = window->max_h;
slouken@7191
  1404
slouken@6848
  1405
    [windata->nswindow setContentMaxSize:maxSize];
slime73@9587
  1406
}}
stopiccot@6681
  1407
stopiccot@6681
  1408
void
slouken@1933
  1409
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slime73@9587
  1410
{ @autoreleasepool
slouken@1933
  1411
{
jorgen@7087
  1412
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1413
    NSWindow *nswindow = windowData->nswindow;
slouken@1933
  1414
slouken@6848
  1415
    if (![nswindow isMiniaturized]) {
jorgen@7087
  1416
        [windowData->listener pauseVisibleObservation];
slouken@6848
  1417
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7087
  1418
        [windowData->listener resumeVisibleObservation];
slouken@1956
  1419
    }
slime73@9587
  1420
}}
slouken@1933
  1421
slouken@1933
  1422
void
slouken@1933
  1423
Cocoa_HideWindow(_THIS, SDL_Window * window)
slime73@9587
  1424
{ @autoreleasepool
slouken@1933
  1425
{
slouken@6848
  1426
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@6848
  1427
slouken@6848
  1428
    [nswindow orderOut:nil];
slime73@9587
  1429
}}
slouken@1933
  1430
slouken@1933
  1431
void
slouken@1933
  1432
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slime73@9587
  1433
{ @autoreleasepool
slouken@1933
  1434
{
jorgen@7087
  1435
    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
jorgen@7087
  1436
    NSWindow *nswindow = windowData->nswindow;
slouken@6848
  1437
slouken@7771
  1438
    /* makeKeyAndOrderFront: has the side-effect of deminiaturizing and showing
slouken@7771
  1439
       a minimized or hidden window, so check for that before showing it.
slouken@7771
  1440
     */
jorgen@7087
  1441
    [windowData->listener pauseVisibleObservation];
jorgen@7469
  1442
    if (![nswindow isMiniaturized] && [nswindow isVisible]) {
alfred@9041
  1443
        [NSApp activateIgnoringOtherApps:YES];
jorgen@7469
  1444
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7469
  1445
    }
jorgen@7087
  1446
    [windowData->listener resumeVisibleObservation];
slime73@9587
  1447
}}
slouken@1933
  1448
slouken@1933
  1449
void
slouken@1933
  1450
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1451
{ @autoreleasepool
slouken@1933
  1452
{
jorgen@7594
  1453
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
jorgen@7594
  1454
    NSWindow *nswindow = windata->nswindow;
slouken@1933
  1455
slouken@6848
  1456
    [nswindow zoom:nil];
slouken@6848
  1457
jorgen@7595
  1458
    ScheduleContextUpdates(windata);
slime73@9587
  1459
}}
slouken@1933
  1460
slouken@1933
  1461
void
slouken@1933
  1462
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slime73@9587
  1463
{ @autoreleasepool
slouken@1933
  1464
{
slouken@7963
  1465
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7963
  1466
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1467
slouken@7968
  1468
    if ([data->listener isInFullscreenSpaceTransition]) {
slouken@7963
  1469
        [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
slouken@7963
  1470
    } else {
slouken@7963
  1471
        [nswindow miniaturize:nil];
slouken@7963
  1472
    }
slime73@9587
  1473
}}
slouken@1933
  1474
slouken@1933
  1475
void
slouken@1933
  1476
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slime73@9587
  1477
{ @autoreleasepool
slouken@1933
  1478
{
slouken@6848
  1479
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1480
slouken@6848
  1481
    if ([nswindow isMiniaturized]) {
slouken@6848
  1482
        [nswindow deminiaturize:nil];
slouken@6848
  1483
    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
slouken@6848
  1484
        [nswindow zoom:nil];
slouken@1956
  1485
    }
slime73@9587
  1486
}}
slouken@1933
  1487
slouken@5400
  1488
static NSWindow *
slouken@5400
  1489
Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
slouken@5400
  1490
{
slouken@5400
  1491
    if (!data->created) {
slouken@5400
  1492
        /* Don't mess with other people's windows... */
slouken@5400
  1493
        return nswindow;
slouken@5400
  1494
    }
slouken@5400
  1495
slouken@5400
  1496
    [data->listener close];
jorgen@7085
  1497
    data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]];
slouken@5400
  1498
    [data->nswindow setContentView:[nswindow contentView]];
jorgen@9237
  1499
    [data->nswindow registerForDraggedTypes:[NSArray arrayWithObject:(NSString *)kUTTypeFileURL]];
jorgen@7085
  1500
    /* See comment in SetupWindowData. */
jorgen@7085
  1501
    [data->nswindow setOneShot:NO];
slouken@5400
  1502
    [data->listener listen:data];
slouken@5400
  1503
slouken@5400
  1504
    [nswindow close];
slouken@5400
  1505
slouken@5400
  1506
    return data->nswindow;
slouken@5400
  1507
}
slouken@5400
  1508
slouken@1933
  1509
void
icculus@6422
  1510
Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
slime73@9587
  1511
{ @autoreleasepool
icculus@6422
  1512
{
slouken@7990
  1513
    if (SetWindowStyle(window, GetWindowStyle(window))) {
slouken@9797
  1514
        if (bordered) {
slouken@7191
  1515
            Cocoa_SetWindowTitle(_this, window);  /* this got blanked out. */
icculus@6426
  1516
        }
icculus@6422
  1517
    }
slime73@9587
  1518
}}
icculus@6422
  1519
slouken@7952
  1520
slouken@7968
  1521
void
slouken@7968
  1522
Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slime73@9587
  1523
{ @autoreleasepool
slouken@7952
  1524
{
slouken@6848
  1525
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6848
  1526
    NSWindow *nswindow = data->nswindow;
slouken@6848
  1527
    NSRect rect;
slouken@5249
  1528
slouken@6848
  1529
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1530
    if ([[nswindow contentView] nextResponder] == data->listener) {
slouken@6848
  1531
        [[nswindow contentView] setNextResponder:nil];
slouken@6848
  1532
    }
slouken@6848
  1533
slouken@6848
  1534
    if (fullscreen) {
slouken@6848
  1535
        SDL_Rect bounds;
slouken@6848
  1536
slouken@6848
  1537
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@6848
  1538
        rect.origin.x = bounds.x;
slouken@6848
  1539
        rect.origin.y = bounds.y;
slouken@6848
  1540
        rect.size.width = bounds.w;
slouken@6848
  1541
        rect.size.height = bounds.h;
slouken@8801
  1542
        ConvertNSRect([nswindow screen], fullscreen, &rect);
slouken@6848
  1543
slouken@6848
  1544
        /* Hack to fix origin on Mac OS X 10.4 */
slouken@6848
  1545
        NSRect screenRect = [[nswindow screen] frame];
slouken@6848
  1546
        if (screenRect.size.height >= 1.0f) {
slouken@6848
  1547
            rect.origin.y += (screenRect.size.height - rect.size.height);
slouken@5401
  1548
        }
slouken@5401
  1549
slouken@6848
  1550
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1551
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
slouken@6848
  1552
        } else {
slouken@6848
  1553
            nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
slouken@6848
  1554
        }
slouken@6848
  1555
    } else {
slouken@6848
  1556
        rect.origin.x = window->windowed.x;
slouken@6848
  1557
        rect.origin.y = window->windowed.y;
slouken@6848
  1558
        rect.size.width = window->windowed.w;
slouken@6848
  1559
        rect.size.height = window->windowed.h;
slouken@8801
  1560
        ConvertNSRect([nswindow screen], fullscreen, &rect);
alexey@6832
  1561
slouken@6848
  1562
        if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
slouken@6848
  1563
            [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
slouken@9790
  1564
slouken@9790
  1565
            /* Hack to restore window decorations on Mac OS X 10.10 */
slouken@9790
  1566
            NSRect frameRect = [nswindow frame];
slouken@9790
  1567
            [nswindow setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO];
slouken@9790
  1568
            [nswindow setFrame:frameRect display:NO];
slouken@5400
  1569
        } else {
slouken@6848
  1570
            nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
slouken@5361
  1571
        }
slouken@5398
  1572
    }
slouken@6848
  1573
slouken@6848
  1574
    /* The view responder chain gets messed with during setStyleMask */
slouken@6848
  1575
    if ([[nswindow contentView] nextResponder] != data->listener) {
slouken@6848
  1576
        [[nswindow contentView] setNextResponder:data->listener];
slouken@6848
  1577
    }
slouken@6848
  1578
slouken@6848
  1579
    s_moveHack = 0;
slouken@7873
  1580
    [nswindow setContentSize:rect.size];
slouken@6848
  1581
    [nswindow setFrameOrigin:rect.origin];
slouken@6848
  1582
    s_moveHack = SDL_GetTicks();
slouken@6848
  1583
slouken@6848
  1584
    /* When the window style changes the title is cleared */
slouken@9797
  1585
    if (!fullscreen) {
slouken@6848
  1586
        Cocoa_SetWindowTitle(_this, window);
slouken@6848
  1587
    }
slouken@6848
  1588
slouken@6848
  1589
    if (SDL_ShouldAllowTopmost() && fullscreen) {
slouken@6848
  1590
        /* OpenGL is rendering to the window, so make it visible! */
slouken@6848
  1591
        [nswindow setLevel:CGShieldingWindowLevel()];
slouken@6848
  1592
    } else {
slouken@6848
  1593
        [nswindow setLevel:kCGNormalWindowLevel];
slouken@6848
  1594
    }
jorgen@7087
  1595
jorgen@7636
  1596
    if ([nswindow isVisible] || fullscreen) {
jorgen@7636
  1597
        [data->listener pauseVisibleObservation];
jorgen@7636
  1598
        [nswindow makeKeyAndOrderFront:nil];
jorgen@7636
  1599
        [data->listener resumeVisibleObservation];
jorgen@7636
  1600
    }
slouken@6848
  1601
jorgen@7595
  1602
    ScheduleContextUpdates(data);
slime73@9587
  1603
}}
slouken@5249
  1604
slouken@5466
  1605
int
slouken@5466
  1606
Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1607
{
slouken@5466
  1608
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1609
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1610
    const uint32_t tableSize = 256;
slouken@5466
  1611
    CGGammaValue redTable[tableSize];
slouken@5466
  1612
    CGGammaValue greenTable[tableSize];
slouken@5466
  1613
    CGGammaValue blueTable[tableSize];
slouken@5466
  1614
    uint32_t i;
slouken@5466
  1615
    float inv65535 = 1.0f / 65535.0f;
slouken@5466
  1616
slouken@5466
  1617
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@5466
  1618
    for (i = 0; i < 256; i++) {
slouken@5466
  1619
        redTable[i] = ramp[0*256+i] * inv65535;
slouken@5466
  1620
        greenTable[i] = ramp[1*256+i] * inv65535;
slouken@5466
  1621
        blueTable[i] = ramp[2*256+i] * inv65535;
slouken@5466
  1622
    }
slouken@5466
  1623
slouken@5466
  1624
    if (CGSetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1625
                                    redTable, greenTable, blueTable) != CGDisplayNoErr) {
icculus@7037
  1626
        return SDL_SetError("CGSetDisplayTransferByTable()");
slouken@5466
  1627
    }
slouken@5466
  1628
    return 0;
slouken@5466
  1629
}
slouken@5466
  1630
slouken@5466
  1631
int
slouken@5466
  1632
Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
  1633
{
slouken@5466
  1634
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
  1635
    CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
slouken@5466
  1636
    const uint32_t tableSize = 256;
slouken@5466
  1637
    CGGammaValue redTable[tableSize];
slouken@5466
  1638
    CGGammaValue greenTable[tableSize];
slouken@5466
  1639
    CGGammaValue blueTable[tableSize];
slouken@5466
  1640
    uint32_t i, tableCopied;
slouken@5466
  1641
slouken@5466
  1642
    if (CGGetDisplayTransferByTable(display_id, tableSize,
slouken@5466
  1643
                                    redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
icculus@7037
  1644
        return SDL_SetError("CGGetDisplayTransferByTable()");
slouken@5466
  1645
    }
slouken@5466
  1646
slouken@5466
  1647
    for (i = 0; i < tableCopied; i++) {
slouken@5466
  1648
        ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
slouken@5466
  1649
        ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
slouken@5466
  1650
        ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
slouken@5466
  1651
    }
slouken@5466
  1652
    return 0;
slouken@5466
  1653
}
slouken@5466
  1654
slouken@5249
  1655
void
slouken@6662
  1656
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1933
  1657
{
slouken@5371
  1658
    /* Move the cursor to the nearest point in the window */
jorgen@8260
  1659
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@8260
  1660
    if (grabbed && data && ![data->listener isMoving]) {
slouken@5371
  1661
        int x, y;
slouken@5371
  1662
        CGPoint cgpoint;
slouken@5371
  1663
slouken@5371
  1664
        SDL_GetMouseState(&x, &y);
slouken@5371
  1665
        cgpoint.x = window->x + x;
slouken@5371
  1666
        cgpoint.y = window->y + y;
jorgen@8261
  1667
jorgen@8261
  1668
        Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
jorgen@8261
  1669
jorgen@8261
  1670
        DLog("Returning cursor to (%g, %g)", cgpoint.x, cgpoint.y);
slouken@5371
  1671
        CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
slouken@5371
  1672
    }
slouken@7191
  1673
icculus@8652
  1674
    if ( data && (window->flags & SDL_WINDOW_FULLSCREEN) ) {
slime73@9897
  1675
        if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)
slime73@9897
  1676
            && ![data->listener isInFullscreenSpace]) {
slouken@7191
  1677
            /* OpenGL is rendering to the window, so make it visible! */
slime73@9897
  1678
            /* Doing this in 10.11 while in a Space breaks things (bug #3152) */
slouken@7191
  1679
            [data->nswindow setLevel:CGShieldingWindowLevel()];
slouken@7191
  1680
        } else {
slouken@7191
  1681
            [data->nswindow setLevel:kCGNormalWindowLevel];
slouken@7191
  1682
        }
slouken@7191
  1683
    }
slouken@1933
  1684
}
slouken@1933
  1685
slouken@1933
  1686
void
slouken@1933
  1687
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slime73@9587
  1688
{ @autoreleasepool
slouken@1933
  1689
{
slouken@6848
  1690
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
  1691
slouken@6848
  1692
    if (data) {
slouken@9907
  1693
        if ([data->listener isInFullscreenSpace]) {
slouken@9907
  1694
            [NSMenu setMenuBarVisible:YES];
slouken@9907
  1695
        }
slouken@6848
  1696
        [data->listener close];
slouken@6848
  1697
        [data->listener release];
slouken@6848
  1698
        if (data->created) {
slouken@6848
  1699
            [data->nswindow close];
slouken@1933
  1700
        }
jorgen@7595
  1701
jorgen@7595
  1702
        NSArray *contexts = [[data->nscontexts copy] autorelease];
jorgen@7595
  1703
        for (SDLOpenGLContext *context in contexts) {
jorgen@7595
  1704
            /* Calling setWindow:NULL causes the context to remove itself from the context list. */            
jorgen@7595
  1705
            [context setWindow:NULL];
jorgen@7595
  1706
        }
jorgen@7595
  1707
        [data->nscontexts release];
jorgen@7595
  1708
slouken@6848
  1709
        SDL_free(data);
slouken@1933
  1710
    }
slouken@8978
  1711
    window->driverdata = NULL;
slime73@9587
  1712
}}
slouken@1933
  1713
slouken@1933
  1714
SDL_bool
slouken@1933
  1715
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
  1716
{
slouken@4900
  1717
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
  1718
slouken@1933
  1719
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
  1720
        info->subsystem = SDL_SYSWM_COCOA;
slouken@5056
  1721
        info->info.cocoa.window = nswindow;
slouken@1933
  1722
        return SDL_TRUE;
slouken@1933
  1723
    } else {
slouken@1933
  1724
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
  1725
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
  1726
        return SDL_FALSE;
slouken@1933
  1727
    }
slouken@1933
  1728
}
slouken@1933
  1729
slouken@7968
  1730
SDL_bool
slouken@7969
  1731
Cocoa_IsWindowInFullscreenSpace(SDL_Window * window)
slouken@7969
  1732
{
slouken@7969
  1733
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7969
  1734
slouken@7969
  1735
    if ([data->listener isInFullscreenSpace]) {
slouken@7969
  1736
        return SDL_TRUE;
slouken@7969
  1737
    } else {
slouken@7969
  1738
        return SDL_FALSE;
slouken@7969
  1739
    }
slouken@7969
  1740
}
slouken@7969
  1741
slouken@7969
  1742
SDL_bool
slouken@7968
  1743
Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state)
slime73@9587
  1744
{ @autoreleasepool
slouken@7968
  1745
{
slouken@7968
  1746
    SDL_bool succeeded = SDL_FALSE;
slouken@7968
  1747
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7968
  1748
slouken@7968
  1749
    if ([data->listener setFullscreenSpace:(state ? YES : NO)]) {
slouken@9904
  1750
        const int maxattempts = 3;
slouken@9904
  1751
        int attempt = 0;
slouken@9904
  1752
        while (++attempt <= maxattempts) {
slouken@9904
  1753
            /* Wait for the transition to complete, so application changes
slouken@9904
  1754
             take effect properly (e.g. setting the window size, etc.)
slouken@9904
  1755
             */
slouken@9904
  1756
            const int limit = 10000;
slouken@9904
  1757
            int count = 0;
slouken@9904
  1758
            while ([data->listener isInFullscreenSpaceTransition]) {
slouken@9904
  1759
                if ( ++count == limit ) {
slouken@9904
  1760
                    /* Uh oh, transition isn't completing. Should we assert? */
slouken@9904
  1761
                    break;
slouken@9904
  1762
                }
slouken@9904
  1763
                SDL_Delay(1);
slouken@9904
  1764
                SDL_PumpEvents();
slouken@9904
  1765
            }
slouken@9904
  1766
            if ([data->listener isInFullscreenSpace] == (state ? YES : NO))
slouken@9904
  1767
                break;
slouken@9904
  1768
            /* Try again, the last attempt was interrupted by user gestures */
slouken@9904
  1769
            if (![data->listener setFullscreenSpace:(state ? YES : NO)])
slouken@9904
  1770
                break; /* ??? */
slouken@9904
  1771
        }
slouken@9904
  1772
        /* Return TRUE to prevent non-space fullscreen logic from running */
slouken@7968
  1773
        succeeded = SDL_TRUE;
slouken@7968
  1774
    }
slouken@7968
  1775
slouken@7968
  1776
    return succeeded;
slime73@9587
  1777
}}
slouken@7968
  1778
icculus@8931
  1779
int
icculus@8935
  1780
Cocoa_SetWindowHitTest(SDL_Window * window, SDL_bool enabled)
icculus@8931
  1781
{
icculus@8931
  1782
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8931
  1783
}
icculus@8931
  1784
slouken@6044
  1785
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
  1786
slouken@1933
  1787
/* vi: set ts=4 sw=4 expandtab: */