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