src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 20:35:44 -0800
changeset 5378 738a96883fa6
parent 5376 183ec2d4485c
child 5379 78c071fe4c37
permissions -rw-r--r--
Fixed compiler warning
     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 @end
   441 
   442 @implementation SDLView
   443 - (void)rightMouseDown:(NSEvent *)theEvent
   444 {
   445     [[self nextResponder] rightMouseDown:theEvent];
   446 }
   447 @end
   448 
   449 static unsigned int
   450 GetWindowStyle(SDL_Window * window)
   451 {
   452     unsigned int style;
   453 
   454 	if (window->flags & SDL_WINDOW_FULLSCREEN) {
   455         style = NSBorderlessWindowMask;
   456 	} else {
   457 		if (window->flags & SDL_WINDOW_BORDERLESS) {
   458 			style = NSBorderlessWindowMask;
   459 		} else {
   460 			style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   461 		}
   462 		if (window->flags & SDL_WINDOW_RESIZABLE) {
   463 			style |= NSResizableWindowMask;
   464 		}
   465 	}
   466     return style;
   467 }
   468 
   469 static int
   470 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   471 {
   472     NSAutoreleasePool *pool;
   473     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   474     SDL_WindowData *data;
   475 
   476     /* Allocate the window data */
   477     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   478     if (!data) {
   479         SDL_OutOfMemory();
   480         return -1;
   481     }
   482     data->window = window;
   483     data->nswindow = nswindow;
   484     data->created = created;
   485     data->videodata = videodata;
   486 
   487     pool = [[NSAutoreleasePool alloc] init];
   488 
   489     /* Create an event listener for the window */
   490     data->listener = [[Cocoa_WindowListener alloc] init];
   491 
   492     /* Fill in the SDL window with the window data */
   493     {
   494         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   495         NSView *contentView = [[SDLView alloc] initWithFrame:rect];
   496         [nswindow setContentView: contentView];
   497         [contentView release];
   498 
   499         ConvertNSRect(&rect);
   500         window->x = (int)rect.origin.x;
   501         window->y = (int)rect.origin.y;
   502         window->w = (int)rect.size.width;
   503         window->h = (int)rect.size.height;
   504     }
   505 
   506     /* Set up the listener after we create the view */
   507     [data->listener listen:data];
   508 
   509     if ([nswindow isVisible]) {
   510         window->flags |= SDL_WINDOW_SHOWN;
   511     } else {
   512         window->flags &= ~SDL_WINDOW_SHOWN;
   513     }
   514     {
   515         unsigned int style = [nswindow styleMask];
   516 
   517         if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
   518             window->flags |= SDL_WINDOW_BORDERLESS;
   519         } else {
   520             window->flags &= ~SDL_WINDOW_BORDERLESS;
   521         }
   522         if (style & NSResizableWindowMask) {
   523             window->flags |= SDL_WINDOW_RESIZABLE;
   524         } else {
   525             window->flags &= ~SDL_WINDOW_RESIZABLE;
   526         }
   527     }
   528     if ([nswindow isZoomed]) {
   529         window->flags |= SDL_WINDOW_MAXIMIZED;
   530     } else {
   531         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   532     }
   533     if ([nswindow isMiniaturized]) {
   534         window->flags |= SDL_WINDOW_MINIMIZED;
   535     } else {
   536         window->flags &= ~SDL_WINDOW_MINIMIZED;
   537     }
   538     if ([nswindow isKeyWindow]) {
   539         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   540         SDL_SetKeyboardFocus(data->window);
   541 
   542         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   543             /* FIXME */
   544         }
   545     }
   546 
   547     /* All done! */
   548     [pool release];
   549     window->driverdata = data;
   550     return 0;
   551 }
   552 
   553 int
   554 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   555 {
   556     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   557     NSWindow *nswindow;
   558     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   559     NSRect rect;
   560     SDL_Rect bounds;
   561     unsigned int style;
   562 
   563     Cocoa_GetDisplayBounds(_this, display, &bounds);
   564     if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
   565         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   566     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
   567         rect.origin.x = bounds.x;
   568     } else {
   569         rect.origin.x = window->x;
   570     }
   571     if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
   572         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   573     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
   574         rect.origin.y = bounds.y;
   575     } else {
   576         rect.origin.y = window->y;
   577     }
   578     rect.size.width = window->w;
   579     rect.size.height = window->h;
   580     ConvertNSRect(&rect);
   581 
   582     style = GetWindowStyle(window);
   583 
   584     /* Figure out which screen to place this window */
   585     NSArray *screens = [NSScreen screens];
   586     NSScreen *screen = nil;
   587     NSScreen *candidate;
   588     int i, count = [screens count];
   589     for (i = 0; i < count; ++i) {
   590         candidate = [screens objectAtIndex:i];
   591         NSRect screenRect = [candidate frame];
   592         if (rect.origin.x >= screenRect.origin.x &&
   593             rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   594             rect.origin.y >= screenRect.origin.y &&
   595             rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   596             screen = candidate;
   597             rect.origin.x -= screenRect.origin.x;
   598             rect.origin.y -= screenRect.origin.y;
   599         }
   600     }
   601     nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
   602 
   603     [pool release];
   604 
   605     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   606         [nswindow release];
   607         return -1;
   608     }
   609     return 0;
   610 }
   611 
   612 int
   613 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   614 {
   615     NSAutoreleasePool *pool;
   616     NSWindow *nswindow = (NSWindow *) data;
   617     NSString *title;
   618 
   619     pool = [[NSAutoreleasePool alloc] init];
   620 
   621     /* Query the title from the existing window */
   622     title = [nswindow title];
   623     if (title) {
   624         window->title = SDL_strdup([title UTF8String]);
   625     }
   626 
   627     [pool release];
   628 
   629     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   630 }
   631 
   632 void
   633 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   634 {
   635     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   636     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   637     NSString *string;
   638 
   639     if(window->title) {
   640         string = [[NSString alloc] initWithUTF8String:window->title];
   641     } else {
   642         string = [[NSString alloc] init];
   643     }
   644     [nswindow setTitle:string];
   645     [string release];
   646 
   647     [pool release];
   648 }
   649 
   650 void
   651 Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   652 {
   653     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   654     NSImage *nsimage = Cocoa_CreateImage(icon);
   655 
   656     if (nsimage) {
   657         [NSApp setApplicationIconImage:nsimage];
   658     }
   659 
   660     [pool release];
   661 }
   662 
   663 void
   664 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   665 {
   666     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   667     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   668     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   669     NSRect rect;
   670     SDL_Rect bounds;
   671 
   672     Cocoa_GetDisplayBounds(_this, display, &bounds);
   673     if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
   674         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   675     } else {
   676         rect.origin.x = window->x;
   677     }
   678     if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
   679         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   680     } else {
   681         rect.origin.y = window->y;
   682     }
   683     rect.size.width = window->w;
   684     rect.size.height = window->h;
   685     ConvertNSRect(&rect);
   686     rect = [nswindow frameRectForContentRect:rect];
   687     [nswindow setFrameOrigin:rect.origin];
   688     [pool release];
   689 }
   690 
   691 void
   692 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   693 {
   694     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   695     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   696     NSSize size;
   697 
   698     size.width = window->w;
   699     size.height = window->h;
   700     [nswindow setContentSize:size];
   701     [pool release];
   702 }
   703 
   704 void
   705 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   706 {
   707     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   708     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   709 
   710     if (![nswindow isMiniaturized]) {
   711         [nswindow makeKeyAndOrderFront:nil];
   712     }
   713     [pool release];
   714 }
   715 
   716 void
   717 Cocoa_HideWindow(_THIS, SDL_Window * window)
   718 {
   719     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   720     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   721 
   722     [nswindow orderOut:nil];
   723     [pool release];
   724 }
   725 
   726 void
   727 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   728 {
   729     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   730     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   731 
   732     [nswindow makeKeyAndOrderFront:nil];
   733     [pool release];
   734 }
   735 
   736 void
   737 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   738 {
   739     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   740     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   741 
   742     [nswindow zoom:nil];
   743     [pool release];
   744 }
   745 
   746 void
   747 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   748 {
   749     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   750     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   751 
   752     [nswindow miniaturize:nil];
   753     [pool release];
   754 }
   755 
   756 void
   757 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   758 {
   759     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   760     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   761 
   762     if ([nswindow isMiniaturized]) {
   763         [nswindow deminiaturize:nil];
   764     } else if ([nswindow isZoomed]) {
   765         [nswindow zoom:nil];
   766     }
   767     [pool release];
   768 }
   769 
   770 void
   771 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   772 {
   773     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   774     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   775     NSWindow *nswindow = data->nswindow;
   776     NSRect rect;
   777 
   778     if (fullscreen) {
   779         SDL_Rect bounds;
   780 
   781         Cocoa_GetDisplayBounds(_this, display, &bounds);
   782         rect.origin.x = bounds.x;
   783         rect.origin.y = bounds.y;
   784         rect.size.width = bounds.w;
   785         rect.size.height = bounds.h;
   786         ConvertNSRect(&rect);
   787 
   788         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   789             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
   790         }
   791         [nswindow setFrameOrigin:rect.origin];
   792         [nswindow setContentSize:rect.size];
   793     } else {
   794         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   795             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
   796         }
   797 
   798         // This doesn't seem to do anything...
   799         //[nswindow setFrameOrigin:origin];
   800     }
   801 
   802 #ifdef FULLSCREEN_TOGGLEABLE
   803     if (fullscreen) {
   804         /* OpenGL is rendering to the window, so make it visible! */
   805         [nswindow setLevel:CGShieldingWindowLevel()];
   806     } else {
   807         [nswindow setLevel:kCGNormalWindowLevel];
   808     }
   809 #endif
   810     [nswindow makeKeyAndOrderFront:nil];
   811 
   812     [pool release];
   813 }
   814 
   815 NSPoint origin;
   816 void
   817 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   818 {
   819 #ifdef RELATIVE_MOTION
   820     /* FIXME: work in progress
   821         You set relative mode by using the following code in conjunction with
   822         CGDisplayHideCursor(kCGDirectMainDisplay) and
   823         CGDisplayShowCursor(kCGDirectMainDisplay)
   824     */
   825     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   826         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   827         CGAssociateMouseAndMouseCursorPosition(NO);
   828     } else {
   829         CGAssociateMouseAndMouseCursorPosition(YES);
   830     }
   831 #else
   832     /* Move the cursor to the nearest point in the window */
   833     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   834         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   835         int x, y;
   836         CGPoint cgpoint;
   837 
   838         SDL_GetMouseState(&x, &y);
   839         cgpoint.x = window->x + x;
   840         cgpoint.y = window->y + y;
   841         CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
   842     }
   843 #endif
   844 }
   845 
   846 void
   847 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   848 {
   849     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   850     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   851 
   852     if (data) {
   853         [data->listener close];
   854         [data->listener release];
   855         if (data->created) {
   856             [data->nswindow close];
   857         }
   858         SDL_free(data);
   859     }
   860     [pool release];
   861 }
   862 
   863 SDL_bool
   864 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   865 {
   866     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   867 
   868     if (info->version.major <= SDL_MAJOR_VERSION) {
   869         info->subsystem = SDL_SYSWM_COCOA;
   870         info->info.cocoa.window = nswindow;
   871         return SDL_TRUE;
   872     } else {
   873         SDL_SetError("Application not compiled with SDL %d.%d\n",
   874                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   875         return SDL_FALSE;
   876     }
   877 }
   878 
   879 /* vi: set ts=4 sw=4 expandtab: */