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