src/video/cocoa/SDL_cocoawindow.m
author Jim Grandpre <jim.tla@gmail.com>
Thu, 29 Jul 2010 12:26:59 -0400
changeset 4675 641c13b0ce5f
parent 4674 89d5e2201b00
child 4676 99b4560b7aa1
permissions -rw-r--r--
Added better logging
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)) {
jim@4675
   302
	  printf("Adding touch: %i\n",touchId)
slouken@4673
   303
            SDL_Touch touch;
slouken@4673
   304
slouken@4673
   305
            touch.id = touchId;
slouken@4673
   306
            touch.x_min = 0;
slouken@4673
   307
            touch.x_max = 1;
slouken@4673
   308
            touch.xres = touch.x_max - touch.x_min;
slouken@4673
   309
            touch.y_min = 0;
slouken@4673
   310
            touch.y_max = 1;
slouken@4673
   311
            touch.yres = touch.y_max - touch.y_min;
slouken@4673
   312
            touch.pressure_min = 0;
slouken@4673
   313
            touch.pressure_max = 1;
slouken@4673
   314
            touch.pressureres = touch.pressure_max - touch.pressure_min;
slouken@4673
   315
            
slouken@4673
   316
            if (SDL_AddTouch(&touch, "") < 0) {
jim@4674
   317
                continue;
slouken@4673
   318
            }
jim@4675
   319
	    printf("Success, added touch: %i\n",touchId)
slouken@4673
   320
        } 
slouken@4673
   321
        float x = [touch normalizedPosition].x;
slouken@4673
   322
        float y = [touch normalizedPosition].y;
slouken@4673
   323
        long fingerId = (long)[touch identity];
slouken@4673
   324
        switch (type) {
slouken@4673
   325
        case COCOA_TOUCH_DOWN:
slouken@4673
   326
            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
slouken@4673
   327
            break;
slouken@4673
   328
        case COCOA_TOUCH_UP:
slouken@4673
   329
        case COCOA_TOUCH_CANCELLED:
slouken@4673
   330
            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   331
            break;
slouken@4673
   332
        case COCOA_TOUCH_MOVE:
slouken@4673
   333
            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
slouken@4673
   334
            break;
slouken@4673
   335
        }
slouken@4673
   336
        
slouken@4673
   337
        touch = (NSTouch*)[enumerator nextObject];
slouken@4673
   338
    }
slouken@4673
   339
}
slouken@4673
   340
slouken@1933
   341
@end
slouken@1933
   342
slouken@1973
   343
@interface SDLWindow : NSWindow
slouken@1973
   344
/* These are needed for borderless/fullscreen windows */
slouken@1973
   345
- (BOOL)canBecomeKeyWindow;
slouken@1973
   346
- (BOOL)canBecomeMainWindow;
slouken@1973
   347
@end
slouken@1973
   348
slouken@1973
   349
@implementation SDLWindow
slouken@1973
   350
- (BOOL)canBecomeKeyWindow
slouken@1973
   351
{
slouken@1973
   352
    return YES;
slouken@1973
   353
}
slouken@1973
   354
slouken@1973
   355
- (BOOL)canBecomeMainWindow
slouken@1973
   356
{
slouken@1973
   357
    return YES;
slouken@1973
   358
}
slouken@1973
   359
@end
slouken@1973
   360
slouken@1933
   361
static int
slouken@1951
   362
SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
slouken@1933
   363
{
slouken@1933
   364
    NSAutoreleasePool *pool;
slouken@1951
   365
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
   366
    SDL_VideoDisplay *display = window->display;
slouken@3530
   367
    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
slouken@1933
   368
    SDL_WindowData *data;
slouken@1933
   369
slouken@1933
   370
    /* Allocate the window data */
slouken@1933
   371
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1933
   372
    if (!data) {
slouken@1933
   373
        SDL_OutOfMemory();
slouken@1933
   374
        return -1;
slouken@1933
   375
    }
slouken@3685
   376
    data->window = window;
slouken@3688
   377
    data->nswindow = nswindow;
slouken@1933
   378
    data->created = created;
slouken@3506
   379
    data->display = displaydata->display;
slouken@1951
   380
    data->videodata = videodata;
slouken@1933
   381
slouken@1933
   382
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   383
slouken@1933
   384
    /* Create an event listener for the window */
slouken@1933
   385
    data->listener = [[Cocoa_WindowListener alloc] init];
slouken@1933
   386
    [data->listener listen:data];
slouken@1933
   387
slouken@1933
   388
    /* Fill in the SDL window with the window data */
slouken@1933
   389
    {
slouken@3530
   390
        SDL_Rect bounds;
slouken@1933
   391
        NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
slouken@1933
   392
        ConvertNSRect(&rect);
slouken@3530
   393
        Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   394
        window->x = (int)rect.origin.x - bounds.x;
slouken@3530
   395
        window->y = (int)rect.origin.y - bounds.y;
slouken@1933
   396
        window->w = (int)rect.size.width;
slouken@1933
   397
        window->h = (int)rect.size.height;
slouken@1933
   398
    }
slouken@1933
   399
    if ([nswindow isVisible]) {
slouken@1933
   400
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1933
   401
    } else {
slouken@1933
   402
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1933
   403
    }
slouken@1933
   404
    {
slouken@1933
   405
        unsigned int style = [nswindow styleMask];
slouken@1933
   406
slouken@1956
   407
        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
slouken@1933
   408
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1933
   409
        } else {
slouken@1933
   410
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1933
   411
        }
slouken@1933
   412
        if (style & NSResizableWindowMask) {
slouken@1933
   413
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1933
   414
        } else {
slouken@1933
   415
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1933
   416
        }
slouken@1933
   417
    }
slouken@1933
   418
    if ([nswindow isZoomed]) {
slouken@1933
   419
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1933
   420
    } else {
slouken@1933
   421
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1933
   422
    }
slouken@1933
   423
    if ([nswindow isMiniaturized]) {
slouken@1933
   424
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1933
   425
    } else {
slouken@1933
   426
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1933
   427
    }
slouken@1933
   428
    if ([nswindow isKeyWindow]) {
slouken@1933
   429
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   430
        SDL_SetKeyboardFocus(data->window);
slouken@1933
   431
slouken@1933
   432
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1933
   433
            /* FIXME */
slouken@1933
   434
        }
slouken@1933
   435
    }
slouken@1933
   436
slouken@1933
   437
    /* All done! */
slouken@1933
   438
    [pool release];
slouken@1933
   439
    window->driverdata = data;
slouken@1933
   440
    return 0;
slouken@1933
   441
}
slouken@1933
   442
slouken@1933
   443
int
slouken@1933
   444
Cocoa_CreateWindow(_THIS, SDL_Window * window)
slouken@1933
   445
{
slouken@3506
   446
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   447
    NSWindow *nswindow;
slouken@3685
   448
    SDL_VideoDisplay *display = window->display;
slouken@1933
   449
    NSRect rect;
slouken@3528
   450
    SDL_Rect bounds;
slouken@1933
   451
    unsigned int style;
slouken@1933
   452
slouken@3528
   453
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   454
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   455
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   456
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3528
   457
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   458
        rect.origin.x = bounds.x;
slouken@3528
   459
    } else {
slouken@3530
   460
        rect.origin.x = bounds.x + window->x;
slouken@1933
   461
    }
slouken@3075
   462
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   463
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   464
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3528
   465
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   466
        rect.origin.y = bounds.y;
slouken@3528
   467
    } else {
slouken@3530
   468
        rect.origin.y = bounds.y + window->y;
slouken@1933
   469
    }
slouken@1933
   470
    rect.size.width = window->w;
slouken@1933
   471
    rect.size.height = window->h;
slouken@1933
   472
    ConvertNSRect(&rect);
slouken@1933
   473
slouken@1933
   474
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1933
   475
        style = NSBorderlessWindowMask;
slouken@1933
   476
    } else {
slouken@1933
   477
        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@1933
   478
    }
slouken@1933
   479
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1933
   480
        style |= NSResizableWindowMask;
slouken@1933
   481
    }
slouken@1933
   482
slouken@3506
   483
    /* Figure out which screen to place this window */
slouken@3506
   484
    NSArray *screens = [NSScreen screens];
slouken@3506
   485
    NSScreen *screen = nil;
slouken@3506
   486
    NSScreen *candidate;
slouken@3525
   487
    int i, count = [screens count];
slouken@3525
   488
    for (i = 0; i < count; ++i) {
slouken@3529
   489
        candidate = [screens objectAtIndex:i];
slouken@3506
   490
        NSRect screenRect = [candidate frame];
slouken@3506
   491
        if (rect.origin.x >= screenRect.origin.x &&
slouken@3506
   492
            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
slouken@3506
   493
            rect.origin.y >= screenRect.origin.y &&
slouken@3506
   494
            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
slouken@3506
   495
            screen = candidate;
slouken@3506
   496
            rect.origin.x -= screenRect.origin.x;
slouken@3506
   497
            rect.origin.y -= screenRect.origin.y;
slouken@3506
   498
        }
slouken@3506
   499
    }
slouken@3506
   500
    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
slouken@1933
   501
slouken@1933
   502
    [pool release];
slouken@1933
   503
slouken@1951
   504
    if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
slouken@1933
   505
        [nswindow release];
slouken@1933
   506
        return -1;
slouken@1933
   507
    }
slouken@1933
   508
    return 0;
slouken@1933
   509
}
slouken@1933
   510
slouken@1933
   511
int
slouken@1933
   512
Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1933
   513
{
slouken@1933
   514
    NSAutoreleasePool *pool;
slouken@1933
   515
    NSWindow *nswindow = (NSWindow *) data;
slouken@1933
   516
    NSString *title;
slouken@1933
   517
slouken@1933
   518
    pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   519
slouken@1933
   520
    /* Query the title from the existing window */
slouken@1933
   521
    title = [nswindow title];
slouken@1933
   522
    if (title) {
slouken@1933
   523
        window->title = SDL_strdup([title UTF8String]);
slouken@1933
   524
    }
slouken@1933
   525
slouken@1933
   526
    [pool release];
slouken@1933
   527
slouken@1951
   528
    return SetupWindowData(_this, window, nswindow, SDL_FALSE);
slouken@1933
   529
}
slouken@1933
   530
slouken@1933
   531
void
slouken@1933
   532
Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1933
   533
{
slouken@1937
   534
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   535
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   536
    NSString *string;
slouken@1933
   537
slouken@1956
   538
    if(window->title) {
slouken@1956
   539
        string = [[NSString alloc] initWithUTF8String:window->title];
slouken@1956
   540
    } else {
slouken@1956
   541
        string = [[NSString alloc] init];
slouken@1956
   542
    }
slouken@1933
   543
    [nswindow setTitle:string];
slouken@1933
   544
    [string release];
slouken@1956
   545
slouken@1937
   546
    [pool release];
slouken@1933
   547
}
slouken@1933
   548
slouken@1933
   549
void
slouken@1933
   550
Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1933
   551
{
slouken@1937
   552
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   553
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@3685
   554
    SDL_VideoDisplay *display = window->display;
slouken@1933
   555
    NSRect rect;
slouken@3528
   556
    SDL_Rect bounds;
slouken@1933
   557
slouken@3528
   558
    Cocoa_GetDisplayBounds(_this, display, &bounds);
slouken@3075
   559
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   560
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   561
        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
slouken@3075
   562
    } else {
slouken@3530
   563
        rect.origin.x = bounds.x + window->x;
slouken@3075
   564
    }
slouken@3075
   565
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@3075
   566
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@3528
   567
        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
slouken@3075
   568
    } else {
slouken@3530
   569
        rect.origin.y = bounds.y + window->y;
slouken@3075
   570
    }
slouken@1933
   571
    rect.size.width = window->w;
slouken@1933
   572
    rect.size.height = window->h;
slouken@1933
   573
    ConvertNSRect(&rect);
slouken@1933
   574
    rect = [nswindow frameRectForContentRect:rect];
slouken@1933
   575
    [nswindow setFrameOrigin:rect.origin];
slouken@1937
   576
    [pool release];
slouken@1933
   577
}
slouken@1933
   578
slouken@1933
   579
void
slouken@1933
   580
Cocoa_SetWindowSize(_THIS, SDL_Window * window)
slouken@1933
   581
{
slouken@1937
   582
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   583
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   584
    NSSize size;
slouken@1933
   585
slouken@1933
   586
    size.width = window->w;
slouken@1933
   587
    size.height = window->h;
slouken@1933
   588
    [nswindow setContentSize:size];
slouken@1937
   589
    [pool release];
slouken@1933
   590
}
slouken@1933
   591
slouken@1933
   592
void
slouken@1933
   593
Cocoa_ShowWindow(_THIS, SDL_Window * window)
slouken@1933
   594
{
slouken@1937
   595
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   596
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   597
slouken@1956
   598
    if (![nswindow isMiniaturized]) {
slouken@1956
   599
        [nswindow makeKeyAndOrderFront:nil];
slouken@1956
   600
    }
slouken@1937
   601
    [pool release];
slouken@1933
   602
}
slouken@1933
   603
slouken@1933
   604
void
slouken@1933
   605
Cocoa_HideWindow(_THIS, SDL_Window * window)
slouken@1933
   606
{
slouken@1956
   607
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   608
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   609
slouken@1956
   610
    [nswindow orderOut:nil];
slouken@1956
   611
    [pool release];
slouken@1933
   612
}
slouken@1933
   613
slouken@1933
   614
void
slouken@1933
   615
Cocoa_RaiseWindow(_THIS, SDL_Window * window)
slouken@1933
   616
{
slouken@1937
   617
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   618
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   619
slouken@1933
   620
    [nswindow makeKeyAndOrderFront:nil];
slouken@1937
   621
    [pool release];
slouken@1933
   622
}
slouken@1933
   623
slouken@1933
   624
void
slouken@1933
   625
Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1933
   626
{
slouken@1937
   627
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   628
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   629
slouken@1956
   630
    [nswindow zoom:nil];
slouken@1937
   631
    [pool release];
slouken@1933
   632
}
slouken@1933
   633
slouken@1933
   634
void
slouken@1933
   635
Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1933
   636
{
slouken@1937
   637
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   638
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   639
slouken@1956
   640
    [nswindow miniaturize:nil];
slouken@1937
   641
    [pool release];
slouken@1933
   642
}
slouken@1933
   643
slouken@1933
   644
void
slouken@1933
   645
Cocoa_RestoreWindow(_THIS, SDL_Window * window)
slouken@1933
   646
{
slouken@1956
   647
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3688
   648
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   649
slouken@1956
   650
    if ([nswindow isMiniaturized]) {
slouken@1956
   651
        [nswindow deminiaturize:nil];
slouken@1956
   652
    } else if ([nswindow isZoomed]) {
slouken@1956
   653
        [nswindow zoom:nil];
slouken@1956
   654
    }
slouken@1956
   655
    [pool release];
slouken@1933
   656
}
slouken@1933
   657
slouken@1933
   658
void
slouken@1933
   659
Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1933
   660
{
slouken@1933
   661
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1933
   662
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1933
   663
        /* FIXME: Grab mouse */
slouken@1933
   664
    } else {
slouken@1933
   665
        /* FIXME: Release mouse */
slouken@1933
   666
    }
slouken@1933
   667
}
slouken@1933
   668
slouken@1933
   669
void
slouken@1933
   670
Cocoa_DestroyWindow(_THIS, SDL_Window * window)
slouken@1933
   671
{
slouken@1937
   672
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@1933
   673
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1933
   674
slouken@1933
   675
    if (data) {
slouken@1933
   676
        [data->listener close];
slouken@1933
   677
        [data->listener release];
slouken@1933
   678
        if (data->created) {
slouken@3688
   679
            [data->nswindow close];
slouken@1933
   680
        }
slouken@1933
   681
        SDL_free(data);
slouken@1933
   682
    }
slouken@1937
   683
    [pool release];
slouken@1933
   684
}
slouken@1933
   685
slouken@1933
   686
SDL_bool
slouken@1933
   687
Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1933
   688
{
slouken@4465
   689
    //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
slouken@1933
   690
slouken@1933
   691
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1933
   692
        //info->window = nswindow;
slouken@1933
   693
        return SDL_TRUE;
slouken@1933
   694
    } else {
slouken@1933
   695
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1933
   696
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1933
   697
        return SDL_FALSE;
slouken@1933
   698
    }
slouken@1933
   699
}
slouken@1933
   700
slouken@1933
   701
/* vi: set ts=4 sw=4 expandtab: */