1 /* xscreensaver, Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
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.physik.uni-regensburg.de/~scs22156/sofie-0.2/mit-shm.html
25 or in the R6 sources as "xc/doc/specs/Xext/mit-shm.ms".
27 (This document seems not to ever remain available on the web in one place
28 for very long; you can search for it by the title, "MIT-SHM -- The MIT
29 Shared Memory Extension".)
31 To monitor the system's shared memory segments, run "ipcs -m".
36 #ifdef HAVE_XSHM_EXTENSION /* whole file */
38 #include <errno.h> /* for perror() */
39 #include <X11/Xutil.h> /* for XDestroyImage() */
42 #include "resources.h" /* for get_string_resource() */
45 extern char *progname;
48 create_xshm_image (Display *dpy, Visual *visual,
50 int format, char *data,
51 XShmSegmentInfo *shm_info,
52 unsigned int width, unsigned int height)
55 if (!get_boolean_resource("useSHM", "Boolean"))
58 if (!XShmQueryExtension (dpy))
61 image = XShmCreateImage(dpy, visual, depth,
62 format, data, shm_info, width, height);
64 fprintf(stderr, "\n%s: XShmCreateImage(... %d, %d)\n", progname,
68 shm_info->shmid = shmget(IPC_PRIVATE,
69 image->bytes_per_line * image->height,
72 fprintf(stderr, "%s: shmget(IPC_PRIVATE, %d, IPC_CREAT | 0777) ==> %d\n",
73 progname, image->bytes_per_line * image->height, shm_info->shmid);
76 if (shm_info->shmid == -1)
79 sprintf (buf, "%s: shmget failed", progname);
81 XDestroyImage (image);
87 shm_info->readOnly = False;
88 image->data = shm_info->shmaddr = shmat(shm_info->shmid, 0, 0);
91 fprintf(stderr, "%s: shmat(%d, 0, 0) ==> %d\n", progname,
92 shm_info->shmid, (int) image->data);
95 if (!XShmAttach(dpy, shm_info))
97 fprintf (stderr, "%s: XShmAttach failed!\n", progname);
98 XDestroyImage (image);
100 shmdt (shm_info->shmaddr);
105 fprintf(stderr, "%s: XShmAttach(dpy, shm_info) ==> True\n", progname);
110 /* Delete the shared segment right now; the segment won't actually
111 go away until both the client and server have deleted it. The
112 server will delete it as soon as the client disconnects, so we
113 should delete our side early in case of abnormal termination.
114 (And note that, in the context of xscreensaver, abnormal
115 termination is the rule rather than the exception, so this would
116 leak like a sieve if we didn't do this...)
118 #### Are we leaking anyway? Perhaps because of the window of
119 opportunity between here and the XShmAttach call above, during
120 which we might be killed? Do we need to establish a signal
121 handler for this case?
123 shmctl (shm_info->shmid, IPC_RMID, 0);
126 fprintf(stderr, "%s: shmctl(%d, IPC_RMID, 0)\n\n", progname,
134 #endif /* HAVE_XSHM_EXTENSION */