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 Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
24 #include "SDL_QuartzVideo.h"
25 #include "SDL_QuartzWM.h"
28 void QZ_FreeWMCursor (_THIS, WMcursor *cursor) {
30 if ( cursor != NULL ) {
31 [ cursor->nscursor release ];
36 WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask,
37 int w, int h, int hot_x, int hot_y) {
39 NSBitmapImageRep *imgrep;
41 unsigned char *planes[5];
43 NSAutoreleasePool *pool;
45 pool = [ [ NSAutoreleasePool alloc ] init ];
47 /* Allocate the cursor memory */
48 cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
49 if (cursor == NULL) goto outOfMemory;
51 /* create the image representation and get the pointers to its storage */
52 imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: w pixelsHigh: h bitsPerSample: 1 samplesPerPixel: 2 hasAlpha: YES isPlanar: YES colorSpaceName: NSDeviceBlackColorSpace bytesPerRow: (w+7)/8 bitsPerPixel: 0 ] autorelease ];
53 if (imgrep == nil) goto outOfMemory;
54 [ imgrep getBitmapDataPlanes: planes ];
56 /* copy data and mask, extending the mask to all black pixels because the inversion effect doesn't work with Cocoa's alpha-blended cursors */
57 for (i = 0; i < (w+7)/8*h; i++) {
58 planes[0][i] = data[i];
59 planes[1][i] = mask[i] | data[i];
62 /* create image and cursor */
63 img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(w, h) ] autorelease ];
64 if (img == nil) goto outOfMemory;
65 [ img addRepresentation: imgrep ];
66 if (system_version < 0x1030) { /* on 10.2, cursors must be 16*16 */
67 if (w > 16 || h > 16) { /* too big: scale it down */
68 [ img setScalesWhenResized: YES ];
72 else { /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */
75 [ img setSize: NSMakeSize(16, 16) ];
77 cursor->nscursor = [ [ NSCursor alloc ] initWithImage: img hotSpot: NSMakePoint(hot_x, hot_y) ];
78 if (cursor->nscursor == nil) goto outOfMemory;
85 if (cursor != NULL) SDL_free(cursor);
90 void QZ_UpdateCursor (_THIS) {
93 if (cursor_should_be_visible || !(SDL_GetAppState() & SDL_APPMOUSEFOCUS)) {
98 if (state != cursor_visible) {
104 cursor_visible = state;
108 BOOL QZ_IsMouseInWindow (_THIS) {
109 if (qz_window == nil || (mode_flags & SDL_FULLSCREEN)) return YES; /*fullscreen*/
111 NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ];
112 p.y -= 1.0f; /* Apparently y goes from 1 to h, not from 0 to h-1 (i.e. the "location of the mouse" seems to be defined as "the location of the top left corner of the mouse pointer's hot pixel" */
113 return NSPointInRect(p, [ window_view frame ]);
117 int QZ_ShowWMCursor (_THIS, WMcursor *cursor) {
119 if ( cursor == NULL) {
120 if ( cursor_should_be_visible ) {
121 cursor_should_be_visible = NO;
122 QZ_ChangeGrabState (this, QZ_HIDECURSOR);
124 QZ_UpdateCursor(this);
127 if (qz_window ==nil || (mode_flags & SDL_FULLSCREEN)) {
128 [ cursor->nscursor set ];
131 [ qz_window invalidateCursorRectsForView: [ qz_window contentView ] ];
133 if ( ! cursor_should_be_visible ) {
134 cursor_should_be_visible = YES;
135 QZ_ChangeGrabState (this, QZ_SHOWCURSOR);
137 QZ_UpdateCursor(this);
144 Coordinate conversion functions, for convenience
145 Cocoa sets the origin at the lower left corner of the window/screen
146 SDL, CoreGraphics/WindowServer, and QuickDraw use the origin at the upper left corner
147 The routines were written so they could be called before SetVideoMode() has finished;
148 this might have limited usefulness at the moment, but the extra cost is trivial.
151 /* Convert Cocoa screen coordinate to Cocoa window coordinate */
152 void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) {
154 *p = [ qz_window convertScreenToBase:*p ];
158 /* Convert Cocoa window coordinate to Cocoa screen coordinate */
159 void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) {
161 *p = [ qz_window convertBaseToScreen:*p ];
164 /* Convert SDL coordinate to Cocoa coordinate */
165 void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) {
167 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
169 p->y = CGDisplayPixelsHigh (display_id) - p->y;
173 *p = [ window_view convertPoint:*p toView: nil ];
174 p->y = [window_view frame].size.height - p->y;
178 /* Convert Cocoa coordinate to SDL coordinate */
179 void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) {
181 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
183 p->y = CGDisplayPixelsHigh (display_id) - p->y;
187 *p = [ window_view convertPoint:*p fromView: nil ];
188 p->y = [window_view frame].size.height - p->y;
192 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */
193 CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) {
197 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
201 QZ_PrivateSDLToCocoa (this, p);
202 QZ_PrivateLocalToGlobal (this, p);
204 height = CGDisplayPixelsHigh (display_id);
205 p->y = height - p->y;
214 #if 0 /* Dead code */
215 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */
216 void QZ_PrivateCGToSDL (_THIS, NSPoint *p) {
218 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
222 /* Convert CG Global to Cocoa Global */
223 height = CGDisplayPixelsHigh (display_id);
224 p->y = height - p->y;
226 QZ_PrivateGlobalToLocal (this, p);
227 QZ_PrivateCocoaToSDL (this, p);
230 #endif /* Dead code */
232 void QZ_PrivateWarpCursor (_THIS, int x, int y) {
237 p = NSMakePoint (x, y);
238 cgp = QZ_PrivateSDLToCG (this, &p);
240 /* this is the magic call that fixes cursor "freezing" after warp */
241 CGSetLocalEventsSuppressionInterval (0.0);
242 CGWarpMouseCursorPosition (cgp);
245 void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) {
247 /* Only allow warping when in foreground */
248 if ( ! [ NSApp isActive ] )
251 /* Do the actual warp */
252 if (grab_state != QZ_INVISIBLE_GRAB) QZ_PrivateWarpCursor (this, x, y);
254 /* Generate the mouse moved event */
255 SDL_PrivateMouseMotion (0, 0, x, y);
258 void QZ_MoveWMCursor (_THIS, int x, int y) { }
259 void QZ_CheckMouseMode (_THIS) { }
261 void QZ_SetCaption (_THIS, const char *title, const char *icon) {
263 if ( qz_window != nil ) {
265 if ( title != NULL ) {
266 string = [ [ NSString alloc ] initWithUTF8String:title ];
267 [ qz_window setTitle:string ];
270 if ( icon != NULL ) {
271 string = [ [ NSString alloc ] initWithUTF8String:icon ];
272 [ qz_window setMiniwindowTitle:string ];
278 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask)
280 NSBitmapImageRep *imgrep;
282 SDL_Surface *mergedSurface;
283 NSAutoreleasePool *pool;
285 SDL_bool iconSrcAlpha;
286 Uint8 iconAlphaValue;
287 int i, j, maskPitch, index;
289 pool = [ [ NSAutoreleasePool alloc ] init ];
291 imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: icon->w pixelsHigh: icon->h bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar: NO colorSpaceName: NSDeviceRGBColorSpace bytesPerRow: 4*icon->w bitsPerPixel: 32 ] autorelease ];
292 if (imgrep == nil) goto freePool;
293 pixels = [ imgrep bitmapData ];
294 SDL_memset(pixels, 0, 4*icon->w*icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */
296 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
297 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
299 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
301 mergedSurface = SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4*icon->w, BYTEORDER_DEPENDENT_RGBA_MASKS);
302 if (mergedSurface == NULL) goto freePool;
304 /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */
305 iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0);
306 iconAlphaValue = icon->format->alpha;
307 SDL_SetAlpha(icon, 0, 255);
308 SDL_BlitSurface(icon, NULL, mergedSurface, NULL);
309 if (iconSrcAlpha) SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue);
311 SDL_FreeSurface(mergedSurface);
313 /* apply mask, source alpha, and premultiply color values by alpha */
314 maskPitch = (icon->w+7)/8;
315 for (i = 0; i < icon->h; i++) {
316 for (j = 0; j < icon->w; j++) {
317 index = i*4*icon->w + j*4;
318 if (!(mask[i*maskPitch + j/8] & (128 >> j%8))) {
319 pixels[index + 3] = 0;
323 if (icon->format->Amask == 0) pixels[index + 3] = icon->format->alpha;
326 pixels[index + 3] = 255;
329 if (pixels[index + 3] < 255) {
330 pixels[index + 0] = (Uint16)pixels[index + 0]*pixels[index + 3]/255;
331 pixels[index + 1] = (Uint16)pixels[index + 1]*pixels[index + 3]/255;
332 pixels[index + 2] = (Uint16)pixels[index + 2]*pixels[index + 3]/255;
337 img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(icon->w, icon->h) ] autorelease ];
338 if (img == nil) goto freePool;
339 [ img addRepresentation: imgrep ];
340 [ NSApp setApplicationIconImage:img ];
346 int QZ_IconifyWindow (_THIS) {
348 if ( ! [ qz_window isMiniaturized ] ) {
349 [ qz_window miniaturize:nil ];
350 if ( ! [ qz_window isMiniaturized ] ) {
351 SDL_SetError ("window iconification failed");
357 SDL_SetError ("window already iconified");
363 int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) {
364 info->nsWindowPtr = qz_window;
368 void QZ_ChangeGrabState (_THIS, int action) {
371 Figure out what the next state should be based on the action.
372 Ignore actions that can't change the current state.
374 if ( grab_state == QZ_UNGRABBED ) {
375 if ( action == QZ_ENABLE_GRAB ) {
376 if ( cursor_should_be_visible )
377 grab_state = QZ_VISIBLE_GRAB;
379 grab_state = QZ_INVISIBLE_GRAB;
382 else if ( grab_state == QZ_VISIBLE_GRAB ) {
383 if ( action == QZ_DISABLE_GRAB )
384 grab_state = QZ_UNGRABBED;
385 else if ( action == QZ_HIDECURSOR )
386 grab_state = QZ_INVISIBLE_GRAB;
389 assert( grab_state == QZ_INVISIBLE_GRAB );
391 if ( action == QZ_DISABLE_GRAB )
392 grab_state = QZ_UNGRABBED;
393 else if ( action == QZ_SHOWCURSOR )
394 grab_state = QZ_VISIBLE_GRAB;
397 /* now apply the new state */
398 if (grab_state == QZ_UNGRABBED) {
400 CGAssociateMouseAndMouseCursorPosition (1);
402 else if (grab_state == QZ_VISIBLE_GRAB) {
404 CGAssociateMouseAndMouseCursorPosition (1);
407 assert( grab_state == QZ_INVISIBLE_GRAB );
409 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
410 CGAssociateMouseAndMouseCursorPosition (0);
414 SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) {
416 int doGrab = grab_mode & SDL_GRAB_ON;
417 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/
419 if ( this->screen == NULL ) {
420 SDL_SetError ("QZ_GrabInput: screen is NULL");
425 /*SDL_SetError ("QZ_GrabInput: video is not set, grab will take effect on mode switch"); */
426 current_grab_mode = grab_mode;
427 return grab_mode; /* Will be set later on mode switch */
430 if ( grab_mode != SDL_GRAB_QUERY ) {
432 QZ_ChangeGrabState (this, QZ_ENABLE_GRAB);
434 QZ_ChangeGrabState (this, QZ_DISABLE_GRAB);
436 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF;
439 return current_grab_mode;