src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 22:03:39 -0800
changeset 5380 2de85077eb0b
parent 5379 78c071fe4c37
child 5388 6e586f65f8ef
permissions -rw-r--r--
Simplified and unified the window creation process a little.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_syswm.h"
    25 #include "../SDL_sysvideo.h"
    26 #include "../../events/SDL_keyboard_c.h"
    27 #include "../../events/SDL_mouse_c.h"
    28 #include "../../events/SDL_touch_c.h"
    29 #include "../../events/SDL_windowevents_c.h"
    30 #include "SDL_cocoavideo.h"
    31 #include "SDL_cocoashape.h"
    32 #include "SDL_cocoamouse.h"
    33 
    34 static __inline__ void ConvertNSRect(NSRect *r)
    35 {
    36     r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
    37 }
    38 
    39 @implementation Cocoa_WindowListener
    40 
    41 - (void)listen:(SDL_WindowData *)data
    42 {
    43     NSNotificationCenter *center;
    44     NSWindow *window = data->nswindow;
    45     NSView *view = [window contentView];
    46 
    47     _data = data;
    48 
    49     center = [NSNotificationCenter defaultCenter];
    50 
    51     if ([window delegate] != nil) {
    52         [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
    53         [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
    54         [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
    55         [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
    56         [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
    57         [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
    58         [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
    59     } else {
    60         [window setDelegate:self];
    61     }
    62     [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
    63     [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
    64 
    65     [window setNextResponder:self];
    66     [window setAcceptsMouseMovedEvents:YES];
    67 
    68     [view setNextResponder:self];
    69     [view addTrackingRect:[view visibleRect] owner:self userData:nil assumeInside:NO];
    70 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
    71     [view setAcceptsTouchEvents:YES];
    72 #endif
    73 }
    74 
    75 - (void)close
    76 {
    77     NSNotificationCenter *center;
    78     NSWindow *window = _data->nswindow;
    79     NSView *view = [window contentView];
    80 
    81     center = [NSNotificationCenter defaultCenter];
    82 
    83     if ([window delegate] != self) {
    84         [center removeObserver:self name:NSWindowDidExposeNotification object:window];
    85         [center removeObserver:self name:NSWindowDidMoveNotification object:window];
    86         [center removeObserver:self name:NSWindowDidResizeNotification object:window];
    87         [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
    88         [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
    89         [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
    90         [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
    91     } else {
    92         [window setDelegate:nil];
    93     }
    94     [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
    95     [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
    96 
    97     if ([window nextResponder] == self) {
    98         [window setNextResponder:nil];
    99     }
   100     if ([view nextResponder] == self) {
   101         [view setNextResponder:nil];
   102     }
   103 }
   104 
   105 - (BOOL)windowShouldClose:(id)sender
   106 {
   107     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   108     return NO;
   109 }
   110 
   111 - (void)windowDidExpose:(NSNotification *)aNotification
   112 {
   113     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
   114 }
   115 
   116 - (void)windowDidMove:(NSNotification *)aNotification
   117 {
   118     int x, y;
   119     NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
   120     ConvertNSRect(&rect);
   121     x = (int)rect.origin.x;
   122     y = (int)rect.origin.y;
   123     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
   124 }
   125 
   126 - (void)windowDidResize:(NSNotification *)aNotification
   127 {
   128     int w, h;
   129     NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
   130     w = (int)rect.size.width;
   131     h = (int)rect.size.height;
   132     if (SDL_IsShapedWindow(_data->window))
   133         Cocoa_ResizeWindowShape(_data->window);
   134     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
   135 }
   136 
   137 - (void)windowDidMiniaturize:(NSNotification *)aNotification
   138 {
   139     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   140 }
   141 
   142 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
   143 {
   144     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   145 }
   146 
   147 - (void)windowDidBecomeKey:(NSNotification *)aNotification
   148 {
   149     SDL_Window *window = _data->window;
   150 
   151     /* We're going to get keyboard events, since we're key. */
   152     SDL_SetKeyboardFocus(window);
   153 
   154     /* If we just gained focus we need the updated mouse position */
   155     if (SDL_GetMouseFocus() == window) {
   156         NSPoint point;
   157         point = [NSEvent mouseLocation];
   158         point = [_data->nswindow convertScreenToBase:point];
   159         point.y = window->h - point.y;
   160         SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
   161     }
   162 
   163     /* Check to see if someone updated the clipboard */
   164     Cocoa_CheckClipboardUpdate(_data->videodata);
   165 }
   166 
   167 - (void)windowDidResignKey:(NSNotification *)aNotification
   168 {
   169     /* Some other window will get mouse events, since we're not key. */
   170     if (SDL_GetMouseFocus() == _data->window) {
   171         SDL_SetMouseFocus(NULL);
   172     }
   173 
   174     /* Some other window will get keyboard events, since we're not key. */
   175     if (SDL_GetKeyboardFocus() == _data->window) {
   176         SDL_SetKeyboardFocus(NULL);
   177     }
   178 }
   179 
   180 - (void)windowDidHide:(NSNotification *)aNotification
   181 {
   182     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   183 }
   184 
   185 - (void)windowDidUnhide:(NSNotification *)aNotification
   186 {
   187     SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   188 }
   189 
   190 - (void)mouseDown:(NSEvent *)theEvent
   191 {
   192     int button;
   193 
   194     switch ([theEvent buttonNumber]) {
   195     case 0:
   196         button = SDL_BUTTON_LEFT;
   197         break;
   198     case 1:
   199         button = SDL_BUTTON_RIGHT;
   200         break;
   201     case 2:
   202         button = SDL_BUTTON_MIDDLE;
   203         break;
   204     default:
   205         button = [theEvent buttonNumber] + 1;
   206         break;
   207     }
   208     SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
   209 }
   210 
   211 - (void)rightMouseDown:(NSEvent *)theEvent
   212 {
   213     [self mouseDown:theEvent];
   214 }
   215 
   216 - (void)otherMouseDown:(NSEvent *)theEvent
   217 {
   218     [self mouseDown:theEvent];
   219 }
   220 
   221 - (void)mouseUp:(NSEvent *)theEvent
   222 {
   223     int button;
   224 
   225     switch ([theEvent buttonNumber]) {
   226     case 0:
   227         button = SDL_BUTTON_LEFT;
   228         break;
   229     case 1:
   230         button = SDL_BUTTON_RIGHT;
   231         break;
   232     case 2:
   233         button = SDL_BUTTON_MIDDLE;
   234         break;
   235     default:
   236         button = [theEvent buttonNumber] + 1;
   237         break;
   238     }
   239     SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
   240 }
   241 
   242 - (void)rightMouseUp:(NSEvent *)theEvent
   243 {
   244     [self mouseUp:theEvent];
   245 }
   246 
   247 - (void)otherMouseUp:(NSEvent *)theEvent
   248 {
   249     [self mouseUp:theEvent];
   250 }
   251 
   252 - (void)mouseEntered:(NSEvent *)theEvent
   253 {
   254     SDL_Mouse *mouse = SDL_GetMouse();
   255 
   256     SDL_SetMouseFocus(_data->window);
   257 
   258     if (!mouse->cursor_shown) {
   259         [NSCursor hide];
   260     }
   261 }
   262 
   263 - (void)mouseExited:(NSEvent *)theEvent
   264 {
   265     SDL_Window *window = _data->window;
   266     SDL_Mouse *mouse = SDL_GetMouse();
   267 
   268     if (SDL_GetMouseFocus() == window) {
   269         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   270             int x, y;
   271             NSPoint point;
   272             CGPoint cgpoint;
   273 
   274             point = [theEvent locationInWindow];
   275             point.y = window->h - point.y;
   276 
   277             SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
   278             SDL_GetMouseState(&x, &y);
   279             cgpoint.x = window->x + x;
   280             cgpoint.y = window->y + y;
   281             CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
   282         } else {
   283             SDL_SetMouseFocus(NULL);
   284         }
   285     }
   286 
   287     if (!mouse->cursor_shown) {
   288         [NSCursor unhide];
   289     }
   290 }
   291 
   292 - (void)mouseMoved:(NSEvent *)theEvent
   293 {
   294     SDL_Window *window = _data->window;
   295 
   296 #ifdef RELATIVE_MOTION
   297     if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   298         return;
   299     }
   300 #endif
   301 
   302     if (SDL_GetMouseFocus() == window) {
   303         NSPoint point;
   304 
   305         point = [theEvent locationInWindow];
   306         point.y = window->h - point.y;
   307 
   308         SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
   309     }
   310 }
   311 
   312 - (void)mouseDragged:(NSEvent *)theEvent
   313 {
   314     [self mouseMoved:theEvent];
   315 }
   316 
   317 - (void)rightMouseDragged:(NSEvent *)theEvent
   318 {
   319     [self mouseMoved:theEvent];
   320 }
   321 
   322 - (void)otherMouseDragged:(NSEvent *)theEvent
   323 {
   324     [self mouseMoved:theEvent];
   325 }
   326 
   327 - (void)scrollWheel:(NSEvent *)theEvent
   328 {
   329     Cocoa_HandleMouseWheel(_data->window, theEvent);
   330 }
   331 
   332 - (void)touchesBeganWithEvent:(NSEvent *) theEvent
   333 {
   334     [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
   335 }
   336 
   337 - (void)touchesMovedWithEvent:(NSEvent *) theEvent
   338 {
   339     [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
   340 }
   341 
   342 - (void)touchesEndedWithEvent:(NSEvent *) theEvent
   343 {
   344     [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
   345 }
   346 
   347 - (void)touchesCancelledWithEvent:(NSEvent *) theEvent
   348 {
   349     [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
   350 }
   351 
   352 - (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
   353 {
   354 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
   355     NSSet *touches = 0;
   356     NSEnumerator *enumerator;
   357     NSTouch *touch;
   358 
   359     switch (type) {
   360         case COCOA_TOUCH_DOWN:
   361             touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
   362             break;
   363         case COCOA_TOUCH_UP:
   364         case COCOA_TOUCH_CANCELLED:
   365             touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
   366             break;
   367         case COCOA_TOUCH_MOVE:
   368             touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
   369             break;
   370     }
   371 
   372     enumerator = [touches objectEnumerator];
   373     touch = (NSTouch*)[enumerator nextObject];
   374     while (touch) {
   375         SDL_TouchID touchId = (SDL_TouchID)[touch device];
   376         if (!SDL_GetTouch(touchId)) {
   377             SDL_Touch touch;
   378 
   379             touch.id = touchId;
   380             touch.x_min = 0;
   381             touch.x_max = 1;
   382             touch.native_xres = touch.x_max - touch.x_min;
   383             touch.y_min = 0;
   384             touch.y_max = 1;
   385             touch.native_yres = touch.y_max - touch.y_min;
   386             touch.pressure_min = 0;
   387             touch.pressure_max = 1;
   388             touch.native_pressureres = touch.pressure_max - touch.pressure_min;
   389             
   390             if (SDL_AddTouch(&touch, "") < 0) {
   391                 return;
   392             }
   393         } 
   394 
   395         SDL_FingerID fingerId = (SDL_FingerID)[touch identity];
   396         float x = [touch normalizedPosition].x;
   397         float y = [touch normalizedPosition].y;
   398         /* Make the origin the upper left instead of the lower left */
   399         y = 1.0f - y;
   400 
   401         switch (type) {
   402         case COCOA_TOUCH_DOWN:
   403             SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
   404             break;
   405         case COCOA_TOUCH_UP:
   406         case COCOA_TOUCH_CANCELLED:
   407             SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
   408             break;
   409         case COCOA_TOUCH_MOVE:
   410             SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
   411             break;
   412         }
   413         
   414         touch = (NSTouch*)[enumerator nextObject];
   415     }
   416 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 */
   417 }
   418 
   419 @end
   420 
   421 @interface SDLWindow : NSWindow
   422 /* These are needed for borderless/fullscreen windows */
   423 - (BOOL)canBecomeKeyWindow;
   424 - (BOOL)canBecomeMainWindow;
   425 @end
   426 
   427 @implementation SDLWindow
   428 - (BOOL)canBecomeKeyWindow
   429 {
   430     return YES;
   431 }
   432 
   433 - (BOOL)canBecomeMainWindow
   434 {
   435     return YES;
   436 }
   437 @end
   438 
   439 @interface SDLView : NSView
   440 /* The default implementation doesn't pass rightMouseDown to responder chain */
   441 - (void)rightMouseDown:(NSEvent *)theEvent;
   442 @end
   443 
   444 @implementation SDLView
   445 - (void)rightMouseDown:(NSEvent *)theEvent
   446 {
   447     [[self nextResponder] rightMouseDown:theEvent];
   448 }
   449 @end
   450 
   451 static unsigned int
   452 GetWindowStyle(SDL_Window * window)
   453 {
   454     unsigned int style;
   455 
   456 	if (window->flags & SDL_WINDOW_FULLSCREEN) {
   457         style = NSBorderlessWindowMask;
   458 	} else {
   459 		if (window->flags & SDL_WINDOW_BORDERLESS) {
   460 			style = NSBorderlessWindowMask;
   461 		} else {
   462 			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   463 		}
   464 		if (window->flags & SDL_WINDOW_RESIZABLE) {
   465 			style |= NSResizableWindowMask;
   466 		}
   467 	}
   468     return style;
   469 }
   470 
   471 static int
   472 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   473 {
   474     NSAutoreleasePool *pool;
   475     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   476     SDL_WindowData *data;
   477 
   478     /* Allocate the window data */
   479     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   480     if (!data) {
   481         SDL_OutOfMemory();
   482         return -1;
   483     }
   484     data->window = window;
   485     data->nswindow = nswindow;
   486     data->created = created;
   487     data->videodata = videodata;
   488 
   489     pool = [[NSAutoreleasePool alloc] init];
   490 
   491     /* Create an event listener for the window */
   492     data->listener = [[Cocoa_WindowListener alloc] init];
   493 
   494     /* Fill in the SDL window with the window data */
   495     {
   496         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   497         NSView *contentView = [[SDLView alloc] initWithFrame:rect];
   498         [nswindow setContentView: contentView];
   499         [contentView release];
   500 
   501         ConvertNSRect(&rect);
   502         window->x = (int)rect.origin.x;
   503         window->y = (int)rect.origin.y;
   504         window->w = (int)rect.size.width;
   505         window->h = (int)rect.size.height;
   506     }
   507 
   508     /* Set up the listener after we create the view */
   509     [data->listener listen:data];
   510 
   511     if ([nswindow isVisible]) {
   512         window->flags |= SDL_WINDOW_SHOWN;
   513     } else {
   514         window->flags &= ~SDL_WINDOW_SHOWN;
   515     }
   516     {
   517         unsigned int style = [nswindow styleMask];
   518 
   519         if (style == NSBorderlessWindowMask) {
   520             window->flags |= SDL_WINDOW_BORDERLESS;
   521         } else {
   522             window->flags &= ~SDL_WINDOW_BORDERLESS;
   523         }
   524         if (style & NSResizableWindowMask) {
   525             window->flags |= SDL_WINDOW_RESIZABLE;
   526         } else {
   527             window->flags &= ~SDL_WINDOW_RESIZABLE;
   528         }
   529     }
   530     /* isZoomed always returns true if the window is not resizable */
   531     if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
   532         window->flags |= SDL_WINDOW_MAXIMIZED;
   533     } else {
   534         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   535     }
   536     if ([nswindow isMiniaturized]) {
   537         window->flags |= SDL_WINDOW_MINIMIZED;
   538     } else {
   539         window->flags &= ~SDL_WINDOW_MINIMIZED;
   540     }
   541     if ([nswindow isKeyWindow]) {
   542         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   543         SDL_SetKeyboardFocus(data->window);
   544     }
   545 
   546     /* All done! */
   547     [pool release];
   548     window->driverdata = data;
   549     return 0;
   550 }
   551 
   552 int
   553 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   554 {
   555     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   556     NSWindow *nswindow;
   557     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   558     NSRect rect;
   559     SDL_Rect bounds;
   560     unsigned int style;
   561 
   562     Cocoa_GetDisplayBounds(_this, display, &bounds);
   563     rect.origin.x = window->x;
   564     rect.origin.y = window->y;
   565     rect.size.width = window->w;
   566     rect.size.height = window->h;
   567     ConvertNSRect(&rect);
   568 
   569     style = GetWindowStyle(window);
   570 
   571     /* Figure out which screen to place this window */
   572     NSArray *screens = [NSScreen screens];
   573     NSScreen *screen = nil;
   574     NSScreen *candidate;
   575     int i, count = [screens count];
   576     for (i = 0; i < count; ++i) {
   577         candidate = [screens objectAtIndex:i];
   578         NSRect screenRect = [candidate frame];
   579         if (rect.origin.x >= screenRect.origin.x &&
   580             rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   581             rect.origin.y >= screenRect.origin.y &&
   582             rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   583             screen = candidate;
   584             rect.origin.x -= screenRect.origin.x;
   585             rect.origin.y -= screenRect.origin.y;
   586         }
   587     }
   588     nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
   589 
   590     [pool release];
   591 
   592     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   593         [nswindow release];
   594         return -1;
   595     }
   596     return 0;
   597 }
   598 
   599 int
   600 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   601 {
   602     NSAutoreleasePool *pool;
   603     NSWindow *nswindow = (NSWindow *) data;
   604     NSString *title;
   605 
   606     pool = [[NSAutoreleasePool alloc] init];
   607 
   608     /* Query the title from the existing window */
   609     title = [nswindow title];
   610     if (title) {
   611         window->title = SDL_strdup([title UTF8String]);
   612     }
   613 
   614     [pool release];
   615 
   616     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   617 }
   618 
   619 void
   620 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   621 {
   622     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   623     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   624     NSString *string;
   625 
   626     if(window->title) {
   627         string = [[NSString alloc] initWithUTF8String:window->title];
   628     } else {
   629         string = [[NSString alloc] init];
   630     }
   631     [nswindow setTitle:string];
   632     [string release];
   633 
   634     [pool release];
   635 }
   636 
   637 void
   638 Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   639 {
   640     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   641     NSImage *nsimage = Cocoa_CreateImage(icon);
   642 
   643     if (nsimage) {
   644         [NSApp setApplicationIconImage:nsimage];
   645     }
   646 
   647     [pool release];
   648 }
   649 
   650 void
   651 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   652 {
   653     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   654     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   655     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   656     NSRect rect;
   657     SDL_Rect bounds;
   658 
   659     Cocoa_GetDisplayBounds(_this, display, &bounds);
   660     if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
   661         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   662     } else {
   663         rect.origin.x = window->x;
   664     }
   665     if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
   666         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   667     } else {
   668         rect.origin.y = window->y;
   669     }
   670     rect.size.width = window->w;
   671     rect.size.height = window->h;
   672     ConvertNSRect(&rect);
   673     rect = [nswindow frameRectForContentRect:rect];
   674     [nswindow setFrameOrigin:rect.origin];
   675     [pool release];
   676 }
   677 
   678 void
   679 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   680 {
   681     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   682     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   683     NSSize size;
   684 
   685     size.width = window->w;
   686     size.height = window->h;
   687     [nswindow setContentSize:size];
   688     [pool release];
   689 }
   690 
   691 void
   692 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   693 {
   694     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   695     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   696 
   697     if (![nswindow isMiniaturized]) {
   698         [nswindow makeKeyAndOrderFront:nil];
   699     }
   700     [pool release];
   701 }
   702 
   703 void
   704 Cocoa_HideWindow(_THIS, SDL_Window * window)
   705 {
   706     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   707     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   708 
   709     [nswindow orderOut:nil];
   710     [pool release];
   711 }
   712 
   713 void
   714 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   715 {
   716     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   717     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   718 
   719     [nswindow makeKeyAndOrderFront:nil];
   720     [pool release];
   721 }
   722 
   723 void
   724 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   725 {
   726     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   727     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   728 
   729     [nswindow zoom:nil];
   730     [pool release];
   731 }
   732 
   733 void
   734 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   735 {
   736     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   737     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   738 
   739     [nswindow miniaturize:nil];
   740     [pool release];
   741 }
   742 
   743 void
   744 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   745 {
   746     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   747     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   748 
   749     if ([nswindow isMiniaturized]) {
   750         [nswindow deminiaturize:nil];
   751     } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
   752         [nswindow zoom:nil];
   753     }
   754     [pool release];
   755 }
   756 
   757 void
   758 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   759 {
   760     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   761     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   762     NSWindow *nswindow = data->nswindow;
   763     NSRect rect;
   764 
   765     if (fullscreen) {
   766         SDL_Rect bounds;
   767 
   768         Cocoa_GetDisplayBounds(_this, display, &bounds);
   769         rect.origin.x = bounds.x;
   770         rect.origin.y = bounds.y;
   771         rect.size.width = bounds.w;
   772         rect.size.height = bounds.h;
   773         ConvertNSRect(&rect);
   774 
   775         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   776             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
   777         }
   778         [nswindow setFrameOrigin:rect.origin];
   779         [nswindow setContentSize:rect.size];
   780     } else {
   781         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   782             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
   783         }
   784 
   785         // This doesn't seem to do anything...
   786         //[nswindow setFrameOrigin:origin];
   787     }
   788 
   789 #ifdef FULLSCREEN_TOGGLEABLE
   790     if (fullscreen) {
   791         /* OpenGL is rendering to the window, so make it visible! */
   792         [nswindow setLevel:CGShieldingWindowLevel()];
   793     } else {
   794         [nswindow setLevel:kCGNormalWindowLevel];
   795     }
   796 #endif
   797     [nswindow makeKeyAndOrderFront:nil];
   798 
   799     [pool release];
   800 }
   801 
   802 NSPoint origin;
   803 void
   804 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   805 {
   806 #ifdef RELATIVE_MOTION
   807     /* FIXME: work in progress
   808         You set relative mode by using the following code in conjunction with
   809         CGDisplayHideCursor(kCGDirectMainDisplay) and
   810         CGDisplayShowCursor(kCGDirectMainDisplay)
   811     */
   812     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   813         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   814         CGAssociateMouseAndMouseCursorPosition(NO);
   815     } else {
   816         CGAssociateMouseAndMouseCursorPosition(YES);
   817     }
   818 #else
   819     /* Move the cursor to the nearest point in the window */
   820     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   821         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   822         int x, y;
   823         CGPoint cgpoint;
   824 
   825         SDL_GetMouseState(&x, &y);
   826         cgpoint.x = window->x + x;
   827         cgpoint.y = window->y + y;
   828         CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
   829     }
   830 #endif
   831 }
   832 
   833 void
   834 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   835 {
   836     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   837     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   838 
   839     if (data) {
   840         [data->listener close];
   841         [data->listener release];
   842         if (data->created) {
   843             [data->nswindow close];
   844         }
   845         SDL_free(data);
   846     }
   847     [pool release];
   848 }
   849 
   850 SDL_bool
   851 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   852 {
   853     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   854 
   855     if (info->version.major <= SDL_MAJOR_VERSION) {
   856         info->subsystem = SDL_SYSWM_COCOA;
   857         info->info.cocoa.window = nswindow;
   858         return SDL_TRUE;
   859     } else {
   860         SDL_SetError("Application not compiled with SDL %d.%d\n",
   861                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   862         return SDL_FALSE;
   863     }
   864 }
   865 
   866 /* vi: set ts=4 sw=4 expandtab: */