src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 06:33:00 +0000
changeset 2710 44e49d3fa6cf
parent 2152 003c1b5b07da
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Many-mouse and tablet support
by Szymon Wilczek, mentored by Ryan C. Gordon

Everything concerning the project is noted on the wiki:
http://wilku.ravenlord.ws/doku.php?id=start
     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     /* We're going to get keyboard events, since we're key. */
   133     index = _data->videodata->keyboard;
   134     SDL_SetKeyboardFocus(index, _data->windowID);
   135 }
   136 
   137 - (void)windowDidResignKey:(NSNotification *)aNotification
   138 {
   139     int index;
   140     SDL_Mouse *mouse;
   141 
   142     /* Some other window will get mouse events, since we're not key. */
   143     index = _data->videodata->mouse;
   144     mouse = SDL_GetMouse(index);
   145     if (mouse->focus == _data->windowID) {
   146         SDL_SetMouseFocus(index, 0);
   147     }
   148 
   149     /* Some other window will get keyboard events, since we're not key. */
   150     index = _data->videodata->keyboard;
   151     SDL_SetKeyboardFocus(index, 0);
   152 }
   153 
   154 - (void)windowDidHide:(NSNotification *)aNotification
   155 {
   156     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   157 }
   158 
   159 - (void)windowDidUnhide:(NSNotification *)aNotification
   160 {
   161     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   162 }
   163 
   164 - (void)mouseDown:(NSEvent *)theEvent
   165 {
   166     int index;
   167     int button;
   168 
   169     index = _data->videodata->mouse;
   170     switch ([theEvent buttonNumber]) {
   171     case 0:
   172         button = SDL_BUTTON_LEFT;
   173         break;
   174     case 1:
   175         button = SDL_BUTTON_RIGHT;
   176         break;
   177     case 2:
   178         button = SDL_BUTTON_MIDDLE;
   179         break;
   180     default:
   181         button = [theEvent buttonNumber];
   182         break;
   183     }
   184     SDL_SendMouseButton(index, SDL_PRESSED, button);
   185 }
   186 
   187 - (void)rightMouseDown:(NSEvent *)theEvent
   188 {
   189     [self mouseDown:theEvent];
   190 }
   191 
   192 - (void)otherMouseDown:(NSEvent *)theEvent
   193 {
   194     [self mouseDown:theEvent];
   195 }
   196 
   197 - (void)mouseUp:(NSEvent *)theEvent
   198 {
   199     int index;
   200     int button;
   201 
   202     index = _data->videodata->mouse;
   203     switch ([theEvent buttonNumber]) {
   204     case 0:
   205         button = SDL_BUTTON_LEFT;
   206         break;
   207     case 1:
   208         button = SDL_BUTTON_RIGHT;
   209         break;
   210     case 2:
   211         button = SDL_BUTTON_MIDDLE;
   212         break;
   213     default:
   214         button = [theEvent buttonNumber];
   215         break;
   216     }
   217     SDL_SendMouseButton(index, SDL_RELEASED, button);
   218 }
   219 
   220 - (void)rightMouseUp:(NSEvent *)theEvent
   221 {
   222     [self mouseUp:theEvent];
   223 }
   224 
   225 - (void)otherMouseUp:(NSEvent *)theEvent
   226 {
   227     [self mouseUp:theEvent];
   228 }
   229 
   230 - (void)mouseMoved:(NSEvent *)theEvent
   231 {
   232     SDL_Window *window = SDL_GetWindowFromID(_data->windowID);
   233     int index;
   234     SDL_Mouse *mouse;
   235     NSPoint point;
   236     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   237 
   238     index = _data->videodata->mouse;
   239     mouse = SDL_GetMouse(index);
   240 
   241     point = [NSEvent mouseLocation];
   242     point.x = point.x - rect.origin.x;
   243     point.y = rect.size.height - (point.y - rect.origin.y);
   244     if ( point.x < 0 || point.x >= rect.size.width ||
   245          point.y < 0 || point.y >= rect.size.height ) {
   246         if (mouse->focus != 0) {
   247             SDL_SetMouseFocus(index, 0);
   248         }
   249     } else {
   250         if (mouse->focus != _data->windowID) {
   251             SDL_SetMouseFocus(index, _data->windowID);
   252         }
   253         SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y, 0);
   254     }
   255 }
   256 
   257 - (void)mouseDragged:(NSEvent *)theEvent
   258 {
   259     [self mouseMoved:theEvent];
   260 }
   261 
   262 - (void)rightMouseDragged:(NSEvent *)theEvent
   263 {
   264     [self mouseMoved:theEvent];
   265 }
   266 
   267 - (void)otherMouseDragged:(NSEvent *)theEvent
   268 {
   269     [self mouseMoved:theEvent];
   270 }
   271 
   272 - (void)scrollWheel:(NSEvent *)theEvent
   273 {
   274     int index;
   275 
   276     index = _data->videodata->mouse;
   277     SDL_SendMouseWheel(index, (int)([theEvent deltaX]+0.9f), (int)([theEvent deltaY]+0.9f));
   278 }
   279 
   280 @end
   281 
   282 @interface SDLWindow : NSWindow
   283 /* These are needed for borderless/fullscreen windows */
   284 - (BOOL)canBecomeKeyWindow;
   285 - (BOOL)canBecomeMainWindow;
   286 @end
   287 
   288 @implementation SDLWindow
   289 - (BOOL)canBecomeKeyWindow
   290 {
   291     return YES;
   292 }
   293 
   294 - (BOOL)canBecomeMainWindow
   295 {
   296     return YES;
   297 }
   298 @end
   299 
   300 static int
   301 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   302 {
   303     NSAutoreleasePool *pool;
   304     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   305     SDL_WindowData *data;
   306 
   307     /* Allocate the window data */
   308     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   309     if (!data) {
   310         SDL_OutOfMemory();
   311         return -1;
   312     }
   313     data->windowID = window->id;
   314     data->window = nswindow;
   315     data->created = created;
   316     data->videodata = videodata;
   317 
   318     pool = [[NSAutoreleasePool alloc] init];
   319 
   320     /* Create an event listener for the window */
   321     data->listener = [[Cocoa_WindowListener alloc] init];
   322     [data->listener listen:data];
   323 
   324     /* Fill in the SDL window with the window data */
   325     {
   326         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   327         ConvertNSRect(&rect);
   328         window->x = (int)rect.origin.x;
   329         window->y = (int)rect.origin.y;
   330         window->w = (int)rect.size.width;
   331         window->h = (int)rect.size.height;
   332     }
   333     if ([nswindow isVisible]) {
   334         window->flags |= SDL_WINDOW_SHOWN;
   335     } else {
   336         window->flags &= ~SDL_WINDOW_SHOWN;
   337     }
   338     {
   339         unsigned int style = [nswindow styleMask];
   340 
   341         if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
   342             window->flags |= SDL_WINDOW_BORDERLESS;
   343         } else {
   344             window->flags &= ~SDL_WINDOW_BORDERLESS;
   345         }
   346         if (style & NSResizableWindowMask) {
   347             window->flags |= SDL_WINDOW_RESIZABLE;
   348         } else {
   349             window->flags &= ~SDL_WINDOW_RESIZABLE;
   350         }
   351     }
   352     if ([nswindow isZoomed]) {
   353         window->flags |= SDL_WINDOW_MAXIMIZED;
   354     } else {
   355         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   356     }
   357     if ([nswindow isMiniaturized]) {
   358         window->flags |= SDL_WINDOW_MINIMIZED;
   359     } else {
   360         window->flags &= ~SDL_WINDOW_MINIMIZED;
   361     }
   362     if ([nswindow isKeyWindow]) {
   363         int index = data->videodata->keyboard;
   364         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   365         SDL_SetKeyboardFocus(index, data->windowID);
   366 
   367         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   368             /* FIXME */
   369         }
   370     }
   371 
   372     /* All done! */
   373     [pool release];
   374     window->driverdata = data;
   375     return 0;
   376 }
   377 
   378 int
   379 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   380 {
   381     NSAutoreleasePool *pool;
   382     NSWindow *nswindow;
   383     NSRect rect;
   384     unsigned int style;
   385     NSString *title;
   386     int status;
   387 
   388     pool = [[NSAutoreleasePool alloc] init];
   389 
   390     if (window->x == SDL_WINDOWPOS_CENTERED) {
   391         rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   392     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   393         rect.origin.x = 0;
   394     } else {
   395         rect.origin.x = window->x;
   396     }
   397     if (window->y == SDL_WINDOWPOS_CENTERED) {
   398         rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   399     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   400         rect.origin.y = 0;
   401     } else {
   402         rect.origin.y = window->y;
   403     }
   404     rect.size.width = window->w;
   405     rect.size.height = window->h;
   406     ConvertNSRect(&rect);
   407 
   408     if (window->flags & SDL_WINDOW_BORDERLESS) {
   409         style = NSBorderlessWindowMask;
   410     } else {
   411         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   412     }
   413     if (window->flags & SDL_WINDOW_RESIZABLE) {
   414         style |= NSResizableWindowMask;
   415     }
   416 
   417     nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   418 
   419     [pool release];
   420 
   421     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   422         [nswindow release];
   423         return -1;
   424     }
   425 #ifdef SDL_VIDEO_OPENGL_CGL
   426     if (window->flags & SDL_WINDOW_OPENGL) {
   427         if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   428             Cocoa_DestroyWindow(_this, window);
   429             return -1;
   430         }
   431     }
   432 #endif
   433     return 0;
   434 }
   435 
   436 int
   437 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   438 {
   439     NSAutoreleasePool *pool;
   440     NSWindow *nswindow = (NSWindow *) data;
   441     NSString *title;
   442     int status;
   443 
   444     pool = [[NSAutoreleasePool alloc] init];
   445 
   446     /* Query the title from the existing window */
   447     title = [nswindow title];
   448     if (title) {
   449         window->title = SDL_strdup([title UTF8String]);
   450     }
   451 
   452     [pool release];
   453 
   454     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   455 }
   456 
   457 void
   458 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   459 {
   460     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   461     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   462     NSString *string;
   463 
   464     if(window->title) {
   465         string = [[NSString alloc] initWithUTF8String:window->title];
   466     } else {
   467         string = [[NSString alloc] init];
   468     }
   469     [nswindow setTitle:string];
   470     [string release];
   471 
   472     [pool release];
   473 }
   474 
   475 void
   476 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   477 {
   478     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   479     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   480     NSRect rect;
   481 
   482     rect.origin.x = window->x;
   483     rect.origin.y = window->y;
   484     rect.size.width = window->w;
   485     rect.size.height = window->h;
   486     ConvertNSRect(&rect);
   487     rect = [nswindow frameRectForContentRect:rect];
   488     [nswindow setFrameOrigin:rect.origin];
   489     [pool release];
   490 }
   491 
   492 void
   493 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   494 {
   495     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   496     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   497     NSSize size;
   498 
   499     size.width = window->w;
   500     size.height = window->h;
   501     [nswindow setContentSize:size];
   502     [pool release];
   503 }
   504 
   505 void
   506 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   507 {
   508     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   509     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   510 
   511     if (![nswindow isMiniaturized]) {
   512         [nswindow makeKeyAndOrderFront:nil];
   513     }
   514     [pool release];
   515 }
   516 
   517 void
   518 Cocoa_HideWindow(_THIS, SDL_Window * window)
   519 {
   520     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   521     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   522 
   523     [nswindow orderOut:nil];
   524     [pool release];
   525 }
   526 
   527 void
   528 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   529 {
   530     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   531     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   532 
   533     [nswindow makeKeyAndOrderFront:nil];
   534     [pool release];
   535 }
   536 
   537 void
   538 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   539 {
   540     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   541     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   542 
   543     [nswindow zoom:nil];
   544     [pool release];
   545 }
   546 
   547 void
   548 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   549 {
   550     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   551     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   552 
   553     [nswindow miniaturize:nil];
   554     [pool release];
   555 }
   556 
   557 void
   558 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   559 {
   560     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   561     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   562 
   563     if ([nswindow isMiniaturized]) {
   564         [nswindow deminiaturize:nil];
   565     } else if ([nswindow isZoomed]) {
   566         [nswindow zoom:nil];
   567     }
   568     [pool release];
   569 }
   570 
   571 void
   572 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   573 {
   574     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   575         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   576         /* FIXME: Grab mouse */
   577     } else {
   578         /* FIXME: Release mouse */
   579     }
   580 }
   581 
   582 void
   583 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   584 {
   585     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   586     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   587 
   588     if (data) {
   589 #ifdef SDL_VIDEO_OPENGL_CGL
   590         if (window->flags & SDL_WINDOW_OPENGL) {
   591             Cocoa_GL_CleanupWindow(_this, window);
   592         }
   593 #endif
   594         [data->listener close];
   595         [data->listener release];
   596         if (data->created) {
   597             [data->window close];
   598         }
   599         SDL_free(data);
   600     }
   601     [pool release];
   602 }
   603 
   604 SDL_bool
   605 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   606 {
   607     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   608 
   609     if (info->version.major <= SDL_MAJOR_VERSION) {
   610         //info->window = nswindow;
   611         return SDL_TRUE;
   612     } else {
   613         SDL_SetError("Application not compiled with SDL %d.%d\n",
   614                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   615         return SDL_FALSE;
   616     }
   617 }
   618 
   619 /* vi: set ts=4 sw=4 expandtab: */