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