
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <signal.h>

#define MUSE_PM_STRING "MUSE_PIXMAP_ID"

#define DEBUG 0

Display *dpy;
Window root;
Atom MusePmapAtom;

quit()
{
  XUngrabServer(dpy);
  XDeleteProperty(dpy,root,MusePmapAtom);
  XFlush(dpy);
  exit(0);
}

signals()
{
  signal(SIGHUP, quit);
  signal(SIGINT, quit);
  signal(SIGQUIT, quit);
  signal(SIGTERM, quit);
}

main(argc,argv)
	int argc;
	char *argv[];
{
  int screen;
  int depth;
  Status result;
  Colormap cmap;
  Pixmap pm;
  int i;
  Atom req_type;
  Atom got_type;
  int got_format;
  unsigned long nitems;
  unsigned long bytes_after;
  unsigned char * propval;
  XEvent ev;
  int fd;
  int failed = 0;
  Visual *visual;
  int maj,fev,fer;

  dpy = XOpenDisplay("");
  if(!dpy)
    {
      printf("couldn't connect to X server.\n");
      exit(1);
    }
  XGrabServer(dpy);
  screen = DefaultScreen(dpy);
  depth = DefaultDepth(dpy,screen);
  cmap = DefaultColormap(dpy,screen);
  root = DefaultRootWindow(dpy);
  visual = DefaultVisual(dpy,screen);

  if(depth != 8)
    {
      printf("%s: depth != 8; can't be Parallax\n",argv[0]);
      XUngrabServer(dpy);
      exit(1);
    }

  if(visual->class != PseudoColor)
    {
      printf("%s: class != PseudoColor; can't be Parallax\n",argv[0]);
      XUngrabServer(dpy);
      exit(1);
    }

  if(!XQueryExtension(dpy,"ParallaxVideo",&maj,&fev,&fer))
    {
      printf("%s: server doesn't have Parallax video extension\n",argv[0]);
      XUngrabServer(dpy);
      exit(1);
    }

  /* see if property already exists */
  MusePmapAtom = XInternAtom(dpy,MUSE_PM_STRING,True);
  if(MusePmapAtom == None)
    {
#if DEBUG
      printf("interning new atom '%s'\n",MUSE_PM_STRING);
#endif
      MusePmapAtom = XInternAtom(dpy,MUSE_PM_STRING,False);
    }
#if DEBUG
  printf("MusePmapAtom is %ld\n",MusePmapAtom);
#endif

  signals();

  XGetWindowProperty(dpy,root,MusePmapAtom,0,8192,
		     False,XA_PIXMAP,&got_type,&got_format,
		     &nitems,&bytes_after,&propval);
  if(got_format)
    {
      pm = (Pixmap)(*(long *)propval);
#if DEBUG
      printf("'%s' property already exists; pixmap id is 0x%lx\n",
	     MUSE_PM_STRING,pm);
#endif
      XFree((char *)propval);
      XUngrabServer(dpy);
      exit(0);
    }

  pm = XCreatePixmap(dpy,root,640,480,8);
  if(!pm)
    {
      printf("%s: unable to create 640x480 video pixmap\n",argv[0]);
      XUngrabServer(dpy);
      exit(1);
    }
  {
    Window          root;
    int             x,y;
    unsigned int    w,h,b,d;

    /*
     * important: force the Pixmap to be realized
     */

    XGetGeometry(dpy, pm, &root, &x, &y, &w, &h, &b, &d);
  }

  /* create the property */

  XChangeProperty(dpy, root, MusePmapAtom, XA_PIXMAP,
		  32, PropModeReplace, (unsigned char *)&pm, 1);
  
  XSelectInput(dpy, root, PropertyChangeMask);

  XUngrabServer(dpy);

  /* shut down controlling terminal */

  for(i=0;i<3;i++)
    close(i);

  if(fork())
    exit(0);

  while(1)
    {
      XNextEvent(dpy,&ev);
      if(ev.type == PropertyNotify && !ev.xany.send_event
	 && ev.xproperty.window == root 
	 && ev.xproperty.atom == MusePmapAtom)
	quit();
    }
  
}
