Mercurial > sdl-ios-xcode
annotate src/video/x11/SDL_x11video.c @ 444:406b12a17b15
Only modifier key state is noted when X11 window opens
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 18 Aug 2002 03:19:59 +0000 |
parents | f6ffac90895c |
children | 8a687496061f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
270
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga |
0 | 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 | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
236
diff
changeset
|
20 slouken@libsdl.org |
0 | 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 | |
270
37fa1484f71b
From: "Mattias Engdeg�rd" <f91-men@nada.kth.se>
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
407 use_mitshm = 0; |
0 | 408 #ifndef NO_SHARED_MEMORY |
409 /* Check for MIT shared memory extension */ | |
410 if ( local_X11 ) { | |
411 use_mitshm = XShmQueryExtension(SDL_Display); | |
412 } | |
413 #endif /* NO_SHARED_MEMORY */ | |
414 | |
415 /* Get the available video modes */ | |
416 if(X11_GetVideoModes(this) < 0) | |
417 return -1; | |
418 | |
419 /* Determine the default screen depth: | |
420 Use the default visual (or at least one with the same depth) */ | |
421 SDL_DisplayColormap = DefaultColormap(SDL_Display, SDL_Screen); | |
422 for(i = 0; i < this->hidden->nvisuals; i++) | |
423 if(this->hidden->visuals[i].depth == DefaultDepth(SDL_Display, | |
424 SDL_Screen)) | |
425 break; | |
426 if(i == this->hidden->nvisuals) { | |
427 /* default visual was useless, take the deepest one instead */ | |
428 i = 0; | |
429 } | |
430 SDL_Visual = this->hidden->visuals[i].visual; | |
431 if ( SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen) ) { | |
432 SDL_XColorMap = SDL_DisplayColormap; | |
433 } else { | |
434 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
435 SDL_Visual, AllocNone); | |
436 } | |
437 this->hidden->depth = this->hidden->visuals[i].depth; | |
438 vformat->BitsPerPixel = this->hidden->visuals[i].bpp; | |
439 if ( vformat->BitsPerPixel > 8 ) { | |
440 vformat->Rmask = SDL_Visual->red_mask; | |
441 vformat->Gmask = SDL_Visual->green_mask; | |
442 vformat->Bmask = SDL_Visual->blue_mask; | |
443 } | |
444 X11_SaveVidModeGamma(this); | |
445 | |
446 /* See if we have been passed a window to use */ | |
447 SDL_windowid = getenv("SDL_WINDOWID"); | |
448 | |
449 /* Create the fullscreen and managed windows */ | |
450 create_aux_windows(this); | |
451 | |
452 /* Create the blank cursor */ | |
453 SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask, | |
454 BLANK_CWIDTH, BLANK_CHEIGHT, | |
455 BLANK_CHOTX, BLANK_CHOTY); | |
456 | |
457 /* Fill in some window manager capabilities */ | |
458 this->info.wm_available = 1; | |
459 | |
460 /* We're done! */ | |
461 XFlush(SDL_Display); | |
462 return(0); | |
463 } | |
464 | |
465 static void X11_DestroyWindow(_THIS, SDL_Surface *screen) | |
466 { | |
467 /* Clean up OpenGL */ | |
468 if ( screen ) { | |
469 screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT); | |
470 } | |
471 X11_GL_Shutdown(this); | |
472 | |
473 if ( ! SDL_windowid ) { | |
474 /* Hide the managed window */ | |
475 if ( WMwindow ) { | |
476 XUnmapWindow(SDL_Display, WMwindow); | |
477 } | |
478 if ( screen && (screen->flags & SDL_FULLSCREEN) ) { | |
479 screen->flags &= ~SDL_FULLSCREEN; | |
480 X11_LeaveFullScreen(this); | |
481 } | |
482 | |
483 /* Destroy the output window */ | |
484 if ( SDL_Window ) { | |
485 XDestroyWindow(SDL_Display, SDL_Window); | |
486 } | |
487 | |
488 /* Free the colormap entries */ | |
489 if ( SDL_XPixels ) { | |
490 int numcolors; | |
491 unsigned long pixel; | |
492 numcolors = SDL_Visual->map_entries; | |
493 for ( pixel=0; pixel<numcolors; ++pixel ) { | |
494 while ( SDL_XPixels[pixel] > 0 ) { | |
495 XFreeColors(GFX_Display, | |
496 SDL_DisplayColormap,&pixel,1,0); | |
497 --SDL_XPixels[pixel]; | |
498 } | |
499 } | |
500 free(SDL_XPixels); | |
501 SDL_XPixels = NULL; | |
502 } | |
503 | |
504 /* Free the graphics context */ | |
505 if ( SDL_GC ) { | |
506 XFreeGC(SDL_Display, SDL_GC); | |
507 SDL_GC = 0; | |
508 } | |
509 } | |
510 } | |
511 | |
512 static void X11_SetSizeHints(_THIS, int w, int h, Uint32 flags) | |
513 { | |
514 XSizeHints *hints; | |
515 | |
516 hints = XAllocSizeHints(); | |
517 if ( hints ) { | |
518 if ( flags & SDL_RESIZABLE ) { | |
519 hints->min_width = 32; | |
520 hints->min_height = 32; | |
521 hints->max_height = 4096; | |
522 hints->max_width = 4096; | |
523 } else { | |
524 hints->min_width = hints->max_width = w; | |
525 hints->min_height = hints->max_height = h; | |
526 } | |
527 hints->flags = PMaxSize | PMinSize; | |
528 if ( flags & SDL_FULLSCREEN ) { | |
529 hints->x = 0; | |
530 hints->y = 0; | |
531 hints->flags |= USPosition; | |
532 } else | |
533 /* Center it, if desired */ | |
534 if ( getenv("SDL_VIDEO_CENTERED") ) { | |
535 int display_w, display_h; | |
536 | |
537 display_w = DisplayWidth(SDL_Display, SDL_Screen); | |
538 display_h = DisplayHeight(SDL_Display, SDL_Screen); | |
539 hints->x = (display_w - w)/2; | |
540 hints->y = (display_h - h)/2; | |
541 hints->flags |= USPosition; | |
542 XMoveWindow(SDL_Display, WMwindow, hints->x, hints->y); | |
543 | |
544 /* Flush the resize event so we don't catch it later */ | |
545 XSync(SDL_Display, True); | |
546 } | |
547 XSetWMNormalHints(SDL_Display, WMwindow, hints); | |
548 XFree(hints); | |
549 } | |
550 | |
551 /* Respect the window caption style */ | |
552 if ( flags & SDL_NOFRAME ) { | |
553 SDL_bool set; | |
554 Atom WM_HINTS; | |
555 | |
556 /* We haven't modified the window manager hints yet */ | |
557 set = SDL_FALSE; | |
558 | |
559 /* First try to set MWM hints */ | |
560 WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); | |
561 if ( WM_HINTS != None ) { | |
562 /* Hints used by Motif compliant window managers */ | |
563 struct { | |
564 unsigned long flags; | |
565 unsigned long functions; | |
566 unsigned long decorations; | |
567 long input_mode; | |
568 unsigned long status; | |
569 } MWMHints = { (1L << 1), 0, 0, 0, 0 }; | |
570 | |
571 XChangeProperty(SDL_Display, WMwindow, | |
572 WM_HINTS, WM_HINTS, 32, | |
573 PropModeReplace, | |
574 (unsigned char *)&MWMHints, | |
575 sizeof(MWMHints)/sizeof(long)); | |
576 set = SDL_TRUE; | |
577 } | |
578 /* Now try to set KWM hints */ | |
579 WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); | |
580 if ( WM_HINTS != None ) { | |
581 long KWMHints = 0; | |
582 | |
583 XChangeProperty(SDL_Display, WMwindow, | |
584 WM_HINTS, WM_HINTS, 32, | |
585 PropModeReplace, | |
586 (unsigned char *)&KWMHints, | |
587 sizeof(KWMHints)/sizeof(long)); | |
588 set = SDL_TRUE; | |
589 } | |
590 /* Now try to set GNOME hints */ | |
591 WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); | |
592 if ( WM_HINTS != None ) { | |
593 long GNOMEHints = 0; | |
594 | |
595 XChangeProperty(SDL_Display, WMwindow, | |
596 WM_HINTS, WM_HINTS, 32, | |
597 PropModeReplace, | |
598 (unsigned char *)&GNOMEHints, | |
599 sizeof(GNOMEHints)/sizeof(long)); | |
600 set = SDL_TRUE; | |
601 } | |
602 /* Finally set the transient hints if necessary */ | |
603 if ( ! set ) { | |
604 XSetTransientForHint(SDL_Display, WMwindow, SDL_Root); | |
605 } | |
606 } else { | |
607 SDL_bool set; | |
608 Atom WM_HINTS; | |
609 | |
610 /* We haven't modified the window manager hints yet */ | |
611 set = SDL_FALSE; | |
612 | |
613 /* First try to unset MWM hints */ | |
614 WM_HINTS = XInternAtom(SDL_Display, "_MOTIF_WM_HINTS", True); | |
615 if ( WM_HINTS != None ) { | |
616 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
617 set = SDL_TRUE; | |
618 } | |
619 /* Now try to unset KWM hints */ | |
620 WM_HINTS = XInternAtom(SDL_Display, "KWM_WIN_DECORATION", True); | |
621 if ( WM_HINTS != None ) { | |
622 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
623 set = SDL_TRUE; | |
624 } | |
625 /* Now try to unset GNOME hints */ | |
626 WM_HINTS = XInternAtom(SDL_Display, "_WIN_HINTS", True); | |
627 if ( WM_HINTS != None ) { | |
628 XDeleteProperty(SDL_Display, WMwindow, WM_HINTS); | |
629 set = SDL_TRUE; | |
630 } | |
631 /* Finally unset the transient hints if necessary */ | |
632 if ( ! set ) { | |
633 /* NOTE: Does this work? */ | |
634 XSetTransientForHint(SDL_Display, WMwindow, None); | |
635 } | |
636 } | |
637 } | |
638 | |
639 static int X11_CreateWindow(_THIS, SDL_Surface *screen, | |
640 int w, int h, int bpp, Uint32 flags) | |
641 { | |
642 int i, depth; | |
643 Visual *vis; | |
644 int vis_change; | |
645 | |
646 /* If a window is already present, destroy it and start fresh */ | |
647 if ( SDL_Window ) { | |
648 X11_DestroyWindow(this, screen); | |
649 } | |
650 | |
651 /* See if we have been given a window id */ | |
652 if ( SDL_windowid ) { | |
653 SDL_Window = strtol(SDL_windowid, NULL, 0); | |
654 } else { | |
655 SDL_Window = 0; | |
656 } | |
657 | |
658 /* find out which visual we are going to use */ | |
659 if ( flags & SDL_OPENGL ) { | |
660 XVisualInfo *vi; | |
661 | |
662 vi = X11_GL_GetVisual(this); | |
663 if( !vi ) { | |
664 return -1; | |
665 } | |
666 vis = vi->visual; | |
667 depth = vi->depth; | |
668 } else if ( SDL_windowid ) { | |
669 XWindowAttributes a; | |
670 | |
671 XGetWindowAttributes(SDL_Display, SDL_Window, &a); | |
672 vis = a.visual; | |
673 depth = a.depth; | |
674 } else { | |
675 for ( i = 0; i < this->hidden->nvisuals; i++ ) { | |
676 if ( this->hidden->visuals[i].bpp == bpp ) | |
677 break; | |
678 } | |
679 if ( i == this->hidden->nvisuals ) { | |
680 SDL_SetError("No matching visual for requested depth"); | |
681 return -1; /* should never happen */ | |
682 } | |
683 vis = this->hidden->visuals[i].visual; | |
684 depth = this->hidden->visuals[i].depth; | |
685 } | |
686 #ifdef X11_DEBUG | |
687 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); | |
688 #endif | |
689 vis_change = (vis != SDL_Visual); | |
690 SDL_Visual = vis; | |
691 this->hidden->depth = depth; | |
692 | |
693 /* Allocate the new pixel format for this video mode */ | |
694 if ( ! SDL_ReallocFormat(screen, bpp, | |
695 vis->red_mask, vis->green_mask, vis->blue_mask, 0) ) | |
696 return -1; | |
697 | |
698 /* Create the appropriate colormap */ | |
699 if ( SDL_XColorMap != SDL_DisplayColormap ) { | |
700 XFreeColormap(SDL_Display, SDL_XColorMap); | |
701 } | |
702 if ( SDL_Visual->class == PseudoColor ) { | |
703 int ncolors; | |
704 | |
705 /* Allocate the pixel flags */ | |
706 ncolors = SDL_Visual->map_entries; | |
707 SDL_XPixels = malloc(ncolors * sizeof(int)); | |
708 if(SDL_XPixels == NULL) { | |
709 SDL_OutOfMemory(); | |
710 return -1; | |
711 } | |
712 memset(SDL_XPixels, 0, ncolors * sizeof(*SDL_XPixels)); | |
713 | |
714 /* always allocate a private colormap on non-default visuals */ | |
715 if ( SDL_Visual != DefaultVisual(SDL_Display, SDL_Screen) ) { | |
716 flags |= SDL_HWPALETTE; | |
717 } | |
718 if ( flags & SDL_HWPALETTE ) { | |
719 screen->flags |= SDL_HWPALETTE; | |
720 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
721 SDL_Visual, AllocAll); | |
722 } else { | |
723 SDL_XColorMap = SDL_DisplayColormap; | |
724 } | |
725 } else if ( SDL_Visual->class == DirectColor ) { | |
726 | |
727 /* Create a colormap which we can manipulate for gamma */ | |
728 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
729 SDL_Visual, AllocAll); | |
730 XSync(SDL_Display, False); | |
731 | |
732 /* Initialize the colormap to the identity mapping */ | |
733 SDL_GetGammaRamp(0, 0, 0); | |
734 this->screen = screen; | |
735 X11_SetGammaRamp(this, this->gamma); | |
736 this->screen = NULL; | |
737 } else { | |
738 /* Create a read-only colormap for our window */ | |
739 SDL_XColorMap = XCreateColormap(SDL_Display, SDL_Root, | |
740 SDL_Visual, AllocNone); | |
741 } | |
742 | |
743 /* Recreate the auxiliary windows, if needed (required for GL) */ | |
744 if ( vis_change ) | |
745 create_aux_windows(this); | |
746 | |
747 if(screen->flags & SDL_HWPALETTE) { | |
748 /* Since the full-screen window might have got a nonzero background | |
749 colour (0 is white on some displays), we should reset the | |
750 background to 0 here since that is what the user expects | |
751 with a private colormap */ | |
752 XSetWindowBackground(SDL_Display, FSwindow, 0); | |
753 XClearWindow(SDL_Display, FSwindow); | |
754 } | |
755 | |
756 /* resize the (possibly new) window manager window */ | |
757 if( !SDL_windowid ) { | |
758 X11_SetSizeHints(this, w, h, flags); | |
759 current_w = w; | |
760 current_h = h; | |
761 XResizeWindow(SDL_Display, WMwindow, w, h); | |
762 } | |
763 | |
764 /* Create (or use) the X11 display window */ | |
765 if ( !SDL_windowid ) { | |
766 if ( flags & SDL_OPENGL ) { | |
767 if ( X11_GL_CreateWindow(this, w, h) < 0 ) { | |
768 return(-1); | |
769 } | |
770 } else { | |
771 XSetWindowAttributes swa; | |
772 | |
773 swa.background_pixel = 0; | |
774 swa.border_pixel = 0; | |
775 swa.colormap = SDL_XColorMap; | |
776 SDL_Window = XCreateWindow(SDL_Display, WMwindow, | |
777 0, 0, w, h, 0, depth, | |
778 InputOutput, SDL_Visual, | |
779 CWBackPixel | CWBorderPixel | |
780 | CWColormap, &swa); | |
781 } | |
782 /* Only manage our input if we own the window */ | |
783 XSelectInput(SDL_Display, SDL_Window, | |
784 ( EnterWindowMask | LeaveWindowMask | |
785 | ButtonPressMask | ButtonReleaseMask | |
786 | PointerMotionMask | ExposureMask )); | |
787 } | |
788 | |
789 /* Create the graphics context here, once we have a window */ | |
790 if ( flags & SDL_OPENGL ) { | |
791 if ( X11_GL_CreateContext(this) < 0 ) { | |
792 return(-1); | |
793 } else { | |
794 screen->flags |= SDL_OPENGL; | |
795 } | |
796 } else { | |
797 XGCValues gcv; | |
798 | |
799 gcv.graphics_exposures = False; | |
800 SDL_GC = XCreateGC(SDL_Display, SDL_Window, | |
801 GCGraphicsExposures, &gcv); | |
802 if ( ! SDL_GC ) { | |
803 SDL_SetError("Couldn't create graphics context"); | |
804 return(-1); | |
805 } | |
806 } | |
807 | |
808 /* Set our colormaps when not setting a GL mode */ | |
809 if ( ! (flags & SDL_OPENGL) ) { | |
810 XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap); | |
811 if( !SDL_windowid ) { | |
812 XSetWindowColormap(SDL_Display, FSwindow, SDL_XColorMap); | |
813 XSetWindowColormap(SDL_Display, WMwindow, SDL_XColorMap); | |
814 } | |
815 } | |
816 | |
817 #if 0 /* This is an experiment - are the graphics faster now? - nope. */ | |
818 if ( getenv("SDL_VIDEO_X11_BACKINGSTORE") ) | |
819 #endif | |
820 /* Cache the window in the server, when possible */ | |
821 { | |
822 Screen *xscreen; | |
823 XSetWindowAttributes a; | |
824 | |
825 xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen); | |
826 a.backing_store = DoesBackingStore(xscreen); | |
827 if ( a.backing_store != NotUseful ) { | |
828 XChangeWindowAttributes(SDL_Display, SDL_Window, | |
829 CWBackingStore, &a); | |
830 } | |
831 } | |
832 | |
833 /* Update the internal keyboard state */ | |
834 X11_SetKeyboardState(SDL_Display, NULL); | |
835 | |
444
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
836 /* When the window is first mapped, ignore non-modifier keys */ |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
837 { |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
838 Uint8 *keys = SDL_GetKeyState(NULL); |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
839 for ( i = 0; i < SDLK_LAST; ++i ) { |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
840 switch (i) { |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
841 case SDLK_NUMLOCK: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
842 case SDLK_CAPSLOCK: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
843 case SDLK_LCTRL: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
844 case SDLK_RCTRL: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
845 case SDLK_LSHIFT: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
846 case SDLK_RSHIFT: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
847 case SDLK_LALT: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
848 case SDLK_RALT: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
849 case SDLK_LMETA: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
850 case SDLK_RMETA: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
851 case SDLK_MODE: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
852 break; |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
853 default: |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
854 keys[i] = SDL_RELEASED; |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
855 break; |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
856 } |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
857 } |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
858 } |
406b12a17b15
Only modifier key state is noted when X11 window opens
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
859 |
0 | 860 /* Map them both and go fullscreen, if requested */ |
861 if ( ! SDL_windowid ) { | |
862 XMapWindow(SDL_Display, SDL_Window); | |
863 XMapWindow(SDL_Display, WMwindow); | |
160 | 864 X11_WaitMapped(this, WMwindow); |
0 | 865 if ( flags & SDL_FULLSCREEN ) { |
866 screen->flags |= SDL_FULLSCREEN; | |
867 X11_EnterFullScreen(this); | |
868 } else { | |
869 screen->flags &= ~SDL_FULLSCREEN; | |
870 } | |
871 } | |
872 return(0); | |
873 } | |
874 | |
875 static int X11_ResizeWindow(_THIS, | |
876 SDL_Surface *screen, int w, int h, Uint32 flags) | |
877 { | |
878 if ( ! SDL_windowid ) { | |
879 /* Resize the window manager window */ | |
880 X11_SetSizeHints(this, w, h, flags); | |
881 current_w = w; | |
882 current_h = h; | |
883 XResizeWindow(SDL_Display, WMwindow, w, h); | |
884 | |
885 /* Resize the fullscreen and display windows */ | |
886 if ( flags & SDL_FULLSCREEN ) { | |
887 if ( screen->flags & SDL_FULLSCREEN ) { | |
888 X11_ResizeFullScreen(this); | |
889 } else { | |
890 screen->flags |= SDL_FULLSCREEN; | |
891 X11_EnterFullScreen(this); | |
892 } | |
893 } else { | |
894 if ( screen->flags & SDL_FULLSCREEN ) { | |
895 screen->flags &= ~SDL_FULLSCREEN; | |
896 X11_LeaveFullScreen(this); | |
897 } | |
898 } | |
899 XResizeWindow(SDL_Display, SDL_Window, w, h); | |
900 } | |
901 return(0); | |
902 } | |
903 | |
904 SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, | |
905 int width, int height, int bpp, Uint32 flags) | |
906 { | |
907 Uint32 saved_flags; | |
908 | |
909 /* Lock the event thread, in multi-threading environments */ | |
910 SDL_Lock_EventThread(); | |
911 | |
912 /* Check the combination of flags we were passed */ | |
913 if ( flags & SDL_FULLSCREEN ) { | |
914 /* Clear fullscreen flag if not supported */ | |
915 if ( SDL_windowid ) { | |
916 flags &= ~SDL_FULLSCREEN; | |
917 } | |
918 } | |
919 | |
920 /* Flush any delayed updates */ | |
921 XSync(GFX_Display, False); | |
922 | |
923 /* Set up the X11 window */ | |
924 saved_flags = current->flags; | |
925 if (SDL_Window && (saved_flags&SDL_OPENGL) == (flags&SDL_OPENGL) | |
926 && bpp == current->format->BitsPerPixel) { | |
927 if (X11_ResizeWindow(this, current, width, height, flags) < 0) { | |
928 current = NULL; | |
929 goto done; | |
930 } | |
931 } else { | |
932 if (X11_CreateWindow(this,current,width,height,bpp,flags) < 0) { | |
933 current = NULL; | |
934 goto done; | |
935 } | |
936 } | |
937 | |
938 /* Set up the new mode framebuffer */ | |
939 if ( ((current->w != width) || (current->h != height)) || | |
940 ((saved_flags&SDL_OPENGL) != (flags&SDL_OPENGL)) ) { | |
941 current->w = width; | |
942 current->h = height; | |
943 current->pitch = SDL_CalculatePitch(current); | |
944 X11_ResizeImage(this, current, flags); | |
945 } | |
946 current->flags |= (flags&(SDL_RESIZABLE|SDL_NOFRAME)); | |
947 | |
948 done: | |
949 /* Release the event thread */ | |
950 XSync(SDL_Display, False); | |
951 SDL_Unlock_EventThread(); | |
952 | |
953 /* We're done! */ | |
954 return(current); | |
955 } | |
956 | |
957 static int X11_ToggleFullScreen(_THIS, int on) | |
958 { | |
959 Uint32 event_thread; | |
960 | |
961 /* Don't switch if we don't own the window */ | |
962 if ( SDL_windowid ) { | |
963 return(0); | |
964 } | |
965 | |
966 /* Don't lock if we are the event thread */ | |
967 event_thread = SDL_EventThreadID(); | |
968 if ( event_thread && (SDL_ThreadID() == event_thread) ) { | |
969 event_thread = 0; | |
970 } | |
971 if ( event_thread ) { | |
972 SDL_Lock_EventThread(); | |
973 } | |
974 if ( on ) { | |
975 this->screen->flags |= SDL_FULLSCREEN; | |
976 X11_EnterFullScreen(this); | |
977 } else { | |
978 this->screen->flags &= ~SDL_FULLSCREEN; | |
979 X11_LeaveFullScreen(this); | |
980 } | |
981 X11_RefreshDisplay(this); | |
982 if ( event_thread ) { | |
983 SDL_Unlock_EventThread(); | |
984 } | |
985 SDL_ResetKeyboard(); | |
986 return(1); | |
987 } | |
988 | |
989 /* Update the current mouse state and position */ | |
990 static void X11_UpdateMouse(_THIS) | |
991 { | |
992 Window u1; int u2; | |
993 Window current_win; | |
994 int x, y; | |
995 unsigned int mask; | |
996 | |
997 /* Lock the event thread, in multi-threading environments */ | |
998 SDL_Lock_EventThread(); | |
999 if ( XQueryPointer(SDL_Display, SDL_Window, &u1, ¤t_win, | |
1000 &u2, &u2, &x, &y, &mask) ) { | |
1001 if ( (x >= 0) && (x < SDL_VideoSurface->w) && | |
1002 (y >= 0) && (y < SDL_VideoSurface->h) ) { | |
1003 SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS); | |
1004 SDL_PrivateMouseMotion(0, 0, x, y); | |
1005 } else { | |
1006 SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS); | |
1007 } | |
1008 } | |
1009 SDL_Unlock_EventThread(); | |
1010 } | |
1011 | |
1012 /* simple colour distance metric. Supposed to be better than a plain | |
1013 Euclidian distance anyway. */ | |
1014 #define COLOUR_FACTOR 3 | |
1015 #define LIGHT_FACTOR 1 | |
1016 #define COLOUR_DIST(r1, g1, b1, r2, g2, b2) \ | |
1017 (COLOUR_FACTOR * (abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)) \ | |
1018 + LIGHT_FACTOR * abs(r1 + g1 + b1 - (r2 + g2 + b2))) | |
1019 | |
1020 static void allocate_nearest(_THIS, SDL_Color *colors, | |
1021 SDL_Color *want, int nwant) | |
1022 { | |
1023 /* | |
1024 * There is no way to know which ones to choose from, so we retrieve | |
1025 * the entire colormap and try the nearest possible, until we find one | |
1026 * that is shared. | |
1027 */ | |
1028 XColor all[256]; | |
1029 int i; | |
1030 for(i = 0; i < 256; i++) | |
1031 all[i].pixel = i; | |
1032 /* | |
1033 * XQueryColors sets the flags in the XColor struct, so we use | |
1034 * that to keep track of which colours are available | |
1035 */ | |
1036 XQueryColors(GFX_Display, SDL_XColorMap, all, 256); | |
1037 | |
1038 for(i = 0; i < nwant; i++) { | |
1039 XColor *c; | |
1040 int j; | |
1041 int best = 0; | |
1042 int mindist = 0x7fffffff; | |
1043 int ri = want[i].r; | |
1044 int gi = want[i].g; | |
1045 int bi = want[i].b; | |
1046 for(j = 0; j < 256; j++) { | |
1047 int rj, gj, bj, d2; | |
1048 if(!all[j].flags) | |
1049 continue; /* unavailable colour cell */ | |
1050 rj = all[j].red >> 8; | |
1051 gj = all[j].green >> 8; | |
1052 bj = all[j].blue >> 8; | |
1053 d2 = COLOUR_DIST(ri, gi, bi, rj, gj, bj); | |
1054 if(d2 < mindist) { | |
1055 mindist = d2; | |
1056 best = j; | |
1057 } | |
1058 } | |
1059 if(SDL_XPixels[best]) | |
1060 continue; /* already allocated, waste no more time */ | |
1061 c = all + best; | |
1062 if(XAllocColor(GFX_Display, SDL_XColorMap, c)) { | |
1063 /* got it */ | |
236
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1064 colors[c->pixel].r = c->red >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1065 colors[c->pixel].g = c->green >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1066 colors[c->pixel].b = c->blue >> 8; |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1067 ++SDL_XPixels[c->pixel]; |
0 | 1068 } else { |
1069 /* | |
1070 * The colour couldn't be allocated, probably being | |
1071 * owned as a r/w cell by another client. Flag it as | |
1072 * unavailable and try again. The termination of the | |
1073 * loop is guaranteed since at least black and white | |
1074 * are always there. | |
1075 */ | |
1076 c->flags = 0; | |
1077 i--; | |
1078 } | |
1079 } | |
1080 } | |
1081 | |
1082 int X11_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
1083 { | |
1084 int nrej = 0; | |
1085 | |
1086 /* Check to make sure we have a colormap allocated */ | |
1087 if ( SDL_XPixels == NULL ) { | |
1088 return(0); | |
1089 } | |
1090 if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) { | |
1091 /* private writable colormap: just set the colours we need */ | |
1092 XColor *xcmap; | |
1093 int i; | |
1094 xcmap = ALLOCA(ncolors*sizeof(*xcmap)); | |
1095 if(xcmap == NULL) | |
1096 return 0; | |
1097 for ( i=0; i<ncolors; ++i ) { | |
1098 xcmap[i].pixel = i + firstcolor; | |
1099 xcmap[i].red = (colors[i].r<<8)|colors[i].r; | |
1100 xcmap[i].green = (colors[i].g<<8)|colors[i].g; | |
1101 xcmap[i].blue = (colors[i].b<<8)|colors[i].b; | |
1102 xcmap[i].flags = (DoRed|DoGreen|DoBlue); | |
1103 } | |
1104 XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors); | |
1105 XSync(GFX_Display, False); | |
1106 FREEA(xcmap); | |
1107 } else { | |
1108 /* | |
1109 * Shared colormap: We only allocate read-only cells, which | |
1110 * increases the likelyhood of colour sharing with other | |
1111 * clients. The pixel values will almost certainly be | |
1112 * different from the requested ones, so the user has to | |
1113 * walk the colormap and see which index got what colour. | |
1114 * | |
1115 * We can work directly with the logical palette since it | |
1116 * has already been set when we get here. | |
1117 */ | |
1118 SDL_Color *want, *reject; | |
1119 unsigned long *freelist; | |
1120 int i; | |
1121 int nfree = 0; | |
1122 int nc = this->screen->format->palette->ncolors; | |
1123 colors = this->screen->format->palette->colors; | |
1124 freelist = ALLOCA(nc * sizeof(*freelist)); | |
1125 /* make sure multiple allocations of the same cell are freed */ | |
1126 for(i = 0; i < ncolors; i++) { | |
1127 int pixel = firstcolor + i; | |
1128 while(SDL_XPixels[pixel]) { | |
1129 freelist[nfree++] = pixel; | |
1130 --SDL_XPixels[pixel]; | |
1131 } | |
1132 } | |
1133 XFreeColors(GFX_Display, SDL_XColorMap, freelist, nfree, 0); | |
1134 FREEA(freelist); | |
1135 | |
1136 want = ALLOCA(ncolors * sizeof(SDL_Color)); | |
1137 reject = ALLOCA(ncolors * sizeof(SDL_Color)); | |
1138 memcpy(want, colors + firstcolor, ncolors * sizeof(SDL_Color)); | |
1139 /* make sure the user isn't fooled by her own wishes | |
1140 (black is safe, always available in the default colormap) */ | |
1141 memset(colors + firstcolor, 0, ncolors * sizeof(SDL_Color)); | |
1142 | |
1143 /* now try to allocate the colours */ | |
1144 for(i = 0; i < ncolors; i++) { | |
1145 XColor col; | |
1146 col.red = want[i].r << 8; | |
1147 col.green = want[i].g << 8; | |
1148 col.blue = want[i].b << 8; | |
1149 col.flags = DoRed | DoGreen | DoBlue; | |
1150 if(XAllocColor(GFX_Display, SDL_XColorMap, &col)) { | |
1151 /* We got the colour, or at least the nearest | |
1152 the hardware could get. */ | |
1153 colors[col.pixel].r = col.red >> 8; | |
1154 colors[col.pixel].g = col.green >> 8; | |
1155 colors[col.pixel].b = col.blue >> 8; | |
1156 ++SDL_XPixels[col.pixel]; | |
1157 } else { | |
1158 /* | |
1159 * no more free cells, add it to the list | |
1160 * of rejected colours | |
1161 */ | |
1162 reject[nrej++] = want[i]; | |
1163 } | |
1164 } | |
1165 if(nrej) | |
1166 allocate_nearest(this, colors, reject, nrej); | |
1167 FREEA(reject); | |
1168 FREEA(want); | |
1169 } | |
1170 return nrej == 0; | |
1171 } | |
1172 | |
1173 int X11_SetGammaRamp(_THIS, Uint16 *ramp) | |
1174 { | |
1175 int i, ncolors; | |
1176 XColor xcmap[256]; | |
1177 | |
1178 /* See if actually setting the gamma is supported */ | |
1179 if ( SDL_Visual->class != DirectColor ) { | |
1180 SDL_SetError("Gamma correction not supported on this visual"); | |
1181 return(-1); | |
1182 } | |
1183 | |
1184 /* Calculate the appropriate palette for the given gamma ramp */ | |
1185 ncolors = SDL_Visual->map_entries; | |
1186 for ( i=0; i<ncolors; ++i ) { | |
1187 Uint8 c = (256 * i / ncolors); | |
1188 xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c); | |
1189 xcmap[i].red = ramp[0*256+c]; | |
1190 xcmap[i].green = ramp[1*256+c]; | |
1191 xcmap[i].blue = ramp[2*256+c]; | |
1192 xcmap[i].flags = (DoRed|DoGreen|DoBlue); | |
1193 } | |
1194 XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors); | |
1195 XSync(GFX_Display, False); | |
1196 return(0); | |
1197 } | |
1198 | |
1199 /* Note: If we are terminated, this could be called in the middle of | |
1200 another SDL video routine -- notably UpdateRects. | |
1201 */ | |
1202 void X11_VideoQuit(_THIS) | |
1203 { | |
1204 /* Shutdown everything that's still up */ | |
1205 /* The event thread should be done, so we can touch SDL_Display */ | |
1206 if ( SDL_Display != NULL ) { | |
1207 /* Flush any delayed updates */ | |
1208 XSync(GFX_Display, False); | |
1209 | |
1210 /* Start shutting down the windows */ | |
1211 X11_DestroyImage(this, this->screen); | |
1212 X11_DestroyWindow(this, this->screen); | |
1213 X11_FreeVideoModes(this); | |
1214 if ( SDL_XColorMap != SDL_DisplayColormap ) { | |
1215 XFreeColormap(SDL_Display, SDL_XColorMap); | |
1216 } | |
1217 if ( SDL_iconcolors ) { | |
1218 unsigned long pixel; | |
236
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1219 Colormap dcmap = DefaultColormap(SDL_Display, |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1220 SDL_Screen); |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1221 for(pixel = 0; pixel < 256; ++pixel) { |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1222 while(SDL_iconcolors[pixel] > 0) { |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1223 XFreeColors(GFX_Display, |
3f09f52ac2cc
Fixed X11 icon color allocation (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
160
diff
changeset
|
1224 dcmap, &pixel, 1, 0); |
0 | 1225 --SDL_iconcolors[pixel]; |
1226 } | |
1227 } | |
1228 free(SDL_iconcolors); | |
1229 SDL_iconcolors = NULL; | |
1230 } | |
1231 /* Restore gamma settings if they've changed */ | |
1232 if ( SDL_GetAppState() & SDL_APPACTIVE ) { | |
1233 X11_SwapVidModeGamma(this); | |
1234 } | |
1235 | |
1236 /* Free that blank cursor */ | |
1237 if ( SDL_BlankCursor != NULL ) { | |
1238 this->FreeWMCursor(this, SDL_BlankCursor); | |
1239 SDL_BlankCursor = NULL; | |
1240 } | |
1241 | |
1242 /* Close the X11 graphics connection */ | |
1243 if ( GFX_Display != NULL ) { | |
1244 XCloseDisplay(GFX_Display); | |
1245 GFX_Display = NULL; | |
1246 } | |
1247 | |
1248 /* Close the X11 display connection */ | |
1249 XCloseDisplay(SDL_Display); | |
1250 SDL_Display = NULL; | |
1251 | |
1252 /* Reset the X11 error handlers */ | |
1253 if ( XIO_handler ) { | |
1254 XSetIOErrorHandler(XIO_handler); | |
1255 } | |
1256 if ( X_handler ) { | |
1257 XSetErrorHandler(X_handler); | |
1258 } | |
1259 | |
1260 /* Unload GL library after X11 shuts down */ | |
1261 X11_GL_UnloadLibrary(this); | |
1262 } | |
1263 if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) { | |
1264 /* Direct screen access, no memory buffer */ | |
1265 this->screen->pixels = NULL; | |
1266 } | |
1267 } | |
1268 |