Fixed compiler warning on 32-bit Mac OS X.
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "SDL_config.h"
23 #include "SDL_syswm.h"
24 #include "SDL_timer.h" /* For SDL_GetTicks() */
25 #include "../SDL_sysvideo.h"
26 #include "../../events/SDL_keyboard_c.h"
27 #include "../../events/SDL_mouse_c.h"
28 #include "../../events/SDL_touch_c.h"
29 #include "../../events/SDL_windowevents_c.h"
30 #include "SDL_cocoavideo.h"
31 #include "SDL_cocoashape.h"
32 #include "SDL_cocoamouse.h"
35 static Uint32 s_moveHack;
37 static __inline__ void ConvertNSRect(NSRect *r)
39 r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
42 @implementation Cocoa_WindowListener
44 - (void)listen:(SDL_WindowData *)data
46 NSNotificationCenter *center;
47 NSWindow *window = data->nswindow;
48 NSView *view = [window contentView];
52 center = [NSNotificationCenter defaultCenter];
54 if ([window delegate] != nil) {
55 [center addObserver:self selector:@selector(windowDidExpose:) name:NSWindowDidExposeNotification object:window];
56 [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:window];
57 [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:window];
58 [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:window];
59 [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
60 [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
61 [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
63 [window setDelegate:self];
66 [window setNextResponder:self];
67 [window setAcceptsMouseMovedEvents:YES];
69 [view setNextResponder:self];
70 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
71 [view setAcceptsTouchEvents:YES];
77 NSNotificationCenter *center;
78 NSWindow *window = _data->nswindow;
79 NSView *view = [window contentView];
81 center = [NSNotificationCenter defaultCenter];
83 if ([window delegate] != self) {
84 [center removeObserver:self name:NSWindowDidExposeNotification object:window];
85 [center removeObserver:self name:NSWindowDidMoveNotification object:window];
86 [center removeObserver:self name:NSWindowDidResizeNotification object:window];
87 [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:window];
88 [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
89 [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
90 [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
92 [window setDelegate:nil];
95 if ([window nextResponder] == self) {
96 [window setNextResponder:nil];
98 if ([view nextResponder] == self) {
99 [view setNextResponder:nil];
103 - (BOOL)windowShouldClose:(id)sender
105 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
109 - (void)windowDidExpose:(NSNotification *)aNotification
111 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
114 - (void)windowDidMove:(NSNotification *)aNotification
117 SDL_VideoDevice *device = SDL_GetVideoDevice();
118 SDL_Window *window = _data->window;
119 NSWindow *nswindow = _data->nswindow;
120 NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
121 ConvertNSRect(&rect);
124 SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
129 /* Cocoa is adjusting the window in response to a mode change */
130 rect.origin.x = window->x;
131 rect.origin.y = window->y;
132 ConvertNSRect(&rect);
133 [nswindow setFrameOrigin:rect.origin];
138 x = (int)rect.origin.x;
139 y = (int)rect.origin.y;
141 if (window == device->current_glwin) {
142 [((NSOpenGLContext *) device->current_glctx) update];
145 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
148 - (void)windowDidResize:(NSNotification *)aNotification
150 SDL_VideoDevice *device = SDL_GetVideoDevice();
152 NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
153 w = (int)rect.size.width;
154 h = (int)rect.size.height;
155 if (SDL_IsShapedWindow(_data->window))
156 Cocoa_ResizeWindowShape(_data->window);
158 if (_data->window == device->current_glwin) {
159 [((NSOpenGLContext *) device->current_glctx) update];
162 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESIZED, w, h);
165 - (void)windowDidMiniaturize:(NSNotification *)aNotification
167 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
170 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
172 SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
175 - (void)windowDidBecomeKey:(NSNotification *)aNotification
177 SDL_Window *window = _data->window;
179 /* We're going to get keyboard events, since we're key. */
180 SDL_SetKeyboardFocus(window);
182 /* If we just gained focus we need the updated mouse position */
187 point = [_data->nswindow mouseLocationOutsideOfEventStream];
189 y = (int)(window->h - point.y);
191 if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
192 if (SDL_GetMouseFocus() != window) {
193 [self mouseEntered:nil];
195 SDL_SendMouseMotion(window, 0, x, y);
199 /* Check to see if someone updated the clipboard */
200 Cocoa_CheckClipboardUpdate(_data->videodata);
203 - (void)windowDidResignKey:(NSNotification *)aNotification
205 /* Some other window will get mouse events, since we're not key. */
206 if (SDL_GetMouseFocus() == _data->window) {
207 SDL_SetMouseFocus(NULL);
210 /* Some other window will get keyboard events, since we're not key. */
211 if (SDL_GetKeyboardFocus() == _data->window) {
212 SDL_SetKeyboardFocus(NULL);
216 - (void)mouseDown:(NSEvent *)theEvent
220 switch ([theEvent buttonNumber]) {
222 button = SDL_BUTTON_LEFT;
225 button = SDL_BUTTON_RIGHT;
228 button = SDL_BUTTON_MIDDLE;
231 button = [theEvent buttonNumber] + 1;
234 SDL_SendMouseButton(_data->window, SDL_PRESSED, button);
237 - (void)rightMouseDown:(NSEvent *)theEvent
239 [self mouseDown:theEvent];
242 - (void)otherMouseDown:(NSEvent *)theEvent
244 [self mouseDown:theEvent];
247 - (void)mouseUp:(NSEvent *)theEvent
251 switch ([theEvent buttonNumber]) {
253 button = SDL_BUTTON_LEFT;
256 button = SDL_BUTTON_RIGHT;
259 button = SDL_BUTTON_MIDDLE;
262 button = [theEvent buttonNumber] + 1;
265 SDL_SendMouseButton(_data->window, SDL_RELEASED, button);
268 - (void)rightMouseUp:(NSEvent *)theEvent
270 [self mouseUp:theEvent];
273 - (void)otherMouseUp:(NSEvent *)theEvent
275 [self mouseUp:theEvent];
278 - (void)mouseEntered:(NSEvent *)theEvent
280 SDL_Mouse *mouse = SDL_GetMouse();
282 SDL_SetMouseFocus(_data->window);
287 - (void)mouseExited:(NSEvent *)theEvent
289 SDL_Window *window = _data->window;
291 if (SDL_GetMouseFocus() == window) {
292 if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
297 point = [theEvent locationInWindow];
298 point.y = window->h - point.y;
300 SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
301 SDL_GetMouseState(&x, &y);
302 cgpoint.x = window->x + x;
303 cgpoint.y = window->y + y;
304 CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
306 SDL_SetMouseFocus(NULL);
308 [[NSCursor arrowCursor] set];
314 - (void)mouseMoved:(NSEvent *)theEvent
316 SDL_Mouse *mouse = SDL_GetMouse();
317 SDL_Window *window = _data->window;
321 if (mouse->relative_mode) {
325 point = [theEvent locationInWindow];
327 y = (int)(window->h - point.y);
329 if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
330 if (SDL_GetMouseFocus() == window) {
331 [self mouseExited:theEvent];
334 if (SDL_GetMouseFocus() != window) {
335 [self mouseEntered:theEvent];
337 SDL_SendMouseMotion(window, 0, x, y);
341 - (void)mouseDragged:(NSEvent *)theEvent
343 [self mouseMoved:theEvent];
346 - (void)rightMouseDragged:(NSEvent *)theEvent
348 [self mouseMoved:theEvent];
351 - (void)otherMouseDragged:(NSEvent *)theEvent
353 [self mouseMoved:theEvent];
356 - (void)scrollWheel:(NSEvent *)theEvent
358 Cocoa_HandleMouseWheel(_data->window, theEvent);
361 - (void)touchesBeganWithEvent:(NSEvent *) theEvent
363 [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
366 - (void)touchesMovedWithEvent:(NSEvent *) theEvent
368 [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
371 - (void)touchesEndedWithEvent:(NSEvent *) theEvent
373 [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
376 - (void)touchesCancelledWithEvent:(NSEvent *) theEvent
378 [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
381 - (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
383 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
385 NSEnumerator *enumerator;
389 case COCOA_TOUCH_DOWN:
390 touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
393 case COCOA_TOUCH_CANCELLED:
394 touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil];
396 case COCOA_TOUCH_MOVE:
397 touches = [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil];
401 enumerator = [touches objectEnumerator];
402 touch = (NSTouch*)[enumerator nextObject];
404 const SDL_TouchID touchId = (SDL_TouchID) ((size_t) [touch device]);
405 if (!SDL_GetTouch(touchId)) {
411 touch.native_xres = touch.x_max - touch.x_min;
414 touch.native_yres = touch.y_max - touch.y_min;
415 touch.pressure_min = 0;
416 touch.pressure_max = 1;
417 touch.native_pressureres = touch.pressure_max - touch.pressure_min;
419 if (SDL_AddTouch(&touch, "") < 0) {
424 const SDL_FingerID fingerId = (SDL_FingerID) ((size_t) [touch identity]);
425 float x = [touch normalizedPosition].x;
426 float y = [touch normalizedPosition].y;
427 /* Make the origin the upper left instead of the lower left */
431 case COCOA_TOUCH_DOWN:
432 SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
435 case COCOA_TOUCH_CANCELLED:
436 SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
438 case COCOA_TOUCH_MOVE:
439 SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
443 touch = (NSTouch*)[enumerator nextObject];
445 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 */
450 @interface SDLWindow : NSWindow
451 /* These are needed for borderless/fullscreen windows */
452 - (BOOL)canBecomeKeyWindow;
453 - (BOOL)canBecomeMainWindow;
456 @implementation SDLWindow
457 - (BOOL)canBecomeKeyWindow
462 - (BOOL)canBecomeMainWindow
468 @interface SDLView : NSView
469 /* The default implementation doesn't pass rightMouseDown to responder chain */
470 - (void)rightMouseDown:(NSEvent *)theEvent;
473 @implementation SDLView
474 - (void)rightMouseDown:(NSEvent *)theEvent
476 [[self nextResponder] rightMouseDown:theEvent];
481 GetWindowStyle(SDL_Window * window)
485 if (window->flags & SDL_WINDOW_FULLSCREEN) {
486 style = NSBorderlessWindowMask;
488 if (window->flags & SDL_WINDOW_BORDERLESS) {
489 style = NSBorderlessWindowMask;
491 style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
493 if (window->flags & SDL_WINDOW_RESIZABLE) {
494 style |= NSResizableWindowMask;
501 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
503 NSAutoreleasePool *pool;
504 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
505 SDL_WindowData *data;
507 /* Allocate the window data */
508 data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
513 data->window = window;
514 data->nswindow = nswindow;
515 data->created = created;
516 data->videodata = videodata;
518 pool = [[NSAutoreleasePool alloc] init];
520 /* Create an event listener for the window */
521 data->listener = [[Cocoa_WindowListener alloc] init];
523 /* Fill in the SDL window with the window data */
525 NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
526 NSView *contentView = [[SDLView alloc] initWithFrame:rect];
527 [nswindow setContentView: contentView];
528 [contentView release];
530 ConvertNSRect(&rect);
531 window->x = (int)rect.origin.x;
532 window->y = (int)rect.origin.y;
533 window->w = (int)rect.size.width;
534 window->h = (int)rect.size.height;
537 /* Set up the listener after we create the view */
538 [data->listener listen:data];
540 if ([nswindow isVisible]) {
541 window->flags |= SDL_WINDOW_SHOWN;
543 window->flags &= ~SDL_WINDOW_SHOWN;
546 unsigned int style = [nswindow styleMask];
548 if (style == NSBorderlessWindowMask) {
549 window->flags |= SDL_WINDOW_BORDERLESS;
551 window->flags &= ~SDL_WINDOW_BORDERLESS;
553 if (style & NSResizableWindowMask) {
554 window->flags |= SDL_WINDOW_RESIZABLE;
556 window->flags &= ~SDL_WINDOW_RESIZABLE;
559 /* isZoomed always returns true if the window is not resizable */
560 if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
561 window->flags |= SDL_WINDOW_MAXIMIZED;
563 window->flags &= ~SDL_WINDOW_MAXIMIZED;
565 if ([nswindow isMiniaturized]) {
566 window->flags |= SDL_WINDOW_MINIMIZED;
568 window->flags &= ~SDL_WINDOW_MINIMIZED;
570 if ([nswindow isKeyWindow]) {
571 window->flags |= SDL_WINDOW_INPUT_FOCUS;
572 SDL_SetKeyboardFocus(data->window);
577 window->driverdata = data;
582 Cocoa_CreateWindow(_THIS, SDL_Window * window)
584 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
586 SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
591 Cocoa_GetDisplayBounds(_this, display, &bounds);
592 rect.origin.x = window->x;
593 rect.origin.y = window->y;
594 rect.size.width = window->w;
595 rect.size.height = window->h;
596 ConvertNSRect(&rect);
598 style = GetWindowStyle(window);
600 /* Figure out which screen to place this window */
601 NSArray *screens = [NSScreen screens];
602 NSScreen *screen = nil;
604 int i, count = [screens count];
605 for (i = 0; i < count; ++i) {
606 candidate = [screens objectAtIndex:i];
607 NSRect screenRect = [candidate frame];
608 if (rect.origin.x >= screenRect.origin.x &&
609 rect.origin.x < screenRect.origin.x + screenRect.size.width &&
610 rect.origin.y >= screenRect.origin.y &&
611 rect.origin.y < screenRect.origin.y + screenRect.size.height) {
613 rect.origin.x -= screenRect.origin.x;
614 rect.origin.y -= screenRect.origin.y;
617 nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
621 if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
629 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
631 NSAutoreleasePool *pool;
632 NSWindow *nswindow = (NSWindow *) data;
635 pool = [[NSAutoreleasePool alloc] init];
637 /* Query the title from the existing window */
638 title = [nswindow title];
640 window->title = SDL_strdup([title UTF8String]);
645 return SetupWindowData(_this, window, nswindow, SDL_FALSE);
649 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
651 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
652 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
656 string = [[NSString alloc] initWithUTF8String:window->title];
658 string = [[NSString alloc] init];
660 [nswindow setTitle:string];
667 Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
669 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
670 NSImage *nsimage = Cocoa_CreateImage(icon);
673 [NSApp setApplicationIconImage:nsimage];
680 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
682 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
683 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
687 rect.origin.x = window->x;
688 rect.origin.y = window->y;
689 rect.size.width = window->w;
690 rect.size.height = window->h;
691 ConvertNSRect(&rect);
693 moveHack = s_moveHack;
695 [nswindow setFrameOrigin:rect.origin];
696 s_moveHack = moveHack;
698 if (window == _this->current_glwin) {
699 [((NSOpenGLContext *) _this->current_glctx) update];
706 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
708 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
709 SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
710 NSWindow *nswindow = windata->nswindow;
713 size.width = window->w;
714 size.height = window->h;
715 [nswindow setContentSize:size];
717 if (window == _this->current_glwin) {
718 [((NSOpenGLContext *) _this->current_glctx) update];
725 Cocoa_ShowWindow(_THIS, SDL_Window * window)
727 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
728 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
730 if (![nswindow isMiniaturized]) {
731 [nswindow makeKeyAndOrderFront:nil];
737 Cocoa_HideWindow(_THIS, SDL_Window * window)
739 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
740 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
742 [nswindow orderOut:nil];
747 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
749 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
750 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
752 [nswindow makeKeyAndOrderFront:nil];
757 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
759 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
760 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
764 if (window == _this->current_glwin) {
765 [((NSOpenGLContext *) _this->current_glctx) update];
772 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
774 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
775 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
777 [nswindow miniaturize:nil];
782 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
784 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
785 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
787 if ([nswindow isMiniaturized]) {
788 [nswindow deminiaturize:nil];
789 } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
796 Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
798 if (!data->created) {
799 /* Don't mess with other people's windows... */
803 [data->listener close];
804 data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:YES screen:[nswindow screen]];
805 [data->nswindow setContentView:[nswindow contentView]];
806 [data->listener listen:data];
810 return data->nswindow;
814 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
816 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
817 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
818 NSWindow *nswindow = data->nswindow;
821 /* The view responder chain gets messed with during setStyleMask */
822 if ([[nswindow contentView] nextResponder] == data->listener) {
823 [[nswindow contentView] setNextResponder:nil];
829 Cocoa_GetDisplayBounds(_this, display, &bounds);
830 rect.origin.x = bounds.x;
831 rect.origin.y = bounds.y;
832 rect.size.width = bounds.w;
833 rect.size.height = bounds.h;
834 ConvertNSRect(&rect);
836 /* Hack to fix origin on Mac OS X 10.4 */
837 NSRect screenRect = [[nswindow screen] frame];
838 if (screenRect.size.height >= 1.0f) {
839 rect.origin.y += (screenRect.size.height - rect.size.height);
842 if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
843 [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
845 nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
848 rect.origin.x = window->windowed.x;
849 rect.origin.y = window->windowed.y;
850 rect.size.width = window->windowed.w;
851 rect.size.height = window->windowed.h;
852 ConvertNSRect(&rect);
854 if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
855 [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
857 nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
861 /* The view responder chain gets messed with during setStyleMask */
862 if ([[nswindow contentView] nextResponder] != data->listener) {
863 [[nswindow contentView] setNextResponder:data->listener];
867 [nswindow setFrameOrigin:rect.origin];
868 [nswindow setContentSize:rect.size];
869 s_moveHack = SDL_GetTicks();
871 /* When the window style changes the title is cleared */
873 Cocoa_SetWindowTitle(_this, window);
876 #ifdef FULLSCREEN_TOGGLEABLE
878 /* OpenGL is rendering to the window, so make it visible! */
879 [nswindow setLevel:CGShieldingWindowLevel()];
881 [nswindow setLevel:kCGNormalWindowLevel];
884 [nswindow makeKeyAndOrderFront:nil];
886 if (window == _this->current_glwin) {
887 [((NSOpenGLContext *) _this->current_glctx) update];
894 Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
896 SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
897 CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
898 const uint32_t tableSize = 256;
899 CGGammaValue redTable[tableSize];
900 CGGammaValue greenTable[tableSize];
901 CGGammaValue blueTable[tableSize];
903 float inv65535 = 1.0f / 65535.0f;
905 /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
906 for (i = 0; i < 256; i++) {
907 redTable[i] = ramp[0*256+i] * inv65535;
908 greenTable[i] = ramp[1*256+i] * inv65535;
909 blueTable[i] = ramp[2*256+i] * inv65535;
912 if (CGSetDisplayTransferByTable(display_id, tableSize,
913 redTable, greenTable, blueTable) != CGDisplayNoErr) {
914 SDL_SetError("CGSetDisplayTransferByTable()");
921 Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
923 SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
924 CGDirectDisplayID display_id = ((SDL_DisplayData *)display->driverdata)->display;
925 const uint32_t tableSize = 256;
926 CGGammaValue redTable[tableSize];
927 CGGammaValue greenTable[tableSize];
928 CGGammaValue blueTable[tableSize];
929 uint32_t i, tableCopied;
931 if (CGGetDisplayTransferByTable(display_id, tableSize,
932 redTable, greenTable, blueTable, &tableCopied) != CGDisplayNoErr) {
933 SDL_SetError("CGGetDisplayTransferByTable()");
937 for (i = 0; i < tableCopied; i++) {
938 ramp[0*256+i] = (Uint16)(redTable[i] * 65535.0f);
939 ramp[1*256+i] = (Uint16)(greenTable[i] * 65535.0f);
940 ramp[2*256+i] = (Uint16)(blueTable[i] * 65535.0f);
946 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
948 /* Move the cursor to the nearest point in the window */
949 if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
950 (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
954 SDL_GetMouseState(&x, &y);
955 cgpoint.x = window->x + x;
956 cgpoint.y = window->y + y;
957 CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
962 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
964 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
965 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
968 [data->listener close];
969 [data->listener release];
971 [data->nswindow close];
979 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
981 NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
983 if (info->version.major <= SDL_MAJOR_VERSION) {
984 info->subsystem = SDL_SYSWM_COCOA;
985 info->info.cocoa.window = nswindow;
988 SDL_SetError("Application not compiled with SDL %d.%d\n",
989 SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
994 /* vi: set ts=4 sw=4 expandtab: */