comparison src/video/quartz/SDL_QuartzWM.m @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children cd14138a8703
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_QuartzVideo.h" 24 #include "SDL_QuartzVideo.h"
25 25
26 26
27 struct WMcursor { 27 struct WMcursor
28 {
28 Cursor curs; 29 Cursor curs;
29 }; 30 };
30 31
31 void QZ_FreeWMCursor (_THIS, WMcursor *cursor) { 32 void
32 33 QZ_FreeWMCursor (_THIS, WMcursor * cursor)
33 if ( cursor != NULL ) 34 {
35
36 if (cursor != NULL)
34 free (cursor); 37 free (cursor);
35 } 38 }
36 39
37 /* Use the Carbon cursor routines for now */ 40 /* Use the Carbon cursor routines for now */
38 WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask, 41 WMcursor *
39 int w, int h, int hot_x, int hot_y) { 42 QZ_CreateWMCursor (_THIS, Uint8 * data, Uint8 * mask,
43 int w, int h, int hot_x, int hot_y)
44 {
40 WMcursor *cursor; 45 WMcursor *cursor;
41 int row, bytes; 46 int row, bytes;
42 47
43 /* Allocate the cursor memory */ 48 /* Allocate the cursor memory */
44 cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor)); 49 cursor = (WMcursor *) SDL_malloc (sizeof (WMcursor));
45 if ( cursor == NULL ) { 50 if (cursor == NULL) {
46 SDL_OutOfMemory(); 51 SDL_OutOfMemory ();
47 return(NULL); 52 return (NULL);
48 } 53 }
49 SDL_memset(cursor, 0, sizeof(*cursor)); 54 SDL_memset (cursor, 0, sizeof (*cursor));
50 55
51 if (w > 16) 56 if (w > 16)
52 w = 16; 57 w = 16;
53 58
54 if (h > 16) 59 if (h > 16)
55 h = 16; 60 h = 16;
56 61
57 bytes = (w+7)/8; 62 bytes = (w + 7) / 8;
58 63
59 for ( row=0; row<h; ++row ) { 64 for (row = 0; row < h; ++row) {
60 SDL_memcpy(&cursor->curs.data[row], data, bytes); 65 SDL_memcpy (&cursor->curs.data[row], data, bytes);
61 data += bytes; 66 data += bytes;
62 } 67 }
63 for ( row=0; row<h; ++row ) { 68 for (row = 0; row < h; ++row) {
64 SDL_memcpy(&cursor->curs.mask[row], mask, bytes); 69 SDL_memcpy (&cursor->curs.mask[row], mask, bytes);
65 mask += bytes; 70 mask += bytes;
66 } 71 }
67 cursor->curs.hotSpot.h = hot_x; 72 cursor->curs.hotSpot.h = hot_x;
68 cursor->curs.hotSpot.v = hot_y; 73 cursor->curs.hotSpot.v = hot_y;
69 74
70 return(cursor); 75 return (cursor);
71 } 76 }
72 77
73 void QZ_ShowMouse (_THIS) { 78 void
79 QZ_ShowMouse (_THIS)
80 {
74 if (!cursor_visible) { 81 if (!cursor_visible) {
75 [ NSCursor unhide ]; 82 [NSCursor unhide];
76 cursor_visible = YES; 83 cursor_visible = YES;
77 } 84 }
78 } 85 }
79 86
80 void QZ_HideMouse (_THIS) { 87 void
81 if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) && cursor_visible) { 88 QZ_HideMouse (_THIS)
82 [ NSCursor hide ]; 89 {
90 if ((SDL_GetAppState () & SDL_APPMOUSEFOCUS) && cursor_visible) {
91 [NSCursor hide];
83 cursor_visible = NO; 92 cursor_visible = NO;
84 } 93 }
85 } 94 }
86 95
87 BOOL QZ_IsMouseInWindow (_THIS) { 96 BOOL
88 if (qz_window == nil) return YES; /*fullscreen*/ 97 QZ_IsMouseInWindow (_THIS)
98 {
99 if (qz_window == nil)
100 return YES; /*fullscreen */
89 else { 101 else {
90 NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ]; 102 NSPoint p =[qz_window mouseLocationOutsideOfEventStream];
91 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" */ 103 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" */
92 return NSPointInRect(p, [ window_view frame ]); 104 return NSPointInRect (p,[window_view frame]);
93 } 105 }
94 } 106 }
95 107
96 int QZ_ShowWMCursor (_THIS, WMcursor *cursor) { 108 int
97 109 QZ_ShowWMCursor (_THIS, WMcursor * cursor)
98 if ( cursor == NULL) { 110 {
99 if ( cursor_should_be_visible ) { 111
112 if (cursor == NULL) {
113 if (cursor_should_be_visible) {
100 QZ_HideMouse (this); 114 QZ_HideMouse (this);
101 cursor_should_be_visible = NO; 115 cursor_should_be_visible = NO;
102 QZ_ChangeGrabState (this, QZ_HIDECURSOR); 116 QZ_ChangeGrabState (this, QZ_HIDECURSOR);
103 } 117 }
104 } 118 } else {
105 else { 119 SetCursor (&cursor->curs);
106 SetCursor(&cursor->curs); 120 if (!cursor_should_be_visible) {
107 if ( ! cursor_should_be_visible ) {
108 QZ_ShowMouse (this); 121 QZ_ShowMouse (this);
109 cursor_should_be_visible = YES; 122 cursor_should_be_visible = YES;
110 QZ_ChangeGrabState (this, QZ_SHOWCURSOR); 123 QZ_ChangeGrabState (this, QZ_SHOWCURSOR);
111 } 124 }
112 } 125 }
121 The routines were written so they could be called before SetVideoMode() has finished; 134 The routines were written so they could be called before SetVideoMode() has finished;
122 this might have limited usefulness at the moment, but the extra cost is trivial. 135 this might have limited usefulness at the moment, but the extra cost is trivial.
123 */ 136 */
124 137
125 /* Convert Cocoa screen coordinate to Cocoa window coordinate */ 138 /* Convert Cocoa screen coordinate to Cocoa window coordinate */
126 void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) { 139 void
127 140 QZ_PrivateGlobalToLocal (_THIS, NSPoint * p)
128 *p = [ qz_window convertScreenToBase:*p ]; 141 {
142
143 *p =[qz_window convertScreenToBase:*p];
129 } 144 }
130 145
131 146
132 /* Convert Cocoa window coordinate to Cocoa screen coordinate */ 147 /* Convert Cocoa window coordinate to Cocoa screen coordinate */
133 void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) { 148 void
134 149 QZ_PrivateLocalToGlobal (_THIS, NSPoint * p)
135 *p = [ qz_window convertBaseToScreen:*p ]; 150 {
151
152 *p =[qz_window convertBaseToScreen:*p];
136 } 153 }
137 154
138 /* Convert SDL coordinate to Cocoa coordinate */ 155 /* Convert SDL coordinate to Cocoa coordinate */
139 void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) { 156 void
140 157 QZ_PrivateSDLToCocoa (_THIS, NSPoint * p)
141 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ 158 {
142 159
160 if (CGDisplayIsCaptured (display_id)) { /* capture signals fullscreen */
161
143 p->y = CGDisplayPixelsHigh (display_id) - p->y; 162 p->y = CGDisplayPixelsHigh (display_id) - p->y;
144 } 163 } else {
145 else { 164
146 165 *p =[window_view convertPoint: *p toView:nil];
147 *p = [ window_view convertPoint:*p toView: nil ]; 166
148
149 /* We need a workaround in OpenGL mode */ 167 /* We need a workaround in OpenGL mode */
150 if ( SDL_VideoSurface->flags & SDL_INTERNALOPENGL ) { 168 if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
151 p->y = [window_view frame].size.height - p->y; 169 p->y =[window_view frame].size.height - p->y;
152 } 170 }
153 } 171 }
154 } 172 }
155 173
156 /* Convert Cocoa coordinate to SDL coordinate */ 174 /* Convert Cocoa coordinate to SDL coordinate */
157 void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) { 175 void
158 176 QZ_PrivateCocoaToSDL (_THIS, NSPoint * p)
159 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ 177 {
160 178
179 if (CGDisplayIsCaptured (display_id)) { /* capture signals fullscreen */
180
161 p->y = CGDisplayPixelsHigh (display_id) - p->y; 181 p->y = CGDisplayPixelsHigh (display_id) - p->y;
162 } 182 } else {
163 else { 183
164 184 *p =[window_view convertPoint: *p fromView:nil];
165 *p = [ window_view convertPoint:*p fromView: nil ]; 185
166
167 /* We need a workaround in OpenGL mode */ 186 /* We need a workaround in OpenGL mode */
168 if ( SDL_VideoSurface != NULL && (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) ) { 187 if (SDL_VideoSurface != NULL
169 p->y = [window_view frame].size.height - p->y; 188 && (SDL_VideoSurface->flags & SDL_INTERNALOPENGL)) {
189 p->y =[window_view frame].size.height - p->y;
170 } 190 }
171 } 191 }
172 } 192 }
173 193
174 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */ 194 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */
175 CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) { 195 CGPoint
176 196 QZ_PrivateSDLToCG (_THIS, NSPoint * p)
197 {
198
177 CGPoint cgp; 199 CGPoint cgp;
178 200
179 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ 201 if (!CGDisplayIsCaptured (display_id)) { /* not captured => not fullscreen => local coord */
180 202
181 int height; 203 int height;
182 204
183 QZ_PrivateSDLToCocoa (this, p); 205 QZ_PrivateSDLToCocoa (this, p);
184 QZ_PrivateLocalToGlobal (this, p); 206 QZ_PrivateLocalToGlobal (this, p);
185 207
186 height = CGDisplayPixelsHigh (display_id); 208 height = CGDisplayPixelsHigh (display_id);
187 p->y = height - p->y; 209 p->y = height - p->y;
188 } 210 }
189 211
190 cgp.x = p->x; 212 cgp.x = p->x;
191 cgp.y = p->y; 213 cgp.y = p->y;
192 214
193 return cgp; 215 return cgp;
194 } 216 }
195 217
196 #if 0 /* Dead code */ 218 #if 0 /* Dead code */
197 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */ 219 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */
198 void QZ_PrivateCGToSDL (_THIS, NSPoint *p) { 220 void
199 221 QZ_PrivateCGToSDL (_THIS, NSPoint * p)
200 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ 222 {
201 223
224 if (!CGDisplayIsCaptured (display_id)) { /* not captured => not fullscreen => local coord */
225
202 int height; 226 int height;
203 227
204 /* Convert CG Global to Cocoa Global */ 228 /* Convert CG Global to Cocoa Global */
205 height = CGDisplayPixelsHigh (display_id); 229 height = CGDisplayPixelsHigh (display_id);
206 p->y = height - p->y; 230 p->y = height - p->y;
209 QZ_PrivateCocoaToSDL (this, p); 233 QZ_PrivateCocoaToSDL (this, p);
210 } 234 }
211 } 235 }
212 #endif /* Dead code */ 236 #endif /* Dead code */
213 237
214 void QZ_PrivateWarpCursor (_THIS, int x, int y) { 238 void
215 239 QZ_PrivateWarpCursor (_THIS, int x, int y)
240 {
241
216 NSPoint p; 242 NSPoint p;
217 CGPoint cgp; 243 CGPoint cgp;
218 244
219 p = NSMakePoint (x, y); 245 p = NSMakePoint (x, y);
220 cgp = QZ_PrivateSDLToCG (this, &p); 246 cgp = QZ_PrivateSDLToCG (this, &p);
221 247
222 /* this is the magic call that fixes cursor "freezing" after warp */ 248 /* this is the magic call that fixes cursor "freezing" after warp */
223 CGSetLocalEventsSuppressionInterval (0.0); 249 CGSetLocalEventsSuppressionInterval (0.0);
224 CGWarpMouseCursorPosition (cgp); 250 CGWarpMouseCursorPosition (cgp);
225 } 251 }
226 252
227 void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) { 253 void
254 QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y)
255 {
228 256
229 /* Only allow warping when in foreground */ 257 /* Only allow warping when in foreground */
230 if ( ! [ NSApp isActive ] ) 258 if (![NSApp isActive])
231 return; 259 return;
232 260
233 /* Do the actual warp */ 261 /* Do the actual warp */
234 if (grab_state != QZ_INVISIBLE_GRAB) QZ_PrivateWarpCursor (this, x, y); 262 if (grab_state != QZ_INVISIBLE_GRAB)
263 QZ_PrivateWarpCursor (this, x, y);
235 264
236 /* Generate the mouse moved event */ 265 /* Generate the mouse moved event */
237 SDL_PrivateMouseMotion (0, 0, x, y); 266 SDL_PrivateMouseMotion (0, 0, x, y);
238 } 267 }
239 268
240 void QZ_MoveWMCursor (_THIS, int x, int y) { } 269 void
241 void QZ_CheckMouseMode (_THIS) { } 270 QZ_MoveWMCursor (_THIS, int x, int y)
242 271 {
243 void QZ_SetCaption (_THIS, const char *title, const char *icon) { 272 }
244 273 void
245 if ( qz_window != nil ) { 274 QZ_CheckMouseMode (_THIS)
275 {
276 }
277
278 void
279 QZ_SetCaption (_THIS, const char *title, const char *icon)
280 {
281
282 if (qz_window != nil) {
246 NSString *string; 283 NSString *string;
247 if ( title != NULL ) { 284 if (title != NULL) {
248 string = [ [ NSString alloc ] initWithUTF8String:title ]; 285 string =[[NSString alloc] initWithUTF8String:title];
249 [ qz_window setTitle:string ]; 286 [qz_window setTitle:string];
250 [ string release ]; 287 [string release];
251 } 288 }
252 if ( icon != NULL ) { 289 if (icon != NULL) {
253 string = [ [ NSString alloc ] initWithUTF8String:icon ]; 290 string =[[NSString alloc] initWithUTF8String:icon];
254 [ qz_window setMiniwindowTitle:string ]; 291 [qz_window setMiniwindowTitle:string];
255 [ string release ]; 292 [string release];
256 } 293 }
257 } 294 }
258 } 295 }
259 296
260 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask) 297 void
298 QZ_SetIcon (_THIS, SDL_Surface * icon, Uint8 * mask)
261 { 299 {
262 NSBitmapImageRep *imgrep; 300 NSBitmapImageRep *imgrep;
263 NSImage *img; 301 NSImage *img;
264 SDL_Surface *mergedSurface; 302 SDL_Surface *mergedSurface;
265 NSAutoreleasePool *pool; 303 NSAutoreleasePool *pool;
266 Uint8 *pixels; 304 Uint8 *pixels;
267 SDL_bool iconSrcAlpha; 305 SDL_bool iconSrcAlpha;
268 Uint8 iconAlphaValue; 306 Uint8 iconAlphaValue;
269 int i, j, maskPitch, index; 307 int i, j, maskPitch, index;
270 308
271 pool = [ [ NSAutoreleasePool alloc ] init ]; 309 pool =[[NSAutoreleasePool alloc] init];
272 310
273 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 ]; 311 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];
274 if (imgrep == nil) goto freePool; 312 if (imgrep == nil)
275 pixels = [ imgrep bitmapData ]; 313 goto freePool;
276 SDL_memset(pixels, 0, 4*icon->w*icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */ 314 pixels =[imgrep bitmapData];
277 315 SDL_memset (pixels, 0, 4 * icon->w * icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */
316
278 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 317 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
279 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 318 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
280 #else 319 #else
281 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 320 #define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
282 #endif 321 #endif
283 mergedSurface = SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4*icon->w, BYTEORDER_DEPENDENT_RGBA_MASKS); 322 mergedSurface =
284 if (mergedSurface == NULL) goto freePool; 323 SDL_CreateRGBSurfaceFrom (pixels, icon->w, icon->h, 32, 4 * icon->w,
285 324 BYTEORDER_DEPENDENT_RGBA_MASKS);
325 if (mergedSurface == NULL)
326 goto freePool;
327
286 /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */ 328 /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */
287 iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0); 329 iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0);
288 iconAlphaValue = icon->format->alpha; 330 iconAlphaValue = icon->format->alpha;
289 SDL_SetAlpha(icon, 0, 255); 331 SDL_SetAlpha (icon, 0, 255);
290 SDL_BlitSurface(icon, NULL, mergedSurface, NULL); 332 SDL_BlitSurface (icon, NULL, mergedSurface, NULL);
291 if (iconSrcAlpha) SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue); 333 if (iconSrcAlpha)
292 334 SDL_SetAlpha (icon, SDL_SRCALPHA, iconAlphaValue);
293 SDL_FreeSurface(mergedSurface); 335
294 336 SDL_FreeSurface (mergedSurface);
337
295 /* apply mask, source alpha, and premultiply color values by alpha */ 338 /* apply mask, source alpha, and premultiply color values by alpha */
296 maskPitch = (icon->w+7)/8; 339 maskPitch = (icon->w + 7) / 8;
297 for (i = 0; i < icon->h; i++) { 340 for (i = 0; i < icon->h; i++) {
298 for (j = 0; j < icon->w; j++) { 341 for (j = 0; j < icon->w; j++) {
299 index = i*4*icon->w + j*4; 342 index = i * 4 * icon->w + j * 4;
300 if (!(mask[i*maskPitch + j/8] & (128 >> j%8))) { 343 if (!(mask[i * maskPitch + j / 8] & (128 >> j % 8))) {
301 pixels[index + 3] = 0; 344 pixels[index + 3] = 0;
302 } 345 } else {
303 else {
304 if (iconSrcAlpha) { 346 if (iconSrcAlpha) {
305 if (icon->format->Amask == 0) pixels[index + 3] = icon->format->alpha; 347 if (icon->format->Amask == 0)
306 } 348 pixels[index + 3] = icon->format->alpha;
307 else { 349 } else {
308 pixels[index + 3] = 255; 350 pixels[index + 3] = 255;
309 } 351 }
310 } 352 }
311 if (pixels[index + 3] < 255) { 353 if (pixels[index + 3] < 255) {
312 pixels[index + 0] = (Uint16)pixels[index + 0]*pixels[index + 3]/255; 354 pixels[index + 0] =
313 pixels[index + 1] = (Uint16)pixels[index + 1]*pixels[index + 3]/255; 355 (Uint16) pixels[index + 0] * pixels[index + 3] / 255;
314 pixels[index + 2] = (Uint16)pixels[index + 2]*pixels[index + 3]/255; 356 pixels[index + 1] =
357 (Uint16) pixels[index + 1] * pixels[index + 3] / 255;
358 pixels[index + 2] =
359 (Uint16) pixels[index + 2] * pixels[index + 3] / 255;
315 } 360 }
316 } 361 }
317 } 362 }
318 363
319 img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(icon->w, icon->h) ] autorelease ]; 364 img =[[[NSImage alloc] initWithSize:NSMakeSize (icon->w,
320 if (img == nil) goto freePool; 365 icon->h)] autorelease];
321 [ img addRepresentation: imgrep ]; 366 if (img == nil)
322 [ NSApp setApplicationIconImage:img ]; 367 goto freePool;
323 368 [img addRepresentation:imgrep];
324 freePool: 369 [NSApp setApplicationIconImage:img];
325 [ pool release ]; 370
326 } 371 freePool:
327 372 [pool release];
328 int QZ_IconifyWindow (_THIS) { 373 }
329 374
330 if ( ! [ qz_window isMiniaturized ] ) { 375 int
331 [ qz_window miniaturize:nil ]; 376 QZ_IconifyWindow (_THIS)
377 {
378
379 if (![qz_window isMiniaturized]) {
380 [qz_window miniaturize:nil];
332 return 1; 381 return 1;
333 } 382 } else {
334 else {
335 SDL_SetError ("window already iconified"); 383 SDL_SetError ("window already iconified");
336 return 0; 384 return 0;
337 } 385 }
338 } 386 }
339 387
341 int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) { 389 int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) {
342 info->nsWindowPtr = qz_window; 390 info->nsWindowPtr = qz_window;
343 return 0; 391 return 0;
344 }*/ 392 }*/
345 393
346 void QZ_ChangeGrabState (_THIS, int action) { 394 void
395 QZ_ChangeGrabState (_THIS, int action)
396 {
347 397
348 /* 398 /*
349 Figure out what the next state should be based on the action. 399 Figure out what the next state should be based on the action.
350 Ignore actions that can't change the current state. 400 Ignore actions that can't change the current state.
351 */ 401 */
352 if ( grab_state == QZ_UNGRABBED ) { 402 if (grab_state == QZ_UNGRABBED) {
353 if ( action == QZ_ENABLE_GRAB ) { 403 if (action == QZ_ENABLE_GRAB) {
354 if ( cursor_should_be_visible ) 404 if (cursor_should_be_visible)
355 grab_state = QZ_VISIBLE_GRAB; 405 grab_state = QZ_VISIBLE_GRAB;
356 else 406 else
357 grab_state = QZ_INVISIBLE_GRAB; 407 grab_state = QZ_INVISIBLE_GRAB;
358 } 408 }
359 } 409 } else if (grab_state == QZ_VISIBLE_GRAB) {
360 else if ( grab_state == QZ_VISIBLE_GRAB ) { 410 if (action == QZ_DISABLE_GRAB)
361 if ( action == QZ_DISABLE_GRAB )
362 grab_state = QZ_UNGRABBED; 411 grab_state = QZ_UNGRABBED;
363 else if ( action == QZ_HIDECURSOR ) 412 else if (action == QZ_HIDECURSOR)
364 grab_state = QZ_INVISIBLE_GRAB; 413 grab_state = QZ_INVISIBLE_GRAB;
365 } 414 } else {
366 else { 415 assert (grab_state == QZ_INVISIBLE_GRAB);
367 assert( grab_state == QZ_INVISIBLE_GRAB ); 416
368 417 if (action == QZ_DISABLE_GRAB)
369 if ( action == QZ_DISABLE_GRAB )
370 grab_state = QZ_UNGRABBED; 418 grab_state = QZ_UNGRABBED;
371 else if ( action == QZ_SHOWCURSOR ) 419 else if (action == QZ_SHOWCURSOR)
372 grab_state = QZ_VISIBLE_GRAB; 420 grab_state = QZ_VISIBLE_GRAB;
373 } 421 }
374 422
375 /* now apply the new state */ 423 /* now apply the new state */
376 if (grab_state == QZ_UNGRABBED) { 424 if (grab_state == QZ_UNGRABBED) {
377 425
378 CGAssociateMouseAndMouseCursorPosition (1); 426 CGAssociateMouseAndMouseCursorPosition (1);
379 } 427 } else if (grab_state == QZ_VISIBLE_GRAB) {
380 else if (grab_state == QZ_VISIBLE_GRAB) { 428
381
382 CGAssociateMouseAndMouseCursorPosition (1); 429 CGAssociateMouseAndMouseCursorPosition (1);
383 } 430 } else {
384 else { 431 assert (grab_state == QZ_INVISIBLE_GRAB);
385 assert( grab_state == QZ_INVISIBLE_GRAB ); 432
386 433 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2,
387 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2); 434 SDL_VideoSurface->h / 2);
388 CGAssociateMouseAndMouseCursorPosition (0); 435 CGAssociateMouseAndMouseCursorPosition (0);
389 } 436 }
390 } 437 }
391 438
392 SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) { 439 SDL_GrabMode
440 QZ_GrabInput (_THIS, SDL_GrabMode grab_mode)
441 {
393 442
394 int doGrab = grab_mode & SDL_GRAB_ON; 443 int doGrab = grab_mode & SDL_GRAB_ON;
395 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/ 444 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN; */
396 445
397 if ( this->screen == NULL ) { 446 if (this->screen == NULL) {
398 SDL_SetError ("QZ_GrabInput: screen is NULL"); 447 SDL_SetError ("QZ_GrabInput: screen is NULL");
399 return SDL_GRAB_OFF; 448 return SDL_GRAB_OFF;
400 } 449 }
401 450
402 if ( ! video_set ) { 451 if (!video_set) {
403 /*SDL_SetError ("QZ_GrabInput: video is not set, grab will take effect on mode switch"); */ 452 /*SDL_SetError ("QZ_GrabInput: video is not set, grab will take effect on mode switch"); */
404 current_grab_mode = grab_mode; 453 current_grab_mode = grab_mode;
405 return grab_mode; /* Will be set later on mode switch */ 454 return grab_mode; /* Will be set later on mode switch */
406 } 455 }
407 456
408 if ( grab_mode != SDL_GRAB_QUERY ) { 457 if (grab_mode != SDL_GRAB_QUERY) {
409 if ( doGrab ) 458 if (doGrab)
410 QZ_ChangeGrabState (this, QZ_ENABLE_GRAB); 459 QZ_ChangeGrabState (this, QZ_ENABLE_GRAB);
411 else 460 else
412 QZ_ChangeGrabState (this, QZ_DISABLE_GRAB); 461 QZ_ChangeGrabState (this, QZ_DISABLE_GRAB);
413 462
414 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF; 463 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF;
415 } 464 }
416 465
417 return current_grab_mode; 466 return current_grab_mode;
418 } 467 }