comparison src/video/quartz/SDL_QuartzVideo.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
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
29 Since there is a bug in Apple's screen switching code 29 Since there is a bug in Apple's screen switching code
30 that does not update this variable when switching 30 that does not update this variable when switching
31 to fullscreen, we'll set it manually (but only for the 31 to fullscreen, we'll set it manually (but only for the
32 main screen). 32 main screen).
33 */ 33 */
34 @interface NSScreen (NSScreenAccess) 34 @ interface NSScreen (NSScreenAccess) - (void) setFrame:(NSRect) frame;
35 - (void) setFrame:(NSRect)frame; 35 @end @ implementation NSScreen (NSScreenAccess) - (void) setFrame:(NSRect)
36 frame;
37 {
38 _frame = frame;
39 }
40
36 @end 41 @end
37
38 @implementation NSScreen (NSScreenAccess)
39 - (void) setFrame:(NSRect)frame;
40 {
41 _frame = frame;
42 }
43 @end
44
45
46 /* 42 /*
47 Structure for rez switch gamma fades 43 Structure for rez switch gamma fades
48 We can hide the monitor flicker by setting the gamma tables to 0 44 We can hide the monitor flicker by setting the gamma tables to 0
49 */ 45 */
50 #define QZ_GAMMA_TABLE_SIZE 256 46 #define QZ_GAMMA_TABLE_SIZE 256
51 47 typedef struct
52 typedef struct { 48 {
53 49
54 CGGammaValue red[QZ_GAMMA_TABLE_SIZE]; 50 CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
55 CGGammaValue green[QZ_GAMMA_TABLE_SIZE]; 51 CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
56 CGGammaValue blue[QZ_GAMMA_TABLE_SIZE]; 52 CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
57 53
58 } SDL_QuartzGammaTable; 54 } SDL_QuartzGammaTable;
59 55
60 56
61 /* Bootstrap functions */ 57 /* Bootstrap functions */
62 static int QZ_Available (); 58 static int QZ_Available ();
63 static SDL_VideoDevice* QZ_CreateDevice (int device_index); 59 static SDL_VideoDevice *QZ_CreateDevice (int device_index);
64 static void QZ_DeleteDevice (SDL_VideoDevice *device); 60 static void QZ_DeleteDevice (SDL_VideoDevice * device);
65 61
66 /* Initialization, Query, Setup, and Redrawing functions */ 62 /* Initialization, Query, Setup, and Redrawing functions */
67 static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format); 63 static int QZ_VideoInit (_THIS, SDL_PixelFormat * video_format);
68 64
69 static SDL_Rect** QZ_ListModes (_THIS, SDL_PixelFormat *format, 65 static SDL_Rect **QZ_ListModes (_THIS, SDL_PixelFormat * format,
70 Uint32 flags); 66 Uint32 flags);
71 static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop); 67 static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop);
72 68
73 static SDL_Surface* QZ_SetVideoMode (_THIS, SDL_Surface *current, 69 static SDL_Surface *QZ_SetVideoMode (_THIS, SDL_Surface * current,
74 int width, int height, int bpp, 70 int width, int height, int bpp,
75 Uint32 flags); 71 Uint32 flags);
76 static int QZ_ToggleFullScreen (_THIS, int on); 72 static int QZ_ToggleFullScreen (_THIS, int on);
77 static int QZ_SetColors (_THIS, int first_color, 73 static int QZ_SetColors (_THIS, int first_color,
78 int num_colors, SDL_Color *colors); 74 int num_colors, SDL_Color * colors);
79 75
80 static int QZ_LockDoubleBuffer (_THIS, SDL_Surface *surface); 76 static int QZ_LockDoubleBuffer (_THIS, SDL_Surface * surface);
81 static void QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface); 77 static void QZ_UnlockDoubleBuffer (_THIS, SDL_Surface * surface);
82 static int QZ_ThreadFlip (_THIS); 78 static int QZ_ThreadFlip (_THIS);
83 static int QZ_FlipDoubleBuffer (_THIS, SDL_Surface *surface); 79 static int QZ_FlipDoubleBuffer (_THIS, SDL_Surface * surface);
84 static void QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects); 80 static void QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect * rects);
85 81
86 static void QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect *rects); 82 static void QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect * rects);
87 static int QZ_LockWindow (_THIS, SDL_Surface *surface); 83 static int QZ_LockWindow (_THIS, SDL_Surface * surface);
88 static void QZ_UnlockWindow (_THIS, SDL_Surface *surface); 84 static void QZ_UnlockWindow (_THIS, SDL_Surface * surface);
89 static void QZ_UpdateRects (_THIS, int num_rects, SDL_Rect *rects); 85 static void QZ_UpdateRects (_THIS, int num_rects, SDL_Rect * rects);
90 static void QZ_VideoQuit (_THIS); 86 static void QZ_VideoQuit (_THIS);
91 87
92 /* Hardware surface functions (for fullscreen mode only) */ 88 /* Hardware surface functions (for fullscreen mode only) */
93 #if 0 /* Not used (apparently, it's really slow) */ 89 #if 0 /* Not used (apparently, it's really slow) */
94 static int QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color); 90 static int QZ_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect,
91 Uint32 color);
95 #endif 92 #endif
96 static int QZ_LockHWSurface(_THIS, SDL_Surface *surface); 93 static int QZ_LockHWSurface (_THIS, SDL_Surface * surface);
97 static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface); 94 static void QZ_UnlockHWSurface (_THIS, SDL_Surface * surface);
98 static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface); 95 static int QZ_AllocHWSurface (_THIS, SDL_Surface * surface);
99 static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface); 96 static void QZ_FreeHWSurface (_THIS, SDL_Surface * surface);
100 /* static int QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */ 97 /* static int QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */
101 98
102 /* Bootstrap binding, enables entry point into the driver */ 99 /* Bootstrap binding, enables entry point into the driver */
103 VideoBootStrap QZ_bootstrap = { 100 VideoBootStrap QZ_bootstrap = {
104 "Quartz", "Mac OS X CoreGraphics", QZ_Available, QZ_CreateDevice 101 "Quartz", "Mac OS X CoreGraphics", QZ_Available, QZ_CreateDevice
105 }; 102 };
106 103
107 104
108 /* Bootstrap functions */ 105 /* Bootstrap functions */
109 static int QZ_Available () { 106 static int
107 QZ_Available ()
108 {
110 return 1; 109 return 1;
111 } 110 }
112 111
113 static SDL_VideoDevice* QZ_CreateDevice (int device_index) { 112 static SDL_VideoDevice *
113 QZ_CreateDevice (int device_index)
114 {
114 115
115 #pragma unused (device_index) 116 #pragma unused (device_index)
116 117
117 SDL_VideoDevice *device; 118 SDL_VideoDevice *device;
118 SDL_PrivateVideoData *hidden; 119 SDL_PrivateVideoData *hidden;
119 120
120 device = (SDL_VideoDevice*) SDL_malloc (sizeof (*device) ); 121 device = (SDL_VideoDevice *) SDL_malloc (sizeof (*device));
121 hidden = (SDL_PrivateVideoData*) SDL_malloc (sizeof (*hidden) ); 122 hidden = (SDL_PrivateVideoData *) SDL_malloc (sizeof (*hidden));
122 123
123 if (device == NULL || hidden == NULL) 124 if (device == NULL || hidden == NULL)
124 SDL_OutOfMemory (); 125 SDL_OutOfMemory ();
125 126
126 SDL_memset (device, 0, sizeof (*device) ); 127 SDL_memset (device, 0, sizeof (*device));
127 SDL_memset (hidden, 0, sizeof (*hidden) ); 128 SDL_memset (hidden, 0, sizeof (*hidden));
128 129
129 device->hidden = hidden; 130 device->hidden = hidden;
130 131
131 device->VideoInit = QZ_VideoInit; 132 device->VideoInit = QZ_VideoInit;
132 device->ListModes = QZ_ListModes; 133 device->ListModes = QZ_ListModes;
133 device->SetVideoMode = QZ_SetVideoMode; 134 device->SetVideoMode = QZ_SetVideoMode;
134 device->ToggleFullScreen = QZ_ToggleFullScreen; 135 device->ToggleFullScreen = QZ_ToggleFullScreen;
135 device->UpdateMouse = QZ_UpdateMouse; 136 device->UpdateMouse = QZ_UpdateMouse;
136 device->SetColors = QZ_SetColors; 137 device->SetColors = QZ_SetColors;
137 /* device->UpdateRects = QZ_UpdateRects; this is determined by SetVideoMode() */ 138 /* device->UpdateRects = QZ_UpdateRects; this is determined by SetVideoMode() */
138 device->VideoQuit = QZ_VideoQuit; 139 device->VideoQuit = QZ_VideoQuit;
139 140
140 device->LockHWSurface = QZ_LockHWSurface; 141 device->LockHWSurface = QZ_LockHWSurface;
141 device->UnlockHWSurface = QZ_UnlockHWSurface; 142 device->UnlockHWSurface = QZ_UnlockHWSurface;
142 device->AllocHWSurface = QZ_AllocHWSurface; 143 device->AllocHWSurface = QZ_AllocHWSurface;
143 device->FreeHWSurface = QZ_FreeHWSurface; 144 device->FreeHWSurface = QZ_FreeHWSurface;
144 /* device->FlipHWSurface = QZ_FlipHWSurface */; 145 /* device->FlipHWSurface = QZ_FlipHWSurface */ ;
145 146
146 device->SetGamma = QZ_SetGamma; 147 device->SetGamma = QZ_SetGamma;
147 device->GetGamma = QZ_GetGamma; 148 device->GetGamma = QZ_GetGamma;
148 device->SetGammaRamp = QZ_SetGammaRamp; 149 device->SetGammaRamp = QZ_SetGammaRamp;
149 device->GetGammaRamp = QZ_GetGammaRamp; 150 device->GetGammaRamp = QZ_GetGammaRamp;
150 151
151 device->GL_GetProcAddress = QZ_GL_GetProcAddress; 152 device->GL_GetProcAddress = QZ_GL_GetProcAddress;
152 device->GL_GetAttribute = QZ_GL_GetAttribute; 153 device->GL_GetAttribute = QZ_GL_GetAttribute;
153 device->GL_MakeCurrent = QZ_GL_MakeCurrent; 154 device->GL_MakeCurrent = QZ_GL_MakeCurrent;
154 device->GL_SwapBuffers = QZ_GL_SwapBuffers; 155 device->GL_SwapBuffers = QZ_GL_SwapBuffers;
155 device->GL_LoadLibrary = QZ_GL_LoadLibrary; 156 device->GL_LoadLibrary = QZ_GL_LoadLibrary;
156 157
157 device->FreeWMCursor = QZ_FreeWMCursor; 158 device->FreeWMCursor = QZ_FreeWMCursor;
158 device->CreateWMCursor = QZ_CreateWMCursor; 159 device->CreateWMCursor = QZ_CreateWMCursor;
159 device->ShowWMCursor = QZ_ShowWMCursor; 160 device->ShowWMCursor = QZ_ShowWMCursor;
160 device->WarpWMCursor = QZ_WarpWMCursor; 161 device->WarpWMCursor = QZ_WarpWMCursor;
161 device->MoveWMCursor = QZ_MoveWMCursor; 162 device->MoveWMCursor = QZ_MoveWMCursor;
162 device->CheckMouseMode = QZ_CheckMouseMode; 163 device->CheckMouseMode = QZ_CheckMouseMode;
163 device->InitOSKeymap = QZ_InitOSKeymap; 164 device->InitOSKeymap = QZ_InitOSKeymap;
164 device->PumpEvents = QZ_PumpEvents; 165 device->PumpEvents = QZ_PumpEvents;
165 166
166 device->SetCaption = QZ_SetCaption; 167 device->SetCaption = QZ_SetCaption;
167 device->SetIcon = QZ_SetIcon; 168 device->SetIcon = QZ_SetIcon;
168 device->IconifyWindow = QZ_IconifyWindow; 169 device->IconifyWindow = QZ_IconifyWindow;
169 /*device->GetWMInfo = QZ_GetWMInfo;*/ 170 /*device->GetWMInfo = QZ_GetWMInfo; */
170 device->GrabInput = QZ_GrabInput; 171 device->GrabInput = QZ_GrabInput;
171 172
172 device->CreateYUVOverlay = QZ_CreateYUVOverlay; 173 device->CreateYUVOverlay = QZ_CreateYUVOverlay;
173 174
174 device->free = QZ_DeleteDevice; 175 device->free = QZ_DeleteDevice;
175 176
176 return device; 177 return device;
177 } 178 }
178 179
179 static void QZ_DeleteDevice (SDL_VideoDevice *device) { 180 static void
181 QZ_DeleteDevice (SDL_VideoDevice * device)
182 {
180 183
181 SDL_free (device->hidden); 184 SDL_free (device->hidden);
182 SDL_free (device); 185 SDL_free (device);
183 } 186 }
184 187
185 static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format) { 188 static int
189 QZ_VideoInit (_THIS, SDL_PixelFormat * video_format)
190 {
186 191
187 /* Initialize the video settings; this data persists between mode switches */ 192 /* Initialize the video settings; this data persists between mode switches */
188 display_id = kCGDirectMainDisplay; 193 display_id = kCGDirectMainDisplay;
189 save_mode = CGDisplayCurrentMode (display_id); 194 save_mode = CGDisplayCurrentMode (display_id);
190 mode_list = CGDisplayAvailableModes (display_id); 195 mode_list = CGDisplayAvailableModes (display_id);
191 palette = CGPaletteCreateDefaultColorPalette (); 196 palette = CGPaletteCreateDefaultColorPalette ();
192 197
193 /* Gather some information that is useful to know about the display */ 198 /* Gather some information that is useful to know about the display */
194 CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayBitsPerPixel), 199 CFNumberGetValue (CFDictionaryGetValue
200 (save_mode, kCGDisplayBitsPerPixel),
195 kCFNumberSInt32Type, &device_bpp); 201 kCFNumberSInt32Type, &device_bpp);
196 202
197 CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayWidth), 203 CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayWidth),
198 kCFNumberSInt32Type, &device_width); 204 kCFNumberSInt32Type, &device_width);
199 205
207 /* Determine the default screen depth */ 213 /* Determine the default screen depth */
208 video_format->BitsPerPixel = device_bpp; 214 video_format->BitsPerPixel = device_bpp;
209 215
210 /* Set misc globals */ 216 /* Set misc globals */
211 current_grab_mode = SDL_GRAB_OFF; 217 current_grab_mode = SDL_GRAB_OFF;
212 cursor_should_be_visible = YES; 218 cursor_should_be_visible = YES;
213 cursor_visible = YES; 219 cursor_visible = YES;
214 current_mods = 0; 220 current_mods = 0;
215 221
216 if ( Gestalt(gestaltSystemVersion, &system_version) != noErr ) 222 if (Gestalt (gestaltSystemVersion, &system_version) != noErr)
217 system_version = 0; 223 system_version = 0;
218 224
219 /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */ 225 /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
220 QZ_RegisterForSleepNotifications (this); 226 QZ_RegisterForSleepNotifications (this);
221 227
222 /* Fill in some window manager capabilities */ 228 /* Fill in some window manager capabilities */
223 this->info.wm_available = 1; 229 this->info.wm_available = 1;
224 230
225 return 0; 231 return 0;
226 } 232 }
227 233
228 static SDL_Rect** QZ_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags) { 234 static SDL_Rect **
235 QZ_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
236 {
229 237
230 CFIndex num_modes; 238 CFIndex num_modes;
231 CFIndex i; 239 CFIndex i;
232 240
233 int list_size = 0; 241 int list_size = 0;
234 242
235 /* Any windowed mode is acceptable */ 243 /* Any windowed mode is acceptable */
236 if ( (flags & SDL_FULLSCREEN) == 0 ) 244 if ((flags & SDL_FULLSCREEN) == 0)
237 return (SDL_Rect**)-1; 245 return (SDL_Rect **) - 1;
238 246
239 /* Free memory from previous call, if any */ 247 /* Free memory from previous call, if any */
240 if ( client_mode_list != NULL ) { 248 if (client_mode_list != NULL) {
241 249
242 int i; 250 int i;
243 251
244 for (i = 0; client_mode_list[i] != NULL; i++) 252 for (i = 0; client_mode_list[i] != NULL; i++)
245 SDL_free (client_mode_list[i]); 253 SDL_free (client_mode_list[i]);
252 260
253 /* Build list of modes with the requested bpp */ 261 /* Build list of modes with the requested bpp */
254 for (i = 0; i < num_modes; i++) { 262 for (i = 0; i < num_modes; i++) {
255 263
256 CFDictionaryRef onemode; 264 CFDictionaryRef onemode;
257 CFNumberRef number; 265 CFNumberRef number;
258 int bpp; 266 int bpp;
259 267
260 onemode = CFArrayGetValueAtIndex (mode_list, i); 268 onemode = CFArrayGetValueAtIndex (mode_list, i);
261 number = CFDictionaryGetValue (onemode, kCGDisplayBitsPerPixel); 269 number = CFDictionaryGetValue (onemode, kCGDisplayBitsPerPixel);
262 CFNumberGetValue (number, kCFNumberSInt32Type, &bpp); 270 CFNumberGetValue (number, kCFNumberSInt32Type, &bpp);
278 /* Check if mode is already in the list */ 286 /* Check if mode is already in the list */
279 { 287 {
280 int i; 288 int i;
281 hasMode = SDL_FALSE; 289 hasMode = SDL_FALSE;
282 for (i = 0; i < list_size; i++) { 290 for (i = 0; i < list_size; i++) {
283 if (client_mode_list[i]->w == width && 291 if (client_mode_list[i]->w == width &&
284 client_mode_list[i]->h == height) { 292 client_mode_list[i]->h == height) {
285 hasMode = SDL_TRUE; 293 hasMode = SDL_TRUE;
286 break; 294 break;
287 } 295 }
288 } 296 }
289 } 297 }
290 298
291 /* Grow the list and add mode to the list */ 299 /* Grow the list and add mode to the list */
292 if ( ! hasMode ) { 300 if (!hasMode) {
293 301
294 SDL_Rect *rect; 302 SDL_Rect *rect;
295 303
296 list_size++; 304 list_size++;
297 305
298 if (client_mode_list == NULL) 306 if (client_mode_list == NULL)
299 client_mode_list = (SDL_Rect**) 307 client_mode_list = (SDL_Rect **)
300 SDL_malloc (sizeof(*client_mode_list) * (list_size+1) ); 308 SDL_malloc (sizeof (*client_mode_list) *
309 (list_size + 1));
301 else 310 else
302 client_mode_list = (SDL_Rect**) 311 client_mode_list = (SDL_Rect **)
303 SDL_realloc (client_mode_list, sizeof(*client_mode_list) * (list_size+1)); 312 SDL_realloc (client_mode_list,
304 313 sizeof (*client_mode_list) *
305 rect = (SDL_Rect*) SDL_malloc (sizeof(**client_mode_list)); 314 (list_size + 1));
315
316 rect = (SDL_Rect *)
317 SDL_malloc (sizeof (**client_mode_list));
306 318
307 if (client_mode_list == NULL || rect == NULL) { 319 if (client_mode_list == NULL || rect == NULL) {
308 SDL_OutOfMemory (); 320 SDL_OutOfMemory ();
309 return NULL; 321 return NULL;
310 } 322 }
311 323
312 rect->x = rect->y = 0; 324 rect->x = rect->y = 0;
313 rect->w = width; 325 rect->w = width;
314 rect->h = height; 326 rect->h = height;
315 327
316 client_mode_list[list_size-1] = rect; 328 client_mode_list[list_size - 1] = rect;
317 client_mode_list[list_size] = NULL; 329 client_mode_list[list_size] = NULL;
318 } 330 }
319 } 331 }
320 } 332 }
321 333
322 /* Sort list largest to smallest (by area) */ 334 /* Sort list largest to smallest (by area) */
323 { 335 {
324 int i, j; 336 int i, j;
325 for (i = 0; i < list_size; i++) { 337 for (i = 0; i < list_size; i++) {
326 for (j = 0; j < list_size-1; j++) { 338 for (j = 0; j < list_size - 1; j++) {
327 339
328 int area1, area2; 340 int area1, area2;
329 area1 = client_mode_list[j]->w * client_mode_list[j]->h; 341 area1 = client_mode_list[j]->w * client_mode_list[j]->h;
330 area2 = client_mode_list[j+1]->w * client_mode_list[j+1]->h; 342 area2 =
343 client_mode_list[j + 1]->w * client_mode_list[j + 1]->h;
331 344
332 if (area1 < area2) { 345 if (area1 < area2) {
333 SDL_Rect *tmp = client_mode_list[j]; 346 SDL_Rect *tmp = client_mode_list[j];
334 client_mode_list[j] = client_mode_list[j+1]; 347 client_mode_list[j] = client_mode_list[j + 1];
335 client_mode_list[j+1] = tmp; 348 client_mode_list[j + 1] = tmp;
336 } 349 }
337 } 350 }
338 } 351 }
339 } 352 }
340 return client_mode_list; 353 return client_mode_list;
341 } 354 }
342 355
343 static SDL_bool QZ_WindowPosition(_THIS, int *x, int *y) 356 static SDL_bool
344 { 357 QZ_WindowPosition (_THIS, int *x, int *y)
345 const char *window = getenv("SDL_VIDEO_WINDOW_POS"); 358 {
346 if ( window ) { 359 const char *window = getenv ("SDL_VIDEO_WINDOW_POS");
347 if ( sscanf(window, "%d,%d", x, y) == 2 ) { 360 if (window) {
361 if (sscanf (window, "%d,%d", x, y) == 2) {
348 return SDL_TRUE; 362 return SDL_TRUE;
349 } 363 }
350 } 364 }
351 return SDL_FALSE; 365 return SDL_FALSE;
352 } 366 }
353 367
354 static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop) { 368 static void
369 QZ_UnsetVideoMode (_THIS, BOOL to_desktop)
370 {
355 371
356 /* Reset values that may change between switches */ 372 /* Reset values that may change between switches */
357 this->info.blit_fill = 0; 373 this->info.blit_fill = 0;
358 this->FillHWRect = NULL; 374 this->FillHWRect = NULL;
359 this->UpdateRects = NULL; 375 this->UpdateRects = NULL;
360 this->LockHWSurface = NULL; 376 this->LockHWSurface = NULL;
361 this->UnlockHWSurface = NULL; 377 this->UnlockHWSurface = NULL;
362 378
363 /* Release fullscreen resources */ 379 /* Release fullscreen resources */
364 if ( mode_flags & SDL_FULLSCREEN ) { 380 if (mode_flags & SDL_FULLSCREEN) {
365 381
366 NSRect screen_rect; 382 NSRect screen_rect;
367 383
368 /* Release double buffer stuff */ 384 /* Release double buffer stuff */
369 if ( mode_flags & SDL_DOUBLEBUF) { 385 if (mode_flags & SDL_DOUBLEBUF) {
370 quit_thread = YES; 386 quit_thread = YES;
371 SDL_SemPost (sem1); 387 SDL_SemPost (sem1);
372 SDL_WaitThread (thread, NULL); 388 SDL_WaitThread (thread, NULL);
373 SDL_DestroySemaphore (sem1); 389 SDL_DestroySemaphore (sem1);
374 SDL_DestroySemaphore (sem2); 390 SDL_DestroySemaphore (sem2);
375 SDL_free (sw_buffers[0]); 391 SDL_free (sw_buffers[0]);
376 } 392 }
377 393
378 /* 394 /*
379 Release the OpenGL context 395 Release the OpenGL context
380 Do this first to avoid trash on the display before fade 396 Do this first to avoid trash on the display before fade
381 */ 397 */
382 if ( mode_flags & SDL_INTERNALOPENGL ) { 398 if (mode_flags & SDL_INTERNALOPENGL) {
383 399
384 QZ_TearDownOpenGL (this); 400 QZ_TearDownOpenGL (this);
385 CGLSetFullScreen (NULL); 401 CGLSetFullScreen (NULL);
386 } 402 }
387 if (to_desktop) { 403 if (to_desktop) {
388 /* Restore original screen resolution/bpp */ 404 /* Restore original screen resolution/bpp */
389 CGDisplaySwitchToMode (display_id, save_mode); 405 CGDisplaySwitchToMode (display_id, save_mode);
390 CGReleaseAllDisplays (); 406 CGReleaseAllDisplays ();
391 ShowMenuBar (); 407 ShowMenuBar ();
392 /* 408 /*
393 Reset the main screen's rectangle 409 Reset the main screen's rectangle
394 See comment in QZ_SetVideoFullscreen for why we do this 410 See comment in QZ_SetVideoFullscreen for why we do this
395 */ 411 */
396 screen_rect = NSMakeRect(0,0,device_width,device_height); 412 screen_rect = NSMakeRect (0, 0, device_width, device_height);
397 [ [ NSScreen mainScreen ] setFrame:screen_rect ]; 413 [[NSScreen mainScreen] setFrame:screen_rect];
398 } 414 }
399 } 415 }
400 /* Release window mode resources */ 416 /* Release window mode resources */
401 else { 417 else {
402 418
403 [ qz_window close ]; 419 [qz_window close];
404 [ qz_window release ]; 420 [qz_window release];
405 qz_window = nil; 421 qz_window = nil;
406 window_view = nil; 422 window_view = nil;
407 423
408 /* Release the OpenGL context */ 424 /* Release the OpenGL context */
409 if ( mode_flags & SDL_INTERNALOPENGL ) 425 if (mode_flags & SDL_INTERNALOPENGL)
410 QZ_TearDownOpenGL (this); 426 QZ_TearDownOpenGL (this);
411 } 427 }
412 428
413 /* Signal successful teardown */ 429 /* Signal successful teardown */
414 video_set = SDL_FALSE; 430 video_set = SDL_FALSE;
415 } 431 }
416 432
417 static SDL_Surface* QZ_SetVideoFullScreen (_THIS, SDL_Surface *current, int width, 433 static SDL_Surface *
418 int height, int bpp, Uint32 flags) { 434 QZ_SetVideoFullScreen (_THIS, SDL_Surface * current, int width,
435 int height, int bpp, Uint32 flags)
436 {
419 boolean_t exact_match = 0; 437 boolean_t exact_match = 0;
420 NSRect screen_rect; 438 NSRect screen_rect;
421 CGError error; 439 CGError error;
422 CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken; 440 CGDisplayFadeReservationToken fade_token =
423 441 kCGDisplayFadeReservationInvalidToken;
442
424 /* Fade to black to hide resolution-switching flicker (and garbage 443 /* Fade to black to hide resolution-switching flicker (and garbage
425 that is displayed by a destroyed OpenGL context, if applicable) */ 444 that is displayed by a destroyed OpenGL context, if applicable) */
426 if ( CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess ) { 445 if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) {
427 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE); 446 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal,
428 } 447 kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
429 448 }
449
430 /* Destroy any previous mode */ 450 /* Destroy any previous mode */
431 if (video_set == SDL_TRUE) 451 if (video_set == SDL_TRUE)
432 QZ_UnsetVideoMode (this, FALSE); 452 QZ_UnsetVideoMode (this, FALSE);
433 453
434 /* See if requested mode exists */ 454 /* See if requested mode exists */
435 mode = CGDisplayBestModeForParameters (display_id, bpp, width, 455 mode = CGDisplayBestModeForParameters (display_id, bpp, width,
436 height, &exact_match); 456 height, &exact_match);
437 457
438 /* Require an exact match to the requested mode */ 458 /* Require an exact match to the requested mode */
439 if ( ! exact_match ) { 459 if (!exact_match) {
440 SDL_SetError ("Failed to find display resolution: %dx%dx%d", width, height, bpp); 460 SDL_SetError ("Failed to find display resolution: %dx%dx%d", width,
461 height, bpp);
441 goto ERR_NO_MATCH; 462 goto ERR_NO_MATCH;
442 } 463 }
443 464
444 /* Put up the blanking window (a window above all other windows) */ 465 /* Put up the blanking window (a window above all other windows) */
445 if (getenv ("SDL_SINGLEDISPLAY")) 466 if (getenv ("SDL_SINGLEDISPLAY"))
446 error = CGDisplayCapture (display_id); 467 error = CGDisplayCapture (display_id);
447 else 468 else
448 error = CGCaptureAllDisplays (); 469 error = CGCaptureAllDisplays ();
449 470
450 if ( CGDisplayNoErr != error ) { 471 if (CGDisplayNoErr != error) {
451 SDL_SetError ("Failed capturing display"); 472 SDL_SetError ("Failed capturing display");
452 goto ERR_NO_CAPTURE; 473 goto ERR_NO_CAPTURE;
453 } 474 }
454 475
455 /* Do the physical switch */ 476 /* Do the physical switch */
456 if ( CGDisplayNoErr != CGDisplaySwitchToMode (display_id, mode) ) { 477 if (CGDisplayNoErr != CGDisplaySwitchToMode (display_id, mode)) {
457 SDL_SetError ("Failed switching display resolution"); 478 SDL_SetError ("Failed switching display resolution");
458 goto ERR_NO_SWITCH; 479 goto ERR_NO_SWITCH;
459 } 480 }
460 481
461 current->pixels = (Uint32*) CGDisplayBaseAddress (display_id); 482 current->pixels = (Uint32 *) CGDisplayBaseAddress (display_id);
462 current->pitch = CGDisplayBytesPerRow (display_id); 483 current->pitch = CGDisplayBytesPerRow (display_id);
463 484
464 current->flags = 0; 485 current->flags = 0;
465 current->w = width; 486 current->w = width;
466 current->h = height; 487 current->h = height;
467 current->flags |= SDL_FULLSCREEN; 488 current->flags |= SDL_FULLSCREEN;
468 current->flags |= SDL_HWSURFACE; 489 current->flags |= SDL_HWSURFACE;
469 current->flags |= SDL_PREALLOC; 490 current->flags |= SDL_PREALLOC;
470 491
471 this->UpdateRects = QZ_DirectUpdate; 492 this->UpdateRects = QZ_DirectUpdate;
472 this->LockHWSurface = QZ_LockHWSurface; 493 this->LockHWSurface = QZ_LockHWSurface;
473 this->UnlockHWSurface = QZ_UnlockHWSurface; 494 this->UnlockHWSurface = QZ_UnlockHWSurface;
474 495
475 /* Setup double-buffer emulation */ 496 /* Setup double-buffer emulation */
476 if ( flags & SDL_DOUBLEBUF ) { 497 if (flags & SDL_DOUBLEBUF) {
477 498
478 /* 499 /*
479 Setup a software backing store for reasonable results when 500 Setup a software backing store for reasonable results when
480 double buffering is requested (since a single-buffered hardware 501 double buffering is requested (since a single-buffered hardware
481 surface looks hideous). 502 surface looks hideous).
482 503
483 The actual screen blit occurs in a separate thread to allow 504 The actual screen blit occurs in a separate thread to allow
484 other blitting while waiting on the VBL (and hence results in higher framerates). 505 other blitting while waiting on the VBL (and hence results in higher framerates).
485 */ 506 */
486 this->LockHWSurface = NULL; 507 this->LockHWSurface = NULL;
487 this->UnlockHWSurface = NULL; 508 this->UnlockHWSurface = NULL;
488 this->UpdateRects = NULL; 509 this->UpdateRects = NULL;
489 510
490 current->flags |= (SDL_HWSURFACE|SDL_DOUBLEBUF); 511 current->flags |= (SDL_HWSURFACE | SDL_DOUBLEBUF);
491 this->UpdateRects = QZ_DoubleBufferUpdate; 512 this->UpdateRects = QZ_DoubleBufferUpdate;
492 this->LockHWSurface = QZ_LockDoubleBuffer; 513 this->LockHWSurface = QZ_LockDoubleBuffer;
493 this->UnlockHWSurface = QZ_UnlockDoubleBuffer; 514 this->UnlockHWSurface = QZ_UnlockDoubleBuffer;
494 this->FlipHWSurface = QZ_FlipDoubleBuffer; 515 this->FlipHWSurface = QZ_FlipDoubleBuffer;
495 516
496 current->pixels = SDL_malloc (current->pitch * current->h * 2); 517 current->pixels = SDL_malloc (current->pitch * current->h * 2);
497 if (current->pixels == NULL) { 518 if (current->pixels == NULL) {
498 SDL_OutOfMemory (); 519 SDL_OutOfMemory ();
499 goto ERR_DOUBLEBUF; 520 goto ERR_DOUBLEBUF;
500 } 521 }
501 522
502 sw_buffers[0] = current->pixels; 523 sw_buffers[0] = current->pixels;
503 sw_buffers[1] = (Uint8*)current->pixels + current->pitch * current->h; 524 sw_buffers[1] =
504 525 (Uint8 *) current->pixels + current->pitch * current->h;
526
505 quit_thread = NO; 527 quit_thread = NO;
506 sem1 = SDL_CreateSemaphore (0); 528 sem1 = SDL_CreateSemaphore (0);
507 sem2 = SDL_CreateSemaphore (1); 529 sem2 = SDL_CreateSemaphore (1);
508 thread = SDL_CreateThread ((int (*)(void *))QZ_ThreadFlip, this); 530 thread = SDL_CreateThread ((int (*)(void *)) QZ_ThreadFlip, this);
509 } 531 }
510 532
511 if ( CGDisplayCanSetPalette (display_id) ) 533 if (CGDisplayCanSetPalette (display_id))
512 current->flags |= SDL_HWPALETTE; 534 current->flags |= SDL_HWPALETTE;
513 535
514 /* Setup OpenGL for a fullscreen context */ 536 /* Setup OpenGL for a fullscreen context */
515 if (flags & SDL_INTERNALOPENGL) { 537 if (flags & SDL_INTERNALOPENGL) {
516 538
517 CGLError err; 539 CGLError err;
518 CGLContextObj ctx; 540 CGLContextObj ctx;
519 541
520 if ( ! QZ_SetupOpenGL (this, bpp, flags) ) { 542 if (!QZ_SetupOpenGL (this, bpp, flags)) {
521 goto ERR_NO_GL; 543 goto ERR_NO_GL;
522 } 544 }
523 545
524 ctx = [ gl_context cglContext ]; 546 ctx =[gl_context cglContext];
525 err = CGLSetFullScreen (ctx); 547 err = CGLSetFullScreen (ctx);
526 548
527 if (err) { 549 if (err) {
528 SDL_SetError ("Error setting OpenGL fullscreen: %s", CGLErrorString(err)); 550 SDL_SetError ("Error setting OpenGL fullscreen: %s",
551 CGLErrorString (err));
529 goto ERR_NO_GL; 552 goto ERR_NO_GL;
530 } 553 }
531 554
532 [ gl_context makeCurrentContext]; 555 [gl_context makeCurrentContext];
533 556
534 glClear (GL_COLOR_BUFFER_BIT); 557 glClear (GL_COLOR_BUFFER_BIT);
535 558
536 [ gl_context flushBuffer ]; 559 [gl_context flushBuffer];
537 560
538 current->flags |= SDL_INTERNALOPENGL; 561 current->flags |= SDL_INTERNALOPENGL;
539 } 562 }
540 563
541 /* If we don't hide menu bar, it will get events and interrupt the program */ 564 /* If we don't hide menu bar, it will get events and interrupt the program */
542 HideMenuBar (); 565 HideMenuBar ();
543 566
544 /* Fade in again (asynchronously) */ 567 /* Fade in again (asynchronously) */
545 if ( fade_token != kCGDisplayFadeReservationInvalidToken ) { 568 if (fade_token != kCGDisplayFadeReservationInvalidToken) {
546 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 569 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor,
547 CGReleaseDisplayFadeReservation(fade_token); 570 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
571 CGReleaseDisplayFadeReservation (fade_token);
548 } 572 }
549 573
550 /* 574 /*
551 There is a bug in Cocoa where NSScreen doesn't synchronize 575 There is a bug in Cocoa where NSScreen doesn't synchronize
552 with CGDirectDisplay, so the main screen's frame is wrong. 576 with CGDirectDisplay, so the main screen's frame is wrong.
553 As a result, coordinate translation produces incorrect results. 577 As a result, coordinate translation produces incorrect results.
554 We can hack around this bug by setting the screen rect 578 We can hack around this bug by setting the screen rect
555 ourselves. This hack should be removed if/when the bug is fixed. 579 ourselves. This hack should be removed if/when the bug is fixed.
556 */ 580 */
557 screen_rect = NSMakeRect(0,0,width,height); 581 screen_rect = NSMakeRect (0, 0, width, height);
558 [ [ NSScreen mainScreen ] setFrame:screen_rect ]; 582 [[NSScreen mainScreen] setFrame:screen_rect];
559 583
560 /* Save the flags to ensure correct tear-down */ 584 /* Save the flags to ensure correct tear-down */
561 mode_flags = current->flags; 585 mode_flags = current->flags;
562 586
563 /* Set app state, hide cursor if necessary, ... */ 587 /* Set app state, hide cursor if necessary, ... */
564 QZ_DoActivate(this); 588 QZ_DoActivate (this);
565 589
566 return current; 590 return current;
567 591
568 /* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */ 592 /* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
569 ERR_NO_GL: 593 ERR_NO_GL:
570 ERR_DOUBLEBUF: CGDisplaySwitchToMode (display_id, save_mode); 594 ERR_DOUBLEBUF:CGDisplaySwitchToMode (display_id, save_mode);
571 ERR_NO_SWITCH: CGReleaseAllDisplays (); 595 ERR_NO_SWITCH:CGReleaseAllDisplays ();
572 ERR_NO_CAPTURE: 596 ERR_NO_CAPTURE:
573 ERR_NO_MATCH: if ( fade_token != kCGDisplayFadeReservationInvalidToken ) { 597 ERR_NO_MATCH:if (fade_token != kCGDisplayFadeReservationInvalidToken) {
574 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 598 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor,
575 CGReleaseDisplayFadeReservation (fade_token); 599 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
576 } 600 CGReleaseDisplayFadeReservation (fade_token);
577 return NULL; 601 }
578 } 602 return NULL;
579 603 }
580 static SDL_Surface* QZ_SetVideoWindowed (_THIS, SDL_Surface *current, int width, 604
581 int height, int *bpp, Uint32 flags) { 605 static SDL_Surface *
606 QZ_SetVideoWindowed (_THIS, SDL_Surface * current, int width,
607 int height, int *bpp, Uint32 flags)
608 {
582 unsigned int style; 609 unsigned int style;
583 NSRect contentRect; 610 NSRect contentRect;
584 BOOL isCustom = NO; 611 BOOL isCustom = NO;
585 int center_window = 1; 612 int center_window = 1;
586 int origin_x, origin_y; 613 int origin_x, origin_y;
587 CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken; 614 CGDisplayFadeReservationToken fade_token =
615 kCGDisplayFadeReservationInvalidToken;
588 616
589 current->flags = 0; 617 current->flags = 0;
590 current->w = width; 618 current->w = width;
591 current->h = height; 619 current->h = height;
592 620
593 contentRect = NSMakeRect (0, 0, width, height); 621 contentRect = NSMakeRect (0, 0, width, height);
594 622
595 /* 623 /*
596 Check if we should completely destroy the previous mode 624 Check if we should completely destroy the previous mode
597 - If it is fullscreen 625 - If it is fullscreen
598 - If it has different noframe or resizable attribute 626 - If it has different noframe or resizable attribute
599 - If it is OpenGL (since gl attributes could be different) 627 - If it is OpenGL (since gl attributes could be different)
600 - If new mode is OpenGL, but previous mode wasn't 628 - If new mode is OpenGL, but previous mode wasn't
601 */ 629 */
602 if (video_set == SDL_TRUE) { 630 if (video_set == SDL_TRUE) {
603 if (mode_flags & SDL_FULLSCREEN) { 631 if (mode_flags & SDL_FULLSCREEN) {
604 /* Fade to black to hide resolution-switching flicker (and garbage 632 /* Fade to black to hide resolution-switching flicker (and garbage
605 that is displayed by a destroyed OpenGL context, if applicable) */ 633 that is displayed by a destroyed OpenGL context, if applicable) */
606 if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) { 634 if (CGAcquireDisplayFadeReservation (5, &fade_token) ==
607 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE); 635 kCGErrorSuccess) {
636 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal,
637 kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0,
638 TRUE);
608 } 639 }
609 QZ_UnsetVideoMode (this, TRUE); 640 QZ_UnsetVideoMode (this, TRUE);
610 } 641 } else if (((mode_flags ^ flags) & (SDL_NOFRAME | SDL_RESIZABLE)) ||
611 else if ( ((mode_flags ^ flags) & (SDL_NOFRAME|SDL_RESIZABLE)) || 642 (mode_flags & SDL_INTERNALOPENGL) ||
612 (mode_flags & SDL_INTERNALOPENGL) || 643 (flags & SDL_INTERNALOPENGL)) {
613 (flags & SDL_INTERNALOPENGL) ) {
614 QZ_UnsetVideoMode (this, TRUE); 644 QZ_UnsetVideoMode (this, TRUE);
615 } 645 }
616 } 646 }
617 647
618 /* Check for user-specified window and view */ 648 /* Check for user-specified window and view */
619 { 649 {
620 char *windowPtrString = getenv ("SDL_NSWindowPointer"); 650 char *windowPtrString = getenv ("SDL_NSWindowPointer");
621 char *viewPtrString = getenv ("SDL_NSQuickDrawViewPointer"); 651 char *viewPtrString = getenv ("SDL_NSQuickDrawViewPointer");
622 652
623 if (windowPtrString && viewPtrString) { 653 if (windowPtrString && viewPtrString) {
624 654
625 /* Release any previous window */ 655 /* Release any previous window */
626 if ( qz_window ) { 656 if (qz_window) {
627 [ qz_window release ]; 657 [qz_window release];
628 qz_window = nil; 658 qz_window = nil;
629 } 659 }
630 660
631 qz_window = (NSWindow*)atoi(windowPtrString); 661 qz_window = (NSWindow *) atoi (windowPtrString);
632 window_view = (NSQuickDrawView*)atoi(viewPtrString); 662 window_view = (NSQuickDrawView *) atoi (viewPtrString);
633 isCustom = YES; 663 isCustom = YES;
634 664
635 /* 665 /*
636 Retain reference to window because we 666 Retain reference to window because we
637 might release it in QZ_UnsetVideoMode 667 might release it in QZ_UnsetVideoMode
638 */ 668 */
639 [ qz_window retain ]; 669 [qz_window retain];
640 670
641 style = [ qz_window styleMask ]; 671 style =[qz_window styleMask];
642 /* Check resizability */ 672 /* Check resizability */
643 if ( style & NSResizableWindowMask ) 673 if (style & NSResizableWindowMask)
644 current->flags |= SDL_RESIZABLE; 674 current->flags |= SDL_RESIZABLE;
645 675
646 /* Check frame */ 676 /* Check frame */
647 if ( style & NSBorderlessWindowMask ) 677 if (style & NSBorderlessWindowMask)
648 current->flags |= SDL_NOFRAME; 678 current->flags |= SDL_NOFRAME;
649 } 679 }
650 } 680 }
651 681
652 /* Check if we should recreate the window */ 682 /* Check if we should recreate the window */
653 if (qz_window == nil) { 683 if (qz_window == nil) {
654 684
655 /* Set the window style based on input flags */ 685 /* Set the window style based on input flags */
656 if ( flags & SDL_NOFRAME ) { 686 if (flags & SDL_NOFRAME) {
657 style = NSBorderlessWindowMask; 687 style = NSBorderlessWindowMask;
658 current->flags |= SDL_NOFRAME; 688 current->flags |= SDL_NOFRAME;
659 } else { 689 } else {
660 style = NSTitledWindowMask; 690 style = NSTitledWindowMask;
661 style |= (NSMiniaturizableWindowMask | NSClosableWindowMask); 691 style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
662 if ( flags & SDL_RESIZABLE ) { 692 if (flags & SDL_RESIZABLE) {
663 style |= NSResizableWindowMask; 693 style |= NSResizableWindowMask;
664 current->flags |= SDL_RESIZABLE; 694 current->flags |= SDL_RESIZABLE;
665 } 695 }
666 } 696 }
667 697
668 if ( QZ_WindowPosition(this, &origin_x, &origin_y) ) { 698 if (QZ_WindowPosition (this, &origin_x, &origin_y)) {
669 center_window = 0; 699 center_window = 0;
670 contentRect.origin.x = (float)origin_x; 700 contentRect.origin.x = (float) origin_x;
671 contentRect.origin.y = (float)origin_y; 701 contentRect.origin.y = (float) origin_y;
672 } 702 }
673 703
674 /* Manually create a window, avoids having a nib file resource */ 704 /* Manually create a window, avoids having a nib file resource */
675 qz_window = [ [ SDL_QuartzWindow alloc ] 705 qz_window =[[SDL_QuartzWindow alloc] initWithContentRect: contentRect styleMask: style backing: NSBackingStoreBuffered defer:NO];
676 initWithContentRect:contentRect 706
677 styleMask:style
678 backing:NSBackingStoreBuffered
679 defer:NO ];
680
681 if (qz_window == nil) { 707 if (qz_window == nil) {
682 SDL_SetError ("Could not create the Cocoa window"); 708 SDL_SetError ("Could not create the Cocoa window");
683 if (fade_token != kCGDisplayFadeReservationInvalidToken) { 709 if (fade_token != kCGDisplayFadeReservationInvalidToken) {
684 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 710 CGDisplayFade (fade_token, 0.5,
711 kCGDisplayBlendSolidColor,
712 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
685 CGReleaseDisplayFadeReservation (fade_token); 713 CGReleaseDisplayFadeReservation (fade_token);
686 } 714 }
687 return NULL; 715 return NULL;
688 } 716 }
689 717
690 /*[ qz_window setReleasedWhenClosed:YES ];*/ 718 /*[ qz_window setReleasedWhenClosed:YES ]; */
691 QZ_SetCaption(this, this->wm_title, this->wm_icon); 719 QZ_SetCaption (this, this->wm_title, this->wm_icon);
692 [ qz_window setAcceptsMouseMovedEvents:YES ]; 720 [qz_window setAcceptsMouseMovedEvents:YES];
693 [ qz_window setViewsNeedDisplay:NO ]; 721 [qz_window setViewsNeedDisplay:NO];
694 if ( center_window ) { 722 if (center_window) {
695 [ qz_window center ]; 723 [qz_window center];
696 } 724 }
697 [ qz_window setDelegate: 725 [qz_window setDelegate:
698 [ [ [ SDL_QuartzWindowDelegate alloc ] init ] autorelease ] ]; 726 [[[SDL_QuartzWindowDelegate alloc] init] autorelease]];
699 } 727 }
700 /* We already have a window, just change its size */ 728 /* We already have a window, just change its size */
701 else { 729 else {
702 730
703 if (!isCustom) { 731 if (!isCustom) {
704 [ qz_window setContentSize:contentRect.size ]; 732 [qz_window setContentSize:contentRect.size];
705 current->flags |= (SDL_NOFRAME|SDL_RESIZABLE) & mode_flags; 733 current->flags |= (SDL_NOFRAME | SDL_RESIZABLE) & mode_flags;
706 [ window_view setFrameSize:contentRect.size ]; 734 [window_view setFrameSize:contentRect.size];
707 } 735 }
708 } 736 }
709 737
710 /* For OpenGL, we bind the context to a subview */ 738 /* For OpenGL, we bind the context to a subview */
711 if ( flags & SDL_INTERNALOPENGL ) { 739 if (flags & SDL_INTERNALOPENGL) {
712 740
713 if ( ! QZ_SetupOpenGL (this, *bpp, flags) ) { 741 if (!QZ_SetupOpenGL (this, *bpp, flags)) {
714 if (fade_token != kCGDisplayFadeReservationInvalidToken) { 742 if (fade_token != kCGDisplayFadeReservationInvalidToken) {
715 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 743 CGDisplayFade (fade_token, 0.5,
744 kCGDisplayBlendSolidColor,
745 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
716 CGReleaseDisplayFadeReservation (fade_token); 746 CGReleaseDisplayFadeReservation (fade_token);
717 } 747 }
718 return NULL; 748 return NULL;
719 } 749 }
720 750
721 window_view = [ [ NSView alloc ] initWithFrame:contentRect ]; 751 window_view =[[NSView alloc] initWithFrame:contentRect];
722 [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ]; 752 [window_view setAutoresizingMask:NSViewWidthSizable |
723 [ [ qz_window contentView ] addSubview:window_view ]; 753 NSViewHeightSizable];
724 [ gl_context setView: window_view ]; 754 [[qz_window contentView] addSubview:window_view];
725 [ window_view release ]; 755 [gl_context setView:window_view];
726 [ gl_context makeCurrentContext]; 756 [window_view release];
727 [ qz_window makeKeyAndOrderFront:nil ]; 757 [gl_context makeCurrentContext];
758 [qz_window makeKeyAndOrderFront:nil];
728 current->flags |= SDL_INTERNALOPENGL; 759 current->flags |= SDL_INTERNALOPENGL;
729 } 760 }
730 /* For 2D, we set the subview to an NSQuickDrawView */ 761 /* For 2D, we set the subview to an NSQuickDrawView */
731 else { 762 else {
732 short qdbpp = 0; 763 short qdbpp = 0;
733 764
734 /* Only recreate the view if it doesn't already exist */ 765 /* Only recreate the view if it doesn't already exist */
735 if (window_view == nil) { 766 if (window_view == nil) {
736 767
737 window_view = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ]; 768 window_view =[[NSQuickDrawView alloc] initWithFrame:contentRect];
738 [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ]; 769 [window_view setAutoresizingMask:NSViewWidthSizable |
739 [ [ qz_window contentView ] addSubview:window_view ]; 770 NSViewHeightSizable];
740 [ window_view release ]; 771 [[qz_window contentView] addSubview:window_view];
741 [ qz_window makeKeyAndOrderFront:nil ]; 772 [window_view release];
742 } 773 [qz_window makeKeyAndOrderFront:nil];
743 774 }
744 LockPortBits ( [ window_view qdPort ] ); 775
745 current->pixels = GetPixBaseAddr ( GetPortPixMap ( [ window_view qdPort ] ) ); 776 LockPortBits ([window_view qdPort]);
746 current->pitch = GetPixRowBytes ( GetPortPixMap ( [ window_view qdPort ] ) ); 777 current->pixels =
747 qdbpp = GetPixDepth ( GetPortPixMap ( [ window_view qdPort ] ) ); 778 GetPixBaseAddr (GetPortPixMap ([window_view qdPort]));
748 UnlockPortBits ( [ window_view qdPort ] ); 779 current->pitch =
780 GetPixRowBytes (GetPortPixMap ([window_view qdPort]));
781 qdbpp = GetPixDepth (GetPortPixMap ([window_view qdPort]));
782 UnlockPortBits ([window_view qdPort]);
749 783
750 /* QuickDraw may give a 16-bit shadow surface on 8-bit displays! */ 784 /* QuickDraw may give a 16-bit shadow surface on 8-bit displays! */
751 *bpp = qdbpp; 785 *bpp = qdbpp;
752 786
753 current->flags |= SDL_SWSURFACE; 787 current->flags |= SDL_SWSURFACE;
754 current->flags |= SDL_PREALLOC; 788 current->flags |= SDL_PREALLOC;
755 current->flags |= SDL_ASYNCBLIT; 789 current->flags |= SDL_ASYNCBLIT;
756 790
757 /* 791 /*
758 current->pixels now points to the window's pixels 792 current->pixels now points to the window's pixels
759 We want it to point to the *view's* pixels 793 We want it to point to the *view's* pixels
760 */ 794 */
761 { 795 {
762 int vOffset = [ qz_window frame ].size.height - 796 int vOffset =[qz_window frame].size.height -
763 [ window_view frame ].size.height - [ window_view frame ].origin.y; 797 [window_view frame].size.height -[window_view frame].origin.y;
764 798
765 int hOffset = [ window_view frame ].origin.x; 799 int hOffset =[window_view frame].origin.x;
766 800
767 current->pixels = (Uint8 *)current->pixels + (vOffset * current->pitch) + hOffset * (qdbpp/8); 801 current->pixels =
768 } 802 (Uint8 *) current->pixels + (vOffset * current->pitch) +
769 this->UpdateRects = QZ_UpdateRects; 803 hOffset * (qdbpp / 8);
770 this->LockHWSurface = QZ_LockWindow; 804 }
805 this->UpdateRects = QZ_UpdateRects;
806 this->LockHWSurface = QZ_LockWindow;
771 this->UnlockHWSurface = QZ_UnlockWindow; 807 this->UnlockHWSurface = QZ_UnlockWindow;
772 } 808 }
773 809
774 /* Save flags to ensure correct teardown */ 810 /* Save flags to ensure correct teardown */
775 mode_flags = current->flags; 811 mode_flags = current->flags;
776 812
777 /* Fade in again (asynchronously) if we came from a fullscreen mode and faded to black */ 813 /* Fade in again (asynchronously) if we came from a fullscreen mode and faded to black */
778 if (fade_token != kCGDisplayFadeReservationInvalidToken) { 814 if (fade_token != kCGDisplayFadeReservationInvalidToken) {
779 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 815 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor,
816 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
780 CGReleaseDisplayFadeReservation (fade_token); 817 CGReleaseDisplayFadeReservation (fade_token);
781 } 818 }
782 819
783 return current; 820 return current;
784 } 821 }
785 822
786 static SDL_Surface* QZ_SetVideoMode (_THIS, SDL_Surface *current, int width, 823 static SDL_Surface *
787 int height, int bpp, Uint32 flags) { 824 QZ_SetVideoMode (_THIS, SDL_Surface * current, int width,
825 int height, int bpp, Uint32 flags)
826 {
788 827
789 current->flags = 0; 828 current->flags = 0;
790 current->pixels = NULL; 829 current->pixels = NULL;
791 830
792 /* Setup full screen video */ 831 /* Setup full screen video */
793 if ( flags & SDL_FULLSCREEN ) { 832 if (flags & SDL_FULLSCREEN) {
794 current = QZ_SetVideoFullScreen (this, current, width, height, bpp, flags ); 833 current =
834 QZ_SetVideoFullScreen (this, current, width, height, bpp, flags);
795 if (current == NULL) 835 if (current == NULL)
796 return NULL; 836 return NULL;
797 } 837 }
798 /* Setup windowed video */ 838 /* Setup windowed video */
799 else { 839 else {
800 /* Force bpp to the device's bpp */ 840 /* Force bpp to the device's bpp */
801 bpp = device_bpp; 841 bpp = device_bpp;
802 current = QZ_SetVideoWindowed (this, current, width, height, &bpp, flags); 842 current =
843 QZ_SetVideoWindowed (this, current, width, height, &bpp, flags);
803 if (current == NULL) 844 if (current == NULL)
804 return NULL; 845 return NULL;
805 } 846 }
806 847
807 /* Setup the new pixel format */ 848 /* Setup the new pixel format */
808 { 849 {
809 int amask = 0, 850 int amask = 0, rmask = 0, gmask = 0, bmask = 0;
810 rmask = 0,
811 gmask = 0,
812 bmask = 0;
813 851
814 switch (bpp) { 852 switch (bpp) {
815 case 16: /* (1)-5-5-5 RGB */ 853 case 16: /* (1)-5-5-5 RGB */
816 amask = 0; 854 amask = 0;
817 rmask = 0x7C00; 855 rmask = 0x7C00;
818 gmask = 0x03E0; 856 gmask = 0x03E0;
819 bmask = 0x001F; 857 bmask = 0x001F;
820 break; 858 break;
821 case 24: 859 case 24:
822 SDL_SetError ("24bpp is not available"); 860 SDL_SetError ("24bpp is not available");
823 return NULL; 861 return NULL;
824 case 32: /* (8)-8-8-8 ARGB */ 862 case 32: /* (8)-8-8-8 ARGB */
825 amask = 0x00000000; 863 amask = 0x00000000;
826 rmask = 0x00FF0000; 864 rmask = 0x00FF0000;
827 gmask = 0x0000FF00; 865 gmask = 0x0000FF00;
828 bmask = 0x000000FF; 866 bmask = 0x000000FF;
829 break; 867 break;
830 } 868 }
831 869
832 if ( ! SDL_ReallocFormat (current, bpp, 870 if (!SDL_ReallocFormat (current, bpp, rmask, gmask, bmask, amask)) {
833 rmask, gmask, bmask, amask ) ) {
834 SDL_SetError ("Couldn't reallocate pixel format"); 871 SDL_SetError ("Couldn't reallocate pixel format");
835 return NULL; 872 return NULL;
836 } 873 }
837 } 874 }
838 875
839 /* Signal successful completion (used internally) */ 876 /* Signal successful completion (used internally) */
840 video_set = SDL_TRUE; 877 video_set = SDL_TRUE;
841 878
842 return current; 879 return current;
843 } 880 }
844 881
845 static int QZ_ToggleFullScreen (_THIS, int on) { 882 static int
883 QZ_ToggleFullScreen (_THIS, int on)
884 {
846 return 0; 885 return 0;
847 } 886 }
848 887
849 static int QZ_SetColors (_THIS, int first_color, int num_colors, 888 static int
850 SDL_Color *colors) { 889 QZ_SetColors (_THIS, int first_color, int num_colors, SDL_Color * colors)
851 890 {
852 CGTableCount index; 891
892 CGTableCount index;
853 CGDeviceColor color; 893 CGDeviceColor color;
854 894
855 for (index = first_color; index < first_color+num_colors; index++) { 895 for (index = first_color; index < first_color + num_colors; index++) {
856 896
857 /* Clamp colors between 0.0 and 1.0 */ 897 /* Clamp colors between 0.0 and 1.0 */
858 color.red = colors->r / 255.0; 898 color.red = colors->r / 255.0;
859 color.blue = colors->b / 255.0; 899 color.blue = colors->b / 255.0;
860 color.green = colors->g / 255.0; 900 color.green = colors->g / 255.0;
861 901
862 colors++; 902 colors++;
863 903
864 CGPaletteSetColorAtIndex (palette, color, index); 904 CGPaletteSetColorAtIndex (palette, color, index);
865 } 905 }
866 906
867 if ( CGDisplayNoErr != CGDisplaySetPalette (display_id, palette) ) 907 if (CGDisplayNoErr != CGDisplaySetPalette (display_id, palette))
868 return 0; 908 return 0;
869 909
870 return 1; 910 return 1;
871 } 911 }
872 912
873 static int QZ_LockDoubleBuffer (_THIS, SDL_Surface *surface) { 913 static int
914 QZ_LockDoubleBuffer (_THIS, SDL_Surface * surface)
915 {
874 916
875 return 1; 917 return 1;
876 } 918 }
877 919
878 static void QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface) { 920 static void
921 QZ_UnlockDoubleBuffer (_THIS, SDL_Surface * surface)
922 {
879 923
880 } 924 }
881 925
882 /* The VBL delay is based on code by Ian R Ollmann's RezLib <iano@cco.caltech.edu> */ 926 /* The VBL delay is based on code by Ian R Ollmann's RezLib <iano@cco.caltech.edu> */
883 static AbsoluteTime QZ_SecondsToAbsolute ( double seconds ) { 927 static AbsoluteTime
884 928 QZ_SecondsToAbsolute (double seconds)
929 {
930
885 union 931 union
886 { 932 {
887 UInt64 i; 933 UInt64 i;
888 Nanoseconds ns; 934 Nanoseconds ns;
889 } temp; 935 } temp;
890 936
891 temp.i = seconds * 1000000000.0; 937 temp.i = seconds * 1000000000.0;
892 938
893 return NanosecondsToAbsolute ( temp.ns ); 939 return NanosecondsToAbsolute (temp.ns);
894 } 940 }
895 941
896 static int QZ_ThreadFlip (_THIS) { 942 static int
943 QZ_ThreadFlip (_THIS)
944 {
897 945
898 Uint8 *src, *dst; 946 Uint8 *src, *dst;
899 int skip, len, h; 947 int skip, len, h;
900 948
901 /* 949 /*
902 Give this thread the highest scheduling priority possible, 950 Give this thread the highest scheduling priority possible,
903 in the hopes that it will immediately run after the VBL delay 951 in the hopes that it will immediately run after the VBL delay
904 */ 952 */
905 { 953 {
906 pthread_t current_thread; 954 pthread_t current_thread;
907 int policy; 955 int policy;
908 struct sched_param param; 956 struct sched_param param;
909 957
910 current_thread = pthread_self (); 958 current_thread = pthread_self ();
911 pthread_getschedparam (current_thread, &policy, &param); 959 pthread_getschedparam (current_thread, &policy, &param);
912 policy = SCHED_RR; 960 policy = SCHED_RR;
913 param.sched_priority = sched_get_priority_max (policy); 961 param.sched_priority = sched_get_priority_max (policy);
914 pthread_setschedparam (current_thread, policy, &param); 962 pthread_setschedparam (current_thread, policy, &param);
915 } 963 }
916 964
917 while (1) { 965 while (1) {
918 966
919 SDL_SemWait (sem1); 967 SDL_SemWait (sem1);
920 if (quit_thread) 968 if (quit_thread)
921 return 0; 969 return 0;
922 970
923 /* 971 /*
924 * We have to add SDL_VideoSurface->offset here, since we might be a 972 * We have to add SDL_VideoSurface->offset here, since we might be a
925 * smaller surface in the center of the framebuffer (you asked for 973 * smaller surface in the center of the framebuffer (you asked for
926 * a fullscreen resolution smaller than the hardware could supply 974 * a fullscreen resolution smaller than the hardware could supply
927 * so SDL is centering it in a bigger resolution)... 975 * so SDL is centering it in a bigger resolution)...
928 */ 976 */
929 dst = (Uint8 *)CGDisplayBaseAddress (display_id) + SDL_VideoSurface->offset; 977 dst =
978 (Uint8 *) CGDisplayBaseAddress (display_id) +
979 SDL_VideoSurface->offset;
930 src = current_buffer + SDL_VideoSurface->offset; 980 src = current_buffer + SDL_VideoSurface->offset;
931 len = SDL_VideoSurface->w * SDL_VideoSurface->format->BytesPerPixel; 981 len = SDL_VideoSurface->w * SDL_VideoSurface->format->BytesPerPixel;
932 h = SDL_VideoSurface->h; 982 h = SDL_VideoSurface->h;
933 skip = SDL_VideoSurface->pitch; 983 skip = SDL_VideoSurface->pitch;
934 984
935 /* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */ 985 /* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
936 { 986 {
937 987
938 /* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */ 988 /* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
939 double refreshRate; 989 double refreshRate;
940 double linesPerSecond; 990 double linesPerSecond;
941 double target; 991 double target;
942 double position; 992 double position;
943 double adjustment; 993 double adjustment;
944 AbsoluteTime nextTime; 994 AbsoluteTime nextTime;
945 CFNumberRef refreshRateCFNumber; 995 CFNumberRef refreshRateCFNumber;
946 996
947 refreshRateCFNumber = CFDictionaryGetValue (mode, kCGDisplayRefreshRate); 997 refreshRateCFNumber =
948 if ( NULL == refreshRateCFNumber ) { 998 CFDictionaryGetValue (mode, kCGDisplayRefreshRate);
999 if (NULL == refreshRateCFNumber) {
949 SDL_SetError ("Mode has no refresh rate"); 1000 SDL_SetError ("Mode has no refresh rate");
950 goto ERROR; 1001 goto ERROR;
951 } 1002 }
952 1003
953 if ( 0 == CFNumberGetValue (refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) ) { 1004 if (0 ==
1005 CFNumberGetValue (refreshRateCFNumber, kCFNumberDoubleType,
1006 &refreshRate)) {
954 SDL_SetError ("Error getting refresh rate"); 1007 SDL_SetError ("Error getting refresh rate");
955 goto ERROR; 1008 goto ERROR;
956 } 1009 }
957 1010
958 if ( 0 == refreshRate ) { 1011 if (0 == refreshRate) {
959 1012
960 SDL_SetError ("Display has no refresh rate, using 60hz"); 1013 SDL_SetError ("Display has no refresh rate, using 60hz");
961 1014
962 /* ok, for LCD's we'll emulate a 60hz refresh, which may or may not look right */ 1015 /* ok, for LCD's we'll emulate a 60hz refresh, which may or may not look right */
963 refreshRate = 60.0; 1016 refreshRate = 60.0;
964 } 1017 }
965 1018
966 linesPerSecond = refreshRate * h; 1019 linesPerSecond = refreshRate * h;
967 target = h; 1020 target = h;
968 1021
969 /* Figure out the first delay so we start off about right */ 1022 /* Figure out the first delay so we start off about right */
970 position = CGDisplayBeamPosition (display_id); 1023 position = CGDisplayBeamPosition (display_id);
971 if (position > target) 1024 if (position > target)
972 position = 0; 1025 position = 0;
973 1026
974 adjustment = (target - position) / linesPerSecond; 1027 adjustment = (target - position) / linesPerSecond;
975 1028
976 nextTime = AddAbsoluteToAbsolute (UpTime (), QZ_SecondsToAbsolute (adjustment)); 1029 nextTime =
977 1030 AddAbsoluteToAbsolute (UpTime (),
1031 QZ_SecondsToAbsolute (adjustment));
1032
978 MPDelayUntil (&nextTime); 1033 MPDelayUntil (&nextTime);
979 } 1034 }
980 1035
981 1036
982 /* On error, skip VBL delay */ 1037 /* On error, skip VBL delay */
983 ERROR: 1038 ERROR:
984 1039
985 while ( h-- ) { 1040 while (h--) {
986 1041
987 SDL_memcpy (dst, src, len); 1042 SDL_memcpy (dst, src, len);
988 src += skip; 1043 src += skip;
989 dst += skip; 1044 dst += skip;
990 } 1045 }
991 1046
992 /* signal flip completion */ 1047 /* signal flip completion */
993 SDL_SemPost (sem2); 1048 SDL_SemPost (sem2);
994 } 1049 }
995 1050
996 return 0; 1051 return 0;
997 } 1052 }
998 1053
999 static int QZ_FlipDoubleBuffer (_THIS, SDL_Surface *surface) { 1054 static int
1055 QZ_FlipDoubleBuffer (_THIS, SDL_Surface * surface)
1056 {
1000 1057
1001 /* wait for previous flip to complete */ 1058 /* wait for previous flip to complete */
1002 SDL_SemWait (sem2); 1059 SDL_SemWait (sem2);
1003 1060
1004 current_buffer = surface->pixels; 1061 current_buffer = surface->pixels;
1005 1062
1006 if (surface->pixels == sw_buffers[0]) 1063 if (surface->pixels == sw_buffers[0])
1007 surface->pixels = sw_buffers[1]; 1064 surface->pixels = sw_buffers[1];
1008 else 1065 else
1009 surface->pixels = sw_buffers[0]; 1066 surface->pixels = sw_buffers[0];
1010 1067
1011 /* signal worker thread to do the flip */ 1068 /* signal worker thread to do the flip */
1012 SDL_SemPost (sem1); 1069 SDL_SemPost (sem1);
1013 1070
1014 return 0; 1071 return 0;
1015 } 1072 }
1016 1073
1017 1074
1018 static void QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects) { 1075 static void
1076 QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect * rects)
1077 {
1019 1078
1020 /* perform a flip if someone calls updaterects on a doublebuferred surface */ 1079 /* perform a flip if someone calls updaterects on a doublebuferred surface */
1021 this->FlipHWSurface (this, SDL_VideoSurface); 1080 this->FlipHWSurface (this, SDL_VideoSurface);
1022 } 1081 }
1023 1082
1024 static void QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect *rects) { 1083 static void
1084 QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect * rects)
1085 {
1025 #pragma unused(this,num_rects,rects) 1086 #pragma unused(this,num_rects,rects)
1026 } 1087 }
1027 1088
1028 /* 1089 /*
1029 The obscured code is based on work by Matt Slot fprefect@ambrosiasw.com, 1090 The obscured code is based on work by Matt Slot fprefect@ambrosiasw.com,
1034 1095
1035 #if TEST_OBSCURED 1096 #if TEST_OBSCURED
1036 #include "CGS.h" 1097 #include "CGS.h"
1037 #endif 1098 #endif
1038 1099
1039 static int QZ_IsWindowObscured (NSWindow *window) { 1100 static int
1101 QZ_IsWindowObscured (NSWindow * window)
1102 {
1040 1103
1041 1104
1042 #if TEST_OBSCURED 1105 #if TEST_OBSCURED
1043 1106
1044 /* 1107 /*
1045 In order to determine if a direct copy to the screen is possible, 1108 In order to determine if a direct copy to the screen is possible,
1046 we must figure out if there are any windows covering ours (including shadows). 1109 we must figure out if there are any windows covering ours (including shadows).
1047 This can be done by querying the window server about the on screen 1110 This can be done by querying the window server about the on screen
1048 windows for their screen rectangle and window level. 1111 windows for their screen rectangle and window level.
1049 The procedure used below is puts accuracy before speed; however, it aims to call 1112 The procedure used below is puts accuracy before speed; however, it aims to call
1050 the window server the fewest number of times possible to keep things reasonable. 1113 the window server the fewest number of times possible to keep things reasonable.
1051 In my testing on a 300mhz G3, this routine typically takes < 2 ms. -DW 1114 In my testing on a 300mhz G3, this routine typically takes < 2 ms. -DW
1052 1115
1053 Notes: 1116 Notes:
1054 -Calls into the Window Server involve IPC which is slow. 1117 -Calls into the Window Server involve IPC which is slow.
1055 -Getting a rectangle seems slower than getting the window level 1118 -Getting a rectangle seems slower than getting the window level
1056 -The window list we get back is in sorted order, top to bottom 1119 -The window list we get back is in sorted order, top to bottom
1057 -On average, I suspect, most windows above ours are dock icon windows (hence optimization) 1120 -On average, I suspect, most windows above ours are dock icon windows (hence optimization)
1058 -Some windows above ours are always there, and cannot move or obscure us (menu bar) 1121 -Some windows above ours are always there, and cannot move or obscure us (menu bar)
1059 1122
1060 Bugs: 1123 Bugs:
1061 -no way (yet) to deactivate direct drawing when a window is dragged, 1124 -no way (yet) to deactivate direct drawing when a window is dragged,
1062 or suddenly obscured, so drawing continues and can produce garbage 1125 or suddenly obscured, so drawing continues and can produce garbage
1063 We need some kind of locking mechanism on window movement to prevent this 1126 We need some kind of locking mechanism on window movement to prevent this
1064 1127
1065 -deactivated normal windows use activated normal 1128 -deactivated normal windows use activated normal
1066 window shadows (slight inaccuraccy) 1129 window shadows (slight inaccuraccy)
1067 */ 1130 */
1068 1131
1069 /* Cache the connection to the window server */ 1132 /* Cache the connection to the window server */
1070 static CGSConnectionID cgsConnection = (CGSConnectionID) -1; 1133 static CGSConnectionID cgsConnection = (CGSConnectionID) - 1;
1071 1134
1072 /* Cache the dock icon windows */ 1135 /* Cache the dock icon windows */
1073 static CGSWindowID dockIcons[kMaxWindows]; 1136 static CGSWindowID dockIcons[kMaxWindows];
1074 static int numCachedDockIcons = 0; 1137 static int numCachedDockIcons = 0;
1075 1138
1076 CGSWindowID windows[kMaxWindows]; 1139 CGSWindowID windows[kMaxWindows];
1077 CGSWindowCount i, count; 1140 CGSWindowCount i, count;
1078 CGSWindowLevel winLevel; 1141 CGSWindowLevel winLevel;
1079 CGSRect winRect; 1142 CGSRect winRect;
1080 1143
1081 CGSRect contentRect; 1144 CGSRect contentRect;
1082 int windowNumber; 1145 int windowNumber;
1083 int firstDockIcon; 1146 int firstDockIcon;
1084 int dockIconCacheMiss; 1147 int dockIconCacheMiss;
1085 int windowContentOffset; 1148 int windowContentOffset;
1086 1149
1087 int obscured = SDL_TRUE; 1150 int obscured = SDL_TRUE;
1088 1151
1089 if ( [ window isVisible ] ) { 1152 if ([window isVisible]) {
1090 1153
1091 /* 1154 /*
1092 walk the window list looking for windows over top of 1155 walk the window list looking for windows over top of
1093 (or casting a shadow on) ours 1156 (or casting a shadow on) ours
1094 */ 1157 */
1095 1158
1096 /* 1159 /*
1097 Get a connection to the window server 1160 Get a connection to the window server
1098 Should probably be moved out into SetVideoMode() or InitVideo() 1161 Should probably be moved out into SetVideoMode() or InitVideo()
1099 */ 1162 */
1100 if (cgsConnection == (CGSConnectionID) -1) { 1163 if (cgsConnection == (CGSConnectionID) - 1) {
1101 cgsConnection = (CGSConnectionID) 0; 1164 cgsConnection = (CGSConnectionID) 0;
1102 cgsConnection = _CGSDefaultConnection (); 1165 cgsConnection = _CGSDefaultConnection ();
1103 } 1166 }
1104 1167
1105 if (cgsConnection) { 1168 if (cgsConnection) {
1106 1169
1107 if ( ! [ window styleMask ] & NSBorderlessWindowMask ) 1170 if (![window styleMask] & NSBorderlessWindowMask)
1108 windowContentOffset = 22; 1171 windowContentOffset = 22;
1109 else 1172 else
1110 windowContentOffset = 0; 1173 windowContentOffset = 0;
1111 1174
1112 windowNumber = [ window windowNumber ]; 1175 windowNumber =[window windowNumber];
1113 1176
1114 /* The window list is sorted according to order on the screen */ 1177 /* The window list is sorted according to order on the screen */
1115 count = 0; 1178 count = 0;
1116 CGSGetOnScreenWindowList (cgsConnection, 0, kMaxWindows, windows, &count); 1179 CGSGetOnScreenWindowList (cgsConnection, 0, kMaxWindows,
1117 CGSGetScreenRectForWindow (cgsConnection, windowNumber, &contentRect); 1180 windows, &count);
1181 CGSGetScreenRectForWindow (cgsConnection, windowNumber,
1182 &contentRect);
1118 1183
1119 /* adjust rect for window title bar (if present) */ 1184 /* adjust rect for window title bar (if present) */
1120 contentRect.origin.y += windowContentOffset; 1185 contentRect.origin.y += windowContentOffset;
1121 contentRect.size.height -= windowContentOffset; 1186 contentRect.size.height -= windowContentOffset;
1122 1187
1123 firstDockIcon = -1; 1188 firstDockIcon = -1;
1124 dockIconCacheMiss = SDL_FALSE; 1189 dockIconCacheMiss = SDL_FALSE;
1125 1190
1126 /* 1191 /*
1127 The first window is always an empty window with level kCGSWindowLevelTop 1192 The first window is always an empty window with level kCGSWindowLevelTop
1128 so start at index 1 1193 so start at index 1
1129 */ 1194 */
1130 for (i = 1; i < count; i++) { 1195 for (i = 1; i < count; i++) {
1131 1196
1132 /* If we reach our window in the list, it cannot be obscured */ 1197 /* If we reach our window in the list, it cannot be obscured */
1133 if (windows[i] == windowNumber) { 1198 if (windows[i] == windowNumber) {
1134 1199
1135 obscured = SDL_FALSE; 1200 obscured = SDL_FALSE;
1136 break; 1201 break;
1137 } 1202 } else {
1138 else {
1139 1203
1140 float shadowSide; 1204 float shadowSide;
1141 float shadowTop; 1205 float shadowTop;
1142 float shadowBottom; 1206 float shadowBottom;
1143 1207
1173 1237
1174 } 1238 }
1175 } 1239 }
1176 1240
1177 continue; 1241 continue;
1178 } 1242 } else if (winLevel == kCGSWindowLevelMenuIgnore
1179 else if (winLevel == kCGSWindowLevelMenuIgnore 1243 /* winLevel == kCGSWindowLevelTop */ ) {
1180 /* winLevel == kCGSWindowLevelTop */) { 1244
1181 1245 continue; /* cannot obscure window */
1182 continue; /* cannot obscure window */ 1246 } else if (winLevel == kCGSWindowLevelDockMenu ||
1183 } 1247 winLevel == kCGSWindowLevelMenu) {
1184 else if (winLevel == kCGSWindowLevelDockMenu ||
1185 winLevel == kCGSWindowLevelMenu) {
1186 1248
1187 shadowSide = 18; 1249 shadowSide = 18;
1188 shadowTop = 4; 1250 shadowTop = 4;
1189 shadowBottom = 22; 1251 shadowBottom = 22;
1190 } 1252 } else if (winLevel == kCGSWindowLevelUtility) {
1191 else if (winLevel == kCGSWindowLevelUtility) {
1192 1253
1193 shadowSide = 8; 1254 shadowSide = 8;
1194 shadowTop = 4; 1255 shadowTop = 4;
1195 shadowBottom = 12; 1256 shadowBottom = 12;
1196 } 1257 } else if (winLevel == kCGSWindowLevelNormal) {
1197 else if (winLevel == kCGSWindowLevelNormal) {
1198 1258
1199 /* 1259 /*
1200 These numbers are for foreground windows, 1260 These numbers are for foreground windows,
1201 they are too big (but will work) for background windows 1261 they are too big (but will work) for background windows
1202 */ 1262 */
1203 shadowSide = 20; 1263 shadowSide = 20;
1204 shadowTop = 10; 1264 shadowTop = 10;
1205 shadowBottom = 24; 1265 shadowBottom = 24;
1206 } 1266 } else if (winLevel == kCGSWindowLevelDock) {
1207 else if (winLevel == kCGSWindowLevelDock) {
1208 1267
1209 /* Create dock icon cache */ 1268 /* Create dock icon cache */
1210 if (numCachedDockIcons != (i-firstDockIcon) || 1269 if (numCachedDockIcons != (i - firstDockIcon)
1211 dockIconCacheMiss) { 1270 || dockIconCacheMiss) {
1212 1271
1213 numCachedDockIcons = i - firstDockIcon; 1272 numCachedDockIcons = i - firstDockIcon;
1214 SDL_memcpy (dockIcons, &(windows[firstDockIcon]), 1273 SDL_memcpy (dockIcons,
1215 numCachedDockIcons * sizeof(*windows)); 1274 &(windows[firstDockIcon]),
1275 numCachedDockIcons *
1276 sizeof (*windows));
1216 } 1277 }
1278
1279 /* no shadow */
1280 shadowSide = 0;
1281 shadowTop = 0;
1282 shadowBottom = 0;
1283 } else {
1284
1285 /*
1286 kCGSWindowLevelDockLabel,
1287 kCGSWindowLevelDock,
1288 kOther???
1289 */
1217 1290
1218 /* no shadow */ 1291 /* no shadow */
1219 shadowSide = 0; 1292 shadowSide = 0;
1220 shadowTop = 0; 1293 shadowTop = 0;
1221 shadowBottom = 0; 1294 shadowBottom = 0;
1222 } 1295 }
1223 else { 1296
1224 1297 CGSGetScreenRectForWindow (cgsConnection,
1225 /* 1298 windows[i], &winRect);
1226 kCGSWindowLevelDockLabel,
1227 kCGSWindowLevelDock,
1228 kOther???
1229 */
1230
1231 /* no shadow */
1232 shadowSide = 0;
1233 shadowTop = 0;
1234 shadowBottom = 0;
1235 }
1236
1237 CGSGetScreenRectForWindow (cgsConnection, windows[i], &winRect);
1238 1299
1239 winRect.origin.x -= shadowSide; 1300 winRect.origin.x -= shadowSide;
1240 winRect.origin.y -= shadowTop; 1301 winRect.origin.y -= shadowTop;
1241 winRect.size.width += shadowSide; 1302 winRect.size.width += shadowSide;
1242 winRect.size.height += shadowBottom; 1303 winRect.size.height += shadowBottom;
1245 1306
1246 obscured = SDL_TRUE; 1307 obscured = SDL_TRUE;
1247 break; 1308 break;
1248 } 1309 }
1249 1310
1250 } /* window was not our window */ 1311 } /* window was not our window */
1251 1312
1252 } /* iterate over windows */ 1313 } /* iterate over windows */
1253 1314
1254 } /* get cgsConnection */ 1315 }
1255 1316 /* get cgsConnection */
1256 } /* window is visible */ 1317 }
1257 1318 /* window is visible */
1258 return obscured; 1319 return obscured;
1259 #else 1320 #else
1260 return SDL_TRUE; 1321 return SDL_TRUE;
1261 #endif 1322 #endif
1262 } 1323 }
1263 1324
1264 1325
1265 /* Locking functions for the software window buffer */ 1326 /* Locking functions for the software window buffer */
1266 static int QZ_LockWindow (_THIS, SDL_Surface *surface) { 1327 static int
1267 1328 QZ_LockWindow (_THIS, SDL_Surface * surface)
1268 return LockPortBits ( [ window_view qdPort ] ); 1329 {
1269 } 1330
1270 1331 return LockPortBits ([window_view qdPort]);
1271 static void QZ_UnlockWindow (_THIS, SDL_Surface *surface) { 1332 }
1272 1333
1273 UnlockPortBits ( [ window_view qdPort ] ); 1334 static void
1335 QZ_UnlockWindow (_THIS, SDL_Surface * surface)
1336 {
1337
1338 UnlockPortBits ([window_view qdPort]);
1274 } 1339 }
1275 1340
1276 /* Resize icon, BMP format */ 1341 /* Resize icon, BMP format */
1277 static const unsigned char QZ_ResizeIcon[] = { 1342 static const unsigned char QZ_ResizeIcon[] = {
1278 0x42,0x4d,0x31,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00, 1343 0x42, 0x4d, 0x31, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00,
1279 0x00,0x00,0x0d,0x00,0x00,0x00,0x0d,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00, 1344 0x00, 0x00, 0x28, 0x00,
1280 0x00,0x00,0xfb,0x01,0x00,0x00,0x13,0x0b,0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00, 1345 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x00,
1281 0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1346 0x18, 0x00, 0x00, 0x00,
1282 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1347 0x00, 0x00, 0xfb, 0x01, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x00, 0x13, 0x0b,
1283 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff, 1348 0x00, 0x00, 0x00, 0x00,
1284 0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda, 1349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1285 0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8, 1350 0xff, 0xff, 0xff, 0xff,
1286 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xda,0xda,0xda,0x87, 1351 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1287 0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8, 1352 0xff, 0xff, 0xff, 0xff,
1288 0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff, 1353 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1289 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd5,0xd5,0xd5,0x87,0x87,0x87,0xe8,0xe8,0xe8, 1354 0xff, 0x0b, 0xff, 0xff,
1290 0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda, 1355 0xff, 0xda, 0xda, 0xda, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff,
1291 0xda,0xda,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1356 0xff, 0xda, 0xda, 0xda,
1292 0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda, 1357 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff, 0xff, 0xda, 0xda, 0xda,
1293 0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff, 1358 0x87, 0x87, 0x87, 0xe8,
1294 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7, 1359 0xe8, 0xe8, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1295 0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8, 1360 0xda, 0xda, 0xda, 0x87,
1296 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1361 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff, 0xff, 0xda, 0xda, 0xda, 0x87,
1297 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8, 1362 0x87, 0x87, 0xe8, 0xe8,
1298 0xe8,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff, 1363 0xe8, 0xff, 0xff, 0xff, 0xda, 0xda, 0xda, 0x87, 0x87, 0x87, 0xff, 0xff,
1299 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1364 0xff, 0x0b, 0xff, 0xff,
1300 0xff,0xff,0xff,0xd9,0xd9,0xd9,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xdc, 1365 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd5, 0xd5, 0xd5, 0x87, 0x87,
1301 0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1366 0x87, 0xe8, 0xe8, 0xe8,
1302 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb, 1367 0xff, 0xff, 0xff, 0xda, 0xda, 0xda, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8,
1303 0xdb,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff, 1368 0xff, 0xff, 0xff, 0xda,
1304 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1369 0xda, 0xda, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1305 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,0xdb,0x87,0x87,0x87,0xe8, 1370 0xff, 0xff, 0xff, 0xff,
1306 0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1371 0xff, 0xff, 0xd7, 0xd7, 0xd7, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff,
1307 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1372 0xff, 0xff, 0xda, 0xda,
1308 0xff,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff, 1373 0xda, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff, 0xff, 0xff, 0xff,
1309 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1374 0xff, 0x0b, 0xff, 0xff,
1310 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdc, 1375 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1311 0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1376 0xff, 0xd7, 0xd7, 0xd7,
1312 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 1377 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff, 0xff, 0xda, 0xda, 0xda,
1313 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b 1378 0x87, 0x87, 0x87, 0xe8,
1379 0xe8, 0xe8, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1380 0xff, 0xff, 0xff, 0xff,
1381 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd7, 0xd7, 0xd7, 0x87,
1382 0x87, 0x87, 0xe8, 0xe8,
1383 0xe8, 0xff, 0xff, 0xff, 0xdc, 0xdc, 0xdc, 0x87, 0x87, 0x87, 0xff, 0xff,
1384 0xff, 0x0b, 0xff, 0xff,
1385 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1386 0xff, 0xff, 0xff, 0xff,
1387 0xff, 0xff, 0xff, 0xd9, 0xd9, 0xd9, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8,
1388 0xff, 0xff, 0xff, 0xdc,
1389 0xdc, 0xdc, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1390 0xff, 0xff, 0xff, 0xff,
1391 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1392 0xff, 0xff, 0xdb, 0xdb,
1393 0xdb, 0x87, 0x87, 0x87, 0xe8, 0xe8, 0xe8, 0xff, 0xff, 0xff, 0xff, 0xff,
1394 0xff, 0x0b, 0xff, 0xff,
1395 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1396 0xff, 0xff, 0xff, 0xff,
1397 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0xdb, 0xdb,
1398 0x87, 0x87, 0x87, 0xe8,
1399 0xe8, 0xe8, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1400 0xff, 0xff, 0xff, 0xff,
1401 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1402 0xff, 0xff, 0xff, 0xff,
1403 0xff, 0xff, 0xff, 0xff, 0xdc, 0xdc, 0xdc, 0x87, 0x87, 0x87, 0xff, 0xff,
1404 0xff, 0x0b, 0xff, 0xff,
1405 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1406 0xff, 0xff, 0xff, 0xff,
1407 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1408 0xff, 0xff, 0xff, 0xdc,
1409 0xdc, 0xdc, 0xff, 0xff, 0xff, 0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1410 0xff, 0xff, 0xff, 0xff,
1411 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1412 0xff, 0xff, 0xff, 0xff,
1413 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1414 0xff, 0x0b
1314 }; 1415 };
1315 1416
1316 static void QZ_DrawResizeIcon (_THIS, RgnHandle dirtyRegion) { 1417 static void
1418 QZ_DrawResizeIcon (_THIS, RgnHandle dirtyRegion)
1419 {
1317 1420
1318 /* Check if we should draw the resize icon */ 1421 /* Check if we should draw the resize icon */
1319 if (SDL_VideoSurface->flags & SDL_RESIZABLE) { 1422 if (SDL_VideoSurface->flags & SDL_RESIZABLE) {
1320 1423
1321 Rect icon; 1424 Rect icon;
1322 SetRect (&icon, SDL_VideoSurface->w - 13, SDL_VideoSurface->h - 13, 1425 SetRect (&icon, SDL_VideoSurface->w - 13, SDL_VideoSurface->h - 13,
1323 SDL_VideoSurface->w, SDL_VideoSurface->h); 1426 SDL_VideoSurface->w, SDL_VideoSurface->h);
1324 1427
1325 if (RectInRgn (&icon, dirtyRegion)) { 1428 if (RectInRgn (&icon, dirtyRegion)) {
1326 1429
1327 SDL_Rect icon_rect; 1430 SDL_Rect icon_rect;
1328 1431
1329 /* Create the icon image */ 1432 /* Create the icon image */
1330 if (resize_icon == NULL) { 1433 if (resize_icon == NULL) {
1331 1434
1332 SDL_RWops *rw; 1435 SDL_RWops *rw;
1333 SDL_Surface *tmp; 1436 SDL_Surface *tmp;
1334 1437
1335 rw = SDL_RWFromConstMem (QZ_ResizeIcon, sizeof(QZ_ResizeIcon)); 1438 rw = SDL_RWFromConstMem (QZ_ResizeIcon,
1439 sizeof (QZ_ResizeIcon));
1336 tmp = SDL_LoadBMP_RW (rw, SDL_TRUE); 1440 tmp = SDL_LoadBMP_RW (rw, SDL_TRUE);
1337 1441
1338 resize_icon = SDL_ConvertSurface (tmp, SDL_VideoSurface->format, SDL_SRCCOLORKEY); 1442 resize_icon =
1443 SDL_ConvertSurface (tmp, SDL_VideoSurface->format,
1444 SDL_SRCCOLORKEY);
1339 SDL_SetColorKey (resize_icon, SDL_SRCCOLORKEY, 0xFFFFFF); 1445 SDL_SetColorKey (resize_icon, SDL_SRCCOLORKEY, 0xFFFFFF);
1340 1446
1341 SDL_FreeSurface (tmp); 1447 SDL_FreeSurface (tmp);
1342 } 1448 }
1343 1449
1344 icon_rect.x = SDL_VideoSurface->w - 13; 1450 icon_rect.x = SDL_VideoSurface->w - 13;
1345 icon_rect.y = SDL_VideoSurface->h - 13; 1451 icon_rect.y = SDL_VideoSurface->h - 13;
1346 icon_rect.w = 13; 1452 icon_rect.w = 13;
1347 icon_rect.h = 13; 1453 icon_rect.h = 13;
1348 1454
1349 SDL_BlitSurface (resize_icon, NULL, SDL_VideoSurface, &icon_rect); 1455 SDL_BlitSurface (resize_icon, NULL, SDL_VideoSurface, &icon_rect);
1350 } 1456 }
1351 } 1457 }
1352 } 1458 }
1353 1459
1354 static void QZ_UpdateRects (_THIS, int numRects, SDL_Rect *rects) { 1460 static void
1355 1461 QZ_UpdateRects (_THIS, int numRects, SDL_Rect * rects)
1356 if ( [ qz_window isMiniaturized ] ) { 1462 {
1357 1463
1464 if ([qz_window isMiniaturized]) {
1465
1358 /* Do nothing if miniaturized */ 1466 /* Do nothing if miniaturized */
1359 } 1467 }
1360 1468
1361 else if ( ! QZ_IsWindowObscured (qz_window) ) { 1469 else if (!QZ_IsWindowObscured (qz_window)) {
1362 1470
1363 /* Use direct copy to flush contents to the display */ 1471 /* Use direct copy to flush contents to the display */
1364 CGrafPtr savePort; 1472 CGrafPtr savePort;
1365 CGrafPtr dstPort, srcPort; 1473 CGrafPtr dstPort, srcPort;
1366 const BitMap *dstBits, *srcBits; 1474 const BitMap *dstBits, *srcBits;
1367 Rect dstRect, srcRect; 1475 Rect dstRect, srcRect;
1368 Point offset; 1476 Point offset;
1369 int i; 1477 int i;
1370 1478
1371 GetPort (&savePort); 1479 GetPort (&savePort);
1372 1480
1373 dstPort = CreateNewPortForCGDisplayID ((UInt32)display_id); 1481 dstPort = CreateNewPortForCGDisplayID ((UInt32) display_id);
1374 srcPort = [ window_view qdPort ]; 1482 srcPort =[window_view qdPort];
1375 1483
1376 offset.h = 0; 1484 offset.h = 0;
1377 offset.v = 0; 1485 offset.v = 0;
1378 SetPort (srcPort); 1486 SetPort (srcPort);
1379 LocalToGlobal (&offset); 1487 LocalToGlobal (&offset);
1387 srcBits = GetPortBitMapForCopyBits (srcPort); 1495 srcBits = GetPortBitMapForCopyBits (srcPort);
1388 1496
1389 for (i = 0; i < numRects; i++) { 1497 for (i = 0; i < numRects; i++) {
1390 1498
1391 SetRect (&srcRect, rects[i].x, rects[i].y, 1499 SetRect (&srcRect, rects[i].x, rects[i].y,
1392 rects[i].x + rects[i].w, 1500 rects[i].x + rects[i].w, rects[i].y + rects[i].h);
1393 rects[i].y + rects[i].h);
1394 1501
1395 SetRect (&dstRect, 1502 SetRect (&dstRect,
1396 rects[i].x + offset.h, 1503 rects[i].x + offset.h,
1397 rects[i].y + offset.v, 1504 rects[i].y + offset.v,
1398 rects[i].x + rects[i].w + offset.h, 1505 rects[i].x + rects[i].w + offset.h,
1399 rects[i].y + rects[i].h + offset.v); 1506 rects[i].y + rects[i].h + offset.v);
1400 1507
1401 CopyBits (srcBits, dstBits, 1508 CopyBits (srcBits, dstBits, &srcRect, &dstRect, srcCopy, NULL);
1402 &srcRect, &dstRect, srcCopy, NULL);
1403 1509
1404 } 1510 }
1405 1511
1406 SetPort (savePort); 1512 SetPort (savePort);
1407 } 1513 } else {
1408 else {
1409 /* Use QDFlushPortBuffer() to flush content to display */ 1514 /* Use QDFlushPortBuffer() to flush content to display */
1410 int i; 1515 int i;
1411 RgnHandle dirty = NewRgn (); 1516 RgnHandle dirty = NewRgn ();
1412 RgnHandle temp = NewRgn (); 1517 RgnHandle temp = NewRgn ();
1413 1518
1414 SetEmptyRgn (dirty); 1519 SetEmptyRgn (dirty);
1415 1520
1416 /* Build the region of dirty rectangles */ 1521 /* Build the region of dirty rectangles */
1417 for (i = 0; i < numRects; i++) { 1522 for (i = 0; i < numRects; i++) {
1418 1523
1419 MacSetRectRgn (temp, rects[i].x, rects[i].y, 1524 MacSetRectRgn (temp, rects[i].x, rects[i].y,
1420 rects[i].x + rects[i].w, rects[i].y + rects[i].h); 1525 rects[i].x + rects[i].w, rects[i].y + rects[i].h);
1421 MacUnionRgn (dirty, temp, dirty); 1526 MacUnionRgn (dirty, temp, dirty);
1422 } 1527 }
1423 1528
1424 QZ_DrawResizeIcon (this, dirty); 1529 QZ_DrawResizeIcon (this, dirty);
1425 1530
1426 /* Flush the dirty region */ 1531 /* Flush the dirty region */
1427 QDFlushPortBuffer ( [ window_view qdPort ], dirty ); 1532 QDFlushPortBuffer ([window_view qdPort], dirty);
1428 DisposeRgn (dirty); 1533 DisposeRgn (dirty);
1429 DisposeRgn (temp); 1534 DisposeRgn (temp);
1430 } 1535 }
1431 } 1536 }
1432 1537
1433 static void QZ_VideoQuit (_THIS) { 1538 static void
1434 1539 QZ_VideoQuit (_THIS)
1435 CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken; 1540 {
1541
1542 CGDisplayFadeReservationToken fade_token =
1543 kCGDisplayFadeReservationInvalidToken;
1436 1544
1437 /* Restore gamma settings */ 1545 /* Restore gamma settings */
1438 CGDisplayRestoreColorSyncSettings (); 1546 CGDisplayRestoreColorSyncSettings ();
1439 1547
1440 /* Ensure the cursor will be visible and working when we quit */ 1548 /* Ensure the cursor will be visible and working when we quit */
1441 CGDisplayShowCursor (display_id); 1549 CGDisplayShowCursor (display_id);
1442 CGAssociateMouseAndMouseCursorPosition (1); 1550 CGAssociateMouseAndMouseCursorPosition (1);
1443 1551
1444 if (mode_flags & SDL_FULLSCREEN) { 1552 if (mode_flags & SDL_FULLSCREEN) {
1445 /* Fade to black to hide resolution-switching flicker (and garbage 1553 /* Fade to black to hide resolution-switching flicker (and garbage
1446 that is displayed by a destroyed OpenGL context, if applicable) */ 1554 that is displayed by a destroyed OpenGL context, if applicable) */
1447 if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) { 1555 if (CGAcquireDisplayFadeReservation (5, &fade_token) ==
1448 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE); 1556 kCGErrorSuccess) {
1557 CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal,
1558 kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
1449 } 1559 }
1450 QZ_UnsetVideoMode (this, TRUE); 1560 QZ_UnsetVideoMode (this, TRUE);
1451 if (fade_token != kCGDisplayFadeReservationInvalidToken) { 1561 if (fade_token != kCGDisplayFadeReservationInvalidToken) {
1452 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE); 1562 CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor,
1563 kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
1453 CGReleaseDisplayFadeReservation (fade_token); 1564 CGReleaseDisplayFadeReservation (fade_token);
1454 } 1565 }
1455 } 1566 } else
1456 else
1457 QZ_UnsetVideoMode (this, TRUE); 1567 QZ_UnsetVideoMode (this, TRUE);
1458 1568
1459 CGPaletteRelease (palette); 1569 CGPaletteRelease (palette);
1460 1570
1461 if (opengl_library) { 1571 if (opengl_library) {
1462 SDL_UnloadObject(opengl_library); 1572 SDL_UnloadObject (opengl_library);
1463 opengl_library = NULL; 1573 opengl_library = NULL;
1464 } 1574 }
1465 this->gl_config.driver_loaded = 0; 1575 this->gl_config.driver_loaded = 0;
1466 } 1576 }
1467 1577
1468 #if 0 /* Not used (apparently, it's really slow) */ 1578 #if 0 /* Not used (apparently, it's really slow) */
1469 static int QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color) { 1579 static int
1580 QZ_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect, Uint32 color)
1581 {
1470 1582
1471 CGSDisplayHWFill (display_id, rect->x, rect->y, rect->w, rect->h, color); 1583 CGSDisplayHWFill (display_id, rect->x, rect->y, rect->w, rect->h, color);
1472 1584
1473 return 0; 1585 return 0;
1474 } 1586 }
1475 #endif 1587 #endif
1476 1588
1477 static int QZ_LockHWSurface(_THIS, SDL_Surface *surface) { 1589 static int
1590 QZ_LockHWSurface (_THIS, SDL_Surface * surface)
1591 {
1478 1592
1479 return 1; 1593 return 1;
1480 } 1594 }
1481 1595
1482 static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface) { 1596 static void
1483 1597 QZ_UnlockHWSurface (_THIS, SDL_Surface * surface)
1484 } 1598 {
1485 1599
1486 static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface) { 1600 }
1487 return(-1); /* unallowed (no HWSURFACE support here). */ 1601
1488 } 1602 static int
1489 1603 QZ_AllocHWSurface (_THIS, SDL_Surface * surface)
1490 static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface) { 1604 {
1605 return (-1); /* unallowed (no HWSURFACE support here). */
1606 }
1607
1608 static void
1609 QZ_FreeHWSurface (_THIS, SDL_Surface * surface)
1610 {
1491 } 1611 }
1492 1612
1493 /* 1613 /*
1494 int QZ_FlipHWSurface (_THIS, SDL_Surface *surface) { 1614 int QZ_FlipHWSurface (_THIS, SDL_Surface *surface) {
1495 return 0; 1615 return 0;
1496 } 1616 }
1497 */ 1617 */
1498 1618
1499 /* Gamma functions */ 1619 /* Gamma functions */
1500 int QZ_SetGamma (_THIS, float red, float green, float blue) { 1620 int
1621 QZ_SetGamma (_THIS, float red, float green, float blue)
1622 {
1501 1623
1502 const CGGammaValue min = 0.0, max = 1.0; 1624 const CGGammaValue min = 0.0, max = 1.0;
1503 1625
1504 if (red == 0.0) 1626 if (red == 0.0)
1505 red = FLT_MAX; 1627 red = FLT_MAX;
1512 green = 1.0 / green; 1634 green = 1.0 / green;
1513 1635
1514 if (blue == 0.0) 1636 if (blue == 0.0)
1515 blue = FLT_MAX; 1637 blue = FLT_MAX;
1516 else 1638 else
1517 blue = 1.0 / blue; 1639 blue = 1.0 / blue;
1518 1640
1519 if ( CGDisplayNoErr == CGSetDisplayTransferByFormula 1641 if (CGDisplayNoErr == CGSetDisplayTransferByFormula
1520 (display_id, min, max, red, min, max, green, min, max, blue) ) { 1642 (display_id, min, max, red, min, max, green, min, max, blue)) {
1521 1643
1522 return 0; 1644 return 0;
1523 } 1645 } else {
1524 else {
1525 1646
1526 return -1; 1647 return -1;
1527 } 1648 }
1528 } 1649 }
1529 1650
1530 int QZ_GetGamma (_THIS, float *red, float *green, float *blue) { 1651 int
1652 QZ_GetGamma (_THIS, float *red, float *green, float *blue)
1653 {
1531 1654
1532 CGGammaValue dummy; 1655 CGGammaValue dummy;
1533 if ( CGDisplayNoErr == CGGetDisplayTransferByFormula 1656 if (CGDisplayNoErr == CGGetDisplayTransferByFormula
1534 (display_id, &dummy, &dummy, red, 1657 (display_id, &dummy, &dummy, red,
1535 &dummy, &dummy, green, &dummy, &dummy, blue) ) 1658 &dummy, &dummy, green, &dummy, &dummy, blue))
1536 1659
1537 return 0; 1660 return 0;
1538 else 1661 else
1539 return -1; 1662 return -1;
1540 } 1663 }
1541 1664
1542 int QZ_SetGammaRamp (_THIS, Uint16 *ramp) { 1665 int
1666 QZ_SetGammaRamp (_THIS, Uint16 * ramp)
1667 {
1543 1668
1544 const CGTableCount tableSize = 255; 1669 const CGTableCount tableSize = 255;
1545 CGGammaValue redTable[tableSize]; 1670 CGGammaValue redTable[tableSize];
1546 CGGammaValue greenTable[tableSize]; 1671 CGGammaValue greenTable[tableSize];
1547 CGGammaValue blueTable[tableSize]; 1672 CGGammaValue blueTable[tableSize];
1550 1675
1551 /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */ 1676 /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
1552 for (i = 0; i < 256; i++) 1677 for (i = 0; i < 256; i++)
1553 redTable[i % 256] = ramp[i] / 65535.0; 1678 redTable[i % 256] = ramp[i] / 65535.0;
1554 1679
1555 for (i=256; i < 512; i++) 1680 for (i = 256; i < 512; i++)
1556 greenTable[i % 256] = ramp[i] / 65535.0; 1681 greenTable[i % 256] = ramp[i] / 65535.0;
1557 1682
1558 for (i=512; i < 768; i++) 1683 for (i = 512; i < 768; i++)
1559 blueTable[i % 256] = ramp[i] / 65535.0; 1684 blueTable[i % 256] = ramp[i] / 65535.0;
1560 1685
1561 if ( CGDisplayNoErr == CGSetDisplayTransferByTable 1686 if (CGDisplayNoErr == CGSetDisplayTransferByTable
1562 (display_id, tableSize, redTable, greenTable, blueTable) ) 1687 (display_id, tableSize, redTable, greenTable, blueTable))
1563 return 0; 1688 return 0;
1564 else 1689 else
1565 return -1; 1690 return -1;
1566 } 1691 }
1567 1692
1568 int QZ_GetGammaRamp (_THIS, Uint16 *ramp) { 1693 int
1694 QZ_GetGammaRamp (_THIS, Uint16 * ramp)
1695 {
1569 1696
1570 const CGTableCount tableSize = 255; 1697 const CGTableCount tableSize = 255;
1571 CGGammaValue redTable[tableSize]; 1698 CGGammaValue redTable[tableSize];
1572 CGGammaValue greenTable[tableSize]; 1699 CGGammaValue greenTable[tableSize];
1573 CGGammaValue blueTable[tableSize]; 1700 CGGammaValue blueTable[tableSize];
1574 CGTableCount actual; 1701 CGTableCount actual;
1575 int i; 1702 int i;
1576 1703
1577 if ( CGDisplayNoErr != CGGetDisplayTransferByTable 1704 if (CGDisplayNoErr != CGGetDisplayTransferByTable
1578 (display_id, tableSize, redTable, greenTable, blueTable, &actual) || 1705 (display_id, tableSize, redTable, greenTable, blueTable, &actual) ||
1579 actual != tableSize) 1706 actual != tableSize)
1580 1707
1581 return -1; 1708 return -1;
1582 1709
1583 /* Pack tables into one array, with values from 0 to 65535 */ 1710 /* Pack tables into one array, with values from 0 to 65535 */
1584 for (i = 0; i < 256; i++) 1711 for (i = 0; i < 256; i++)
1585 ramp[i] = redTable[i % 256] * 65535.0; 1712 ramp[i] = redTable[i % 256] * 65535.0;
1586 1713
1587 for (i=256; i < 512; i++) 1714 for (i = 256; i < 512; i++)
1588 ramp[i] = greenTable[i % 256] * 65535.0; 1715 ramp[i] = greenTable[i % 256] * 65535.0;
1589 1716
1590 for (i=512; i < 768; i++) 1717 for (i = 512; i < 768; i++)
1591 ramp[i] = blueTable[i % 256] * 65535.0; 1718 ramp[i] = blueTable[i % 256] * 65535.0;
1592 1719
1593 return 0; 1720 return 0;
1594 } 1721 }
1595