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