home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / contrib / dvx / demos / plaid / plaid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-15  |  7.5 KB  |  275 lines

  1. #ifndef lint
  2. static char    *rcsid = "$XConsortium: plaid.c,v 1.12 89/12/14 09:54:16 rws Exp $";
  3. #endif
  4. /***********************************************************
  5. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  6. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  7.  
  8.              /gh10/X.V11R2/demos/plaid           All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the names of Digital or MIT not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25.  
  26. ******************************************************************/
  27. #if defined (PROTO) || defined(PROTO1) /* JDC 91/04/22 */
  28. #define NeedFunctionPrototypes 0
  29. #define NeedFunctionPtrPrototypes 1
  30. #include "plaid.fd1"
  31. #endif
  32.  
  33. #if PROTO
  34. #include "plaid.fd2"
  35. #endif
  36.  
  37. #include "X11/Xlib.h"
  38. #include "X11/Xatom.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <errno.h>
  42.  
  43. char *ProgramName;
  44.  
  45. int rand();
  46.  
  47. Window XCreateWindow();
  48.  
  49. Window myWin, newWin, threeWin;
  50.  
  51. Display *dpy;
  52.  
  53. #define NUMRECTS 10
  54. XRectangle rects[NUMRECTS];
  55. GC gc;
  56.  
  57. usage ()
  58. {
  59.     fprintf (stderr, "usage:  %s [-options ...]\n\n", ProgramName);
  60.     fprintf (stderr, "where options include:\n");
  61.     fprintf (stderr, "    -display host:dpy        X server to use\n");
  62.     fprintf (stderr, "    -geometry geom           geometry of window\n");
  63.     fprintf (stderr, "    -b                       use backing store\n");
  64.     fprintf (stderr, "    -fg color                set foreground color\n");
  65.     fprintf (stderr, "    -bg color                set background color\n");
  66.     fprintf (stderr, "    -bd color                set border color\n");
  67.     fprintf (stderr, "    -bw width                set border width\n");
  68.     fprintf (stderr, "\n");
  69.     exit (1);
  70. }
  71.  
  72.  
  73. main(argc, argv)
  74. int    argc;
  75. char    *argv[];
  76. {
  77.     int i, j ;
  78.     char *geom = NULL;
  79.     int winx, winy, winw, winh;
  80.     register int xdir, ydir;
  81.     register int xoff, yoff;
  82.     register int centerX, centerY;
  83.     XGCValues xgcv;
  84.     XSetWindowAttributes xswa;
  85.     Colormap map;
  86.     unsigned int bw = 1;
  87.     char *display = NULL;
  88.     Status status;
  89.     char *fg = NULL;
  90.     char *bg = NULL;
  91.     char *bd = NULL;
  92.     long fg_pix, bg_pix, bd_pix; /* Was int. POHC 90/09/17 */
  93.     XColor fg_def, fg_exact, bg_def, bg_exact, bd_def, bd_exact;
  94.     int bs = NotUseful;
  95.     Visual visual;
  96.  
  97.     ProgramName = argv[0];
  98.  
  99.     for (i=1; i < argc; i++)
  100.     {
  101.     char *arg = argv[i];
  102.  
  103.     if (arg[0] == '-') {
  104.         switch (arg[1]) {
  105.         case 'd':            /* -display host:dpy */
  106.             if (++i >= argc) usage ();
  107.             display = argv[i];
  108.             continue;
  109.         case 'g':            /* -geometry host:dpy */
  110.             if (++i >= argc) usage ();
  111.             geom = argv[i];
  112.             continue;
  113.         case 'b':            /* -b or -bg or -bd */
  114.             if (!strcmp(argv[i], "-bg")) {
  115.             if (++i >= argc) usage ();
  116.             bg = argv[i];
  117.             } else if (!strcmp(argv[i], "-bd")) {
  118.             if (++i >= argc) usage ();
  119.             bd = argv[i];
  120.             } else if (!strcmp(argv[i], "-bw")) {
  121.             if (++i >= argc) usage ();
  122.             bw = atoi(argv[i]);
  123.             } else
  124.             bs = Always;
  125.             continue;
  126.         case 'f':            /* assume -fg */
  127.             if (++i >= argc) usage ();
  128.             fg = argv[i];
  129.             continue;
  130.         default:
  131.             usage ();
  132.         }
  133.     } else if (argv [i] [0] == '=')     /* obsolete */
  134.             geom = argv[i];
  135.     else
  136.         usage ();
  137.     }
  138.  
  139.     if (!(dpy = XOpenDisplay(display)))
  140.     {
  141.     perror("Cannot open display\n");
  142.     exit(-1);
  143.     }
  144.  
  145.     map = XDefaultColormap(dpy,DefaultScreen(dpy));
  146.     if (fg) {
  147.     status = XAllocNamedColor(dpy, map, fg, &fg_def, &fg_exact);
  148.     fg_pix = status ? fg_def.pixel : WhitePixel(dpy, DefaultScreen(dpy));
  149.     } else
  150.     fg_pix = WhitePixel(dpy, DefaultScreen(dpy));
  151.  
  152.     if (bg) {
  153.     status = XAllocNamedColor(dpy, map, bg, &bg_def, &bg_exact);
  154.     bg_pix = status ? bg_def.pixel : BlackPixel(dpy, DefaultScreen(dpy));
  155.     } else
  156.     bg_pix = BlackPixel(dpy, DefaultScreen(dpy));
  157.  
  158.     if (bd) {
  159.     status = XAllocNamedColor(dpy, map, bd, &bd_def, &bd_exact);
  160.     bd_pix = status ? bd_def.pixel : WhitePixel(dpy, DefaultScreen(dpy));
  161.     } else
  162.     bd_pix = WhitePixel(dpy, DefaultScreen(dpy));
  163.  
  164.     winx = 0;
  165.     winy = 0;
  166.     winw = 101;
  167.     winh = 201;
  168.  
  169.     if (geom) 
  170.     {
  171.         (void) XParseGeometry(geom, &winx, &winy,
  172.                   (unsigned int *)&winw,
  173.                   (unsigned int *)&winh);
  174.     }
  175.  
  176.     xswa.backing_store = bs;
  177.     xswa.event_mask = ExposureMask | StructureNotifyMask;
  178.     xswa.background_pixel = bg_pix;
  179.     xswa.border_pixel = bd_pix;
  180.     visual.visualid = CopyFromParent;
  181.     myWin = XCreateWindow(dpy,
  182.         RootWindow(dpy, DefaultScreen(dpy)),
  183.         winx, winy, winw, winh, bw, 
  184.         DefaultDepth(dpy, DefaultScreen(dpy)), InputOutput,
  185.         &visual, 
  186.             CWEventMask | CWBackingStore | CWBorderPixel | CWBackPixel, 
  187.         &xswa);
  188.  
  189.     XChangeProperty(dpy, myWin, XA_WM_NAME, XA_STRING, 8,
  190.         PropModeReplace, (unsigned char *)"Plaid", 5);
  191.     XMapWindow(dpy, myWin);
  192.  
  193.     xgcv.foreground = fg_pix;
  194.     xgcv.function = GXinvert;
  195.     xgcv.plane_mask = fg_pix ^ bg_pix;
  196.     xgcv.fill_style = FillSolid;
  197.     xgcv.graphics_exposures = False;
  198.     gc = XCreateGC(dpy, myWin,
  199.         GCForeground | GCFunction | GCPlaneMask | GCFillStyle |
  200.         GCGraphicsExposures, &xgcv);
  201.     j=0;
  202.     while(1)
  203.     {
  204.     XEvent pe;
  205.     XExposeEvent *ee;
  206.     XConfigureEvent *ce;
  207.  
  208.         XNextEvent(dpy, &pe);    /* this should get first exposure event */    
  209.     switch (pe.type) {
  210.       case Expose:
  211.         ee = (XExposeEvent *) &pe;
  212.         while (ee->count)
  213.         {
  214.                 XNextEvent(dpy, &pe);        
  215.             ee = (XExposeEvent *) &pe;
  216.         }
  217.         break;
  218.       case ConfigureNotify:
  219.         ce = (XConfigureEvent *)&pe;
  220.         winx = ce->x;
  221.         winy = ce->y;
  222.         winw = ce->width;
  223.         winh = ce->height;
  224.         break;
  225.       case CirculateNotify:
  226.       case DestroyNotify:
  227.       case GravityNotify:
  228.       case MapNotify:
  229.       case ReparentNotify:
  230.       case UnmapNotify:
  231.         break;
  232.       default:
  233.         printf("Unknown event type: %d\n", pe.type);
  234.     }
  235.  
  236.     printf("PLAID: Dealing with exposures\n");    
  237.     XClearArea(dpy, myWin, 0, 0, winw, winh, 0);
  238.         printf("PLAID: drawing rects\n");
  239.  
  240.     centerX = winw / 2;
  241.     centerY = winh / 2;
  242.     xdir = ydir = -2;
  243.     xoff = yoff = 2;
  244.  
  245.     i = 0;
  246.         while (! XPending(dpy))
  247.     {
  248.         
  249.         rects[i].x = centerX - xoff;
  250.         rects[i].y = centerY - yoff;
  251.         rects[i].width = 2 * xoff;
  252.         rects[i].height = 2 * yoff;
  253.         xoff += xdir;
  254.         yoff += ydir;
  255.         if ((xoff <= 0) || (xoff >= centerX))
  256.         {
  257.         xoff -= 2*xdir;
  258.             xdir = -xdir;
  259.         }
  260.         if ((yoff <= 0) || (yoff >= centerY))
  261.         {
  262.             yoff -= 2*ydir;
  263.         ydir = -ydir;
  264.         }
  265.         if (i == (NUMRECTS - 1))
  266.         {
  267.                 XFillRectangles(dpy, myWin, gc, rects, NUMRECTS);
  268.         i = 0;
  269.         }
  270.         else
  271.         i++;
  272.     }
  273.     }
  274. }
  275.