comparison src/video/x11/SDL_x11image.c @ 1168:045f186426e1

Dynamically load X11 libraries like we currently do for alsa, esd, etc. This allows you to run an SDL program on a system without Xlib, since it'll just report the x11 target unavailable at runtime.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 05 Nov 2005 19:53:37 +0000
parents 26a0e2d69d39
children c9b51268668f
comparison
equal deleted inserted replaced
1167:435c2e481299 1168:045f186426e1
32 #include "SDL_endian.h" 32 #include "SDL_endian.h"
33 #include "SDL_events_c.h" 33 #include "SDL_events_c.h"
34 #include "SDL_x11image_c.h" 34 #include "SDL_x11image_c.h"
35 35
36 #ifndef NO_SHARED_MEMORY 36 #ifndef NO_SHARED_MEMORY
37
38 /* Shared memory information */
39 extern int XShmQueryExtension(Display *dpy); /* Not in X11 headers */
40 37
41 /* Shared memory error handler routine */ 38 /* Shared memory error handler routine */
42 static int shm_error; 39 static int shm_error;
43 static int (*X_handler)(Display *, XErrorEvent *) = NULL; 40 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
44 static int shm_errhandler(Display *d, XErrorEvent *e) 41 static int shm_errhandler(Display *d, XErrorEvent *e)
59 if ( shminfo.shmid >= 0 ) { 56 if ( shminfo.shmid >= 0 ) {
60 shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0); 57 shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0);
61 shminfo.readOnly = False; 58 shminfo.readOnly = False;
62 if ( shminfo.shmaddr != (char *)-1 ) { 59 if ( shminfo.shmaddr != (char *)-1 ) {
63 shm_error = False; 60 shm_error = False;
64 X_handler = XSetErrorHandler(shm_errhandler); 61 X_handler = pXSetErrorHandler(shm_errhandler);
65 XShmAttach(SDL_Display, &shminfo); 62 pXShmAttach(SDL_Display, &shminfo);
66 XSync(SDL_Display, True); 63 pXSync(SDL_Display, True);
67 XSetErrorHandler(X_handler); 64 pXSetErrorHandler(X_handler);
68 if ( shm_error ) 65 if ( shm_error )
69 shmdt(shminfo.shmaddr); 66 shmdt(shminfo.shmaddr);
70 } else { 67 } else {
71 shm_error = True; 68 shm_error = True;
72 } 69 }
88 int X11_SetupImage(_THIS, SDL_Surface *screen) 85 int X11_SetupImage(_THIS, SDL_Surface *screen)
89 { 86 {
90 #ifndef NO_SHARED_MEMORY 87 #ifndef NO_SHARED_MEMORY
91 try_mitshm(this, screen); 88 try_mitshm(this, screen);
92 if(use_mitshm) { 89 if(use_mitshm) {
93 SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual, 90 SDL_Ximage = pXShmCreateImage(SDL_Display, SDL_Visual,
94 this->hidden->depth, ZPixmap, 91 this->hidden->depth, ZPixmap,
95 shminfo.shmaddr, &shminfo, 92 shminfo.shmaddr, &shminfo,
96 screen->w, screen->h); 93 screen->w, screen->h);
97 if(!SDL_Ximage) { 94 if(!SDL_Ximage) {
98 XShmDetach(SDL_Display, &shminfo); 95 pXShmDetach(SDL_Display, &shminfo);
99 XSync(SDL_Display, False); 96 pXSync(SDL_Display, False);
100 shmdt(shminfo.shmaddr); 97 shmdt(shminfo.shmaddr);
101 screen->pixels = NULL; 98 screen->pixels = NULL;
102 goto error; 99 goto error;
103 } 100 }
104 this->UpdateRects = X11_MITSHMUpdate; 101 this->UpdateRects = X11_MITSHMUpdate;
111 if ( screen->pixels == NULL ) { 108 if ( screen->pixels == NULL ) {
112 SDL_OutOfMemory(); 109 SDL_OutOfMemory();
113 return -1; 110 return -1;
114 } 111 }
115 bpp = screen->format->BytesPerPixel; 112 bpp = screen->format->BytesPerPixel;
116 SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual, 113 SDL_Ximage = pXCreateImage(SDL_Display, SDL_Visual,
117 this->hidden->depth, ZPixmap, 0, 114 this->hidden->depth, ZPixmap, 0,
118 (char *)screen->pixels, 115 (char *)screen->pixels,
119 screen->w, screen->h, 116 screen->w, screen->h,
120 32, 0); 117 32, 0);
121 if ( SDL_Ximage == NULL ) 118 if ( SDL_Ximage == NULL )
134 } 131 }
135 132
136 void X11_DestroyImage(_THIS, SDL_Surface *screen) 133 void X11_DestroyImage(_THIS, SDL_Surface *screen)
137 { 134 {
138 if ( SDL_Ximage ) { 135 if ( SDL_Ximage ) {
139 XDestroyImage(SDL_Ximage); 136 pXDestroyImage(SDL_Ximage);
140 #ifndef NO_SHARED_MEMORY 137 #ifndef NO_SHARED_MEMORY
141 if ( use_mitshm ) { 138 if ( use_mitshm ) {
142 XShmDetach(SDL_Display, &shminfo); 139 pXShmDetach(SDL_Display, &shminfo);
143 XSync(SDL_Display, False); 140 pXSync(SDL_Display, False);
144 shmdt(shminfo.shmaddr); 141 shmdt(shminfo.shmaddr);
145 } 142 }
146 #endif /* ! NO_SHARED_MEMORY */ 143 #endif /* ! NO_SHARED_MEMORY */
147 SDL_Ximage = NULL; 144 SDL_Ximage = NULL;
148 } 145 }
219 } 216 }
220 217
221 int X11_LockHWSurface(_THIS, SDL_Surface *surface) 218 int X11_LockHWSurface(_THIS, SDL_Surface *surface)
222 { 219 {
223 if ( (surface == SDL_VideoSurface) && blit_queued ) { 220 if ( (surface == SDL_VideoSurface) && blit_queued ) {
224 XSync(GFX_Display, False); 221 pXSync(GFX_Display, False);
225 blit_queued = 0; 222 blit_queued = 0;
226 } 223 }
227 return(0); 224 return(0);
228 } 225 }
229 void X11_UnlockHWSurface(_THIS, SDL_Surface *surface) 226 void X11_UnlockHWSurface(_THIS, SDL_Surface *surface)
242 239
243 for (i = 0; i < numrects; ++i) { 240 for (i = 0; i < numrects; ++i) {
244 if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */ 241 if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */
245 continue; 242 continue;
246 } 243 }
247 XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, 244 pXPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
248 rects[i].x, rects[i].y, 245 rects[i].x, rects[i].y,
249 rects[i].x, rects[i].y, rects[i].w, rects[i].h); 246 rects[i].x, rects[i].y, rects[i].w, rects[i].h);
250 } 247 }
251 if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) { 248 if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
252 XFlush(GFX_Display); 249 pXFlush(GFX_Display);
253 blit_queued = 1; 250 blit_queued = 1;
254 } else { 251 } else {
255 XSync(GFX_Display, False); 252 pXSync(GFX_Display, False);
256 } 253 }
257 } 254 }
258 255
259 static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects) 256 static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects)
260 { 257 {
263 260
264 for ( i=0; i<numrects; ++i ) { 261 for ( i=0; i<numrects; ++i ) {
265 if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */ 262 if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */
266 continue; 263 continue;
267 } 264 }
268 XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, 265 pXShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
269 rects[i].x, rects[i].y, 266 rects[i].x, rects[i].y,
270 rects[i].x, rects[i].y, rects[i].w, rects[i].h, 267 rects[i].x, rects[i].y, rects[i].w, rects[i].h,
271 False); 268 False);
272 } 269 }
273 if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) { 270 if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
274 XFlush(GFX_Display); 271 pXFlush(GFX_Display);
275 blit_queued = 1; 272 blit_queued = 1;
276 } else { 273 } else {
277 XSync(GFX_Display, False); 274 pXSync(GFX_Display, False);
278 } 275 }
279 #endif /* ! NO_SHARED_MEMORY */ 276 #endif /* ! NO_SHARED_MEMORY */
280 } 277 }
281 278
282 /* There's a problem with the automatic refreshing of the display. 279 /* There's a problem with the automatic refreshing of the display.
306 SDL_PrivateExpose(); 303 SDL_PrivateExpose();
307 return; 304 return;
308 } 305 }
309 #ifndef NO_SHARED_MEMORY 306 #ifndef NO_SHARED_MEMORY
310 if ( this->UpdateRects == X11_MITSHMUpdate ) { 307 if ( this->UpdateRects == X11_MITSHMUpdate ) {
311 XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, 308 pXShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
312 0, 0, 0, 0, this->screen->w, this->screen->h, 309 0, 0, 0, 0, this->screen->w, this->screen->h,
313 False); 310 False);
314 } else 311 } else
315 #endif /* ! NO_SHARED_MEMORY */ 312 #endif /* ! NO_SHARED_MEMORY */
316 { 313 {
317 XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, 314 pXPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
318 0, 0, 0, 0, this->screen->w, this->screen->h); 315 0, 0, 0, 0, this->screen->w, this->screen->h);
319 } 316 }
320 XSync(SDL_Display, False); 317 pXSync(SDL_Display, False);
321 } 318 }