1 /* xscreensaver, Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 2001
2 * by Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
13 /* The MIT-SHM (Shared Memory) extension is pretty tricky to use.
14 This file contains the common boiler-plate for creating a shared
15 XImage structure, and for making sure that the shared memory segments
16 get allocated and shut down cleanly.
18 This code currently deals only with shared XImages, not with shared Pixmaps.
19 It also doesn't use "completion events", but so far that doesn't seem to
20 be a problem (and I'm not entirely clear on when they would actually be
23 If you don't have man pages for this extension, see
24 http://www.x.org/X11R6.8.1/docs/Xext/
26 (This document seems not to ever remain available on the web in one place
27 for very long; you can search for it by the title, "MIT-SHM -- The MIT
28 Shared Memory Extension".)
30 To monitor the system's shared memory segments, run "ipcs -m".
35 #ifdef HAVE_XSHM_EXTENSION /* whole file */
39 #include <errno.h> /* for perror() */
40 #include <X11/Xutil.h> /* for XDestroyImage() */
43 #include "resources.h" /* for get_string_resource() */
46 # include <X11/Xmu/Error.h>
49 extern char *progname;
52 /* The documentation for the XSHM extension implies that if the server
53 supports XSHM but is not the local machine, the XShm calls will return
54 False; but this turns out not to be the case. Instead, the server
55 throws a BadAccess error. So, we need to catch X errors around all
56 of our XSHM calls, sigh.
59 static Bool shm_got_x_error = False;
60 XErrorHandler old_handler = 0;
62 shm_ehandler (Display *dpy, XErrorEvent *error)
64 shm_got_x_error = True;
67 fprintf (stderr, "\n%s: ignoring X error from XSHM:\n", progname);
68 XmuPrintDefaultErrorMessage (dpy, error, stderr);
69 fprintf (stderr, "\n");
76 #define CATCH_X_ERROR(DPY) do { \
77 XSync((DPY), False); \
78 shm_got_x_error = False; \
79 if (old_handler != shm_ehandler) \
80 old_handler = XSetErrorHandler (shm_ehandler); \
83 #define UNCATCH_X_ERROR(DPY) do { \
84 XSync((DPY), False); \
86 XSetErrorHandler (old_handler); \
92 create_xshm_image (Display *dpy, Visual *visual,
94 int format, char *data,
95 XShmSegmentInfo *shm_info,
96 unsigned int width, unsigned int height)
100 if (!get_boolean_resource("useSHM", "Boolean"))
103 if (!XShmQueryExtension (dpy))
107 image = XShmCreateImage(dpy, visual, depth,
108 format, data, shm_info, width, height);
109 UNCATCH_X_ERROR(dpy);
114 fprintf(stderr, "\n%s: XShmCreateImage(... %d, %d)\n", progname,
118 shm_info->shmid = shmget(IPC_PRIVATE,
119 image->bytes_per_line * image->height,
122 fprintf(stderr, "%s: shmget(IPC_PRIVATE, %d, IPC_CREAT | 0777) ==> %d\n",
123 progname, image->bytes_per_line * image->height, shm_info->shmid);
126 if (shm_info->shmid == -1)
129 sprintf (buf, "%s: shmget failed", progname);
131 XDestroyImage (image);
137 shm_info->readOnly = False;
138 image->data = shm_info->shmaddr = shmat(shm_info->shmid, 0, 0);
141 fprintf(stderr, "%s: shmat(%d, 0, 0) ==> %d\n", progname,
142 shm_info->shmid, (int) image->data);
146 status = XShmAttach(dpy, shm_info);
147 UNCATCH_X_ERROR(dpy);
153 fprintf (stderr, "%s: XShmAttach failed!\n", progname);
154 XDestroyImage (image);
156 shmdt (shm_info->shmaddr);
161 fprintf(stderr, "%s: XShmAttach(dpy, shm_info) ==> True\n", progname);
166 /* Delete the shared segment right now; the segment won't actually
167 go away until both the client and server have deleted it. The
168 server will delete it as soon as the client disconnects, so we
169 should delete our side early in case of abnormal termination.
170 (And note that, in the context of xscreensaver, abnormal
171 termination is the rule rather than the exception, so this would
172 leak like a sieve if we didn't do this...)
174 #### Are we leaking anyway? Perhaps because of the window of
175 opportunity between here and the XShmAttach call above, during
176 which we might be killed? Do we need to establish a signal
177 handler for this case?
179 shmctl (shm_info->shmid, IPC_RMID, 0);
182 fprintf(stderr, "%s: shmctl(%d, IPC_RMID, 0)\n\n", progname,
192 destroy_xshm_image (Display *dpy, XImage *image, XShmSegmentInfo *shm_info)
197 status = XShmDetach (dpy, shm_info);
198 UNCATCH_X_ERROR(dpy);
202 fprintf (stderr, "%s: XShmDetach failed!\n", progname);
205 fprintf (stderr, "%s: XShmDetach(dpy, shm_info) ==> True\n", progname);
208 XDestroyImage (image);
211 status = shmdt (shm_info->shmaddr);
216 sprintf (buf, "%s: shmdt(0x%lx) failed", progname,
217 (unsigned long) shm_info->shmaddr);
222 fprintf (stderr, "%s: shmdt(shm_info->shmaddr) ==> 0\n", progname);
229 #endif /* HAVE_XSHM_EXTENSION */