|
1 /* |
|
2 SDL - Simple DirectMedia Layer |
|
3 Copyright (C) 1997-2003 Sam Lantinga |
|
4 |
|
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. |
|
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 Library General Public License for more details. |
|
14 |
|
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 |
|
18 |
|
19 Sam Lantinga |
|
20 slouken@libsdl.org |
|
21 */ |
|
22 #include "SDL_config.h" |
|
23 |
|
24 #include "SDL_QuartzVideo.h" |
|
25 #include "SDL_QuartzWM.h" |
|
26 |
|
27 |
|
28 void QZ_FreeWMCursor (_THIS, WMcursor *cursor) { |
|
29 |
|
30 if ( cursor != NULL ) { |
|
31 [ cursor->nscursor release ]; |
|
32 free (cursor); |
|
33 } |
|
34 } |
|
35 |
|
36 WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask, |
|
37 int w, int h, int hot_x, int hot_y) { |
|
38 WMcursor *cursor; |
|
39 NSBitmapImageRep *imgrep; |
|
40 NSImage *img; |
|
41 unsigned char *planes[5]; |
|
42 int i; |
|
43 NSAutoreleasePool *pool; |
|
44 |
|
45 pool = [ [ NSAutoreleasePool alloc ] init ]; |
|
46 |
|
47 /* Allocate the cursor memory */ |
|
48 cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor)); |
|
49 if (cursor == NULL) goto outOfMemory; |
|
50 |
|
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 ]; |
|
55 |
|
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]; |
|
60 } |
|
61 |
|
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 ]; |
|
69 hot_x = hot_x*16/w; |
|
70 hot_y = hot_y*16/h; |
|
71 } |
|
72 else { /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */ |
|
73 hot_y += 16 - h; |
|
74 } |
|
75 [ img setSize: NSMakeSize(16, 16) ]; |
|
76 } |
|
77 cursor->nscursor = [ [ NSCursor alloc ] initWithImage: img hotSpot: NSMakePoint(hot_x, hot_y) ]; |
|
78 if (cursor->nscursor == nil) goto outOfMemory; |
|
79 |
|
80 [ pool release ]; |
|
81 return(cursor); |
|
82 |
|
83 outOfMemory: |
|
84 [ pool release ]; |
|
85 if (cursor != NULL) SDL_free(cursor); |
|
86 SDL_OutOfMemory(); |
|
87 return(NULL); |
|
88 } |
|
89 |
|
90 void QZ_UpdateCursor (_THIS) { |
|
91 BOOL state; |
|
92 |
|
93 if (cursor_should_be_visible || !(SDL_GetAppState() & SDL_APPMOUSEFOCUS)) { |
|
94 state = YES; |
|
95 } else { |
|
96 state = NO; |
|
97 } |
|
98 if (state != cursor_visible) { |
|
99 if (state) { |
|
100 [ NSCursor unhide ]; |
|
101 } else { |
|
102 [ NSCursor hide ]; |
|
103 } |
|
104 cursor_visible = state; |
|
105 } |
|
106 } |
|
107 |
|
108 BOOL QZ_IsMouseInWindow (_THIS) { |
|
109 if (qz_window == nil || (mode_flags & SDL_FULLSCREEN)) return YES; /*fullscreen*/ |
|
110 else { |
|
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 ]); |
|
114 } |
|
115 } |
|
116 |
|
117 int QZ_ShowWMCursor (_THIS, WMcursor *cursor) { |
|
118 |
|
119 if ( cursor == NULL) { |
|
120 if ( cursor_should_be_visible ) { |
|
121 cursor_should_be_visible = NO; |
|
122 QZ_ChangeGrabState (this, QZ_HIDECURSOR); |
|
123 } |
|
124 QZ_UpdateCursor(this); |
|
125 } |
|
126 else { |
|
127 if (qz_window ==nil || (mode_flags & SDL_FULLSCREEN)) { |
|
128 [ cursor->nscursor set ]; |
|
129 } |
|
130 else { |
|
131 [ qz_window invalidateCursorRectsForView: [ qz_window contentView ] ]; |
|
132 } |
|
133 if ( ! cursor_should_be_visible ) { |
|
134 cursor_should_be_visible = YES; |
|
135 QZ_ChangeGrabState (this, QZ_SHOWCURSOR); |
|
136 } |
|
137 QZ_UpdateCursor(this); |
|
138 } |
|
139 |
|
140 return 1; |
|
141 } |
|
142 |
|
143 /* |
|
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. |
|
149 */ |
|
150 |
|
151 /* Convert Cocoa screen coordinate to Cocoa window coordinate */ |
|
152 void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) { |
|
153 |
|
154 *p = [ qz_window convertScreenToBase:*p ]; |
|
155 } |
|
156 |
|
157 |
|
158 /* Convert Cocoa window coordinate to Cocoa screen coordinate */ |
|
159 void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) { |
|
160 |
|
161 *p = [ qz_window convertBaseToScreen:*p ]; |
|
162 } |
|
163 |
|
164 /* Convert SDL coordinate to Cocoa coordinate */ |
|
165 void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) { |
|
166 |
|
167 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ |
|
168 |
|
169 p->y = CGDisplayPixelsHigh (display_id) - p->y; |
|
170 } |
|
171 else { |
|
172 |
|
173 *p = [ window_view convertPoint:*p toView: nil ]; |
|
174 |
|
175 /* We need a workaround in OpenGL mode */ |
|
176 if ( SDL_VideoSurface->flags & SDL_OPENGL ) { |
|
177 p->y = [window_view frame].size.height - p->y; |
|
178 } |
|
179 } |
|
180 } |
|
181 |
|
182 /* Convert Cocoa coordinate to SDL coordinate */ |
|
183 void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) { |
|
184 |
|
185 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ |
|
186 |
|
187 p->y = CGDisplayPixelsHigh (display_id) - p->y; |
|
188 } |
|
189 else { |
|
190 |
|
191 *p = [ window_view convertPoint:*p fromView: nil ]; |
|
192 |
|
193 /* We need a workaround in OpenGL mode */ |
|
194 if ( SDL_VideoSurface != NULL && (SDL_VideoSurface->flags & SDL_OPENGL) ) { |
|
195 p->y = [window_view frame].size.height - p->y; |
|
196 } |
|
197 } |
|
198 } |
|
199 |
|
200 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */ |
|
201 CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) { |
|
202 |
|
203 CGPoint cgp; |
|
204 |
|
205 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ |
|
206 |
|
207 int height; |
|
208 |
|
209 QZ_PrivateSDLToCocoa (this, p); |
|
210 QZ_PrivateLocalToGlobal (this, p); |
|
211 |
|
212 height = CGDisplayPixelsHigh (display_id); |
|
213 p->y = height - p->y; |
|
214 } |
|
215 |
|
216 cgp.x = p->x; |
|
217 cgp.y = p->y; |
|
218 |
|
219 return cgp; |
|
220 } |
|
221 |
|
222 #if 0 /* Dead code */ |
|
223 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */ |
|
224 void QZ_PrivateCGToSDL (_THIS, NSPoint *p) { |
|
225 |
|
226 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ |
|
227 |
|
228 int height; |
|
229 |
|
230 /* Convert CG Global to Cocoa Global */ |
|
231 height = CGDisplayPixelsHigh (display_id); |
|
232 p->y = height - p->y; |
|
233 |
|
234 QZ_PrivateGlobalToLocal (this, p); |
|
235 QZ_PrivateCocoaToSDL (this, p); |
|
236 } |
|
237 } |
|
238 #endif /* Dead code */ |
|
239 |
|
240 void QZ_PrivateWarpCursor (_THIS, int x, int y) { |
|
241 |
|
242 NSPoint p; |
|
243 CGPoint cgp; |
|
244 |
|
245 p = NSMakePoint (x, y); |
|
246 cgp = QZ_PrivateSDLToCG (this, &p); |
|
247 |
|
248 /* this is the magic call that fixes cursor "freezing" after warp */ |
|
249 CGSetLocalEventsSuppressionInterval (0.0); |
|
250 CGWarpMouseCursorPosition (cgp); |
|
251 } |
|
252 |
|
253 void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) { |
|
254 |
|
255 /* Only allow warping when in foreground */ |
|
256 if ( ! [ NSApp isActive ] ) |
|
257 return; |
|
258 |
|
259 /* Do the actual warp */ |
|
260 if (grab_state != QZ_INVISIBLE_GRAB) QZ_PrivateWarpCursor (this, x, y); |
|
261 |
|
262 /* Generate the mouse moved event */ |
|
263 SDL_PrivateMouseMotion (0, 0, x, y); |
|
264 } |
|
265 |
|
266 void QZ_MoveWMCursor (_THIS, int x, int y) { } |
|
267 void QZ_CheckMouseMode (_THIS) { } |
|
268 |
|
269 void QZ_SetCaption (_THIS, const char *title, const char *icon) { |
|
270 |
|
271 if ( qz_window != nil ) { |
|
272 NSString *string; |
|
273 if ( title != NULL ) { |
|
274 string = [ [ NSString alloc ] initWithUTF8String:title ]; |
|
275 [ qz_window setTitle:string ]; |
|
276 [ string release ]; |
|
277 } |
|
278 if ( icon != NULL ) { |
|
279 string = [ [ NSString alloc ] initWithUTF8String:icon ]; |
|
280 [ qz_window setMiniwindowTitle:string ]; |
|
281 [ string release ]; |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask) |
|
287 { |
|
288 NSBitmapImageRep *imgrep; |
|
289 NSImage *img; |
|
290 SDL_Surface *mergedSurface; |
|
291 NSAutoreleasePool *pool; |
|
292 Uint8 *pixels; |
|
293 SDL_bool iconSrcAlpha; |
|
294 Uint8 iconAlphaValue; |
|
295 int i, j, maskPitch, index; |
|
296 |
|
297 pool = [ [ NSAutoreleasePool alloc ] init ]; |
|
298 |
|
299 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 ]; |
|
300 if (imgrep == nil) goto freePool; |
|
301 pixels = [ imgrep bitmapData ]; |
|
302 SDL_memset(pixels, 0, 4*icon->w*icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */ |
|
303 |
|
304 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
|
305 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
|
306 #else |
|
307 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
|
308 #endif |
|
309 mergedSurface = SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4*icon->w, BYTEORDER_DEPENDENT_RGBA_MASKS); |
|
310 if (mergedSurface == NULL) goto freePool; |
|
311 |
|
312 /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */ |
|
313 iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0); |
|
314 iconAlphaValue = icon->format->alpha; |
|
315 SDL_SetAlpha(icon, 0, 255); |
|
316 SDL_BlitSurface(icon, NULL, mergedSurface, NULL); |
|
317 if (iconSrcAlpha) SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue); |
|
318 |
|
319 SDL_FreeSurface(mergedSurface); |
|
320 |
|
321 /* apply mask, source alpha, and premultiply color values by alpha */ |
|
322 maskPitch = (icon->w+7)/8; |
|
323 for (i = 0; i < icon->h; i++) { |
|
324 for (j = 0; j < icon->w; j++) { |
|
325 index = i*4*icon->w + j*4; |
|
326 if (!(mask[i*maskPitch + j/8] & (128 >> j%8))) { |
|
327 pixels[index + 3] = 0; |
|
328 } |
|
329 else { |
|
330 if (iconSrcAlpha) { |
|
331 if (icon->format->Amask == 0) pixels[index + 3] = icon->format->alpha; |
|
332 } |
|
333 else { |
|
334 pixels[index + 3] = 255; |
|
335 } |
|
336 } |
|
337 if (pixels[index + 3] < 255) { |
|
338 pixels[index + 0] = (Uint16)pixels[index + 0]*pixels[index + 3]/255; |
|
339 pixels[index + 1] = (Uint16)pixels[index + 1]*pixels[index + 3]/255; |
|
340 pixels[index + 2] = (Uint16)pixels[index + 2]*pixels[index + 3]/255; |
|
341 } |
|
342 } |
|
343 } |
|
344 |
|
345 img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(icon->w, icon->h) ] autorelease ]; |
|
346 if (img == nil) goto freePool; |
|
347 [ img addRepresentation: imgrep ]; |
|
348 [ NSApp setApplicationIconImage:img ]; |
|
349 |
|
350 freePool: |
|
351 [ pool release ]; |
|
352 } |
|
353 |
|
354 int QZ_IconifyWindow (_THIS) { |
|
355 |
|
356 if ( ! [ qz_window isMiniaturized ] ) { |
|
357 [ qz_window miniaturize:nil ]; |
|
358 if ( ! [ qz_window isMiniaturized ] ) { |
|
359 SDL_SetError ("window iconification failed"); |
|
360 return 0; |
|
361 } |
|
362 return 1; |
|
363 } |
|
364 else { |
|
365 SDL_SetError ("window already iconified"); |
|
366 return 0; |
|
367 } |
|
368 } |
|
369 |
|
370 /* |
|
371 int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) { |
|
372 info->nsWindowPtr = qz_window; |
|
373 return 0; |
|
374 }*/ |
|
375 |
|
376 void QZ_ChangeGrabState (_THIS, int action) { |
|
377 |
|
378 /* |
|
379 Figure out what the next state should be based on the action. |
|
380 Ignore actions that can't change the current state. |
|
381 */ |
|
382 if ( grab_state == QZ_UNGRABBED ) { |
|
383 if ( action == QZ_ENABLE_GRAB ) { |
|
384 if ( cursor_should_be_visible ) |
|
385 grab_state = QZ_VISIBLE_GRAB; |
|
386 else |
|
387 grab_state = QZ_INVISIBLE_GRAB; |
|
388 } |
|
389 } |
|
390 else if ( grab_state == QZ_VISIBLE_GRAB ) { |
|
391 if ( action == QZ_DISABLE_GRAB ) |
|
392 grab_state = QZ_UNGRABBED; |
|
393 else if ( action == QZ_HIDECURSOR ) |
|
394 grab_state = QZ_INVISIBLE_GRAB; |
|
395 } |
|
396 else { |
|
397 assert( grab_state == QZ_INVISIBLE_GRAB ); |
|
398 |
|
399 if ( action == QZ_DISABLE_GRAB ) |
|
400 grab_state = QZ_UNGRABBED; |
|
401 else if ( action == QZ_SHOWCURSOR ) |
|
402 grab_state = QZ_VISIBLE_GRAB; |
|
403 } |
|
404 |
|
405 /* now apply the new state */ |
|
406 if (grab_state == QZ_UNGRABBED) { |
|
407 |
|
408 CGAssociateMouseAndMouseCursorPosition (1); |
|
409 } |
|
410 else if (grab_state == QZ_VISIBLE_GRAB) { |
|
411 |
|
412 CGAssociateMouseAndMouseCursorPosition (1); |
|
413 } |
|
414 else { |
|
415 assert( grab_state == QZ_INVISIBLE_GRAB ); |
|
416 |
|
417 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2); |
|
418 CGAssociateMouseAndMouseCursorPosition (0); |
|
419 } |
|
420 } |
|
421 |
|
422 SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) { |
|
423 |
|
424 int doGrab = grab_mode & SDL_GRAB_ON; |
|
425 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/ |
|
426 |
|
427 if ( this->screen == NULL ) { |
|
428 SDL_SetError ("QZ_GrabInput: screen is NULL"); |
|
429 return SDL_GRAB_OFF; |
|
430 } |
|
431 |
|
432 if ( ! video_set ) { |
|
433 /*SDL_SetError ("QZ_GrabInput: video is not set, grab will take effect on mode switch"); */ |
|
434 current_grab_mode = grab_mode; |
|
435 return grab_mode; /* Will be set later on mode switch */ |
|
436 } |
|
437 |
|
438 if ( grab_mode != SDL_GRAB_QUERY ) { |
|
439 if ( doGrab ) |
|
440 QZ_ChangeGrabState (this, QZ_ENABLE_GRAB); |
|
441 else |
|
442 QZ_ChangeGrabState (this, QZ_DISABLE_GRAB); |
|
443 |
|
444 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF; |
|
445 } |
|
446 |
|
447 return current_grab_mode; |
|
448 } |