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