src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 25 Jul 2006 06:59:14 +0000
changeset 1937 05e88d266921
parent 1936 83946ee0ff1f
child 1951 7177581dc9fa
permissions -rw-r--r--
Fixed Objective-C memory leaks
Send an SDL quit event when Command-Q is pressed.
     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(SDL_Window * window, NSWindow *nswindow, BOOL created)
   246 {
   247     NSAutoreleasePool *pool;
   248     SDL_WindowData *data;
   249 
   250     /* Allocate the window data */
   251     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   252     if (!data) {
   253         SDL_OutOfMemory();
   254         return -1;
   255     }
   256     data->windowID = window->id;
   257     data->window = nswindow;
   258     data->created = created;
   259     data->videodata = (SDL_VideoData *) SDL_GetVideoDevice()->driverdata;
   260 
   261     pool = [[NSAutoreleasePool alloc] init];
   262 
   263     /* Create an event listener for the window */
   264     data->listener = [[Cocoa_WindowListener alloc] init];
   265     [data->listener listen:data];
   266 
   267     /* Fill in the SDL window with the window data */
   268     {
   269         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   270         ConvertNSRect(&rect);
   271         window->x = (int)rect.origin.x;
   272         window->y = (int)rect.origin.y;
   273         window->w = (int)rect.size.width;
   274         window->h = (int)rect.size.height;
   275     }
   276     if ([nswindow isVisible]) {
   277         window->flags |= SDL_WINDOW_SHOWN;
   278     } else {
   279         window->flags &= ~SDL_WINDOW_SHOWN;
   280     }
   281     {
   282         unsigned int style = [nswindow styleMask];
   283 
   284         if (style == NSBorderlessWindowMask) {
   285             window->flags |= SDL_WINDOW_BORDERLESS;
   286         } else {
   287             window->flags &= ~SDL_WINDOW_BORDERLESS;
   288         }
   289         if (style & NSResizableWindowMask) {
   290             window->flags |= SDL_WINDOW_RESIZABLE;
   291         } else {
   292             window->flags &= ~SDL_WINDOW_RESIZABLE;
   293         }
   294     }
   295     if ([nswindow isZoomed]) {
   296         window->flags |= SDL_WINDOW_MAXIMIZED;
   297     } else {
   298         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   299     }
   300     if ([nswindow isMiniaturized]) {
   301         window->flags |= SDL_WINDOW_MINIMIZED;
   302     } else {
   303         window->flags &= ~SDL_WINDOW_MINIMIZED;
   304     }
   305     if ([nswindow isKeyWindow]) {
   306         int index = data->videodata->keyboard;
   307         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   308         SDL_SetKeyboardFocus(index, data->windowID);
   309 
   310         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   311             /* FIXME */
   312         }
   313     }
   314 
   315     /* All done! */
   316     [pool release];
   317     window->driverdata = data;
   318     return 0;
   319 }
   320 
   321 int
   322 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   323 {
   324     NSAutoreleasePool *pool;
   325     NSWindow *nswindow;
   326     NSRect rect;
   327     unsigned int style;
   328     NSString *title;
   329     int status;
   330 
   331     pool = [[NSAutoreleasePool alloc] init];
   332 
   333     if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   334         window->x == SDL_WINDOWPOS_CENTERED) {
   335         rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   336     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   337         rect.origin.x = 0;
   338     } else {
   339         rect.origin.x = window->x;
   340     }
   341     if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
   342         window->y == SDL_WINDOWPOS_CENTERED) {
   343         rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   344     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   345         rect.origin.y = 0;
   346     } else {
   347         rect.origin.y = window->y;
   348     }
   349     rect.size.width = window->w;
   350     rect.size.height = window->h;
   351     ConvertNSRect(&rect);
   352 
   353     if (window->flags & SDL_WINDOW_BORDERLESS) {
   354         style = NSBorderlessWindowMask;
   355     } else {
   356         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   357     }
   358     if (window->flags & SDL_WINDOW_RESIZABLE) {
   359         style |= NSResizableWindowMask;
   360     }
   361 
   362     nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   363 
   364     if (window->flags & SDL_WINDOW_SHOWN) {
   365         [nswindow makeKeyAndOrderFront:nil];
   366     }
   367     if (window->flags & SDL_WINDOW_MAXIMIZED) {
   368         [nswindow performZoom:nil];
   369     }
   370     if (window->flags & SDL_WINDOW_MINIMIZED) {
   371         [nswindow performMiniaturize:nil];
   372     }
   373 
   374     if (window->title) {
   375         title = [[NSString alloc] initWithUTF8String:window->title];
   376         [nswindow setTitle:title];
   377         [nswindow setMiniwindowTitle:title];
   378         [title release];
   379     }
   380 
   381     [pool release];
   382 
   383     if (SetupWindowData(window, nswindow, YES) < 0) {
   384         [nswindow release];
   385         return -1;
   386     }
   387 #ifdef SDL_VIDEO_OPENGL
   388     if (window->flags & SDL_WINDOW_OPENGL) {
   389         if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   390             Cocoa_DestroyWindow(_this, window);
   391             return -1;
   392         }
   393     }
   394 #endif
   395     return 0;
   396 }
   397 
   398 int
   399 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   400 {
   401     NSAutoreleasePool *pool;
   402     NSWindow *nswindow = (NSWindow *) data;
   403     NSString *title;
   404     int status;
   405 
   406     pool = [[NSAutoreleasePool alloc] init];
   407 
   408     /* Query the title from the existing window */
   409     title = [nswindow title];
   410     if (title) {
   411         window->title = SDL_strdup([title UTF8String]);
   412     }
   413 
   414     [pool release];
   415 
   416     return SetupWindowData(window, nswindow, NO);
   417 }
   418 
   419 void
   420 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   421 {
   422     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   423     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   424     NSString *string;
   425 
   426     string = [[NSString alloc] initWithUTF8String:window->title];
   427     [nswindow setTitle:string];
   428     [nswindow setMiniwindowTitle:string];
   429     [string release];
   430     [pool release];
   431 }
   432 
   433 void
   434 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   435 {
   436     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   437     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   438     NSRect rect;
   439 
   440     rect.origin.x = window->x;
   441     rect.origin.y = window->y;
   442     rect.size.width = window->w;
   443     rect.size.height = window->h;
   444     ConvertNSRect(&rect);
   445     rect = [nswindow frameRectForContentRect:rect];
   446     [nswindow setFrameOrigin:rect.origin];
   447     [pool release];
   448 }
   449 
   450 void
   451 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   452 {
   453     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   454     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   455     NSSize size;
   456 
   457     size.width = window->w;
   458     size.height = window->h;
   459     [nswindow setContentSize:size];
   460     [pool release];
   461 }
   462 
   463 void
   464 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   465 {
   466     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   467     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   468 
   469     [nswindow makeKeyAndOrderFront:nil];
   470     [pool release];
   471 }
   472 
   473 void
   474 Cocoa_HideWindow(_THIS, SDL_Window * window)
   475 {
   476     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   477 
   478     /* FIXME */
   479 }
   480 
   481 void
   482 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   483 {
   484     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   485     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   486 
   487     [nswindow makeKeyAndOrderFront:nil];
   488     [pool release];
   489 }
   490 
   491 void
   492 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   493 {
   494     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   495     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   496 
   497     [nswindow performZoom:nil];
   498     [pool release];
   499 }
   500 
   501 void
   502 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   503 {
   504     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   505     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   506 
   507     [nswindow performMiniaturize:nil];
   508     [pool release];
   509 }
   510 
   511 void
   512 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   513 {
   514     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   515 
   516     /* FIXME */
   517 }
   518 
   519 void
   520 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   521 {
   522     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   523         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   524         /* FIXME: Grab mouse */
   525     } else {
   526         /* FIXME: Release mouse */
   527     }
   528 }
   529 
   530 void
   531 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   532 {
   533     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   534     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   535 
   536     if (data) {
   537 #ifdef SDL_VIDEO_OPENGL
   538         if (window->flags & SDL_WINDOW_OPENGL) {
   539             Cocoa_GL_CleanupWindow(_this, window);
   540         }
   541 #endif
   542         [data->listener close];
   543         [data->listener release];
   544         if (data->created) {
   545             [data->window close];
   546         }
   547         SDL_free(data);
   548     }
   549     [pool release];
   550 }
   551 
   552 SDL_bool
   553 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   554 {
   555     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   556 
   557     if (info->version.major <= SDL_MAJOR_VERSION) {
   558         //info->window = nswindow;
   559         return SDL_TRUE;
   560     } else {
   561         SDL_SetError("Application not compiled with SDL %d.%d\n",
   562                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   563         return SDL_FALSE;
   564     }
   565 }
   566 
   567 /* vi: set ts=4 sw=4 expandtab: */