src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Jul 2014 12:48:25 -0700
changeset 8986 1c81316ac642
parent 8978 7753e4fd3d1d
child 9041 89e97caa2387
permissions -rw-r--r--
Fixed bug 2631 - Mac: minor code cleanup

Alex Szpakowski

Some minor changes to the Mac-specific backend code:

- Fixed up some code style issues (mostly brace style inconsistencies).

- Fixed a compiler warning in SDL_cocoaevents.m.

- Removed some useless code now that the 10.7 SDK is required to build SDL.

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