#include <vncviewer.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
static XShmSegmentInfo shminfo;
static Bool caughtShmError = False;
static Bool needShmCleanup = False;
void
ShmCleanup()
{
fprintf(stderr,"ShmCleanup called\n");
if (needShmCleanup) {
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid, IPC_RMID, 0);
needShmCleanup = False;
}
}
static int
ShmCreationXErrorHandler(Display *dpy, XErrorEvent *error)
{
caughtShmError = True;
return 0;
}
XImage *
CreateShmImage()
{
XImage *image;
XErrorHandler oldXErrorHandler;
if (!XShmQueryExtension(dpy))
return NULL;
image = XShmCreateImage(dpy, vis, visdepth, ZPixmap, NULL, &shminfo,
si.framebufferWidth, si.framebufferHeight);
if (!image) return NULL;
shminfo.shmid = shmget(IPC_PRIVATE,
image->bytes_per_line * image->height,
IPC_CREAT|0777);
if (shminfo.shmid == -1) {
XDestroyImage(image);
return NULL;
}
shminfo.shmaddr = image->data = shmat(shminfo.shmid, 0, 0);
if (shminfo.shmaddr == (char *)-1) {
XDestroyImage(image);
shmctl(shminfo.shmid, IPC_RMID, 0);
return NULL;
}
shminfo.readOnly = True;
oldXErrorHandler = XSetErrorHandler(ShmCreationXErrorHandler);
XShmAttach(dpy, &shminfo);
XSync(dpy, False);
XSetErrorHandler(oldXErrorHandler);
if (caughtShmError) {
XDestroyImage(image);
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid, IPC_RMID, 0);
return NULL;
}
needShmCleanup = True;
fprintf(stderr,"Using shared memory PutImage\n");
return image;
}