src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Jul 2006 06:53:23 +0000
changeset 1951 7177581dc9fa
parent 1937 05e88d266921
child 1952 420716272158
permissions -rw-r--r--
Initial work on X11 window code in.
     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     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
   131 }
   132 
   133 - (void)windowDidResignKey:(NSNotification *)aNotification
   134 {
   135     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   136 }
   137 
   138 - (void)windowDidHide:(NSNotification *)aNotification
   139 {
   140     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   141 }
   142 
   143 - (void)windowDidUnhide:(NSNotification *)aNotification
   144 {
   145     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   146 }
   147 
   148 - (void)mouseDown:(NSEvent *)theEvent
   149 {
   150     int index;
   151 
   152     index = _data->videodata->mouse;
   153     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_LEFT);
   154 }
   155 
   156 - (void)rightMouseDown:(NSEvent *)theEvent
   157 {
   158     int index;
   159 
   160     index = _data->videodata->mouse;
   161     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_RIGHT);
   162 }
   163 
   164 - (void)otherMouseDown:(NSEvent *)theEvent
   165 {
   166     int index;
   167 
   168     index = _data->videodata->mouse;
   169     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_MIDDLE);
   170 }
   171 
   172 - (void)mouseUp:(NSEvent *)theEvent
   173 {
   174     int index;
   175 
   176     index = _data->videodata->mouse;
   177     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_LEFT);
   178 }
   179 
   180 - (void)rightMouseUp:(NSEvent *)theEvent
   181 {
   182     int index;
   183 
   184     index = _data->videodata->mouse;
   185     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_RIGHT);
   186 }
   187 
   188 - (void)otherMouseUp:(NSEvent *)theEvent
   189 {
   190     int index;
   191 
   192     index = _data->videodata->mouse;
   193     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_MIDDLE);
   194 }
   195 
   196 - (void)mouseMoved:(NSEvent *)theEvent
   197 {
   198     int index;
   199     SDL_Mouse *mouse;
   200     NSPoint point;
   201     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   202 
   203     index = _data->videodata->mouse;
   204     mouse = SDL_GetMouse(index);
   205     if (mouse->focus != _data->windowID) {
   206         SDL_SetMouseFocus(index, _data->windowID);
   207     }
   208 
   209     point = [NSEvent mouseLocation];
   210     point.x = point.x - rect.origin.x;
   211     point.y = rect.size.height - (point.y - rect.origin.y);
   212     SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
   213 }
   214 
   215 - (void)scrollWheel:(NSEvent *)theEvent
   216 {
   217 fprintf(stderr, "scrollWheel\n");
   218 }
   219 
   220 - (void)mouseEntered:(NSEvent *)theEvent
   221 {
   222 fprintf(stderr, "mouseEntered\n");
   223     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_ENTER, 0, 0);
   224 }
   225 
   226 - (void)mouseExited:(NSEvent *)theEvent
   227 {
   228 fprintf(stderr, "mouseExited\n");
   229     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_LEAVE, 0, 0);
   230 }
   231 
   232 - (void)keyDown:(NSEvent *)theEvent
   233 {
   234 fprintf(stderr, "keyDown\n");
   235 }
   236 
   237 - (void)keyUp:(NSEvent *)theEvent
   238 {
   239 fprintf(stderr, "keyUp\n");
   240 }
   241 
   242 @end
   243 
   244 static int
   245 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   246 {
   247     NSAutoreleasePool *pool;
   248     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   249     SDL_WindowData *data;
   250 
   251     /* Allocate the window data */
   252     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   253     if (!data) {
   254         SDL_OutOfMemory();
   255         return -1;
   256     }
   257     data->windowID = window->id;
   258     data->window = nswindow;
   259     data->created = created;
   260     data->videodata = videodata;
   261 
   262     pool = [[NSAutoreleasePool alloc] init];
   263 
   264     /* Create an event listener for the window */
   265     data->listener = [[Cocoa_WindowListener alloc] init];
   266     [data->listener listen:data];
   267 
   268     /* Fill in the SDL window with the window data */
   269     {
   270         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   271         ConvertNSRect(&rect);
   272         window->x = (int)rect.origin.x;
   273         window->y = (int)rect.origin.y;
   274         window->w = (int)rect.size.width;
   275         window->h = (int)rect.size.height;
   276     }
   277     if ([nswindow isVisible]) {
   278         window->flags |= SDL_WINDOW_SHOWN;
   279     } else {
   280         window->flags &= ~SDL_WINDOW_SHOWN;
   281     }
   282     {
   283         unsigned int style = [nswindow styleMask];
   284 
   285         if (style == NSBorderlessWindowMask) {
   286             window->flags |= SDL_WINDOW_BORDERLESS;
   287         } else {
   288             window->flags &= ~SDL_WINDOW_BORDERLESS;
   289         }
   290         if (style & NSResizableWindowMask) {
   291             window->flags |= SDL_WINDOW_RESIZABLE;
   292         } else {
   293             window->flags &= ~SDL_WINDOW_RESIZABLE;
   294         }
   295     }
   296     if ([nswindow isZoomed]) {
   297         window->flags |= SDL_WINDOW_MAXIMIZED;
   298     } else {
   299         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   300     }
   301     if ([nswindow isMiniaturized]) {
   302         window->flags |= SDL_WINDOW_MINIMIZED;
   303     } else {
   304         window->flags &= ~SDL_WINDOW_MINIMIZED;
   305     }
   306     if ([nswindow isKeyWindow]) {
   307         int index = data->videodata->keyboard;
   308         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   309         SDL_SetKeyboardFocus(index, data->windowID);
   310 
   311         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   312             /* FIXME */
   313         }
   314     }
   315 
   316     /* All done! */
   317     [pool release];
   318     window->driverdata = data;
   319     return 0;
   320 }
   321 
   322 int
   323 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   324 {
   325     NSAutoreleasePool *pool;
   326     NSWindow *nswindow;
   327     NSRect rect;
   328     unsigned int style;
   329     NSString *title;
   330     int status;
   331 
   332     pool = [[NSAutoreleasePool alloc] init];
   333 
   334     if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   335         window->x == SDL_WINDOWPOS_CENTERED) {
   336         rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   337     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   338         rect.origin.x = 0;
   339     } else {
   340         rect.origin.x = window->x;
   341     }
   342     if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   343         window->y == SDL_WINDOWPOS_CENTERED) {
   344         rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   345     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   346         rect.origin.y = 0;
   347     } else {
   348         rect.origin.y = window->y;
   349     }
   350     rect.size.width = window->w;
   351     rect.size.height = window->h;
   352     ConvertNSRect(&rect);
   353 
   354     if (window->flags & SDL_WINDOW_BORDERLESS) {
   355         style = NSBorderlessWindowMask;
   356     } else {
   357         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   358     }
   359     if (window->flags & SDL_WINDOW_RESIZABLE) {
   360         style |= NSResizableWindowMask;
   361     }
   362 
   363     nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   364 
   365     if (window->flags & SDL_WINDOW_SHOWN) {
   366         [nswindow makeKeyAndOrderFront:nil];
   367     }
   368     if (window->flags & SDL_WINDOW_MAXIMIZED) {
   369         [nswindow performZoom:nil];
   370     }
   371     if (window->flags & SDL_WINDOW_MINIMIZED) {
   372         [nswindow performMiniaturize:nil];
   373     }
   374 
   375     if (window->title) {
   376         title = [[NSString alloc] initWithUTF8String:window->title];
   377         [nswindow setTitle:title];
   378         [nswindow setMiniwindowTitle:title];
   379         [title release];
   380     }
   381 
   382     [pool release];
   383 
   384     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   385         [nswindow release];
   386         return -1;
   387     }
   388 #ifdef SDL_VIDEO_OPENGL
   389     if (window->flags & SDL_WINDOW_OPENGL) {
   390         if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   391             Cocoa_DestroyWindow(_this, window);
   392             return -1;
   393         }
   394     }
   395 #endif
   396     return 0;
   397 }
   398 
   399 int
   400 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   401 {
   402     NSAutoreleasePool *pool;
   403     NSWindow *nswindow = (NSWindow *) data;
   404     NSString *title;
   405     int status;
   406 
   407     pool = [[NSAutoreleasePool alloc] init];
   408 
   409     /* Query the title from the existing window */
   410     title = [nswindow title];
   411     if (title) {
   412         window->title = SDL_strdup([title UTF8String]);
   413     }
   414 
   415     [pool release];
   416 
   417     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   418 }
   419 
   420 void
   421 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   422 {
   423     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   424     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   425     NSString *string;
   426 
   427     string = [[NSString alloc] initWithUTF8String:window->title];
   428     [nswindow setTitle:string];
   429     [nswindow setMiniwindowTitle:string];
   430     [string release];
   431     [pool release];
   432 }
   433 
   434 void
   435 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   436 {
   437     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   438     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   439     NSRect rect;
   440 
   441     rect.origin.x = window->x;
   442     rect.origin.y = window->y;
   443     rect.size.width = window->w;
   444     rect.size.height = window->h;
   445     ConvertNSRect(&rect);
   446     rect = [nswindow frameRectForContentRect:rect];
   447     [nswindow setFrameOrigin:rect.origin];
   448     [pool release];
   449 }
   450 
   451 void
   452 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   453 {
   454     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   455     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   456     NSSize size;
   457 
   458     size.width = window->w;
   459     size.height = window->h;
   460     [nswindow setContentSize:size];
   461     [pool release];
   462 }
   463 
   464 void
   465 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   466 {
   467     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   468     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   469 
   470     [nswindow makeKeyAndOrderFront:nil];
   471     [pool release];
   472 }
   473 
   474 void
   475 Cocoa_HideWindow(_THIS, SDL_Window * window)
   476 {
   477     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   478 
   479     /* FIXME */
   480 }
   481 
   482 void
   483 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   484 {
   485     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   486     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   487 
   488     [nswindow makeKeyAndOrderFront:nil];
   489     [pool release];
   490 }
   491 
   492 void
   493 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   494 {
   495     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   496     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   497 
   498     [nswindow performZoom:nil];
   499     [pool release];
   500 }
   501 
   502 void
   503 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   504 {
   505     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   506     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   507 
   508     [nswindow performMiniaturize:nil];
   509     [pool release];
   510 }
   511 
   512 void
   513 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   514 {
   515     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   516 
   517     /* FIXME */
   518 }
   519 
   520 void
   521 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   522 {
   523     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   524         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   525         /* FIXME: Grab mouse */
   526     } else {
   527         /* FIXME: Release mouse */
   528     }
   529 }
   530 
   531 void
   532 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   533 {
   534     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   535     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   536 
   537     if (data) {
   538 #ifdef SDL_VIDEO_OPENGL
   539         if (window->flags & SDL_WINDOW_OPENGL) {
   540             Cocoa_GL_CleanupWindow(_this, window);
   541         }
   542 #endif
   543         [data->listener close];
   544         [data->listener release];
   545         if (data->created) {
   546             [data->window close];
   547         }
   548         SDL_free(data);
   549     }
   550     [pool release];
   551 }
   552 
   553 SDL_bool
   554 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   555 {
   556     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   557 
   558     if (info->version.major <= SDL_MAJOR_VERSION) {
   559         //info->window = nswindow;
   560         return SDL_TRUE;
   561     } else {
   562         SDL_SetError("Application not compiled with SDL %d.%d\n",
   563                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   564         return SDL_FALSE;
   565     }
   566 }
   567 
   568 /* vi: set ts=4 sw=4 expandtab: */