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