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