src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Aug 2006 08:55:37 +0000
changeset 1973 81255f93dfcd
parent 1962 c92e5f3e68d9
child 2059 4685ccd33d0e
permissions -rw-r--r--
Fixed fullscreen modes with Cocoa video driver.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_windowevents_c.h"
    29 
    30 #include "SDL_cocoavideo.h"
    31 
    32 static __inline__ void ConvertNSRect(NSRect *r)
    33 {
    34     /* FIXME: Cache the display used for this window */
    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->window setNextResponder:self];
    49     if ([_data->window delegate] != nil) {
    50         [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
    51         [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
    52         [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
    53         [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
    54         [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
    55         [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
    56         [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
    57     } else {
    58         [_data->window 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->window setAcceptsMouseMovedEvents:YES];
    64 }
    65 
    66 - (void)close
    67 {
    68     NSNotificationCenter *center;
    69 
    70     center = [NSNotificationCenter defaultCenter];
    71 
    72     [_data->window setNextResponder:nil];
    73     if ([_data->window delegate] != self) {
    74         [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
    75         [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
    76         [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
    77         [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
    78         [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
    79         [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
    80         [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
    81     } else {
    82         [_data->window setDelegate:nil];
    83     }
    84     [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
    85     [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
    86 }
    87 
    88 - (BOOL)windowShouldClose:(id)sender
    89 {
    90     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
    91     return NO;
    92 }
    93 
    94 - (void)windowDidExpose:(NSNotification *)aNotification
    95 {
    96     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
    97 }
    98 
    99 - (void)windowDidMove:(NSNotification *)aNotification
   100 {
   101     int x, y;
   102     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   103     ConvertNSRect(&rect);
   104     x = (int)rect.origin.x;
   105     y = (int)rect.origin.y;
   106     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
   107 }
   108 
   109 - (void)windowDidResize:(NSNotification *)aNotification
   110 {
   111     int w, h;
   112     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   113     w = (int)rect.size.width;
   114     h = (int)rect.size.height;
   115     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
   116 }
   117 
   118 - (void)windowDidMiniaturize:(NSNotification *)aNotification
   119 {
   120     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   121 }
   122 
   123 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
   124 {
   125     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   126 }
   127 
   128 - (void)windowDidBecomeKey:(NSNotification *)aNotification
   129 {
   130     int index;
   131 
   132     index = _data->videodata->keyboard;
   133     SDL_SetKeyboardFocus(index, _data->windowID);
   134 }
   135 
   136 - (void)windowDidResignKey:(NSNotification *)aNotification
   137 {
   138     int index;
   139 
   140     index = _data->videodata->keyboard;
   141     SDL_SetKeyboardFocus(index, 0);
   142 }
   143 
   144 - (void)windowDidHide:(NSNotification *)aNotification
   145 {
   146     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   147 }
   148 
   149 - (void)windowDidUnhide:(NSNotification *)aNotification
   150 {
   151     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   152 }
   153 
   154 - (void)mouseDown:(NSEvent *)theEvent
   155 {
   156     int index;
   157     int button;
   158 
   159     index = _data->videodata->mouse;
   160     switch ([theEvent buttonNumber]) {
   161     case 0:
   162         button = SDL_BUTTON_LEFT;
   163         break;
   164     case 1:
   165         button = SDL_BUTTON_RIGHT;
   166         break;
   167     case 2:
   168         button = SDL_BUTTON_MIDDLE;
   169         break;
   170     default:
   171         button = [theEvent buttonNumber];
   172         break;
   173     }
   174     SDL_SendMouseButton(index, SDL_PRESSED, button);
   175 }
   176 
   177 - (void)rightMouseDown:(NSEvent *)theEvent
   178 {
   179     [self mouseDown:theEvent];
   180 }
   181 
   182 - (void)otherMouseDown:(NSEvent *)theEvent
   183 {
   184     [self mouseDown:theEvent];
   185 }
   186 
   187 - (void)mouseUp:(NSEvent *)theEvent
   188 {
   189     int index;
   190     int button;
   191 
   192     index = _data->videodata->mouse;
   193     switch ([theEvent buttonNumber]) {
   194     case 0:
   195         button = SDL_BUTTON_LEFT;
   196         break;
   197     case 1:
   198         button = SDL_BUTTON_RIGHT;
   199         break;
   200     case 2:
   201         button = SDL_BUTTON_MIDDLE;
   202         break;
   203     default:
   204         button = [theEvent buttonNumber];
   205         break;
   206     }
   207     SDL_SendMouseButton(index, SDL_RELEASED, button);
   208 }
   209 
   210 - (void)rightMouseUp:(NSEvent *)theEvent
   211 {
   212     [self mouseUp:theEvent];
   213 }
   214 
   215 - (void)otherMouseUp:(NSEvent *)theEvent
   216 {
   217     [self mouseUp:theEvent];
   218 }
   219 
   220 - (void)mouseMoved:(NSEvent *)theEvent
   221 {
   222     SDL_Window *window = SDL_GetWindowFromID(_data->windowID);
   223     int index;
   224     SDL_Mouse *mouse;
   225     NSPoint point;
   226     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   227 
   228     index = _data->videodata->mouse;
   229     mouse = SDL_GetMouse(index);
   230     if (mouse->focus != _data->windowID) {
   231         SDL_SetMouseFocus(index, _data->windowID);
   232     }
   233 
   234     point = [NSEvent mouseLocation];
   235     point.x = point.x - rect.origin.x;
   236     point.y = rect.size.height - (point.y - rect.origin.y);
   237     SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
   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     int index;
   258 
   259     index = _data->videodata->mouse;
   260     SDL_SendMouseWheel(index, (int)([theEvent deltaY]+0.9f));
   261 }
   262 
   263 @end
   264 
   265 @interface SDLWindow : NSWindow
   266 /* These are needed for borderless/fullscreen windows */
   267 - (BOOL)canBecomeKeyWindow;
   268 - (BOOL)canBecomeMainWindow;
   269 @end
   270 
   271 @implementation SDLWindow
   272 - (BOOL)canBecomeKeyWindow
   273 {
   274     return YES;
   275 }
   276 
   277 - (BOOL)canBecomeMainWindow
   278 {
   279     return YES;
   280 }
   281 @end
   282 
   283 static int
   284 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   285 {
   286     NSAutoreleasePool *pool;
   287     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   288     SDL_WindowData *data;
   289 
   290     /* Allocate the window data */
   291     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   292     if (!data) {
   293         SDL_OutOfMemory();
   294         return -1;
   295     }
   296     data->windowID = window->id;
   297     data->window = nswindow;
   298     data->created = created;
   299     data->videodata = videodata;
   300 
   301     pool = [[NSAutoreleasePool alloc] init];
   302 
   303     /* Create an event listener for the window */
   304     data->listener = [[Cocoa_WindowListener alloc] init];
   305     [data->listener listen:data];
   306 
   307     /* Fill in the SDL window with the window data */
   308     {
   309         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   310         ConvertNSRect(&rect);
   311         window->x = (int)rect.origin.x;
   312         window->y = (int)rect.origin.y;
   313         window->w = (int)rect.size.width;
   314         window->h = (int)rect.size.height;
   315     }
   316     if ([nswindow isVisible]) {
   317         window->flags |= SDL_WINDOW_SHOWN;
   318     } else {
   319         window->flags &= ~SDL_WINDOW_SHOWN;
   320     }
   321     {
   322         unsigned int style = [nswindow styleMask];
   323 
   324         if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
   325             window->flags |= SDL_WINDOW_BORDERLESS;
   326         } else {
   327             window->flags &= ~SDL_WINDOW_BORDERLESS;
   328         }
   329         if (style & NSResizableWindowMask) {
   330             window->flags |= SDL_WINDOW_RESIZABLE;
   331         } else {
   332             window->flags &= ~SDL_WINDOW_RESIZABLE;
   333         }
   334     }
   335     if ([nswindow isZoomed]) {
   336         window->flags |= SDL_WINDOW_MAXIMIZED;
   337     } else {
   338         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   339     }
   340     if ([nswindow isMiniaturized]) {
   341         window->flags |= SDL_WINDOW_MINIMIZED;
   342     } else {
   343         window->flags &= ~SDL_WINDOW_MINIMIZED;
   344     }
   345     if ([nswindow isKeyWindow]) {
   346         int index = data->videodata->keyboard;
   347         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   348         SDL_SetKeyboardFocus(index, data->windowID);
   349 
   350         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   351             /* FIXME */
   352         }
   353     }
   354 
   355     /* All done! */
   356     [pool release];
   357     window->driverdata = data;
   358     return 0;
   359 }
   360 
   361 int
   362 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   363 {
   364     NSAutoreleasePool *pool;
   365     NSWindow *nswindow;
   366     NSRect rect;
   367     unsigned int style;
   368     NSString *title;
   369     int status;
   370 
   371     pool = [[NSAutoreleasePool alloc] init];
   372 
   373     if (window->x == SDL_WINDOWPOS_CENTERED) {
   374         rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   375     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   376         rect.origin.x = 0;
   377     } else {
   378         rect.origin.x = window->x;
   379     }
   380     if (window->y == SDL_WINDOWPOS_CENTERED) {
   381         rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   382     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   383         rect.origin.y = 0;
   384     } else {
   385         rect.origin.y = window->y;
   386     }
   387     rect.size.width = window->w;
   388     rect.size.height = window->h;
   389     ConvertNSRect(&rect);
   390 
   391     if (window->flags & SDL_WINDOW_BORDERLESS) {
   392         style = NSBorderlessWindowMask;
   393     } else {
   394         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   395     }
   396     if (window->flags & SDL_WINDOW_RESIZABLE) {
   397         style |= NSResizableWindowMask;
   398     }
   399 
   400     nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   401 
   402     [pool release];
   403 
   404     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   405         [nswindow release];
   406         return -1;
   407     }
   408 #ifdef SDL_VIDEO_OPENGL_CGL
   409     if (window->flags & SDL_WINDOW_OPENGL) {
   410         if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   411             Cocoa_DestroyWindow(_this, window);
   412             return -1;
   413         }
   414     }
   415 #endif
   416     return 0;
   417 }
   418 
   419 int
   420 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   421 {
   422     NSAutoreleasePool *pool;
   423     NSWindow *nswindow = (NSWindow *) data;
   424     NSString *title;
   425     int status;
   426 
   427     pool = [[NSAutoreleasePool alloc] init];
   428 
   429     /* Query the title from the existing window */
   430     title = [nswindow title];
   431     if (title) {
   432         window->title = SDL_strdup([title UTF8String]);
   433     }
   434 
   435     [pool release];
   436 
   437     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   438 }
   439 
   440 void
   441 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   442 {
   443     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   444     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   445     NSString *string;
   446 
   447     if(window->title) {
   448         string = [[NSString alloc] initWithUTF8String:window->title];
   449     } else {
   450         string = [[NSString alloc] init];
   451     }
   452     [nswindow setTitle:string];
   453     [string release];
   454 
   455     [pool release];
   456 }
   457 
   458 void
   459 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   460 {
   461     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   462     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   463     NSRect rect;
   464 
   465     rect.origin.x = window->x;
   466     rect.origin.y = window->y;
   467     rect.size.width = window->w;
   468     rect.size.height = window->h;
   469     ConvertNSRect(&rect);
   470     rect = [nswindow frameRectForContentRect:rect];
   471     [nswindow setFrameOrigin:rect.origin];
   472     [pool release];
   473 }
   474 
   475 void
   476 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   477 {
   478     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   479     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   480     NSSize size;
   481 
   482     size.width = window->w;
   483     size.height = window->h;
   484     [nswindow setContentSize:size];
   485     [pool release];
   486 }
   487 
   488 void
   489 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   490 {
   491     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   492     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   493 
   494     if (![nswindow isMiniaturized]) {
   495         [nswindow makeKeyAndOrderFront:nil];
   496     }
   497     [pool release];
   498 }
   499 
   500 void
   501 Cocoa_HideWindow(_THIS, SDL_Window * window)
   502 {
   503     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   504     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   505 
   506     [nswindow orderOut:nil];
   507     [pool release];
   508 }
   509 
   510 void
   511 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   512 {
   513     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   514     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   515 
   516     [nswindow makeKeyAndOrderFront:nil];
   517     [pool release];
   518 }
   519 
   520 void
   521 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   522 {
   523     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   524     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   525 
   526     [nswindow zoom:nil];
   527     [pool release];
   528 }
   529 
   530 void
   531 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   532 {
   533     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   534     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   535 
   536     [nswindow miniaturize:nil];
   537     [pool release];
   538 }
   539 
   540 void
   541 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   542 {
   543     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   544     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   545 
   546     if ([nswindow isMiniaturized]) {
   547         [nswindow deminiaturize:nil];
   548     } else if ([nswindow isZoomed]) {
   549         [nswindow zoom:nil];
   550     }
   551     [pool release];
   552 }
   553 
   554 void
   555 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   556 {
   557     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   558         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   559         /* FIXME: Grab mouse */
   560     } else {
   561         /* FIXME: Release mouse */
   562     }
   563 }
   564 
   565 void
   566 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   567 {
   568     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   569     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   570 
   571     if (data) {
   572 #ifdef SDL_VIDEO_OPENGL_CGL
   573         if (window->flags & SDL_WINDOW_OPENGL) {
   574             Cocoa_GL_CleanupWindow(_this, window);
   575         }
   576 #endif
   577         [data->listener close];
   578         [data->listener release];
   579         if (data->created) {
   580             [data->window close];
   581         }
   582         SDL_free(data);
   583     }
   584     [pool release];
   585 }
   586 
   587 SDL_bool
   588 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   589 {
   590     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   591 
   592     if (info->version.major <= SDL_MAJOR_VERSION) {
   593         //info->window = nswindow;
   594         return SDL_TRUE;
   595     } else {
   596         SDL_SetError("Application not compiled with SDL %d.%d\n",
   597                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   598         return SDL_FALSE;
   599     }
   600 }
   601 
   602 /* vi: set ts=4 sw=4 expandtab: */