src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 28 Jul 2010 23:32:13 -0700
changeset 4673 c17ac64abb70
parent 4503 524dfefd554c
child 4674 89d5e2201b00
permissions -rw-r--r--
Fixed the code so we receive Cocoa touch events
slouken@1933
     1
/*
slouken@1933
     2
    SDL - Simple DirectMedia Layer
slouken@4498
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1933
     4
slouken@1933
     5
    This library is free software; you can redistribute it and/or
slouken@1933
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1933
     7
    License as published by the Free Software Foundation; either
slouken@1933
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1933
     9
slouken@1933
    10
    This library is distributed in the hope that it will be useful,
slouken@1933
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1933
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1933
    13
    Lesser General Public License for more details.
slouken@1933
    14
slouken@1933
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1933
    16
    License along with this library; if not, write to the Free Software
slouken@1933
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1933
    18
slouken@1933
    19
    Sam Lantinga
slouken@1933
    20
    slouken@libsdl.org
slouken@1933
    21
*/
slouken@1933
    22
#include "SDL_config.h"
slouken@1933
    23
slouken@1933
    24
#include "SDL_syswm.h"
slouken@1933
    25
#include "../SDL_sysvideo.h"
slouken@1933
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1933
    27
#include "../../events/SDL_mouse_c.h"
slouken@4673
    28
#include "../../events/SDL_touch_c.h"
slouken@1933
    29
#include "../../events/SDL_windowevents_c.h"
slouken@1933
    30
slouken@1933
    31
#include "SDL_cocoavideo.h"
slouken@1933
    32
slouken@1933
    33
static __inline__ void ConvertNSRect(NSRect *r)
slouken@1933
    34
{
slouken@1933
    35
    r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
slouken@1933
    36
}
slouken@1933
    37
slouken@1933
    38
@implementation Cocoa_WindowListener
slouken@1933
    39
slouken@1933
    40
- (void)listen:(SDL_WindowData *)data
slouken@1933
    41
{
slouken@1933
    42
    NSNotificationCenter *center;
slouken@1933
    43
slouken@1933
    44
    _data = data;
slouken@1933
    45
slouken@1933
    46
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    47
slouken@3688
    48
    [_data->nswindow setNextResponder:self];
slouken@3688
    49
    if ([_data->nswindow delegate] != nil) {
slouken@3688
    50
        [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->nswindow];
slouken@3688
    51
        [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->nswindow];
slouken@3688
    52
        [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->nswindow];
slouken@3688
    53
        [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
slouken@3688
    54
        [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
slouken@3688
    55
        [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
slouken@3688
    56
        [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->nswindow];
slouken@1933
    57
    } else {
slouken@3688
    58
        [_data->nswindow setDelegate:self];
slouken@1933
    59
    }
slouken@1933
    60
    [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    61
    [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    62
slouken@3688
    63
    [_data->nswindow setAcceptsMouseMovedEvents:YES];
slouken@4673
    64
    [[_data->nswindow contentView] setAcceptsTouchEvents:YES];
slouken@1933
    65
}
slouken@1933
    66
slouken@1933
    67
- (void)close
slouken@1933
    68
{
slouken@1933
    69
    NSNotificationCenter *center;
slouken@1933
    70
slouken@1933
    71
    center = [NSNotificationCenter defaultCenter];
slouken@1933
    72
slouken@3688
    73
    [_data->nswindow setNextResponder:nil];
slouken@3688
    74
    if ([_data->nswindow delegate] != self) {
slouken@3688
    75
        [center removeObserver:self name:NSWindowDidExposeNotification object:_data->nswindow];
slouken@3688
    76
        [center removeObserver:self name:NSWindowDidMoveNotification object:_data->nswindow];
slouken@3688
    77
        [center removeObserver:self name:NSWindowDidResizeNotification object:_data->nswindow];
slouken@3688
    78
        [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
slouken@3688
    79
        [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
slouken@3688
    80
        [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
slouken@3688
    81
        [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->nswindow];
slouken@1933
    82
    } else {
slouken@3688
    83
        [_data->nswindow setDelegate:nil];
slouken@1933
    84
    }
slouken@1933
    85
    [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
slouken@1933
    86
    [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
slouken@1933
    87
}
slouken@1933
    88
slouken@1933
    89
- (BOOL)windowShouldClose:(id)sender
slouken@1933
    90
{
slouken@3685
    91
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1933
    92
    return NO;
slouken@1933
    93
}
slouken@1933
    94
slouken@1933
    95
- (void)windowDidExpose:(NSNotification *)aNotification
slouken@1933
    96
{
slouken@3685
    97
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1933
    98
}
slouken@1933
    99
slouken@1933
   100
- (void)windowDidMove:(NSNotification *)aNotification
slouken@1933
   101
{
slouken@1933
   102
    int x, y;
slouken@3688
   103
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   104
    ConvertNSRect(&rect);
slouken@3507
   105
    x = (int)rect.origin.x;
slouken@3507
   106
    y = (int)rect.origin.y;
slouken@3685
   107
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1933
   108
}
slouken@1933
   109
slouken@1933
   110
- (void)windowDidResize:(NSNotification *)aNotification
slouken@1933
   111
{
slouken@1933
   112
    int w, h;
slouken@3688
   113
    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
slouken@1933
   114
    w = (int)rect.size.width;
slouken@1933
   115
    h = (int)rect.size.height;
slouken@3685
   116
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1933
   117
}
slouken@1933
   118
slouken@1933
   119
- (void)windowDidMiniaturize:(NSNotification *)aNotification
slouken@1933
   120
{
slouken@3685
   121
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1933
   122
}
slouken@1933
   123
slouken@1933
   124
- (void)windowDidDeminiaturize:(NSNotification *)aNotification
slouken@1933
   125
{
slouken@3685
   126
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1933
   127
}
slouken@1933
   128
slouken@1933
   129
- (void)windowDidBecomeKey:(NSNotification *)aNotification
slouken@1933
   130
{
slouken@2059
   131
    /* We're going to get keyboard events, since we're key. */
slouken@4465
   132
    SDL_SetKeyboardFocus(_data->window);
slouken@4503
   133
slouken@4503
   134
    /* Check to see if someone updated the clipboard */
slouken@4503
   135
    Cocoa_CheckClipboardUpdate(_data->videodata);
slouken@1933
   136
}
slouken@1933
   137
slouken@1933
   138
- (void)windowDidResignKey:(NSNotification *)aNotification
slouken@1933
   139
{
slouken@2059
   140
    /* Some other window will get mouse events, since we're not key. */
slouken@4465
   141
    if (SDL_GetMouseFocus() == _data->window) {
slouken@4465
   142
        SDL_SetMouseFocus(NULL);
slouken@2059
   143
    }
slouken@2059
   144
slouken@2059
   145
    /* Some other window will get keyboard events, since we're not key. */
slouken@4465
   146
    if (SDL_GetKeyboardFocus() == _data->window) {
slouken@4465
   147
        SDL_SetKeyboardFocus(NULL);
slouken@4465
   148
    }
slouken@1933
   149
}
slouken@1933
   150
slouken@1933
   151
- (void)windowDidHide:(NSNotification *)aNotification
slouken@1933
   152
{
slouken@3685
   153
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1933
   154
}
slouken@1933
   155
slouken@1933
   156
- (void)windowDidUnhide:(NSNotification *)aNotification
slouken@1933
   157
{
slouken@3685
   158
    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1933
   159
}
slouken@1933
   160
slouken@1933
   161
- (void)mouseDown:(NSEvent *)theEvent
slouken@1933
   162
{
slouken@1959
   163
    int button;
slouken@1933
   164
slouken@1959
   165
    switch ([theEvent buttonNumber]) {
slouken@1959
   166
    case 0:
slouken@1959
   167
        button = SDL_BUTTON_LEFT;
slouken@1959
   168
        break;
slouken@1959
   169
    case 1:
slouken@1959
   170
        button = SDL_BUTTON_RIGHT;
slouken@1959
   171
        break;
slouken@1959
   172
    case 2:
slouken@1959
   173
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   174
        break;
slouken@1959
   175
    default:
slouken@1959
   176
        button = [theEvent buttonNumber];
slouken@1959
   177
        break;
slouken@1959
   178
    }
slouken@4484
   179
    SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
slouken@1933
   180
}
slouken@1933
   181
slouken@1933
   182
- (void)rightMouseDown:(NSEvent *)theEvent
slouken@1933
   183
{
slouken@1959
   184
    [self mouseDown:theEvent];
slouken@1933
   185
}
slouken@1933
   186
slouken@1933
   187
- (void)otherMouseDown:(NSEvent *)theEvent
slouken@1933
   188
{
slouken@1959
   189
    [self mouseDown:theEvent];
slouken@1933
   190
}
slouken@1933
   191
slouken@1933
   192
- (void)mouseUp:(NSEvent *)theEvent
slouken@1933
   193
{
slouken@1959
   194
    int button;
slouken@1933
   195
slouken@1959
   196
    switch ([theEvent buttonNumber]) {
slouken@1959
   197
    case 0:
slouken@1959
   198
        button = SDL_BUTTON_LEFT;
slouken@1959
   199
        break;
slouken@1959
   200
    case 1:
slouken@1959
   201
        button = SDL_BUTTON_RIGHT;
slouken@1959
   202
        break;
slouken@1959
   203
    case 2:
slouken@1959
   204
        button = SDL_BUTTON_MIDDLE;
slouken@1959
   205
        break;
slouken@1959
   206
    default:
slouken@1959
   207
        button = [theEvent buttonNumber];
slouken@1959
   208
        break;
slouken@1959
   209
    }
slouken@4484
   210
    SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
slouken@1933
   211
}
slouken@1933
   212
slouken@1933
   213
- (void)rightMouseUp:(NSEvent *)theEvent
slouken@1933
   214
{
slouken@1959
   215
    [self mouseUp:theEvent];
slouken@1933
   216
}
slouken@1933
   217
slouken@1933
   218
- (void)otherMouseUp:(NSEvent *)theEvent
slouken@1933
   219
{
slouken@1959
   220
    [self mouseUp:theEvent];
slouken@1933
   221
}
slouken@1933
   222
slouken@1933
   223
- (void)mouseMoved:(NSEvent *)theEvent
slouken@1933
   224
{
slouken@3685
   225
    SDL_Window *window = _data->window;
slouken@1933
   226
    NSPoint point;
slouken@1933
   227
slouken@3517
   228
    point = [theEvent locationInWindow];
slouken@3517
   229
    point.y = window->h - point.y;
slouken@3507
   230
    if ( point.x < 0 || point.x >= window->w ||
slouken@3507
   231
         point.y < 0 || point.y >= window->h ) {
slouken@4465
   232
        if (SDL_GetMouseFocus() == window) {
slouken@4465
   233
            SDL_SetMouseFocus(NULL);
slouken@2059
   234
        }
slouken@2059
   235
    } else {
slouken@4484
   236
        SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
slouken@2059
   237
    }
slouken@1933
   238
}
slouken@1933
   239
slouken@1957
   240
- (void)mouseDragged:(NSEvent *)theEvent
slouken@1957
   241
{
slouken@1957
   242
    [self mouseMoved:theEvent];
slouken@1957
   243
}
slouken@1957
   244
slouken@1958
   245
- (void)rightMouseDragged:(NSEvent *)theEvent
slouken@1958
   246
{
slouken@1958
   247
    [self mouseMoved:theEvent];
slouken@1958
   248
}
slouken@1958
   249
slouken@1958
   250
- (void)otherMouseDragged:(NSEvent *)theEvent
slouken@1958
   251
{
slouken@1958
   252
    [self mouseMoved:theEvent];
slouken@1958
   253
}
slouken@1958
   254
slouken@1933
   255
- (void)scrollWheel:(NSEvent *)theEvent
slouken@1933
   256
{
slouken@4484
   257
    float x = [theEvent deltaX];
slouken@4484
   258
    float y = [theEvent deltaY];
slouken@4484
   259
slouken@4484
   260
    if (x > 0) {
slouken@4484
   261
        x += 0.9f;
slouken@4484
   262
    } else if (x < 0) {
slouken@4484
   263
        x -= 0.9f;
slouken@4484
   264
    }
slouken@4484
   265
    if (y > 0) {
slouken@4484
   266
        y += 0.9f;
slouken@4484
   267
    } else if (y < 0) {
slouken@4484
   268
        y -= 0.9f;
slouken@4484
   269
    }
slouken@4484
   270
    SDL_SendMouseWheel(_data->window, (int)x, (int)y);
slouken@1933
   271
}
slouken@1933
   272
slouken@4673
   273
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
slouken@4673
   274
{
slouken@4673
   275
    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
slouken@4673
   276
}
slouken@4673
   277
slouken@4673
   278
- (void)touchesMovedWithEvent:(NSEvent *) theEvent
slouken@4673
   279
{
slouken@4673
   280
    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
slouken@4673
   281
}
slouken@4673
   282
slouken@4673
   283
- (void)touchesEndedWithEvent:(NSEvent *) theEvent
slouken@4673
   284
{
slouken@4673
   285
    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
slouken@4673
   286
}
slouken@4673
   287
slouken@4673
   288
- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
slouken@4673
   289
{
slouken@4673
   290
    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
slouken@4673
   291
}
slouken@4673
   292
slouken@4673
   293
- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
slouken@4673
   294
{
slouken@4673
   295
    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
slouken@4673
   296
slouken@4673
   297
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4673
   298
    NSTouch *touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   299
    while (touch) {
slouken@4673
   300
        long touchId = (long)[touch device];
slouken@4673
   301
        if (!SDL_GetTouch(touchId)) {
slouken@4673
   302
            SDL_Touch touch;
slouken@4673
   303
slouken@4673
   304
            touch.id = touchId;
slouken@4673
   305
            touch.x_min = 0;
slouken@4673
   306
            touch.x_max = 1;
slouken@4673
   307
            touch.xres = touch.x_max - touch.x_min;
slouken@4673
   308
            touch.y_min = 0;
slouken@4673
   309
            touch.y_max = 1;
slouken@4673
   310
            touch.yres = touch.y_max - touch.y_min;
slouken@4673
   311
            touch.pressure_min = 0;
slouken@4673
   312
            touch.pressure_max = 1;
slouken@4673
   313
            touch.pressureres = touch.pressure_max - touch.pressure_min;
slouken@4673
   314
            
slouken@4673
   315
            if (SDL_AddTouch(&touch, "") < 0) {
slouken@4673
   316
                return; 
slouken@4673
   317
            }
slouken@4673
   318
        } 
slouken@4673
   319
        float x = [touch normalizedPosition].x;
slouken@4673
   320
        float y = [touch normalizedPosition].y;
slouken@4673
   321
        long fingerId = (long)[touch identity];
slouken@4673
   322
        switch (type) {
slouken@4673
   323
        case COCOA_TOUCH_DOWN:
slouken@4673
   324
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
slouken@4673
   325
            break;
slouken@4673
   326
        case COCOA_TOUCH_UP:
slouken@4673
   327
        case COCOA_TOUCH_CANCELLED:
slouken@4673
   328
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   329
            break;
slouken@4673
   330
        case COCOA_TOUCH_MOVE:
slouken@4673
   331
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   332
            break;
slouken@4673
   333
        }
slouken@4673
   334
        
slouken@4673
   335
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   336
    }
slouken@4673
   337
}
slouken@4673
   338
slouken@1933
   339
@end
slouken@1933
   340
slouken@1973
   341
@interface SDLWindow : NSWindow
slouken@1973
   342
/* These are needed for borderless/fullscreen windows */
slouken@1973
   343
- (BOOL)canBecomeKeyWindow;
slouken@1973
   344
- (BOOL)canBecomeMainWindow;
slouken@1973
   345
@end
slouken@1973
   346
slouken@1973
   347
@implementation SDLWindow
slouken@1973
   348
- (BOOL)canBecomeKeyWindow
slouken@1973
   349
{
slouken@1973
   350
    return YES;
slouken@1973
   351
}
slouken@1973
   352
slouken@1973
   353
- (BOOL)canBecomeMainWindow
slouken@1973
   354
{
slouken@1973
   355
    return YES;
slouken@1973
   356
}
slouken@1973
   357
@end
slouken@1973
   358
slouken@1933
   359
static int
slouken@1951
   360
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   361
{
slouken@1933
   362
    NSAutoreleasePool *pool;
slouken@1951
   363
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
   364
    SDL_VideoDisplay *display = window->display;
slouken@3530
   365
    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
slouken@1933
   366
    SDL_WindowData *data;
slouken@1933
   367
slouken@1933
   368
    /* Allocate the window data */
slouken@1933
   369
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   370
    if (!data) {
slouken@1933
   371
        SDL_OutOfMemory();
slouken@1933
   372
        return -1;
slouken@1933
   373
    }
slouken@3685
   374
    data->window = window;
slouken@3688
   375
    data->nswindow = nswindow;
slouken@1933
   376
    data->created = created;
slouken@3506
   377
    data->display = displaydata->display;
slouken@1951
   378
    data->videodata = videodata;
slouken@1933
   379
slouken@1933
   380
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   381
slouken@1933
   382
    /* Create an event listener for the window */
slouken@1933
   383
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   384
    [data->listener listen:data];
slouken@1933
   385
slouken@1933
   386
    /* Fill in the SDL window with the window data */
slouken@1933
   387
    {
slouken@3530
   388
        SDL_Rect bounds;
slouken@1933
   389
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   390
        ConvertNSRect(&rect);
slouken@3530
   391
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   392
        window->x = (int)rect.origin.x - bounds.x;
slouken@3530
   393
        window->y = (int)rect.origin.y - bounds.y;
slouken@1933
   394
        window->w = (int)rect.size.width;
slouken@1933
   395
        window->h = (int)rect.size.height;
slouken@1933
   396
    }
slouken@1933
   397
    if ([nswindow isVisible]) {
slouken@1933
   398
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   399
    } else {
slouken@1933
   400
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   401
    }
slouken@1933
   402
    {
slouken@1933
   403
        unsigned int style = [nswindow styleMask];
slouken@1933
   404
slouken@1956
   405
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   406
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   407
        } else {
slouken@1933
   408
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   409
        }
slouken@1933
   410
        if (style & NSResizableWindowMask) {
slouken@1933
   411
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   412
        } else {
slouken@1933
   413
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   414
        }
slouken@1933
   415
    }
slouken@1933
   416
    if ([nswindow isZoomed]) {
slouken@1933
   417
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   418
    } else {
slouken@1933
   419
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   420
    }
slouken@1933
   421
    if ([nswindow isMiniaturized]) {
slouken@1933
   422
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   423
    } else {
slouken@1933
   424
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   425
    }
slouken@1933
   426
    if ([nswindow isKeyWindow]) {
slouken@1933
   427
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   428
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   429
slouken@1933
   430
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   431
            /* FIXME */
slouken@1933
   432
        }
slouken@1933
   433
    }
slouken@1933
   434
slouken@1933
   435
    /* All done! */
slouken@1933
   436
    [pool release];
slouken@1933
   437
    window->driverdata = data;
slouken@1933
   438
    return 0;
slouken@1933
   439
}
slouken@1933
   440
slouken@1933
   441
int
slouken@1933
   442
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   443
{
slouken@3506
   444
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   445
    NSWindow *nswindow;
slouken@3685
   446
    SDL_VideoDisplay *display = window->display;
slouken@1933
   447
    NSRect rect;
slouken@3528
   448
    SDL_Rect bounds;
slouken@1933
   449
    unsigned int style;
slouken@1933
   450
slouken@3528
   451
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   452
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   453
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   454
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3528
   455
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   456
        rect.origin.x = bounds.x;
slouken@3528
   457
    } else {
slouken@3530
   458
        rect.origin.x = bounds.x + window->x;
slouken@1933
   459
    }
slouken@3075
   460
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   461
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   462
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3528
   463
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   464
        rect.origin.y = bounds.y;
slouken@3528
   465
    } else {
slouken@3530
   466
        rect.origin.y = bounds.y + window->y;
slouken@1933
   467
    }
slouken@1933
   468
    rect.size.width = window->w;
slouken@1933
   469
    rect.size.height = window->h;
slouken@1933
   470
    ConvertNSRect(&rect);
slouken@1933
   471
slouken@1933
   472
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   473
        style = NSBorderlessWindowMask;
slouken@1933
   474
    } else {
slouken@1933
   475
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   476
    }
slouken@1933
   477
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   478
        style |= NSResizableWindowMask;
slouken@1933
   479
    }
slouken@1933
   480
slouken@3506
   481
    /* Figure out which screen to place this window */
slouken@3506
   482
    NSArray *screens = [NSScreen screens];
slouken@3506
   483
    NSScreen *screen = nil;
slouken@3506
   484
    NSScreen *candidate;
slouken@3525
   485
    int i, count = [screens count];
slouken@3525
   486
    for (i = 0; i < count; ++i) {
slouken@3529
   487
        candidate = [screens objectAtIndex:i];
slouken@3506
   488
        NSRect screenRect = [candidate frame];
slouken@3506
   489
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   490
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   491
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   492
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   493
            screen = candidate;
slouken@3506
   494
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   495
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   496
        }
slouken@3506
   497
    }
slouken@3506
   498
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   499
slouken@1933
   500
    [pool release];
slouken@1933
   501
slouken@1951
   502
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   503
        [nswindow release];
slouken@1933
   504
        return -1;
slouken@1933
   505
    }
slouken@1933
   506
    return 0;
slouken@1933
   507
}
slouken@1933
   508
slouken@1933
   509
int
slouken@1933
   510
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   511
{
slouken@1933
   512
    NSAutoreleasePool *pool;
slouken@1933
   513
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   514
    NSString *title;
slouken@1933
   515
slouken@1933
   516
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   517
slouken@1933
   518
    /* Query the title from the existing window */
slouken@1933
   519
    title = [nswindow title];
slouken@1933
   520
    if (title) {
slouken@1933
   521
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   522
    }
slouken@1933
   523
slouken@1933
   524
    [pool release];
slouken@1933
   525
slouken@1951
   526
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   527
}
slouken@1933
   528
slouken@1933
   529
void
slouken@1933
   530
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   531
{
slouken@1937
   532
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   533
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   534
    NSString *string;
slouken@1933
   535
slouken@1956
   536
    if(window->title) {
slouken@1956
   537
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   538
    } else {
slouken@1956
   539
        string = [[NSString alloc] init];
slouken@1956
   540
    }
slouken@1933
   541
    [nswindow setTitle:string];
slouken@1933
   542
    [string release];
slouken@1956
   543
slouken@1937
   544
    [pool release];
slouken@1933
   545
}
slouken@1933
   546
slouken@1933
   547
void
slouken@1933
   548
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   549
{
slouken@1937
   550
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   551
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@3685
   552
    SDL_VideoDisplay *display = window->display;
slouken@1933
   553
    NSRect rect;
slouken@3528
   554
    SDL_Rect bounds;
slouken@1933
   555
slouken@3528
   556
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   557
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   558
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   559
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   560
    } else {
slouken@3530
   561
        rect.origin.x = bounds.x + window->x;
slouken@3075
   562
    }
slouken@3075
   563
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   564
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   565
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   566
    } else {
slouken@3530
   567
        rect.origin.y = bounds.y + window->y;
slouken@3075
   568
    }
slouken@1933
   569
    rect.size.width = window->w;
slouken@1933
   570
    rect.size.height = window->h;
slouken@1933
   571
    ConvertNSRect(&rect);
slouken@1933
   572
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   573
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   574
    [pool release];
slouken@1933
   575
}
slouken@1933
   576
slouken@1933
   577
void
slouken@1933
   578
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   579
{
slouken@1937
   580
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   581
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   582
    NSSize size;
slouken@1933
   583
slouken@1933
   584
    size.width = window->w;
slouken@1933
   585
    size.height = window->h;
slouken@1933
   586
    [nswindow setContentSize:size];
slouken@1937
   587
    [pool release];
slouken@1933
   588
}
slouken@1933
   589
slouken@1933
   590
void
slouken@1933
   591
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   592
{
slouken@1937
   593
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   594
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   595
slouken@1956
   596
    if (![nswindow isMiniaturized]) {
slouken@1956
   597
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   598
    }
slouken@1937
   599
    [pool release];
slouken@1933
   600
}
slouken@1933
   601
slouken@1933
   602
void
slouken@1933
   603
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   604
{
slouken@1956
   605
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   606
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   607
slouken@1956
   608
    [nswindow orderOut:nil];
slouken@1956
   609
    [pool release];
slouken@1933
   610
}
slouken@1933
   611
slouken@1933
   612
void
slouken@1933
   613
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   614
{
slouken@1937
   615
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   616
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   617
slouken@1933
   618
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   619
    [pool release];
slouken@1933
   620
}
slouken@1933
   621
slouken@1933
   622
void
slouken@1933
   623
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   624
{
slouken@1937
   625
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   626
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   627
slouken@1956
   628
    [nswindow zoom:nil];
slouken@1937
   629
    [pool release];
slouken@1933
   630
}
slouken@1933
   631
slouken@1933
   632
void
slouken@1933
   633
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   634
{
slouken@1937
   635
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   636
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   637
slouken@1956
   638
    [nswindow miniaturize:nil];
slouken@1937
   639
    [pool release];
slouken@1933
   640
}
slouken@1933
   641
slouken@1933
   642
void
slouken@1933
   643
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   644
{
slouken@1956
   645
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   646
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   647
slouken@1956
   648
    if ([nswindow isMiniaturized]) {
slouken@1956
   649
        [nswindow deminiaturize:nil];
slouken@1956
   650
    } else if ([nswindow isZoomed]) {
slouken@1956
   651
        [nswindow zoom:nil];
slouken@1956
   652
    }
slouken@1956
   653
    [pool release];
slouken@1933
   654
}
slouken@1933
   655
slouken@1933
   656
void
slouken@1933
   657
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   658
{
slouken@1933
   659
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   660
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   661
        /* FIXME: Grab mouse */
slouken@1933
   662
    } else {
slouken@1933
   663
        /* FIXME: Release mouse */
slouken@1933
   664
    }
slouken@1933
   665
}
slouken@1933
   666
slouken@1933
   667
void
slouken@1933
   668
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   669
{
slouken@1937
   670
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   671
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   672
slouken@1933
   673
    if (data) {
slouken@1933
   674
        [data->listener close];
slouken@1933
   675
        [data->listener release];
slouken@1933
   676
        if (data->created) {
slouken@3688
   677
            [data->nswindow close];
slouken@1933
   678
        }
slouken@1933
   679
        SDL_free(data);
slouken@1933
   680
    }
slouken@1937
   681
    [pool release];
slouken@1933
   682
}
slouken@1933
   683
slouken@1933
   684
SDL_bool
slouken@1933
   685
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   686
{
slouken@4465
   687
    //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   688
slouken@1933
   689
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   690
        //info->window = nswindow;
slouken@1933
   691
        return SDL_TRUE;
slouken@1933
   692
    } else {
slouken@1933
   693
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   694
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   695
        return SDL_FALSE;
slouken@1933
   696
    }
slouken@1933
   697
}
slouken@1933
   698
slouken@1933
   699
/* vi: set ts=4 sw=4 expandtab: */