comparison src/video/quartz/SDL_QuartzWM.m @ 761:c5b2b6d2d1fe

Date: Wed, 31 Dec 2003 21:55:30 +0100 From: Max Horn Subject: SDL: video/quartz cleanup while doing some experimental changes in the quartz code, I was annoyed by having to recompile that one big .o file over and over again. So I decided to finally realize one TODO: properly splitting the code over multiple files :-). With two exceptions, I didn't make code changes, only rearranged files and added new headers. Since there are several new files, making a patch didn't work out so well, so I decided to just send you all the new & modified files. The one source change I made is related to showing/hiding the mouse. I renamed cursor_visible to cursor_should_be_visible and cursor_hidden to cursor_visible; I think that makes reading the code easier. Then I added two new functions: QZ_ShowMouse and QZ_HideMouse. They help manage cursor_visible (the former 'cursor_hidden'). Finally I replaced the Carbon ShowCursor/HiderCuror calls by [NSCursor hide] and [NSCursor unhide]. The API docs are not conclusive, but it might be that with those the "cursor_visible" (former 'cursor_hidden') hack may not be necessary anymore; however so far I didn't test this hypothesis, so I left that in. The other change was to remove in_foreground and use [NSApp isActive] instead: Manually keeping track of whether we are in the foreground is error prone. This should work better in some corner cases.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 04 Jan 2004 14:55:35 +0000
parents 5d2f027b3349
children 68c8da837fc0
comparison
equal deleted inserted replaced
760:cf9dd3aa6756 761:c5b2b6d2d1fe
1 /* 1 /*
2 SDL - Simple DirectMedia Layer 2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga 3 Copyright (C) 1997-2003 Sam Lantinga
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
18 18
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22 22
23 static void QZ_ChangeGrabState (_THIS, int action); 23 #include "SDL_QuartzVideo.h"
24
24 25
25 struct WMcursor { 26 struct WMcursor {
26 Cursor curs; 27 Cursor curs;
27 }; 28 };
28 29
29 static void QZ_FreeWMCursor (_THIS, WMcursor *cursor) { 30 void QZ_FreeWMCursor (_THIS, WMcursor *cursor) {
30 31
31 if ( cursor != NULL ) 32 if ( cursor != NULL )
32 free (cursor); 33 free (cursor);
33 } 34 }
34 35
35 /* Use the Carbon cursor routines for now */ 36 /* Use the Carbon cursor routines for now */
36 static WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask, 37 WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask,
37 int w, int h, int hot_x, int hot_y) { 38 int w, int h, int hot_x, int hot_y) {
38 WMcursor *cursor; 39 WMcursor *cursor;
39 int row, bytes; 40 int row, bytes;
40 41
41 /* Allocate the cursor memory */ 42 /* Allocate the cursor memory */
66 cursor->curs.hotSpot.v = hot_y; 67 cursor->curs.hotSpot.v = hot_y;
67 68
68 return(cursor); 69 return(cursor);
69 } 70 }
70 71
71 static int QZ_ShowWMCursor (_THIS, WMcursor *cursor) { 72 void QZ_ShowMouse (_THIS) {
73 if (!cursor_visible) {
74 [ NSCursor unhide ];
75 cursor_visible = YES;
76 }
77 }
78
79 void QZ_HideMouse (_THIS) {
80 if (cursor_visible) {
81 [ NSCursor hide ];
82 cursor_visible = NO;
83 }
84 }
85
86 int QZ_ShowWMCursor (_THIS, WMcursor *cursor) {
72 87
73 if ( cursor == NULL) { 88 if ( cursor == NULL) {
74 if ( cursor_visible ) { 89 if ( cursor_should_be_visible ) {
75 if (!cursor_hidden) { 90 QZ_HideMouse (this);
76 HideCursor (); 91 cursor_should_be_visible = NO;
77 cursor_hidden = YES;
78 }
79 cursor_visible = NO;
80 QZ_ChangeGrabState (this, QZ_HIDECURSOR); 92 QZ_ChangeGrabState (this, QZ_HIDECURSOR);
81 } 93 }
82 } 94 }
83 else { 95 else {
84 SetCursor(&cursor->curs); 96 SetCursor(&cursor->curs);
85 if ( ! cursor_visible ) { 97 if ( ! cursor_should_be_visible ) {
86 if (cursor_hidden) { 98 QZ_ShowMouse (this);
87 ShowCursor (); 99 cursor_should_be_visible = YES;
88 cursor_hidden = NO;
89 }
90 cursor_visible = YES;
91 QZ_ChangeGrabState (this, QZ_SHOWCURSOR); 100 QZ_ChangeGrabState (this, QZ_SHOWCURSOR);
92 } 101 }
93 } 102 }
94 103
95 return 1; 104 return 1;
102 The routines were written so they could be called before SetVideoMode() has finished; 111 The routines were written so they could be called before SetVideoMode() has finished;
103 this might have limited usefulness at the moment, but the extra cost is trivial. 112 this might have limited usefulness at the moment, but the extra cost is trivial.
104 */ 113 */
105 114
106 /* Convert Cocoa screen coordinate to Cocoa window coordinate */ 115 /* Convert Cocoa screen coordinate to Cocoa window coordinate */
107 static void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) { 116 void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) {
108 117
109 *p = [ qz_window convertScreenToBase:*p ]; 118 *p = [ qz_window convertScreenToBase:*p ];
110 } 119 }
111 120
112 121
113 /* Convert Cocoa window coordinate to Cocoa screen coordinate */ 122 /* Convert Cocoa window coordinate to Cocoa screen coordinate */
114 static void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) { 123 void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) {
115 124
116 *p = [ qz_window convertBaseToScreen:*p ]; 125 *p = [ qz_window convertBaseToScreen:*p ];
117 } 126 }
118 127
119 /* Convert SDL coordinate to Cocoa coordinate */ 128 /* Convert SDL coordinate to Cocoa coordinate */
120 static void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) { 129 void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) {
121 130
122 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ 131 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
123 132
124 p->y = CGDisplayPixelsHigh (display_id) - p->y - 1; 133 p->y = CGDisplayPixelsHigh (display_id) - p->y - 1;
125 } 134 }
132 *p = newPoint; 141 *p = newPoint;
133 } 142 }
134 } 143 }
135 144
136 /* Convert Cocoa coordinate to SDL coordinate */ 145 /* Convert Cocoa coordinate to SDL coordinate */
137 static void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) { 146 void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) {
138 147
139 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */ 148 if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
140 149
141 p->y = CGDisplayPixelsHigh (display_id) - p->y - 1; 150 p->y = CGDisplayPixelsHigh (display_id) - p->y - 1;
142 } 151 }
149 *p = newPoint; 158 *p = newPoint;
150 } 159 }
151 } 160 }
152 161
153 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */ 162 /* Convert SDL coordinate to window server (CoreGraphics) coordinate */
154 static CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) { 163 CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) {
155 164
156 CGPoint cgp; 165 CGPoint cgp;
157 166
158 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ 167 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
159 168
172 return cgp; 181 return cgp;
173 } 182 }
174 183
175 #if 0 /* Dead code */ 184 #if 0 /* Dead code */
176 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */ 185 /* Convert window server (CoreGraphics) coordinate to SDL coordinate */
177 static void QZ_PrivateCGToSDL (_THIS, NSPoint *p) { 186 void QZ_PrivateCGToSDL (_THIS, NSPoint *p) {
178 187
179 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */ 188 if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
180 189
181 int height; 190 int height;
182 191
188 QZ_PrivateCocoaToSDL (this, p); 197 QZ_PrivateCocoaToSDL (this, p);
189 } 198 }
190 } 199 }
191 #endif /* Dead code */ 200 #endif /* Dead code */
192 201
193 static void QZ_PrivateWarpCursor (_THIS, int x, int y) { 202 void QZ_PrivateWarpCursor (_THIS, int x, int y) {
194 203
195 NSPoint p; 204 NSPoint p;
196 CGPoint cgp; 205 CGPoint cgp;
197 206
198 p = NSMakePoint (x, y); 207 p = NSMakePoint (x, y);
201 /* this is the magic call that fixes cursor "freezing" after warp */ 210 /* this is the magic call that fixes cursor "freezing" after warp */
202 CGSetLocalEventsSuppressionInterval (0.0); 211 CGSetLocalEventsSuppressionInterval (0.0);
203 CGWarpMouseCursorPosition (cgp); 212 CGWarpMouseCursorPosition (cgp);
204 } 213 }
205 214
206 static void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) { 215 void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) {
207 216
208 /* Only allow warping when in foreground */ 217 /* Only allow warping when in foreground */
209 if ( ! in_foreground ) 218 if ( ! [ NSApp isActive ] )
210 return; 219 return;
211 220
212 /* Do the actual warp */ 221 /* Do the actual warp */
213 QZ_PrivateWarpCursor (this, x, y); 222 QZ_PrivateWarpCursor (this, x, y);
214 223
215 /* Generate the mouse moved event */ 224 /* Generate the mouse moved event */
216 SDL_PrivateMouseMotion (0, 0, x, y); 225 SDL_PrivateMouseMotion (0, 0, x, y);
217 } 226 }
218 227
219 static void QZ_MoveWMCursor (_THIS, int x, int y) { } 228 void QZ_MoveWMCursor (_THIS, int x, int y) { }
220 static void QZ_CheckMouseMode (_THIS) { } 229 void QZ_CheckMouseMode (_THIS) { }
221 230
222 static void QZ_SetCaption (_THIS, const char *title, const char *icon) { 231 void QZ_SetCaption (_THIS, const char *title, const char *icon) {
223 232
224 if ( qz_window != nil ) { 233 if ( qz_window != nil ) {
225 NSString *string; 234 NSString *string;
226 if ( title != NULL ) { 235 if ( title != NULL ) {
227 string = [ [ NSString alloc ] initWithCString:title ]; 236 string = [ [ NSString alloc ] initWithCString:title ];
234 [ string release ]; 243 [ string release ];
235 } 244 }
236 } 245 }
237 } 246 }
238 247
239 static void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask) 248 void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask)
240 { 249 {
241 NSBitmapImageRep *imgrep; 250 NSBitmapImageRep *imgrep;
242 NSImage *img; 251 NSImage *img;
243 SDL_Surface *mergedSurface; 252 SDL_Surface *mergedSurface;
244 int i,j; 253 int i,j;
303 SDL_FreeSurface(mergedSurface); 312 SDL_FreeSurface(mergedSurface);
304 freePool: 313 freePool:
305 [pool release]; 314 [pool release];
306 } 315 }
307 316
308 static int QZ_IconifyWindow (_THIS) { 317 int QZ_IconifyWindow (_THIS) {
309 318
310 if ( ! [ qz_window isMiniaturized ] ) { 319 if ( ! [ qz_window isMiniaturized ] ) {
311 [ qz_window miniaturize:nil ]; 320 [ qz_window miniaturize:nil ];
312 return 1; 321 return 1;
313 } 322 }
316 return 0; 325 return 0;
317 } 326 }
318 } 327 }
319 328
320 /* 329 /*
321 static int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) { 330 int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) {
322 info->nsWindowPtr = qz_window; 331 info->nsWindowPtr = qz_window;
323 return 0; 332 return 0;
324 }*/ 333 }*/
325 334
326 static void QZ_ChangeGrabState (_THIS, int action) { 335 void QZ_ChangeGrabState (_THIS, int action) {
327 336
328 /* 337 /*
329 Figure out what the next state should be based on the action. 338 Figure out what the next state should be based on the action.
330 Ignore actions that can't change the current state. 339 Ignore actions that can't change the current state.
331 */ 340 */
332 if ( grab_state == QZ_UNGRABBED ) { 341 if ( grab_state == QZ_UNGRABBED ) {
333 if ( action == QZ_ENABLE_GRAB ) { 342 if ( action == QZ_ENABLE_GRAB ) {
334 if ( cursor_visible ) 343 if ( cursor_should_be_visible )
335 grab_state = QZ_VISIBLE_GRAB; 344 grab_state = QZ_VISIBLE_GRAB;
336 else 345 else
337 grab_state = QZ_INVISIBLE_GRAB; 346 grab_state = QZ_INVISIBLE_GRAB;
338 } 347 }
339 } 348 }
367 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2); 376 QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
368 CGAssociateMouseAndMouseCursorPosition (0); 377 CGAssociateMouseAndMouseCursorPosition (0);
369 } 378 }
370 } 379 }
371 380
372 static SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) { 381 SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) {
373 382
374 int doGrab = grab_mode & SDL_GRAB_ON; 383 int doGrab = grab_mode & SDL_GRAB_ON;
375 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/ 384 /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/
376 385
377 if ( this->screen == NULL ) { 386 if ( this->screen == NULL ) {
394 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF; 403 current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF;
395 } 404 }
396 405
397 return current_grab_mode; 406 return current_grab_mode;
398 } 407 }
399
400 /* Resize icon, BMP format */
401 static unsigned char QZ_ResizeIcon[] = {
402 0x42,0x4d,0x31,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
403 0x00,0x00,0x0d,0x00,0x00,0x00,0x0d,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
404 0x00,0x00,0xfb,0x01,0x00,0x00,0x13,0x0b,0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
405 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
406 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
407 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
408 0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,
409 0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
410 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xda,0xda,0xda,0x87,
411 0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,
412 0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
413 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd5,0xd5,0xd5,0x87,0x87,0x87,0xe8,0xe8,0xe8,
414 0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,
415 0xda,0xda,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
416 0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,
417 0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
418 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,
419 0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
420 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
421 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,
422 0xe8,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
423 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
424 0xff,0xff,0xff,0xd9,0xd9,0xd9,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xdc,
425 0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
426 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,
427 0xdb,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
428 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
429 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,0xdb,0x87,0x87,0x87,0xe8,
430 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
431 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
432 0xff,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
433 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
434 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdc,
435 0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
436 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
437 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b
438 };
439
440 static void QZ_DrawResizeIcon (_THIS, RgnHandle dirtyRegion) {
441
442 /* Check if we should draw the resize icon */
443 if (SDL_VideoSurface->flags & SDL_RESIZABLE) {
444
445 Rect icon;
446 SetRect (&icon, SDL_VideoSurface->w - 13, SDL_VideoSurface->h - 13,
447 SDL_VideoSurface->w, SDL_VideoSurface->h);
448
449 if (RectInRgn (&icon, dirtyRegion)) {
450
451 SDL_Rect icon_rect;
452
453 /* Create the icon image */
454 if (resize_icon == NULL) {
455
456 SDL_RWops *rw;
457 SDL_Surface *tmp;
458
459 rw = SDL_RWFromMem (QZ_ResizeIcon, sizeof(QZ_ResizeIcon));
460 tmp = SDL_LoadBMP_RW (rw, SDL_TRUE);
461
462 resize_icon = SDL_ConvertSurface (tmp, SDL_VideoSurface->format, SDL_SRCCOLORKEY);
463 SDL_SetColorKey (resize_icon, SDL_SRCCOLORKEY, 0xFFFFFF);
464
465 SDL_FreeSurface (tmp);
466 }
467
468 icon_rect.x = SDL_VideoSurface->w - 13;
469 icon_rect.y = SDL_VideoSurface->h - 13;
470 icon_rect.w = 13;
471 icon_rect.h = 13;
472
473 SDL_BlitSurface (resize_icon, NULL, SDL_VideoSurface, &icon_rect);
474 }
475 }
476 }