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