Mercurial > sdl-ios-xcode
annotate src/video/x11/SDL_x11video.c @ 236:3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 07 Nov 2001 17:59:07 +0000 |
parents | 2a8d929f50e0 |
children | e8157fcb3114 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* X11 based SDL video driver implementation. | |
29 Note: This implementation does not currently need X11 thread locking, | |
30 since the event thread uses a separate X connection and any | |
31 additional locking necessary is handled internally. However, | |
32 if full locking is neccessary, take a look at XInitThreads(). | |
33 */ | |
34 | |
35 #include <stdlib.h> | |
36 #include <stdio.h> | |
37 #include <unistd.h> | |
38 #include <string.h> | |
39 #include <sys/ioctl.h> | |
40 #ifdef MTRR_SUPPORT | |
41 #include <asm/mtrr.h> | |
42 #include <sys/fcntl.h> | |
43 #endif | |
44 | |
45 #ifdef HAVE_ALLOCA_H | |
46 #include <alloca.h> | |
47 #endif | |
48 | |
49 #ifdef HAVE_ALLOCA | |
50 #define ALLOCA(n) ((void*)alloca(n)) | |
51 #define FREEA(p) | |
52 #else | |
53 #define ALLOCA(n) malloc(n) | |
54 #define FREEA(p) free(p) | |
55 #endif | |
56 | |
57 #include "SDL.h" | |
58 #include "SDL_error.h" | |
59 #include "SDL_timer.h" | |
60 #include "SDL_thread.h" | |
61 #include "SDL_video.h" | |
62 #include "SDL_mouse.h" | |
63 #include "SDL_endian.h" | |
64 #include "SDL_sysvideo.h" | |
65 #include "SDL_pixels_c.h" | |
66 #include "SDL_events_c.h" | |
67 #include "SDL_x11video.h" | |
68 #include "SDL_x11wm_c.h" | |
69 #include "SDL_x11mouse_c.h" | |
70 #include "SDL_x11events_c.h" | |
71 #include "SDL_x11modes_c.h" | |
72 #include "SDL_x11image_c.h" | |
73 #include "SDL_x11yuv_c.h" | |
74 #include "SDL_x11gl_c.h" | |
75 #include "SDL_x11gamma_c.h" | |
76 #include "blank_cursor.h" | |
77 | |
78 /* Initialization/Query functions */ | |
79 static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat); | |
80 static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); | |
81 static int X11_ToggleFullScreen(_THIS, int on); | |
82 static void X11_UpdateMouse(_THIS); | |
83 static int X11_SetColors(_THIS, int firstcolor, int ncolors, | |
84 SDL_Color *colors); | |
85 static int X11_SetGammaRamp(_THIS, Uint16 *ramp); | |
86 static void X11_VideoQuit(_THIS); | |
87 | |
88 /* X11 driver bootstrap functions */ | |
89 | |
90 static int X11_Available(void) | |
91 { | |
92 Display *display; | |
93 | |
94 display = XOpenDisplay(NULL); | |
95 if ( display != NULL ) { | |
96 XCloseDisplay(display); | |
97 } | |
98 return(display != NULL); | |
99 } | |
100 | |
101 static void X11_DeleteDevice(SDL_VideoDevice *device) | |
102 { | |
103 if ( device ) { | |
104 if ( device->hidden ) { | |
105 free(device->hidden); | |
106 } | |
107 if ( device->gl_data ) { | |
108 free(device->gl_data); | |
109 } | |
110 free(device); | |
111 } | |
112 } | |
113 | |
114 static SDL_VideoDevice *X11_CreateDevice(int devindex) | |
115 { | |
116 SDL_VideoDevice *device; | |
117 | |
118 /* Initialize all variables that we clean on shutdown */ | |
119 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice)); | |
120 if ( device ) { | |
121 memset(device, 0, (sizeof *device)); | |
122 device->hidden = (struct SDL_PrivateVideoData *) | |
123 malloc((sizeof *device->hidden)); | |
124 device->gl_data = (struct SDL_PrivateGLData *) | |
125 malloc((sizeof *device->gl_data)); | |
126 } | |
127 if ( (device == NULL) || (device->hidden == NULL) || | |
128 (device->gl_data == NULL) ) { | |
129 SDL_OutOfMemory(); | |
130 X11_DeleteDevice(device); | |
131 return(0); | |
132 } | |
133 memset(device->hidden, 0, (sizeof *device->hidden)); | |
134 memset(device->gl_data, 0, (sizeof *device->gl_data)); | |
135 | |
136 /* Set the driver flags */ | |
137 device->handles_any_size = 1; | |
138 | |
139 /* Set the function pointers */ | |
140 device->VideoInit = X11_VideoInit; | |
141 device->ListModes = X11_ListModes; | |
142 device->SetVideoMode = X11_SetVideoMode; | |
143 device->ToggleFullScreen = X11_ToggleFullScreen; | |
144 device->UpdateMouse = X11_UpdateMouse; | |
145 #ifdef XFREE86_XV | |
146 device->CreateYUVOverlay = X11_CreateYUVOverlay; | |
147 #endif | |
148 device->SetColors = X11_SetColors; | |
149 device->UpdateRects = NULL; | |
150 device->VideoQuit = X11_VideoQuit; | |
151 device->AllocHWSurface = X11_AllocHWSurface; | |
152 device->CheckHWBlit = NULL; | |
153 device->FillHWRect = NULL; | |
154 device->SetHWColorKey = NULL; | |
155 device->SetHWAlpha = NULL; | |
156 device->LockHWSurface = X11_LockHWSurface; | |
157 device->UnlockHWSurface = X11_UnlockHWSurface; | |
158 device->FlipHWSurface = X11_FlipHWSurface; | |
159 device->FreeHWSurface = X11_FreeHWSurface; | |
160 device->SetGamma = X11_SetVidModeGamma; | |
161 device->GetGamma = X11_GetVidModeGamma; | |
162 device->SetGammaRamp = X11_SetGammaRamp; | |
163 device->GetGammaRamp = NULL; | |
164 #ifdef HAVE_OPENGL | |
165 device->GL_LoadLibrary = X11_GL_LoadLibrary; | |
166 device->GL_GetProcAddress = X11_GL_GetProcAddress; | |
167 device->GL_GetAttribute = X11_GL_GetAttribute; | |
168 device->GL_MakeCurrent = X11_GL_MakeCurrent; | |
169 device->GL_SwapBuffers = X11_GL_SwapBuffers; | |
170 #endif | |
171 device->SetCaption = X11_SetCaption; | |
172 device->SetIcon = X11_SetIcon; | |
173 device->IconifyWindow = X11_IconifyWindow; | |
174 device->GrabInput = X11_GrabInput; | |
175 device->GetWMInfo = X11_GetWMInfo; | |
176 device->FreeWMCursor = X11_FreeWMCursor; | |
177 device->CreateWMCursor = X11_CreateWMCursor; | |
178 device->ShowWMCursor = X11_ShowWMCursor; | |
179 device->WarpWMCursor = X11_WarpWMCursor; | |
180 device->CheckMouseMode = X11_CheckMouseMode; | |
181 device->InitOSKeymap = X11_InitOSKeymap; | |
182 device->PumpEvents = X11_PumpEvents; | |
183 | |
184 device->free = X11_DeleteDevice; | |
185 | |
186 return device; | |
187 } | |
188 | |
189 VideoBootStrap X11_bootstrap = { | |
190 "x11", "X Window System", | |
191 X11_Available, X11_CreateDevice | |
192 }; | |
193 | |
194 /* Shared memory information */ | |
195 extern int XShmQueryExtension(Display *dpy); /* Not in X11 headers */ | |
196 | |
197 /* Normal X11 error handler routine */ | |
198 static int (*X_handler)(Display *, XErrorEvent *) = NULL; | |
199 static int x_errhandler(Display *d, XErrorEvent *e) | |
200 { | |
201 #ifdef XFREE86_VM | |
202 extern int vm_error; | |
203 #endif | |
204 #ifdef XFREE86_DGAMOUSE | |
205 extern int dga_error; | |
206 #endif | |
207 | |
208 #ifdef XFREE86_VM | |
209 /* VidMode errors are non-fatal. :) */ | |
210 /* Are the errors offset by one from the error base? | |
211 e.g. the error base is 143, the code is 148, and the | |
212 actual error is XF86VidModeExtensionDisabled (4) ? | |
213 */ | |
214 if ( (vm_error >= 0) && | |
215 (((e->error_code == BadRequest)&&(e->request_code == vm_error)) || | |
216 ((e->error_code > vm_error) && | |
217 (e->error_code <= (vm_error+XF86VidModeNumberErrors)))) ) { | |
218 #ifdef XFREE86_DEBUG | |
219 { char errmsg[1024]; | |
220 XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg)); | |
221 printf("VidMode error: %s\n", errmsg); | |
222 } | |
223 #endif | |
224 return(0); | |
225 } | |
226 #endif /* XFREE86_VM */ | |
227 | |
228 #ifdef XFREE86_DGAMOUSE | |
229 /* DGA errors can be non-fatal. :) */ | |
230 if ( (dga_error >= 0) && | |
231 ((e->error_code > dga_error) && | |
232 (e->error_code <= (dga_error+XF86DGANumberErrors))) ) { | |
233 #ifdef XFREE86_DEBUG | |
234 { char errmsg[1024]; | |
235 XGetErrorText(d, e->error_code, errmsg, sizeof(errmsg)); | |
236 printf("DGA error: %s\n", errmsg); | |
237 } | |
238 #endif | |
239 return(0); | |
240 } | |
241 #endif /* XFREE86_DGAMOUSE */ | |
242 | |
243 return(X_handler(d,e)); | |
244 } | |
245 | |
246 /* X11 I/O error handler routine */ | |
247 static int (*XIO_handler)(Display *) = NULL; | |
248 static int xio_errhandler(Display *d) | |
249 { | |
250 /* Ack! Lost X11 connection! */ | |
251 | |
252 /* We will crash if we try to clean up our display */ | |
253 if ( current_video->hidden->Ximage ) { | |
254 SDL_VideoSurface->pixels = NULL; | |
255 } | |
256 current_video->hidden->X11_Display = NULL; | |
257 | |
258 /* Continue with the standard X11 error handler */ | |
259 return(XIO_handler(d)); | |
260 } | |
261 | |
262 /* Create auxiliary (toplevel) windows with the current visual */ | |
263 static void create_aux_windows(_THIS) | |
264 { | |
265 XSetWindowAttributes xattr; | |
266 XWMHints *hints; | |
267 XTextProperty titleprop, iconprop; | |
268 int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen)); | |
269 | |
270 /* Don't create any extra windows if we are being managed */ | |
271 if ( SDL_windowid ) { | |
272 FSwindow = 0; | |
273 WMwindow = strtol(SDL_windowid, NULL, 0); | |
274 return; | |
275 } | |
276 | |
277 if(FSwindow) | |
278 XDestroyWindow(SDL_Display, FSwindow); | |
279 | |
280 xattr.override_redirect = True; | |
281 xattr.background_pixel = def_vis ? BlackPixel(SDL_Display, SDL_Screen) : 0; | |
282 xattr.border_pixel = 0; | |
283 xattr.colormap = SDL_XColorMap; | |
284 | |
285 FSwindow = XCreateWindow(SDL_Display, SDL_Root, 0, 0, 32, 32, 0, | |
286 this->hidden->depth, InputOutput, SDL_Visual, | |
287 CWOverrideRedirect | CWBackPixel | CWBorderPixel | |
288 | CWColormap, | |
289 &xattr); | |
290 | |
291 XSelectInput(SDL_Display, FSwindow, StructureNotifyMask); | |
292 | |
293 /* Tell KDE to keep the fullscreen window on top */ | |
294 { | |
295 XEvent ev; | |
296 long mask; | |
297 | |
298 memset(&ev, 0, sizeof(ev)); | |
299 ev.xclient.type = ClientMessage; | |
300 ev.xclient.window = SDL_Root; | |
301 ev.xclient.message_type = XInternAtom(SDL_Display, | |
302 "KWM_KEEP_ON_TOP", False); | |
303 ev.xclient.format = 32; | |
304 ev.xclient.data.l[0] = FSwindow; | |
305 ev.xclient.data.l[1] = CurrentTime; | |
306 mask = SubstructureRedirectMask; | |
307 XSendEvent(SDL_Display, SDL_Root, False, mask, &ev); | |
308 } | |
309 | |
310 hints = NULL; | |
311 titleprop.value = iconprop.value = NULL; | |
312 if(WMwindow) { | |
313 /* All window attributes must survive the recreation */ | |
314 hints = XGetWMHints(SDL_Display, WMwindow); | |
315 XGetWMName(SDL_Display, WMwindow, &titleprop); | |
316 XGetWMIconName(SDL_Display, WMwindow, &iconprop); | |
317 XDestroyWindow(SDL_Display, WMwindow); | |
318 } | |
319 | |
320 /* Create the window for windowed management */ | |
321 /* (reusing the xattr structure above) */ | |
322 WMwindow = XCreateWindow(SDL_Display, SDL_Root, 0, 0, 32, 32, 0, | |
323 this->hidden->depth, InputOutput, SDL_Visual, | |
324 CWBackPixel | CWBorderPixel | CWColormap, | |
325 &xattr); | |
326 | |
327 /* Set the input hints so we get keyboard input */ | |
328 if(!hints) { | |
329 hints = XAllocWMHints(); | |
330 hints->input = True; | |
331 hints->flags = InputHint; | |
332 } | |
333 XSetWMHints(SDL_Display, WMwindow, hints); | |
334 XFree(hints); | |
335 if(titleprop.value) { | |
336 XSetWMName(SDL_Display, WMwindow, &titleprop); | |
337 XFree(titleprop.value); | |
338 } | |
339 if(iconprop.value) { | |
340 XSetWMIconName(SDL_Display, WMwindow, &iconprop); | |
341 XFree(iconprop.value); | |
342 } | |
343 | |
344 XSelectInput(SDL_Display, WMwindow, | |
345 FocusChangeMask | KeyPressMask | KeyReleaseMask | |
346 | PropertyChangeMask | StructureNotifyMask | KeymapStateMask); | |
347 | |
348 /* Set the class hints so we can get an icon (AfterStep) */ | |
349 { | |
350 XClassHint *classhints; | |
351 classhints = XAllocClassHint(); | |
352 if(classhints != NULL) { | |
353 classhints->res_name = "SDL_App"; | |
354 classhints->res_class = "SDL_App"; | |
355 XSetClassHint(SDL_Display, WMwindow, classhints); | |
356 XFree(classhints); | |
357 } | |
358 } | |
359 | |
360 /* Allow the window to be deleted by the window manager */ | |
361 WM_DELETE_WINDOW = XInternAtom(SDL_Display, "WM_DELETE_WINDOW", False); | |
362 XSetWMProtocols(SDL_Display, WMwindow, &WM_DELETE_WINDOW, 1); | |
363 } | |
364 | |
365 static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat) | |
366 { | |
367 char *display; | |
368 int i; | |
369 | |
370 /* Open the X11 display */ | |
371 display = NULL; /* Get it from DISPLAY environment variable */ | |
372 | |
373 if ( (strncmp(XDisplayName(display), ":", 1) == 0) || | |
374 (strncmp(XDisplayName(display), "unix:", 5) == 0) ) { | |
375 local_X11 = 1; | |
376 } else { | |
377 local_X11 = 0; | |
378 } | |
379 SDL_Display = XOpenDisplay(display); | |
380 if ( SDL_Display == NULL ) { | |
381 SDL_SetError("Couldn't open X11 display"); | |
382 return(-1); | |
383 } | |
384 #ifdef X11_DEBUG | |
385 XSynchronize(SDL_Display, True); | |
386 #endif | |
387 | |
388 /* Create an alternate X display for graphics updates -- allows us | |
389 to do graphics updates in a separate thread from event handling. | |
390 Thread-safe X11 doesn't seem to exist. | |
391 */ | |
392 GFX_Display = XOpenDisplay(display); | |
393 if ( GFX_Display == NULL ) { | |
394 SDL_SetError("Couldn't open X11 display"); | |
395 return(-1); | |
396 } | |
397 | |
398 /* Set the normal X error handler */ | |
399 X_handler = XSetErrorHandler(x_errhandler); | |
400 | |
401 /* Set the error handler if we lose the X display */ | |
402 XIO_handler = XSetIOErrorHandler(xio_errhandler); | |
403 | |
404 /* use default screen (from $DISPLAY) */ | |
405 SDL_Screen = DefaultScreen(SDL_Display); | |
406 | |
407 #ifndef NO_SHARED_MEMORY | |
408 /* Check for MIT shared memory extension */ | |
409 use_mitshm = 0; | |
410 if ( local_X11 ) { | |
411 use_mitshm = XShmQueryExtension(SDL_Display); | |
412 } | |
413 #endif /* NO_SHARED_MEMORY */ | |
414 | |
415 /* See whether or not we need to swap pixels */ | |
416 swap_pixels = 0; | |
417 if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) { | |
418 if ( XImageByteOrder(SDL_Display) == MSBFirst ) { | |
419 swap_pixels = 1; | |
420 } | |
421 } else { | |
422 if ( XImageByteOrder(SDL_Display) == LSBFirst ) { | |
423 swap_pixels = 1; | |
424 } | |
425 } | |
426 | |
427 /* Get the available video modes */ | |
428 if(X11_GetVideoModes(this) < 0) | |
429 return -1; | |
430 | |
431 /* Determine the default screen depth: | |
432 Use the default visual (or at least one with the same depth) */ | |
433 SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen); | |
434 for(i = 0; i < this->hidden->nvisuals; i++) | |
435 if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display, | |
436 SDL_Screen)) | |
437 break; | |
438 if(i == this->hidden->nvisuals) { | |
439 /* default visual was useless, take the deepest one instead */ | |
440 i = 0; | |
441 } | |
442 SDL_Visual = this->hidden->visuals[i].visual; | |
443 if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) { | |
444 SDL_XColorMap = SDL_DisplayColormap; | |
445 } else { | |
446 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
447 SDL_Visual, AllocNone); | |
448 } | |
449 this->hidden->depth = this->hidden->visuals[i].depth; | |
450 vformat->BitsPerPixel = this->hidden->visuals[i].bpp; | |
451 if ( vformat->BitsPerPixel > 8 ) { | |
452 vformat->Rmask = SDL_Visual->red_mask; | |
453 vformat->Gmask = SDL_Visual->green_mask; | |
454 vformat->Bmask = SDL_Visual->blue_mask; | |
455 } | |
456 X11_SaveVidModeGamma(this); | |
457 | |
458 /* See if we have been passed a window to use */ | |
459 SDL_windowid = getenv("SDL_WINDOWID"); | |
460 | |
461 /* Create the fullscreen and managed windows */ | |
462 create_aux_windows(this); | |
463 | |
464 /* Create the blank cursor */ | |
465 SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask, | |
466 BLANK_CWIDTH, BLANK_CHEIGHT, | |
467 BLANK_CHOTX, BLANK_CHOTY); | |
468 | |
469 /* Fill in some window manager capabilities */ | |
470 this->info.wm_available = 1; | |
471 | |
472 /* We're done! */ | |
473 XFlush(SDL_Display); | |
474 return(0); | |
475 } | |
476 | |
477 static void X11_DestroyWindow(_THIS, SDL_Surface *screen) | |
478 { | |
479 /* Clean up OpenGL */ | |
480 if ( screen ) { | |
481 screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT); | |
482 } | |
483 X11_GL_Shutdown(this); | |
484 | |
485 if ( ! SDL_windowid ) { | |
486 /* Hide the managed window */ | |
487 if ( WMwindow ) { | |
488 XUnmapWindow(SDL_Display, WMwindow); | |
489 } | |
490 if ( screen && (screen->flags & SDL_FULLSCREEN) ) { | |
491 screen->flags &= ~SDL_FULLSCREEN; | |
492 X11_LeaveFullScreen(this); | |
493 } | |
494 | |
495 /* Destroy the output window */ | |
496 if ( SDL_Window ) { | |
497 XDestroyWindow(SDL_Display, SDL_Window); | |
498 } | |
499 | |
500 /* Free the colormap entries */ | |
501 if ( SDL_XPixels ) { | |
502 int numcolors; | |
503 unsigned long pixel; | |
504 numcolors = SDL_Visual->map_entries; | |
505 for ( pixel=0; pixel<numcolors; ++pixel ) { | |
506 while ( SDL_XPixels[pixel] > 0 ) { | |
507 XFreeColors(GFX_Display, | |
508 SDL_DisplayColormap,&pixel,1,0); | |
509 --SDL_XPixels[pixel]; | |
510 } | |
511 } | |
512 free(SDL_XPixels); | |
513 SDL_XPixels = NULL; | |
514 } | |
515 | |
516 /* Free the graphics context */ | |
517 if ( SDL_GC ) { | |
518 XFreeGC(SDL_Display, SDL_GC); | |
519 SDL_GC = 0; | |
520 } | |
521 } | |
522 } | |
523 | |
524 static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags) | |
525 { | |
526 XSizeHints *hints; | |
527 | |
528 hints = XAllocSizeHints(); | |
529 if ( hints ) { | |
530 if ( flags & SDL_RESIZABLE ) { | |
531 hints->min_width = 32; | |
532 hints->min_height = 32; | |
533 hints->max_height = 4096; | |
534 hints->max_width = 4096; | |
535 } else { | |
536 hints->min_width = hints->max_width = w; | |
537 hints->min_height = hints->max_height = h; | |
538 } | |
539 hints->flags = PMaxSize | PMinSize; | |
540 if ( flags & SDL_FULLSCREEN ) { | |
541 hints->x = 0; | |
542 hints->y = 0; | |
543 hints->flags |= USPosition; | |
544 } else | |
545 /* Center it, if desired */ | |
546 if ( getenv("SDL_VIDEO_CENTERED") ) { | |
547 int display_w, display_h; | |
548 | |
549 display_w = DisplayWidth(SDL_Display, SDL_Screen); | |
550 display_h = DisplayHeight(SDL_Display, SDL_Screen); | |
551 hints->x = (display_w - w)/2; | |
552 hints->y = (display_h - h)/2; | |
553 hints->flags |= USPosition; | |
554 XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y); | |
555 | |
556 /* Flush the resize event so we don't catch it later */ | |
557 XSync(SDL_Display, True); | |
558 } | |
559 XSetWMNormalHints(SDL_Display, WMwindow, hints); | |
560 XFree(hints); | |
561 } | |
562 | |
563 /* Respect the window caption style */ | |
564 if ( flags & SDL_NOFRAME ) { | |
565 SDL_bool set; | |
566 Atom WM_HINTS; | |
567 | |
568 /* We haven't modified the window manager hints yet */ | |
569 set = SDL_FALSE; | |
570 | |
571 /* First try to set MWM hints */ | |
572 WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); | |
573 if ( WM_HINTS != None ) { | |
574 /* Hints used by Motif compliant window managers */ | |
575 struct { | |
576 unsigned long flags; | |
577 unsigned long functions; | |
578 unsigned long decorations; | |
579 long input_mode; | |
580 unsigned long status; | |
581 } MWMHints = { (1L << 1), 0, 0, 0, 0 }; | |
582 | |
583 XChangeProperty(SDL_Display, WMwindow, | |
584 WM_HINTS, WM_HINTS, 32, | |
585 PropModeReplace, | |
586 (unsigned char *)&MWMHints, | |
587 sizeof(MWMHints)/sizeof(long)); | |
588 set = SDL_TRUE; | |
589 } | |
590 /* Now try to set KWM hints */ | |
591 WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); | |
592 if ( WM_HINTS != None ) { | |
593 long KWMHints = 0; | |
594 | |
595 XChangeProperty(SDL_Display, WMwindow, | |
596 WM_HINTS, WM_HINTS, 32, | |
597 PropModeReplace, | |
598 (unsigned char *)&KWMHints, | |
599 sizeof(KWMHints)/sizeof(long)); | |
600 set = SDL_TRUE; | |
601 } | |
602 /* Now try to set GNOME hints */ | |
603 WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); | |
604 if ( WM_HINTS != None ) { | |
605 long GNOMEHints = 0; | |
606 | |
607 XChangeProperty(SDL_Display, WMwindow, | |
608 WM_HINTS, WM_HINTS, 32, | |
609 PropModeReplace, | |
610 (unsigned char *)&GNOMEHints, | |
611 sizeof(GNOMEHints)/sizeof(long)); | |
612 set = SDL_TRUE; | |
613 } | |
614 /* Finally set the transient hints if necessary */ | |
615 if ( ! set ) { | |
616 XSetTransientForHint(SDL_Display, WMwindow, SDL_Root); | |
617 } | |
618 } else { | |
619 SDL_bool set; | |
620 Atom WM_HINTS; | |
621 | |
622 /* We haven't modified the window manager hints yet */ | |
623 set = SDL_FALSE; | |
624 | |
625 /* First try to unset MWM hints */ | |
626 WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); | |
627 if ( WM_HINTS != None ) { | |
628 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
629 set = SDL_TRUE; | |
630 } | |
631 /* Now try to unset KWM hints */ | |
632 WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); | |
633 if ( WM_HINTS != None ) { | |
634 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
635 set = SDL_TRUE; | |
636 } | |
637 /* Now try to unset GNOME hints */ | |
638 WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); | |
639 if ( WM_HINTS != None ) { | |
640 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
641 set = SDL_TRUE; | |
642 } | |
643 /* Finally unset the transient hints if necessary */ | |
644 if ( ! set ) { | |
645 /* NOTE: Does this work? */ | |
646 XSetTransientForHint(SDL_Display, WMwindow, None); | |
647 } | |
648 } | |
649 } | |
650 | |
651 static int X11_CreateWindow(_THIS, SDL_Surface *screen, | |
652 int w, int h, int bpp, Uint32 flags) | |
653 { | |
654 int i, depth; | |
655 Visual *vis; | |
656 int vis_change; | |
657 | |
658 /* If a window is already present, destroy it and start fresh */ | |
659 if ( SDL_Window ) { | |
660 X11_DestroyWindow(this, screen); | |
661 } | |
662 | |
663 /* See if we have been given a window id */ | |
664 if ( SDL_windowid ) { | |
665 SDL_Window = strtol(SDL_windowid, NULL, 0); | |
666 } else { | |
667 SDL_Window = 0; | |
668 } | |
669 | |
670 /* find out which visual we are going to use */ | |
671 if ( flags & SDL_OPENGL ) { | |
672 XVisualInfo *vi; | |
673 | |
674 vi = X11_GL_GetVisual(this); | |
675 if( !vi ) { | |
676 return -1; | |
677 } | |
678 vis = vi->visual; | |
679 depth = vi->depth; | |
680 } else if ( SDL_windowid ) { | |
681 XWindowAttributes a; | |
682 | |
683 XGetWindowAttributes(SDL_Display, SDL_Window, &a); | |
684 vis = a.visual; | |
685 depth = a.depth; | |
686 } else { | |
687 for ( i = 0; i < this->hidden->nvisuals; i++ ) { | |
688 if ( this->hidden->visuals[i].bpp == bpp ) | |
689 break; | |
690 } | |
691 if ( i == this->hidden->nvisuals ) { | |
692 SDL_SetError("No matching visual for requested depth"); | |
693 return -1; /* should never happen */ | |
694 } | |
695 vis = this->hidden->visuals[i].visual; | |
696 depth = this->hidden->visuals[i].depth; | |
697 } | |
698 #ifdef X11_DEBUG | |
699 printf("Choosing %s visual at %d bpp - %d colormap entries\n", vis->class == PseudoColor ? "PseudoColor" : (vis->class == TrueColor ? "TrueColor" : (vis->class == DirectColor ? "DirectColor" : "Unknown")), depth, vis->map_entries); | |
700 #endif | |
701 vis_change = (vis != SDL_Visual); | |
702 SDL_Visual = vis; | |
703 this->hidden->depth = depth; | |
704 | |
705 /* Allocate the new pixel format for this video mode */ | |
706 if ( ! SDL_ReallocFormat(screen, bpp, | |
707 vis->red_mask, vis->green_mask, vis->blue_mask, 0) ) | |
708 return -1; | |
709 | |
710 /* Create the appropriate colormap */ | |
711 if ( SDL_XColorMap != SDL_DisplayColormap ) { | |
712 XFreeColormap(SDL_Display, SDL_XColorMap); | |
713 } | |
714 if ( SDL_Visual->class == PseudoColor ) { | |
715 int ncolors; | |
716 | |
717 /* Allocate the pixel flags */ | |
718 ncolors = SDL_Visual->map_entries; | |
719 SDL_XPixels = malloc(ncolors * sizeof(int)); | |
720 if(SDL_XPixels == NULL) { | |
721 SDL_OutOfMemory(); | |
722 return -1; | |
723 } | |
724 memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels)); | |
725 | |
726 /* always allocate a private colormap on non-default visuals */ | |
727 if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) { | |
728 flags |= SDL_HWPALETTE; | |
729 } | |
730 if ( flags & SDL_HWPALETTE ) { | |
731 screen->flags |= SDL_HWPALETTE; | |
732 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
733 SDL_Visual, AllocAll); | |
734 } else { | |
735 SDL_XColorMap = SDL_DisplayColormap; | |
736 } | |
737 } else if ( SDL_Visual->class == DirectColor ) { | |
738 | |
739 /* Create a colormap which we can manipulate for gamma */ | |
740 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
741 SDL_Visual, AllocAll); | |
742 XSync(SDL_Display, False); | |
743 | |
744 /* Initialize the colormap to the identity mapping */ | |
745 SDL_GetGammaRamp(0, 0, 0); | |
746 this->screen = screen; | |
747 X11_SetGammaRamp(this, this->gamma); | |
748 this->screen = NULL; | |
749 } else { | |
750 /* Create a read-only colormap for our window */ | |
751 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
752 SDL_Visual, AllocNone); | |
753 } | |
754 | |
755 /* Recreate the auxiliary windows, if needed (required for GL) */ | |
756 if ( vis_change ) | |
757 create_aux_windows(this); | |
758 | |
759 if(screen->flags & SDL_HWPALETTE) { | |
760 /* Since the full-screen window might have got a nonzero background | |
761 colour (0 is white on some displays), we should reset the | |
762 background to 0 here since that is what the user expects | |
763 with a private colormap */ | |
764 XSetWindowBackground(SDL_Display, FSwindow, 0); | |
765 XClearWindow(SDL_Display, FSwindow); | |
766 } | |
767 | |
768 /* resize the (possibly new) window manager window */ | |
769 if( !SDL_windowid ) { | |
770 X11_SetSizeHints(this, w, h, flags); | |
771 current_w = w; | |
772 current_h = h; | |
773 XResizeWindow(SDL_Display, WMwindow, w, h); | |
774 } | |
775 | |
776 /* Create (or use) the X11 display window */ | |
777 if ( !SDL_windowid ) { | |
778 if ( flags & SDL_OPENGL ) { | |
779 if ( X11_GL_CreateWindow(this, w, h) < 0 ) { | |
780 return(-1); | |
781 } | |
782 } else { | |
783 XSetWindowAttributes swa; | |
784 | |
785 swa.background_pixel = 0; | |
786 swa.border_pixel = 0; | |
787 swa.colormap = SDL_XColorMap; | |
788 SDL_Window = XCreateWindow(SDL_Display, WMwindow, | |
789 0, 0, w, h, 0, depth, | |
790 InputOutput, SDL_Visual, | |
791 CWBackPixel | CWBorderPixel | |
792 | CWColormap, &swa); | |
793 } | |
794 /* Only manage our input if we own the window */ | |
795 XSelectInput(SDL_Display, SDL_Window, | |
796 ( EnterWindowMask | LeaveWindowMask | |
797 | ButtonPressMask | ButtonReleaseMask | |
798 | PointerMotionMask | ExposureMask )); | |
799 } | |
800 | |
801 /* Create the graphics context here, once we have a window */ | |
802 if ( flags & SDL_OPENGL ) { | |
803 if ( X11_GL_CreateContext(this) < 0 ) { | |
804 return(-1); | |
805 } else { | |
806 screen->flags |= SDL_OPENGL; | |
807 } | |
808 } else { | |
809 XGCValues gcv; | |
810 | |
811 gcv.graphics_exposures = False; | |
812 SDL_GC = XCreateGC(SDL_Display, SDL_Window, | |
813 GCGraphicsExposures, &gcv); | |
814 if ( ! SDL_GC ) { | |
815 SDL_SetError("Couldn't create graphics context"); | |
816 return(-1); | |
817 } | |
818 } | |
819 | |
820 /* Set our colormaps when not setting a GL mode */ | |
821 if ( ! (flags & SDL_OPENGL) ) { | |
822 XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap); | |
823 if( !SDL_windowid ) { | |
824 XSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap); | |
825 XSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap); | |
826 } | |
827 } | |
828 | |
829 #if 0 /* This is an experiment - are the graphics faster now? - nope. */ | |
830 if ( getenv("SDL_VIDEO_X11_BACKINGSTORE") ) | |
831 #endif | |
832 /* Cache the window in the server, when possible */ | |
833 { | |
834 Screen *xscreen; | |
835 XSetWindowAttributes a; | |
836 | |
837 xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen); | |
838 a.backing_store = DoesBackingStore(xscreen); | |
839 if ( a.backing_store != NotUseful ) { | |
840 XChangeWindowAttributes(SDL_Display, SDL_Window, | |
841 CWBackingStore, &a); | |
842 } | |
843 } | |
844 | |
845 /* Update the internal keyboard state */ | |
846 X11_SetKeyboardState(SDL_Display, NULL); | |
847 | |
848 /* Map them both and go fullscreen, if requested */ | |
849 if ( ! SDL_windowid ) { | |
850 XMapWindow(SDL_Display, SDL_Window); | |
851 XMapWindow(SDL_Display, WMwindow); | |
160 | 852 X11_WaitMapped(this, WMwindow); |
0 | 853 if ( flags & SDL_FULLSCREEN ) { |
854 screen->flags |= SDL_FULLSCREEN; | |
855 X11_EnterFullScreen(this); | |
856 } else { | |
857 screen->flags &= ~SDL_FULLSCREEN; | |
858 } | |
859 } | |
860 return(0); | |
861 } | |
862 | |
863 static int X11_ResizeWindow(_THIS, | |
864 SDL_Surface *screen, int w, int h, Uint32 flags) | |
865 { | |
866 if ( ! SDL_windowid ) { | |
867 /* Resize the window manager window */ | |
868 X11_SetSizeHints(this, w, h, flags); | |
869 current_w = w; | |
870 current_h = h; | |
871 XResizeWindow(SDL_Display, WMwindow, w, h); | |
872 | |
873 /* Resize the fullscreen and display windows */ | |
874 if ( flags & SDL_FULLSCREEN ) { | |
875 if ( screen->flags & SDL_FULLSCREEN ) { | |
876 X11_ResizeFullScreen(this); | |
877 } else { | |
878 screen->flags |= SDL_FULLSCREEN; | |
879 X11_EnterFullScreen(this); | |
880 } | |
881 } else { | |
882 if ( screen->flags & SDL_FULLSCREEN ) { | |
883 screen->flags &= ~SDL_FULLSCREEN; | |
884 X11_LeaveFullScreen(this); | |
885 } | |
886 } | |
887 XResizeWindow(SDL_Display, SDL_Window, w, h); | |
888 } | |
889 return(0); | |
890 } | |
891 | |
892 SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, | |
893 int width, int height, int bpp, Uint32 flags) | |
894 { | |
895 Uint32 saved_flags; | |
896 | |
897 /* Lock the event thread, in multi-threading environments */ | |
898 SDL_Lock_EventThread(); | |
899 | |
900 /* Check the combination of flags we were passed */ | |
901 if ( flags & SDL_FULLSCREEN ) { | |
902 /* Clear fullscreen flag if not supported */ | |
903 if ( SDL_windowid ) { | |
904 flags &= ~SDL_FULLSCREEN; | |
905 } | |
906 } | |
907 | |
908 /* Flush any delayed updates */ | |
909 XSync(GFX_Display, False); | |
910 | |
911 /* Set up the X11 window */ | |
912 saved_flags = current->flags; | |
913 if (SDL_Window && (saved_flags&SDL_OPENGL) == (flags&SDL_OPENGL) | |
914 && bpp == current->format->BitsPerPixel) { | |
915 if (X11_ResizeWindow(this, current, width, height, flags) < 0) { | |
916 current = NULL; | |
917 goto done; | |
918 } | |
919 } else { | |
920 if (X11_CreateWindow(this,current,width,height,bpp,flags) < 0) { | |
921 current = NULL; | |
922 goto done; | |
923 } | |
924 } | |
925 | |
926 /* Set up the new mode framebuffer */ | |
927 if ( ((current->w != width) || (current->h != height)) || | |
928 ((saved_flags&SDL_OPENGL) != (flags&SDL_OPENGL)) ) { | |
929 current->w = width; | |
930 current->h = height; | |
931 current->pitch = SDL_CalculatePitch(current); | |
932 X11_ResizeImage(this, current, flags); | |
933 } | |
934 current->flags |= (flags&(SDL_RESIZABLE|SDL_NOFRAME)); | |
935 | |
936 done: | |
937 /* Release the event thread */ | |
938 XSync(SDL_Display, False); | |
939 SDL_Unlock_EventThread(); | |
940 | |
941 /* We're done! */ | |
942 return(current); | |
943 } | |
944 | |
945 static int X11_ToggleFullScreen(_THIS, int on) | |
946 { | |
947 Uint32 event_thread; | |
948 | |
949 /* Don't switch if we don't own the window */ | |
950 if ( SDL_windowid ) { | |
951 return(0); | |
952 } | |
953 | |
954 /* Don't lock if we are the event thread */ | |
955 event_thread = SDL_EventThreadID(); | |
956 if ( event_thread && (SDL_ThreadID() == event_thread) ) { | |
957 event_thread = 0; | |
958 } | |
959 if ( event_thread ) { | |
960 SDL_Lock_EventThread(); | |
961 } | |
962 if ( on ) { | |
963 this->screen->flags |= SDL_FULLSCREEN; | |
964 X11_EnterFullScreen(this); | |
965 } else { | |
966 this->screen->flags &= ~SDL_FULLSCREEN; | |
967 X11_LeaveFullScreen(this); | |
968 } | |
969 X11_RefreshDisplay(this); | |
970 if ( event_thread ) { | |
971 SDL_Unlock_EventThread(); | |
972 } | |
973 SDL_ResetKeyboard(); | |
974 return(1); | |
975 } | |
976 | |
977 /* Update the current mouse state and position */ | |
978 static void X11_UpdateMouse(_THIS) | |
979 { | |
980 Window u1; int u2; | |
981 Window current_win; | |
982 int x, y; | |
983 unsigned int mask; | |
984 | |
985 /* Lock the event thread, in multi-threading environments */ | |
986 SDL_Lock_EventThread(); | |
987 if ( XQueryPointer(SDL_Display, SDL_Window, &u1, ¤t_win, | |
988 &u2, &u2, &x, &y, &mask) ) { | |
989 if ( (x >= 0) && (x < SDL_VideoSurface->w) && | |
990 (y >= 0) && (y < SDL_VideoSurface->h) ) { | |
991 SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS); | |
992 SDL_PrivateMouseMotion(0, 0, x, y); | |
993 } else { | |
994 SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS); | |
995 } | |
996 } | |
997 SDL_Unlock_EventThread(); | |
998 } | |
999 | |
1000 /* simple colour distance metric. Supposed to be better than a plain | |
1001 Euclidian distance anyway. */ | |
1002 #define COLOUR_FACTOR 3 | |
1003 #define LIGHT_FACTOR 1 | |
1004 #define COLOUR_DIST(r1, g1, b1, r2, g2, b2) \ | |
1005 (COLOUR_FACTOR * (abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)) \ | |
1006 + LIGHT_FACTOR * abs(r1 + g1 + b1 - (r2 + g2 + b2))) | |
1007 | |
1008 static void allocate_nearest(_THIS, SDL_Color *colors, | |
1009 SDL_Color *want, int nwant) | |
1010 { | |
1011 /* | |
1012 * There is no way to know which ones to choose from, so we retrieve | |
1013 * the entire colormap and try the nearest possible, until we find one | |
1014 * that is shared. | |
1015 */ | |
1016 XColor all[256]; | |
1017 int i; | |
1018 for(i = 0; i < 256; i++) | |
1019 all[i].pixel = i; | |
1020 /* | |
1021 * XQueryColors sets the flags in the XColor struct, so we use | |
1022 * that to keep track of which colours are available | |
1023 */ | |
1024 XQueryColors(GFX_Display, SDL_XColorMap, all, 256); | |
1025 | |
1026 for(i = 0; i < nwant; i++) { | |
1027 XColor *c; | |
1028 int j; | |
1029 int best = 0; | |
1030 int mindist = 0x7fffffff; | |
1031 int ri = want[i].r; | |
1032 int gi = want[i].g; | |
1033 int bi = want[i].b; | |
1034 for(j = 0; j < 256; j++) { | |
1035 int rj, gj, bj, d2; | |
1036 if(!all[j].flags) | |
1037 continue; /* unavailable colour cell */ | |
1038 rj = all[j].red >> 8; | |
1039 gj = all[j].green >> 8; | |
1040 bj = all[j].blue >> 8; | |
1041 d2 = COLOUR_DIST(ri, gi, bi, rj, gj, bj); | |
1042 if(d2 < mindist) { | |
1043 mindist = d2; | |
1044 best = j; | |
1045 } | |
1046 } | |
1047 if(SDL_XPixels[best]) | |
1048 continue; /* already allocated, waste no more time */ | |
1049 c = all + best; | |
1050 if(XAllocColor(GFX_Display, SDL_XColorMap, c)) { | |
1051 /* got it */ | |
236
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1052 colors[c->pixel].r = c->red >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1053 colors[c->pixel].g = c->green >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1054 colors[c->pixel].b = c->blue >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1055 ++SDL_XPixels[c->pixel]; |
0 | 1056 } else { |
1057 /* | |
1058 * The colour couldn't be allocated, probably being | |
1059 * owned as a r/w cell by another client. Flag it as | |
1060 * unavailable and try again. The termination of the | |
1061 * loop is guaranteed since at least black and white | |
1062 * are always there. | |
1063 */ | |
1064 c->flags = 0; | |
1065 i--; | |
1066 } | |
1067 } | |
1068 } | |
1069 | |
1070 int X11_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
1071 { | |
1072 int nrej = 0; | |
1073 | |
1074 /* Check to make sure we have a colormap allocated */ | |
1075 if ( SDL_XPixels == NULL ) { | |
1076 return(0); | |
1077 } | |
1078 if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) { | |
1079 /* private writable colormap: just set the colours we need */ | |
1080 XColor *xcmap; | |
1081 int i; | |
1082 xcmap = ALLOCA(ncolors*sizeof(*xcmap)); | |
1083 if(xcmap == NULL) | |
1084 return 0; | |
1085 for ( i=0; i<ncolors; ++i ) { | |
1086 xcmap[i].pixel = i + firstcolor; | |
1087 xcmap[i].red = (colors[i].r<<8)|colors[i].r; | |
1088 xcmap[i].green = (colors[i].g<<8)|colors[i].g; | |
1089 xcmap[i].blue = (colors[i].b<<8)|colors[i].b; | |
1090 xcmap[i].flags = (DoRed|DoGreen|DoBlue); | |
1091 } | |
1092 XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors); | |
1093 XSync(GFX_Display, False); | |
1094 FREEA(xcmap); | |
1095 } else { | |
1096 /* | |
1097 * Shared colormap: We only allocate read-only cells, which | |
1098 * increases the likelyhood of colour sharing with other | |
1099 * clients. The pixel values will almost certainly be | |
1100 * different from the requested ones, so the user has to | |
1101 * walk the colormap and see which index got what colour. | |
1102 * | |
1103 * We can work directly with the logical palette since it | |
1104 * has already been set when we get here. | |
1105 */ | |
1106 SDL_Color *want, *reject; | |
1107 unsigned long *freelist; | |
1108 int i; | |
1109 int nfree = 0; | |
1110 int nc = this->screen->format->palette->ncolors; | |
1111 colors = this->screen->format->palette->colors; | |
1112 freelist = ALLOCA(nc * sizeof(*freelist)); | |
1113 /* make sure multiple allocations of the same cell are freed */ | |
1114 for(i = 0; i < ncolors; i++) { | |
1115 int pixel = firstcolor + i; | |
1116 while(SDL_XPixels[pixel]) { | |
1117 freelist[nfree++] = pixel; | |
1118 --SDL_XPixels[pixel]; | |
1119 } | |
1120 } | |
1121 XFreeColors(GFX_Display, SDL_XColorMap, freelist, nfree, 0); | |
1122 FREEA(freelist); | |
1123 | |
1124 want = ALLOCA(ncolors * sizeof(SDL_Color)); | |
1125 reject = ALLOCA(ncolors * sizeof(SDL_Color)); | |
1126 memcpy(want, colors + firstcolor, ncolors * sizeof(SDL_Color)); | |
1127 /* make sure the user isn't fooled by her own wishes | |
1128 (black is safe, always available in the default colormap) */ | |
1129 memset(colors + firstcolor, 0, ncolors * sizeof(SDL_Color)); | |
1130 | |
1131 /* now try to allocate the colours */ | |
1132 for(i = 0; i < ncolors; i++) { | |
1133 XColor col; | |
1134 col.red = want[i].r << 8; | |
1135 col.green = want[i].g << 8; | |
1136 col.blue = want[i].b << 8; | |
1137 col.flags = DoRed | DoGreen | DoBlue; | |
1138 if(XAllocColor(GFX_Display, SDL_XColorMap, &col)) { | |
1139 /* We got the colour, or at least the nearest | |
1140 the hardware could get. */ | |
1141 colors[col.pixel].r = col.red >> 8; | |
1142 colors[col.pixel].g = col.green >> 8; | |
1143 colors[col.pixel].b = col.blue >> 8; | |
1144 ++SDL_XPixels[col.pixel]; | |
1145 } else { | |
1146 /* | |
1147 * no more free cells, add it to the list | |
1148 * of rejected colours | |
1149 */ | |
1150 reject[nrej++] = want[i]; | |
1151 } | |
1152 } | |
1153 if(nrej) | |
1154 allocate_nearest(this, colors, reject, nrej); | |
1155 FREEA(reject); | |
1156 FREEA(want); | |
1157 } | |
1158 return nrej == 0; | |
1159 } | |
1160 | |
1161 int X11_SetGammaRamp(_THIS, Uint16 *ramp) | |
1162 { | |
1163 int i, ncolors; | |
1164 XColor xcmap[256]; | |
1165 | |
1166 /* See if actually setting the gamma is supported */ | |
1167 if ( SDL_Visual->class != DirectColor ) { | |
1168 SDL_SetError("Gamma correction not supported on this visual"); | |
1169 return(-1); | |
1170 } | |
1171 | |
1172 /* Calculate the appropriate palette for the given gamma ramp */ | |
1173 ncolors = SDL_Visual->map_entries; | |
1174 for ( i=0; i<ncolors; ++i ) { | |
1175 Uint8 c = (256 * i / ncolors); | |
1176 xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c); | |
1177 xcmap[i].red = ramp[0*256+c]; | |
1178 xcmap[i].green = ramp[1*256+c]; | |
1179 xcmap[i].blue = ramp[2*256+c]; | |
1180 xcmap[i].flags = (DoRed|DoGreen|DoBlue); | |
1181 } | |
1182 XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors); | |
1183 XSync(GFX_Display, False); | |
1184 return(0); | |
1185 } | |
1186 | |
1187 /* Note: If we are terminated, this could be called in the middle of | |
1188 another SDL video routine -- notably UpdateRects. | |
1189 */ | |
1190 void X11_VideoQuit(_THIS) | |
1191 { | |
1192 /* Shutdown everything that's still up */ | |
1193 /* The event thread should be done, so we can touch SDL_Display */ | |
1194 if ( SDL_Display != NULL ) { | |
1195 /* Flush any delayed updates */ | |
1196 XSync(GFX_Display, False); | |
1197 | |
1198 /* Start shutting down the windows */ | |
1199 X11_DestroyImage(this, this->screen); | |
1200 X11_DestroyWindow(this, this->screen); | |
1201 X11_FreeVideoModes(this); | |
1202 if ( SDL_XColorMap != SDL_DisplayColormap ) { | |
1203 XFreeColormap(SDL_Display, SDL_XColorMap); | |
1204 } | |
1205 if ( SDL_iconcolors ) { | |
1206 unsigned long pixel; | |
236
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1207 Colormap dcmap = DefaultColormap(SDL_Display, |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1208 SDL_Screen); |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1209 for(pixel = 0; pixel < 256; ++pixel) { |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1210 while(SDL_iconcolors[pixel] > 0) { |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1211 XFreeColors(GFX_Display, |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1212 dcmap, &pixel, 1, 0); |
0 | 1213 --SDL_iconcolors[pixel]; |
1214 } | |
1215 } | |
1216 free(SDL_iconcolors); | |
1217 SDL_iconcolors = NULL; | |
1218 } | |
1219 /* Restore gamma settings if they've changed */ | |
1220 if ( SDL_GetAppState() & SDL_APPACTIVE ) { | |
1221 X11_SwapVidModeGamma(this); | |
1222 } | |
1223 | |
1224 /* Free that blank cursor */ | |
1225 if ( SDL_BlankCursor != NULL ) { | |
1226 this->FreeWMCursor(this, SDL_BlankCursor); | |
1227 SDL_BlankCursor = NULL; | |
1228 } | |
1229 | |
1230 /* Close the X11 graphics connection */ | |
1231 if ( GFX_Display != NULL ) { | |
1232 XCloseDisplay(GFX_Display); | |
1233 GFX_Display = NULL; | |
1234 } | |
1235 | |
1236 /* Close the X11 display connection */ | |
1237 XCloseDisplay(SDL_Display); | |
1238 SDL_Display = NULL; | |
1239 | |
1240 /* Reset the X11 error handlers */ | |
1241 if ( XIO_handler ) { | |
1242 XSetIOErrorHandler(XIO_handler); | |
1243 } | |
1244 if ( X_handler ) { | |
1245 XSetErrorHandler(X_handler); | |
1246 } | |
1247 | |
1248 /* Unload GL library after X11 shuts down */ | |
1249 X11_GL_UnloadLibrary(this); | |
1250 } | |
1251 if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) { | |
1252 /* Direct screen access, no memory buffer */ | |
1253 this->screen->pixels = NULL; | |
1254 } | |
1255 } | |
1256 |