2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
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"
30 #include "SDL_cocoavideo.h"
32 static __inline__ void ConvertNSRect(NSRect *r)
34 r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
37 @implementation Cocoa_WindowListener
39 - (void)listen:(SDL_WindowData *)data
41 NSNotificationCenter *center;
45 center = [NSNotificationCenter defaultCenter];
47 [_data->window setNextResponder:self];
48 if ([_data->window delegate] != nil) {
49 [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
50 [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
51 [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
52 [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
53 [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
54 [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
55 [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
57 [_data->window setDelegate:self];
59 [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
60 [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
62 [_data->window setAcceptsMouseMovedEvents:YES];
67 NSNotificationCenter *center;
69 center = [NSNotificationCenter defaultCenter];
71 [_data->window setNextResponder:nil];
72 if ([_data->window delegate] != self) {
73 [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
74 [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
75 [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
76 [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
77 [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
78 [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
79 [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
81 [_data->window setDelegate:nil];
83 [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
84 [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
87 - (BOOL)windowShouldClose:(id)sender
89 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
93 - (void)windowDidExpose:(NSNotification *)aNotification
95 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
98 - (void)windowDidMove:(NSNotification *)aNotification
101 NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
102 ConvertNSRect(&rect);
103 x = (int)rect.origin.x;
104 y = (int)rect.origin.y;
105 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
108 - (void)windowDidResize:(NSNotification *)aNotification
111 NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
112 w = (int)rect.size.width;
113 h = (int)rect.size.height;
114 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
117 - (void)windowDidMiniaturize:(NSNotification *)aNotification
119 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
122 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
124 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
127 - (void)windowDidBecomeKey:(NSNotification *)aNotification
131 /* We're going to get keyboard events, since we're key. */
132 index = _data->videodata->keyboard;
133 SDL_SetKeyboardFocus(index, _data->windowID);
136 - (void)windowDidResignKey:(NSNotification *)aNotification
141 /* Some other window will get mouse events, since we're not key. */
142 index = _data->videodata->mouse;
143 mouse = SDL_GetMouse(index);
144 if (mouse->focus == _data->windowID) {
145 SDL_SetMouseFocus(index, 0);
148 /* Some other window will get keyboard events, since we're not key. */
149 index = _data->videodata->keyboard;
150 SDL_SetKeyboardFocus(index, 0);
153 - (void)windowDidHide:(NSNotification *)aNotification
155 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
158 - (void)windowDidUnhide:(NSNotification *)aNotification
160 SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
163 - (void)mouseDown:(NSEvent *)theEvent
168 index = _data->videodata->mouse;
169 switch ([theEvent buttonNumber]) {
171 button = SDL_BUTTON_LEFT;
174 button = SDL_BUTTON_RIGHT;
177 button = SDL_BUTTON_MIDDLE;
180 button = [theEvent buttonNumber];
183 SDL_SendMouseButton(index, SDL_PRESSED, button);
186 - (void)rightMouseDown:(NSEvent *)theEvent
188 [self mouseDown:theEvent];
191 - (void)otherMouseDown:(NSEvent *)theEvent
193 [self mouseDown:theEvent];
196 - (void)mouseUp:(NSEvent *)theEvent
201 index = _data->videodata->mouse;
202 switch ([theEvent buttonNumber]) {
204 button = SDL_BUTTON_LEFT;
207 button = SDL_BUTTON_RIGHT;
210 button = SDL_BUTTON_MIDDLE;
213 button = [theEvent buttonNumber];
216 SDL_SendMouseButton(index, SDL_RELEASED, button);
219 - (void)rightMouseUp:(NSEvent *)theEvent
221 [self mouseUp:theEvent];
224 - (void)otherMouseUp:(NSEvent *)theEvent
226 [self mouseUp:theEvent];
229 - (void)mouseMoved:(NSEvent *)theEvent
231 SDL_Window *window = SDL_GetWindowFromID(_data->windowID);
236 index = _data->videodata->mouse;
237 mouse = SDL_GetMouse(index);
238 point = [theEvent locationInWindow];
239 point.y = window->h - point.y;
240 if ( point.x < 0 || point.x >= window->w ||
241 point.y < 0 || point.y >= window->h ) {
242 if (mouse->focus != 0) {
243 SDL_SetMouseFocus(index, 0);
246 if (mouse->focus != _data->windowID) {
247 SDL_SetMouseFocus(index, _data->windowID);
249 SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y, 0);
253 - (void)mouseDragged:(NSEvent *)theEvent
255 [self mouseMoved:theEvent];
258 - (void)rightMouseDragged:(NSEvent *)theEvent
260 [self mouseMoved:theEvent];
263 - (void)otherMouseDragged:(NSEvent *)theEvent
265 [self mouseMoved:theEvent];
268 - (void)scrollWheel:(NSEvent *)theEvent
272 index = _data->videodata->mouse;
273 SDL_SendMouseWheel(index, (int)([theEvent deltaX]+0.9f), (int)([theEvent deltaY]+0.9f));
278 @interface SDLWindow : NSWindow
279 /* These are needed for borderless/fullscreen windows */
280 - (BOOL)canBecomeKeyWindow;
281 - (BOOL)canBecomeMainWindow;
284 @implementation SDLWindow
285 - (BOOL)canBecomeKeyWindow
290 - (BOOL)canBecomeMainWindow
297 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
299 NSAutoreleasePool *pool;
300 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
301 SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
302 SDL_WindowData *data;
304 /* Allocate the window data */
305 data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
310 data->windowID = window->id;
311 data->window = nswindow;
312 data->created = created;
313 data->display = displaydata->display;
314 data->videodata = videodata;
316 pool = [[NSAutoreleasePool alloc] init];
318 /* Create an event listener for the window */
319 data->listener = [[Cocoa_WindowListener alloc] init];
320 [data->listener listen:data];
322 /* Fill in the SDL window with the window data */
324 NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
325 ConvertNSRect(&rect);
326 window->x = (int)rect.origin.x;
327 window->y = (int)rect.origin.y;
328 window->w = (int)rect.size.width;
329 window->h = (int)rect.size.height;
331 if ([nswindow isVisible]) {
332 window->flags |= SDL_WINDOW_SHOWN;
334 window->flags &= ~SDL_WINDOW_SHOWN;
337 unsigned int style = [nswindow styleMask];
339 if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
340 window->flags |= SDL_WINDOW_BORDERLESS;
342 window->flags &= ~SDL_WINDOW_BORDERLESS;
344 if (style & NSResizableWindowMask) {
345 window->flags |= SDL_WINDOW_RESIZABLE;
347 window->flags &= ~SDL_WINDOW_RESIZABLE;
350 if ([nswindow isZoomed]) {
351 window->flags |= SDL_WINDOW_MAXIMIZED;
353 window->flags &= ~SDL_WINDOW_MAXIMIZED;
355 if ([nswindow isMiniaturized]) {
356 window->flags |= SDL_WINDOW_MINIMIZED;
358 window->flags &= ~SDL_WINDOW_MINIMIZED;
360 if ([nswindow isKeyWindow]) {
361 int index = data->videodata->keyboard;
362 window->flags |= SDL_WINDOW_INPUT_FOCUS;
363 SDL_SetKeyboardFocus(index, data->windowID);
365 if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
372 window->driverdata = data;
377 Cocoa_CreateWindow(_THIS, SDL_Window * window)
379 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
381 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
388 Cocoa_GetDisplayBounds(_this, display, &bounds);
389 if ((window->flags & SDL_WINDOW_FULLSCREEN)
390 || window->x == SDL_WINDOWPOS_CENTERED) {
391 rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
392 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
393 rect.origin.x = bounds.x;
395 rect.origin.x = window->x;
397 if ((window->flags & SDL_WINDOW_FULLSCREEN)
398 || window->y == SDL_WINDOWPOS_CENTERED) {
399 rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
400 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
401 rect.origin.y = bounds.y;
403 rect.origin.y = window->y;
405 rect.size.width = window->w;
406 rect.size.height = window->h;
407 ConvertNSRect(&rect);
409 if (window->flags & SDL_WINDOW_BORDERLESS) {
410 style = NSBorderlessWindowMask;
412 style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
414 if (window->flags & SDL_WINDOW_RESIZABLE) {
415 style |= NSResizableWindowMask;
418 /* Figure out which screen to place this window */
419 NSArray *screens = [NSScreen screens];
420 NSScreen *screen = nil;
422 int i, count = [screens count];
423 for (i = 0; i < count; ++i) {
424 screen = [screens objectAtIndex:i];
425 NSRect screenRect = [candidate frame];
426 if (rect.origin.x >= screenRect.origin.x &&
427 rect.origin.x < screenRect.origin.x + screenRect.size.width &&
428 rect.origin.y >= screenRect.origin.y &&
429 rect.origin.y < screenRect.origin.y + screenRect.size.height) {
431 rect.origin.x -= screenRect.origin.x;
432 rect.origin.y -= screenRect.origin.y;
435 nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
439 if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
447 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
449 NSAutoreleasePool *pool;
450 NSWindow *nswindow = (NSWindow *) data;
454 pool = [[NSAutoreleasePool alloc] init];
456 /* Query the title from the existing window */
457 title = [nswindow title];
459 window->title = SDL_strdup([title UTF8String]);
464 return SetupWindowData(_this, window, nswindow, SDL_FALSE);
468 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
470 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
471 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
475 string = [[NSString alloc] initWithUTF8String:window->title];
477 string = [[NSString alloc] init];
479 [nswindow setTitle:string];
486 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
488 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
489 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
490 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
494 Cocoa_GetDisplayBounds(_this, display, &bounds);
495 if ((window->flags & SDL_WINDOW_FULLSCREEN)
496 || window->x == SDL_WINDOWPOS_CENTERED) {
497 rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
499 rect.origin.x = window->x;
501 if ((window->flags & SDL_WINDOW_FULLSCREEN)
502 || window->y == SDL_WINDOWPOS_CENTERED) {
503 rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
505 rect.origin.y = window->y;
507 rect.size.width = window->w;
508 rect.size.height = window->h;
509 ConvertNSRect(&rect);
510 rect = [nswindow frameRectForContentRect:rect];
511 [nswindow setFrameOrigin:rect.origin];
516 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
518 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
519 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
522 size.width = window->w;
523 size.height = window->h;
524 [nswindow setContentSize:size];
529 Cocoa_ShowWindow(_THIS, SDL_Window * window)
531 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
532 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
534 if (![nswindow isMiniaturized]) {
535 [nswindow makeKeyAndOrderFront:nil];
541 Cocoa_HideWindow(_THIS, SDL_Window * window)
543 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
544 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
546 [nswindow orderOut:nil];
551 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
553 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
554 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
556 [nswindow makeKeyAndOrderFront:nil];
561 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
563 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
564 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
571 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
573 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
574 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
576 [nswindow miniaturize:nil];
581 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
583 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
584 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
586 if ([nswindow isMiniaturized]) {
587 [nswindow deminiaturize:nil];
588 } else if ([nswindow isZoomed]) {
595 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
597 if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
598 (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
599 /* FIXME: Grab mouse */
601 /* FIXME: Release mouse */
606 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
608 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
609 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
612 [data->listener close];
613 [data->listener release];
615 [data->window close];
623 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
625 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
627 if (info->version.major <= SDL_MAJOR_VERSION) {
628 //info->window = nswindow;
631 SDL_SetError("Application not compiled with SDL %d.%d\n",
632 SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
637 /* vi: set ts=4 sw=4 expandtab: */