1 /* xscreensaver, Copyright (c) 1993-2017 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* The MIT-SHM (Shared Memory) extension is pretty tricky to use.
13 This file contains the common boiler-plate for creating a shared
14 XImage structure, and for making sure that the shared memory segments
15 get allocated and shut down cleanly.
17 This code currently deals only with shared XImages, not with shared Pixmaps.
18 It also doesn't use "completion events", but so far that doesn't seem to
19 be a problem (and I'm not entirely clear on when they would actually be
22 If you don't have man pages for this extension, see
23 https://www.x.org/releases/current/doc/xextproto/shm.html
25 (This document seems not to ever remain available on the web in one place
26 for very long; you can search for it by the title, "MIT-SHM -- The MIT
27 Shared Memory Extension".)
29 To monitor the system's shared memory segments, run "ipcs -m".
36 #include <errno.h> /* for perror() */
41 # include <X11/Xutil.h> /* for XDestroyImage() */
45 #include "resources.h" /* for get_string_resource() */
46 #include "thread_util.h" /* for thread_malloc() */
49 # include <X11/Xmu/Error.h>
52 extern char *progname;
55 /* The documentation for the XSHM extension implies that if the server
56 supports XSHM but is not the local machine, the XShm calls will return
57 False; but this turns out not to be the case. Instead, the server
58 throws a BadAccess error. So, we need to catch X errors around all
59 of our XSHM calls, sigh.
62 #ifdef HAVE_XSHM_EXTENSION
64 static Bool shm_got_x_error = False;
65 XErrorHandler old_handler = 0;
67 shm_ehandler (Display *dpy, XErrorEvent *error)
69 shm_got_x_error = True;
72 fprintf (stderr, "\n%s: ignoring X error from XSHM:\n", progname);
73 XmuPrintDefaultErrorMessage (dpy, error, stderr);
74 fprintf (stderr, "\n");
81 #define CATCH_X_ERROR(DPY) do { \
82 XSync((DPY), False); \
83 shm_got_x_error = False; \
84 if (old_handler != shm_ehandler) \
85 old_handler = XSetErrorHandler (shm_ehandler); \
88 #define UNCATCH_X_ERROR(DPY) do { \
89 XSync((DPY), False); \
91 XSetErrorHandler (old_handler); \
95 #endif /* HAVE_XSHM_EXTENSION */
101 fprintf(stderr, "%s: %s\n", progname, strerror(err));
105 create_fallback (Display *dpy, Visual *visual,
107 int format, XShmSegmentInfo *shm_info,
108 unsigned int width, unsigned int height)
110 XImage *image = XCreateImage (dpy, visual, depth, format, 0, NULL,
111 width, height, BitmapPad(dpy), 0);
112 shm_info->shmid = -1;
115 print_error (ENOMEM);
117 /* Sometimes the XImage data needs to be aligned, such as for SIMD (SSE2
118 in Fireworkx), or multithreading (AnalogTV).
120 int error = thread_malloc ((void **)&image->data, dpy,
121 image->height * image->bytes_per_line);
124 XDestroyImage (image);
127 memset (image->data, 0, image->height * image->bytes_per_line);
136 create_xshm_image (Display *dpy, Visual *visual,
138 int format, XShmSegmentInfo *shm_info,
139 unsigned int width, unsigned int height)
141 #ifndef HAVE_XSHM_EXTENSION
143 return create_fallback (dpy, visual, depth, format, shm_info, width, height);
145 #else /* HAVE_XSHM_EXTENSION */
149 if (!get_boolean_resource(dpy, "useSHM", "Boolean") ||
150 !XShmQueryExtension (dpy)) {
151 return create_fallback (dpy, visual, depth, format, shm_info,
156 image = XShmCreateImage(dpy, visual, depth,
157 format, NULL, shm_info, width, height);
158 UNCATCH_X_ERROR(dpy);
160 return create_fallback (dpy, visual, depth, format, shm_info,
164 fprintf(stderr, "\n%s: XShmCreateImage(... %d, %d)\n", progname,
168 shm_info->shmid = shmget(IPC_PRIVATE,
169 image->bytes_per_line * image->height,
172 fprintf(stderr, "%s: shmget(IPC_PRIVATE, %d, IPC_CREAT | 0777) ==> %d\n",
173 progname, image->bytes_per_line * image->height, shm_info->shmid);
176 if (shm_info->shmid == -1)
179 sprintf (buf, "%s: shmget failed", progname);
181 XDestroyImage (image);
187 shm_info->readOnly = False;
188 image->data = shm_info->shmaddr = shmat(shm_info->shmid, 0, 0);
191 fprintf(stderr, "%s: shmat(%d, 0, 0) ==> %d\n", progname,
192 shm_info->shmid, (int) image->data);
196 status = XShmAttach(dpy, shm_info);
197 UNCATCH_X_ERROR(dpy);
203 fprintf (stderr, "%s: XShmAttach failed!\n", progname);
204 XDestroyImage (image);
206 shmdt (shm_info->shmaddr);
211 fprintf(stderr, "%s: XShmAttach(dpy, shm_info) ==> True\n", progname);
216 /* Delete the shared segment right now; the segment won't actually
217 go away until both the client and server have deleted it. The
218 server will delete it as soon as the client disconnects, so we
219 should delete our side early in case of abnormal termination.
220 (And note that, in the context of xscreensaver, abnormal
221 termination is the rule rather than the exception, so this would
222 leak like a sieve if we didn't do this...)
224 #### Are we leaking anyway? Perhaps because of the window of
225 opportunity between here and the XShmAttach call above, during
226 which we might be killed? Do we need to establish a signal
227 handler for this case?
229 shmctl (shm_info->shmid, IPC_RMID, 0);
232 fprintf(stderr, "%s: shmctl(%d, IPC_RMID, 0)\n\n", progname,
238 return create_fallback (dpy, visual, depth, format, shm_info,
244 #endif /* HAVE_XSHM_EXTENSION */
249 put_xshm_image (Display *dpy, Drawable d, GC gc, XImage *image,
250 int src_x, int src_y, int dest_x, int dest_y,
251 unsigned int width, unsigned int height,
252 XShmSegmentInfo *shm_info)
254 #ifdef HAVE_XSHM_EXTENSION
255 assert (shm_info); /* Don't just s/XShmPutImage/put_xshm_image/. */
256 if (shm_info->shmid != -1) {
257 /* XShmPutImage is asynchronous; the contents of the XImage must not be
258 modified until the server has placed the pixels on the screen and the
259 client has received an XShmCompletionEvent. Breaking this rule can cause
260 tearing. That said, put_xshm_image doesn't provide a send_event
261 parameter, so we're always breaking this rule. Not that it seems to
262 matter; everything (so far) looks fine without it.
264 ####: Add a send_event parameter. And fake it for XPutImage.
266 return XShmPutImage (dpy, d, gc, image, src_x, src_y, dest_x, dest_y,
267 width, height, False);
269 #endif /* HAVE_XSHM_EXTENSION */
271 return XPutImage (dpy, d, gc, image, src_x, src_y, dest_x, dest_y,
277 get_xshm_image (Display *dpy, Drawable d, XImage *image, int x, int y,
278 unsigned long plane_mask, XShmSegmentInfo *shm_info)
280 #ifdef HAVE_XSHM_EXTENSION
281 if (shm_info->shmid != -1) {
282 return XShmGetImage (dpy, d, image, x, y, plane_mask);
284 #endif /* HAVE_XSHM_EXTENSION */
285 return XGetSubImage (dpy, d, x, y, image->width, image->height, plane_mask,
286 image->format, image, 0, 0) != NULL;
291 destroy_xshm_image (Display *dpy, XImage *image, XShmSegmentInfo *shm_info)
293 #ifdef HAVE_XSHM_EXTENSION
294 if (shm_info->shmid == -1) {
295 #endif /* HAVE_XSHM_EXTENSION */
297 /* Don't let XDestroyImage free image->data. */
298 thread_free (image->data);
300 XDestroyImage (image);
303 #ifdef HAVE_XSHM_EXTENSION
309 status = XShmDetach (dpy, shm_info);
310 UNCATCH_X_ERROR(dpy);
314 fprintf (stderr, "%s: XShmDetach failed!\n", progname);
317 fprintf (stderr, "%s: XShmDetach(dpy, shm_info) ==> True\n", progname);
320 XDestroyImage (image);
323 status = shmdt (shm_info->shmaddr);
328 sprintf (buf, "%s: shmdt(0x%lx) failed", progname,
329 (unsigned long) shm_info->shmaddr);
334 fprintf (stderr, "%s: shmdt(shm_info->shmaddr) ==> 0\n", progname);
339 #endif /* HAVE_XSHM_EXTENSION */