Cleaned up the mouse window focus handling: you always pass in the relative window when sending a mouse event.
Fixed a bug where only mouse wheel up was sent on Mac OS X
Fixed a bug where mouse window focus was getting hosed by the fullscreen mouse code on Mac OS X
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->nswindow setNextResponder:self];
48 if ([_data->nswindow delegate] != nil) {
49 [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->nswindow];
50 [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->nswindow];
51 [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->nswindow];
52 [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
53 [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
54 [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
55 [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->nswindow];
57 [_data->nswindow 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->nswindow setAcceptsMouseMovedEvents:YES];
67 NSNotificationCenter *center;
69 center = [NSNotificationCenter defaultCenter];
71 [_data->nswindow setNextResponder:nil];
72 if ([_data->nswindow delegate] != self) {
73 [center removeObserver:self name:NSWindowDidExposeNotification object:_data->nswindow];
74 [center removeObserver:self name:NSWindowDidMoveNotification object:_data->nswindow];
75 [center removeObserver:self name:NSWindowDidResizeNotification object:_data->nswindow];
76 [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->nswindow];
77 [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->nswindow];
78 [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->nswindow];
79 [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->nswindow];
81 [_data->nswindow 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->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
93 - (void)windowDidExpose:(NSNotification *)aNotification
95 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
98 - (void)windowDidMove:(NSNotification *)aNotification
101 NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
102 ConvertNSRect(&rect);
103 x = (int)rect.origin.x;
104 y = (int)rect.origin.y;
105 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
108 - (void)windowDidResize:(NSNotification *)aNotification
111 NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
112 w = (int)rect.size.width;
113 h = (int)rect.size.height;
114 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
117 - (void)windowDidMiniaturize:(NSNotification *)aNotification
119 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
122 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
124 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
127 - (void)windowDidBecomeKey:(NSNotification *)aNotification
129 /* We're going to get keyboard events, since we're key. */
130 SDL_SetKeyboardFocus(_data->window);
133 - (void)windowDidResignKey:(NSNotification *)aNotification
135 /* Some other window will get mouse events, since we're not key. */
136 if (SDL_GetMouseFocus() == _data->window) {
137 SDL_SetMouseFocus(NULL);
140 /* Some other window will get keyboard events, since we're not key. */
141 if (SDL_GetKeyboardFocus() == _data->window) {
142 SDL_SetKeyboardFocus(NULL);
146 - (void)windowDidHide:(NSNotification *)aNotification
148 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
151 - (void)windowDidUnhide:(NSNotification *)aNotification
153 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
156 - (void)mouseDown:(NSEvent *)theEvent
160 switch ([theEvent buttonNumber]) {
162 button = SDL_BUTTON_LEFT;
165 button = SDL_BUTTON_RIGHT;
168 button = SDL_BUTTON_MIDDLE;
171 button = [theEvent buttonNumber];
174 SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
177 - (void)rightMouseDown:(NSEvent *)theEvent
179 [self mouseDown:theEvent];
182 - (void)otherMouseDown:(NSEvent *)theEvent
184 [self mouseDown:theEvent];
187 - (void)mouseUp:(NSEvent *)theEvent
191 switch ([theEvent buttonNumber]) {
193 button = SDL_BUTTON_LEFT;
196 button = SDL_BUTTON_RIGHT;
199 button = SDL_BUTTON_MIDDLE;
202 button = [theEvent buttonNumber];
205 SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
208 - (void)rightMouseUp:(NSEvent *)theEvent
210 [self mouseUp:theEvent];
213 - (void)otherMouseUp:(NSEvent *)theEvent
215 [self mouseUp:theEvent];
218 - (void)mouseMoved:(NSEvent *)theEvent
220 SDL_Window *window = _data->window;
223 point = [theEvent locationInWindow];
224 point.y = window->h - point.y;
225 if ( point.x < 0 || point.x >= window->w ||
226 point.y < 0 || point.y >= window->h ) {
227 if (SDL_GetMouseFocus() == window) {
228 SDL_SetMouseFocus(NULL);
231 SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
235 - (void)mouseDragged:(NSEvent *)theEvent
237 [self mouseMoved:theEvent];
240 - (void)rightMouseDragged:(NSEvent *)theEvent
242 [self mouseMoved:theEvent];
245 - (void)otherMouseDragged:(NSEvent *)theEvent
247 [self mouseMoved:theEvent];
250 - (void)scrollWheel:(NSEvent *)theEvent
252 float x = [theEvent deltaX];
253 float y = [theEvent deltaY];
265 SDL_SendMouseWheel(_data->window, (int)x, (int)y);
270 @interface SDLWindow : NSWindow
271 /* These are needed for borderless/fullscreen windows */
272 - (BOOL)canBecomeKeyWindow;
273 - (BOOL)canBecomeMainWindow;
276 @implementation SDLWindow
277 - (BOOL)canBecomeKeyWindow
282 - (BOOL)canBecomeMainWindow
289 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
291 NSAutoreleasePool *pool;
292 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
293 SDL_VideoDisplay *display = window->display;
294 SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
295 SDL_WindowData *data;
297 /* Allocate the window data */
298 data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
303 data->window = window;
304 data->nswindow = nswindow;
305 data->created = created;
306 data->display = displaydata->display;
307 data->videodata = videodata;
309 pool = [[NSAutoreleasePool alloc] init];
311 /* Create an event listener for the window */
312 data->listener = [[Cocoa_WindowListener alloc] init];
313 [data->listener listen:data];
315 /* Fill in the SDL window with the window data */
318 NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
319 ConvertNSRect(&rect);
320 Cocoa_GetDisplayBounds(_this, display, &bounds);
321 window->x = (int)rect.origin.x - bounds.x;
322 window->y = (int)rect.origin.y - bounds.y;
323 window->w = (int)rect.size.width;
324 window->h = (int)rect.size.height;
326 if ([nswindow isVisible]) {
327 window->flags |= SDL_WINDOW_SHOWN;
329 window->flags &= ~SDL_WINDOW_SHOWN;
332 unsigned int style = [nswindow styleMask];
334 if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
335 window->flags |= SDL_WINDOW_BORDERLESS;
337 window->flags &= ~SDL_WINDOW_BORDERLESS;
339 if (style & NSResizableWindowMask) {
340 window->flags |= SDL_WINDOW_RESIZABLE;
342 window->flags &= ~SDL_WINDOW_RESIZABLE;
345 if ([nswindow isZoomed]) {
346 window->flags |= SDL_WINDOW_MAXIMIZED;
348 window->flags &= ~SDL_WINDOW_MAXIMIZED;
350 if ([nswindow isMiniaturized]) {
351 window->flags |= SDL_WINDOW_MINIMIZED;
353 window->flags &= ~SDL_WINDOW_MINIMIZED;
355 if ([nswindow isKeyWindow]) {
356 window->flags |= SDL_WINDOW_INPUT_FOCUS;
357 SDL_SetKeyboardFocus(data->window);
359 if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
366 window->driverdata = data;
371 Cocoa_CreateWindow(_THIS, SDL_Window * window)
373 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
375 SDL_VideoDisplay *display = window->display;
380 Cocoa_GetDisplayBounds(_this, display, &bounds);
381 if ((window->flags & SDL_WINDOW_FULLSCREEN)
382 || window->x == SDL_WINDOWPOS_CENTERED) {
383 rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
384 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
385 rect.origin.x = bounds.x;
387 rect.origin.x = bounds.x + window->x;
389 if ((window->flags & SDL_WINDOW_FULLSCREEN)
390 || window->y == SDL_WINDOWPOS_CENTERED) {
391 rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
392 } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
393 rect.origin.y = bounds.y;
395 rect.origin.y = bounds.y + window->y;
397 rect.size.width = window->w;
398 rect.size.height = window->h;
399 ConvertNSRect(&rect);
401 if (window->flags & SDL_WINDOW_BORDERLESS) {
402 style = NSBorderlessWindowMask;
404 style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
406 if (window->flags & SDL_WINDOW_RESIZABLE) {
407 style |= NSResizableWindowMask;
410 /* Figure out which screen to place this window */
411 NSArray *screens = [NSScreen screens];
412 NSScreen *screen = nil;
414 int i, count = [screens count];
415 for (i = 0; i < count; ++i) {
416 candidate = [screens objectAtIndex:i];
417 NSRect screenRect = [candidate frame];
418 if (rect.origin.x >= screenRect.origin.x &&
419 rect.origin.x < screenRect.origin.x + screenRect.size.width &&
420 rect.origin.y >= screenRect.origin.y &&
421 rect.origin.y < screenRect.origin.y + screenRect.size.height) {
423 rect.origin.x -= screenRect.origin.x;
424 rect.origin.y -= screenRect.origin.y;
427 nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
431 if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
439 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
441 NSAutoreleasePool *pool;
442 NSWindow *nswindow = (NSWindow *) data;
445 pool = [[NSAutoreleasePool alloc] init];
447 /* Query the title from the existing window */
448 title = [nswindow title];
450 window->title = SDL_strdup([title UTF8String]);
455 return SetupWindowData(_this, window, nswindow, SDL_FALSE);
459 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
461 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
462 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
466 string = [[NSString alloc] initWithUTF8String:window->title];
468 string = [[NSString alloc] init];
470 [nswindow setTitle:string];
477 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
479 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
480 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
481 SDL_VideoDisplay *display = window->display;
485 Cocoa_GetDisplayBounds(_this, display, &bounds);
486 if ((window->flags & SDL_WINDOW_FULLSCREEN)
487 || window->x == SDL_WINDOWPOS_CENTERED) {
488 rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
490 rect.origin.x = bounds.x + window->x;
492 if ((window->flags & SDL_WINDOW_FULLSCREEN)
493 || window->y == SDL_WINDOWPOS_CENTERED) {
494 rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
496 rect.origin.y = bounds.y + window->y;
498 rect.size.width = window->w;
499 rect.size.height = window->h;
500 ConvertNSRect(&rect);
501 rect = [nswindow frameRectForContentRect:rect];
502 [nswindow setFrameOrigin:rect.origin];
507 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
509 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
510 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
513 size.width = window->w;
514 size.height = window->h;
515 [nswindow setContentSize:size];
520 Cocoa_ShowWindow(_THIS, SDL_Window * window)
522 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
523 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
525 if (![nswindow isMiniaturized]) {
526 [nswindow makeKeyAndOrderFront:nil];
532 Cocoa_HideWindow(_THIS, SDL_Window * window)
534 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
535 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
537 [nswindow orderOut:nil];
542 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
544 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
545 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
547 [nswindow makeKeyAndOrderFront:nil];
552 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
554 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
555 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
562 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
564 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
565 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
567 [nswindow miniaturize:nil];
572 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
574 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
575 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
577 if ([nswindow isMiniaturized]) {
578 [nswindow deminiaturize:nil];
579 } else if ([nswindow isZoomed]) {
586 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
588 if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
589 (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
590 /* FIXME: Grab mouse */
592 /* FIXME: Release mouse */
597 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
599 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
600 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
603 [data->listener close];
604 [data->listener release];
606 [data->nswindow close];
614 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
616 //NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
618 if (info->version.major <= SDL_MAJOR_VERSION) {
619 //info->window = nswindow;
622 SDL_SetError("Application not compiled with SDL %d.%d\n",
623 SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
628 /* vi: set ts=4 sw=4 expandtab: */