home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xstatic / part01 / xstatic.c < prev   
Encoding:
C/C++ Source or Header  |  1989-03-20  |  3.3 KB  |  122 lines

  1. /*
  2. ** Portions written by Tim Bray AND
  3. ** Portions stolen from 'xphoon' with grateful acknowledgement are
  4. **  Copyright (C) 1988 by Jef Poskanzer and Craig Leres.
  5. **
  6. ** Permission to use, copy, modify, and distribute this software and its
  7. ** documentation for any purpose and without fee is hereby granted, provided
  8. ** that the above copyright notice appear in all copies and that both that
  9. ** copyright notice and this permission notice appear in supporting
  10. ** documentation.  This software is provided "as is" without express or
  11. ** implied warranty.
  12. */
  13.  
  14. #include <X11/Xlib.h>
  15. #include <X11/Xatom.h>
  16. #include <stdio.h>
  17.  
  18. extern double atof();
  19. extern char * malloc();
  20.  
  21. static char * argv0;
  22.  
  23. main(argc, argv)
  24.   int    argc;
  25.   char * argv[];
  26. {
  27.   XWindowAttributes attributes;
  28.   char *        bits;
  29.   int               bytecount;
  30.   char *        display = NULL;
  31.   Display *        dpy = NULL;
  32.   void          genbits();
  33.   double        percent = 0.5;
  34.  
  35.   argv0 = argv[0];
  36.   if ((dpy = XOpenDisplay(display)) == 0)
  37.   {
  38.     fprintf(stderr, "%s: Can't open display \"%s\"\n", argv0,
  39.       XDisplayName(display));
  40.     exit(1);
  41.   }
  42.  
  43.   /* make sure percent is reasonable */
  44.   if (argc > 1)
  45.     percent = atof(argv[1]) / 100.0;
  46.   if (percent < 0.0 || percent > 1.0)
  47.   {
  48.     fprintf(stderr, "%s: bogus percentage\n", argv0);
  49.     exit(1);
  50.   }
  51.  
  52.   /* how many bits to fill the window? */
  53.   XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &attributes);
  54.  
  55.   /* compute the byte count, rounding up, and get some memory */
  56.   bytecount = ((attributes.width * attributes.height) + 7) / 8;
  57.   if ((bits = malloc((unsigned) bytecount)) == NULL)
  58.   {
  59.     fprintf(stderr, "%s: Can't malloc\n", argv0);
  60.     exit(1);
  61.   }
  62.  
  63.   genbits(percent, bytecount, bits);
  64.   setroot(attributes.width, attributes.height, bits, dpy);
  65.   XCloseDisplay(dpy);
  66.   exit(0);
  67. }
  68.  
  69. setroot(w, h, bits, dpy)
  70.   int w, h;
  71.   char *bits;
  72.   Display * dpy;
  73. {
  74.   Pixmap bitmap;
  75.   Pixmap pixmap;
  76.   GC gc;
  77.   XGCValues gcv;
  78.   unsigned long length, after;
  79.   int format;
  80.   Atom prop, type;
  81.   unsigned char *data;
  82.  
  83.   bitmap = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), bits, w, h);
  84.   if (bitmap == 0)
  85.   {
  86.     fprintf(stderr, "%s: Unable to store Bitmap\n", argv0);
  87.     exit(1);
  88.   }
  89.  
  90.   /* xphoon had these the other way round - why? */
  91.   gcv.background = BlackPixel(dpy,DefaultScreen(dpy));
  92.   gcv.foreground = WhitePixel(dpy,DefaultScreen(dpy));
  93.   gc = XCreateGC(dpy, DefaultRootWindow(dpy), GCForeground|GCBackground, &gcv);
  94.  
  95.   pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), w, h,
  96.          DefaultDepth(dpy, DefaultScreen(dpy)));
  97.   if (pixmap == 0)
  98.   {
  99.     fprintf(stderr, "%s: Unable to create Pixmap", argv0);
  100.     exit(1);
  101.   }
  102.  
  103.   XCopyPlane(dpy, bitmap, pixmap, gc, 0, 0, w, h, 0, 0, 1L);
  104.   XSetWindowBackgroundPixmap(dpy, DefaultRootWindow(dpy), pixmap);
  105.   XFreeGC(dpy, gc);
  106.   XFreePixmap(dpy, bitmap);
  107.   XFreePixmap(dpy, pixmap);
  108.   XClearWindow(dpy, DefaultRootWindow(dpy));
  109.   XFlush(dpy);
  110.  
  111.   /* Not sure what this garbage does, but xsetroot has it, so... */
  112.   prop = XInternAtom(dpy, "_XSETROOT_ID", False);
  113.   (void) XGetWindowProperty(dpy, DefaultRootWindow(dpy), prop, 0L, 1L, True,
  114.        AnyPropertyType, &type, &format, &length, &after, &data);
  115.   if ((type == XA_PIXMAP) && (format == 32) && (length == 1) && (after == 0))
  116.     XKillClient(dpy, *((Pixmap *)data));
  117.   else if (type != None)
  118.     fprintf(stderr, "%s: warning: _XSETROOT_ID property is garbage\n", argv0);
  119.   XFlush(dpy);
  120. }
  121.  
  122.