home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / spaceout / spaceout.c < prev   
C/C++ Source or Header  |  1991-08-07  |  18KB  |  887 lines

  1. /*
  2.  *
  3.  * SpaceOut for X11. 
  4.  * Version 1.1
  5.  *
  6.  * By John Pochmara (jpochma@polyslo)
  7.  * at Cal Poly
  8.  * 1988  
  9.  * 
  10.  * Random number generator writen by Pual Shupak.
  11.  *
  12.  * Copyright (c) 1988, John H. Pochmara
  13.  * 
  14.  * You may copy Spaceout in whole or in part as long as you don't
  15.  * try to make money off it, or pretend that you wrote it.
  16.  * 
  17.  *
  18.  * Original SpaceOut writen in Mesa for Xerox workstations by
  19.  * Jef Poskanzer at Versatec, a Xerox Company.
  20.  *
  21.  * 
  22.  * Options:
  23.  *    R - run in root window.
  24.  *    r - reverse screen.
  25.  *    f - full screen.
  26.  *    c - all stars go right down the center of the screen.
  27.  *     C - Color Space.
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <X11/Xlib.h>
  33. #include <X11/Xatom.h>
  34. #include <X11/Xutil.h>
  35.  
  36. #define STIPPLE_DRAW
  37.  
  38. #define NUM_STARS    (70)        /* Number of Stars */
  39. #define STAR_MAXSIZE    (400)        /* Max size of Stars */
  40. #define SPEEDZ        (270)        /* Speed at which Stars move forward */
  41. #define MIN_VISIBLEZ    (400)        /* Closets you can get to a star */
  42. #define MAX_VISIBLEZ    (65000)        /* Farthest away star you can see */
  43. #define MINX        (-20000)    /* Min value of X cordanit */
  44. #define MAXX        (20000)        /* Max value of X cordanit */
  45. #define MINY        (-20000)    /* Min value of Y cordanit */
  46. #define MAXY        (20000)        /* Max value of Y cordanit */
  47. #define MINZ        (30000)        /* Min value of Z cordanit */
  48. #define MAXZ        (65535)        /* Max value of Z cordanit */
  49. #define SCREENZ        (1500)        /* Distance of projected image */
  50. #define RADIUS        (40)        /* Size of a Star at SCREENZ */
  51. #define MAG_RAD        (1)        /* Magnification factor of Star Radius */
  52.  
  53. #define TRUE        (1)
  54. #define FALSE        (0)
  55.  
  56. #define RNDRANGE( min, max ) \
  57. ( ( ( (max) - (min) ) ? ( randomLong() % ( (max) - (min) ) ) : 0 ) + (min) )
  58.  
  59. #define OBSCURED    (1)
  60. #define UNOBSCURED    (0)
  61.  
  62. /*
  63.  * Star Types.
  64.  */
  65. #define ROUND        (0)
  66. #define RINGED        (1)
  67. #define PLANETX        (2)
  68.  
  69. /*
  70.  * Init. Window Postion & Dimension.
  71.  */
  72. #define INT_WIDTH    (400)
  73. #define INT_HEIGHT    (250)
  74. #define INT_X        (50)
  75. #define INT_Y        (50)
  76.  
  77.  
  78. #define WHITE        (0)
  79. #define COLOR1        (1)
  80. #define COLOR2        (2)
  81. #define COLOR3        (3)
  82. #define COLOR4        (4)
  83. #define COLOR5         (5)
  84. #define COLOR6        (6)
  85.  
  86. char *color1[] = {
  87.         "pink",
  88.         "plum",
  89.         "red",
  90.         "IndianRed",
  91.         "MediumVioletRed",
  92.         "OrangeRed",
  93.         "VioletRed",
  94.         ""
  95. };
  96.  
  97. char *color2[] = {
  98.         "Green",
  99.         "DarkGreen",
  100.         "DarkOliveGreen",
  101.         "ForestGreen",
  102.         "LimeGreen",
  103.         "MediumForestGreen",
  104.         "MediumSeaGreen",
  105.         "MediumSpringGreen",
  106.         "PaleGreen",
  107.         "SeaGreen",
  108.         "SpringGreen",
  109.         "YellowGreen",
  110.         ""
  111. };
  112.  
  113. char *color3[] = {
  114.         "Blue",
  115.         "CadetBlue",
  116.         "CornflowerBlue",
  117.         "DarkSlateBlue",
  118.         "LightBlue",
  119.         "LightSteelBlue",
  120.         "MediumBlue",
  121.         "MediumSlateBlue",
  122.         "MidnightBlue",
  123.         "NavyBlue",
  124.         "Navy",
  125.         "SkyBlue",
  126.         "SlateBlue",
  127.         "SteelBlue",
  128.         ""
  129. };
  130.  
  131. char *color4[] = {
  132.         "Yellow",
  133.         "GreenYellow",
  134.         "Wheat",
  135.         "Turquoise",
  136.         "Sienna",
  137.         ""
  138. };
  139.  
  140. char *color5[] = {
  141.         "orange",
  142.         "Khaki",
  143.         "Maroon",
  144.         "Orchid",
  145.         ""
  146. };
  147.  
  148. char *color6[] = {
  149.         "violet",
  150.         "BlueViolet",
  151.         "Salmon",
  152.         "Gold",
  153.         "Goldenrod",
  154.         "Coral",
  155.         ""
  156. };
  157.  
  158. #define MAX_USED_COLORS ( 7 )
  159.  
  160. XColor starColors[MAX_USED_COLORS];
  161.  
  162. typedef struct starrec {
  163.     int sx,sy;
  164.     int x,y,z;
  165.     int radius;
  166.     int displayed;
  167.     int type;
  168.     int color;
  169. } StarRec;
  170.  
  171. StarRec Stars[NUM_STARS];
  172.  
  173. Pixmap Starpix[STAR_MAXSIZE];
  174. Pixmap Ringpix[STAR_MAXSIZE];
  175. Pixmap Xpix[STAR_MAXSIZE];
  176. Display *dpy;
  177. Window SpaceWin;
  178. Window root;
  179. Window parent;
  180. Colormap cmap;
  181. int screen;
  182. int depth;
  183. int SpaceVis;
  184.  
  185. /*
  186.  * Option Var.
  187.  *
  188.  */
  189. int rootwin = FALSE;
  190. int fullscreen = FALSE;
  191. int reverse = FALSE;
  192. int color = FALSE;
  193. int center = FALSE;
  194. char *host = NULL;
  195.  
  196. int winwidth;
  197. int winheight;
  198. int winx;
  199. int winy;
  200.  
  201. static void init(), initwin(), initpos(), initpix(), initstar();
  202. static void initcolors(), setcolor();
  203. static void MoveStars(), DisplayStar(), NewStar(), ShowStar();
  204. static void DrawStar(), Newpixmap(), ProcEvent(), ReDrawStars();
  205. static void ReStart(), ClearStars(), usage(), prev();
  206. static unsigned long int randomLong();
  207.  
  208. main(argc,argv)
  209. int argc;
  210. char *argv[];
  211. {
  212.  
  213.     while(--argc > 0) {
  214.         if(index(argv[argc],':') != 0) {
  215.             if(host != NULL)
  216.                 usage();
  217.             else
  218.                 host = argv[argc];
  219.         } else if(argv[argc][0] == '-') {
  220.             int i = 1;
  221.             
  222.             while(argv[argc][i] != NULL) {
  223.                 switch(argv[argc][i]) {
  224.                     case 'f': fullscreen = TRUE;
  225.                           break;
  226.                     case 'r': reverse = TRUE;
  227.                           break;
  228.                     case 'c': center = TRUE;
  229.                           break;
  230.                     case 'C': color = TRUE;
  231.                           break;
  232.                     case 'R': rootwin = TRUE;
  233.                           break;
  234.                     default:  usage();
  235.                 }
  236.                 i++;
  237.             }
  238.         }
  239.     }
  240.  
  241.     if(host == NULL) {
  242.         if((host = (char *) getenv("DISPLAY")) == NULL) {
  243.             usage();
  244.         }
  245.     }
  246.  
  247.     init(host);
  248.     MoveStars(Stars);
  249.  
  250. }
  251.  
  252. static void init(host)
  253. char *host;
  254. {
  255.  
  256.     initwin(host);
  257.     srandom((int) time(0));
  258.     initstar();
  259.     initpix();
  260.  
  261.     SpaceVis = UNOBSCURED;
  262.  
  263. }
  264.  
  265. static void MoveStars(stars)
  266. StarRec stars[];
  267. {
  268.     int i;
  269.  
  270.     while(TRUE) {
  271.         ProcEvent();
  272.         for(i=0;i<NUM_STARS;i++) {
  273.             if(stars[i].z > SPEEDZ) {
  274.                 stars[i].z = stars[i].z - SPEEDZ;
  275.             } else {
  276.                 stars[i].z = MIN_VISIBLEZ/2;
  277.             }
  278.  
  279.             DisplayStar(&stars[i]);
  280.         }
  281.         XFlush(dpy);
  282.  
  283.     }
  284.  
  285. }
  286.  
  287. static void DisplayStar(star)
  288. StarRec *star;
  289. {
  290.     int scrx;
  291.     int scry;
  292.     int scrradius;
  293.  
  294.  
  295.     if(star->z <= 0 ) star->z = 1;
  296.  
  297.     scrx = (star->x * SCREENZ) / star->z;
  298.     scry = (star->y * SCREENZ) / star->z;
  299.     scrradius = (RADIUS * SCREENZ) / star->z;
  300.  
  301.     scrx = (winwidth/2 + scrx);
  302.     scry = (winheight/2 + scry);
  303.  
  304.     if((scrx == star->sx)
  305.         && (scry == star->sy) 
  306.         && ( scrradius == star->radius ))
  307.             return;
  308.  
  309.     if(star->z < MIN_VISIBLEZ) {
  310.         if(star->displayed == TRUE) {
  311.             ShowStar(star);
  312.             star->displayed = FALSE;
  313.         }
  314.         NewStar(star);
  315.         return;
  316.     }
  317.  
  318.     if(star->z > MAX_VISIBLEZ) {
  319.         if(star->displayed == TRUE) {
  320.             ShowStar(star);
  321.             star->displayed = FALSE;
  322.         }
  323.         return;
  324.     }
  325.  
  326.     if(star->displayed == TRUE) {
  327.         ShowStar(star);
  328.         star->displayed = FALSE;
  329.     }
  330.  
  331.     if((scrx > winwidth) || (scrx < -(2 * star->radius)) ||
  332.        (scry > winheight) || (scry < -(2 * star->radius))) {
  333.         NewStar(star);
  334.         return;
  335.     }
  336.  
  337.     star->sx = scrx;
  338.     star->sy = scry;
  339.     star->radius = scrradius;
  340.  
  341.     ShowStar(star);
  342.     star->displayed = TRUE;
  343.  
  344. }
  345.  
  346. static void NewStar(star)
  347. StarRec *star;
  348. {
  349.     int rnd = RNDRANGE(0,100);
  350.     int x_range, y_range;
  351.  
  352.     star->z = RNDRANGE(MINZ,MAXZ);
  353.  
  354.     if(center != TRUE) {
  355.         x_range = (star->z * winwidth) / ( 2 * SCREENZ);
  356.         y_range = (star->z * winheight) / (2 * SCREENZ);
  357.  
  358.         star->x = RNDRANGE(-(x_range),x_range);
  359.         star->y = RNDRANGE(-(y_range),y_range);
  360.     } else {
  361.         star->x = 0; 
  362.         star->y = 0;
  363.     }
  364.  
  365.     star->sx = (star->x * SCREENZ) / star->z;
  366.     star->sy = (star->y * SCREENZ) / star->z;
  367.     star->radius = (RADIUS * SCREENZ) / star->z;
  368.     star->displayed = FALSE;
  369.     star->color = WHITE;
  370.  
  371.     if(rnd < 3)
  372.         star->type = PLANETX;
  373.     else if(rnd < 10)
  374.         star->type = RINGED;
  375.     else {
  376.         star->type = ROUND;
  377.         star->color = RNDRANGE(0, MAX_USED_COLORS - 1);
  378.     }
  379.  
  380. }
  381.  
  382. static void ShowStar(star)
  383. StarRec *star;
  384. {
  385.  
  386.     if(star->radius <= 0) 
  387.         star->radius = 1;
  388.  
  389.     if(star->radius > STAR_MAXSIZE) 
  390.         star->radius = STAR_MAXSIZE;
  391.  
  392.     switch(star->type) {
  393.         case RINGED:
  394.             if(Ringpix[star->radius] == 0) Newpixmap(star);
  395.             break;
  396.         case ROUND:
  397.             if(Starpix[star->radius] == 0) Newpixmap(star);
  398.             break;
  399.         case PLANETX:
  400.             if(Xpix[star->radius] == 0) Newpixmap(star);
  401.             break;
  402.     }
  403.  
  404.     DrawStar(star);
  405.  
  406. }
  407.  
  408. static void DrawStar(star)
  409. StarRec *star;
  410. {
  411.     static GC gc[MAX_USED_COLORS] = { 0 };
  412.     int p_width = 2 * ((star->radius) * MAG_RAD);
  413.     int p_height = 2 * ((star->radius) * MAG_RAD);
  414.     Pixmap drawpix;
  415.     GC stargc;
  416.     int sx,sy;
  417.     int i;
  418.  
  419.     if(gc[0] == 0) {
  420.  
  421.         for(i = 0; i < MAX_USED_COLORS; i++) {
  422.             gc[i] = XCreateGC(dpy,SpaceWin,0,NULL);
  423.             XSetFunction(dpy,gc[i],GXxor);
  424.             XSetBackground(dpy,gc[i],BlackPixel(dpy,screen));
  425.             if( color == TRUE ) 
  426.                 XSetForeground(dpy,gc[i],starColors[i].pixel);
  427.             else
  428.                 XSetForeground(dpy,gc[i],WhitePixel(dpy,screen));
  429. #ifdef STIPPLE_DRAW
  430.             XSetFillStyle(dpy,gc[i],FillStippled);
  431. #else
  432.             XSetGraphicsExposures(dpy,gc[i],FALSE);
  433. #endif STIPPLE_DRAW
  434.         }
  435.     }
  436.  
  437.     stargc = gc[star->color];
  438.  
  439.     switch(star->type) {
  440.         case ROUND:
  441.             drawpix = Starpix[star->radius];
  442.             break;
  443.         case RINGED:
  444.             drawpix = Ringpix[star->radius];
  445.             p_width *= 2;
  446.             break;
  447.         case PLANETX:
  448.             drawpix = Xpix[star->radius];
  449.             break;
  450.         default:
  451.             drawpix = Starpix[star->radius];
  452.             break;
  453.             
  454.     }
  455.             
  456.     sx = star->sx - p_width/2;
  457.     sy = star->sy - p_height/2;
  458.  
  459. #ifdef STIPPLE_DRAW
  460.     XSetStipple(dpy,stargc,drawpix);
  461.     XSetTSOrigin(dpy,stargc,sx,sy);
  462.     XFillRectangle(dpy,SpaceWin,stargc,sx,sy,p_width,p_height);
  463. #else
  464.     XCopyArea(dpy,drawpix,SpaceWin,stargc,0,0,p_width,p_height,sx,sy);
  465. #endif STIPPLE_DRAW
  466.  
  467. }
  468.  
  469. static void Newpixmap(star)
  470. StarRec *star;
  471. {
  472.     static GC gc = 0;
  473.     Pixmap pixtmp;
  474.     int i;
  475.     int p_width = 2 * ((star->radius) * MAG_RAD);
  476.     int p_height = 2 * ((star->radius) * MAG_RAD);
  477.     int r = star->radius;
  478.  
  479.     if(star->type == RINGED) {
  480.         p_width = 2 * p_width;
  481.     }
  482.  
  483. #ifdef STIPPLE_DRAW
  484.     pixtmp = XCreatePixmap(dpy,SpaceWin,p_width,p_height,1);
  485. #else
  486.     pixtmp = XCreatePixmap(dpy,SpaceWin,p_width,p_height,depth);
  487. #endif STIPPLE_DRAW
  488.  
  489.     if(gc == 0) {
  490.         gc = XCreateGC(dpy,pixtmp,0,NULL);
  491.         XSetBackground(dpy,gc,BlackPixel(dpy,screen));
  492.         XSetForeground(dpy,gc,WhitePixel(dpy,screen));
  493.         XSetFillStyle(dpy,gc,FillSolid);
  494.     }
  495.  
  496.     switch(star->type) {
  497.         case ROUND:
  498.             XSetFunction(dpy,gc,GXxor);
  499.             XFillArc(dpy,pixtmp,gc,0,0,p_width,p_height,0,360*64);
  500.             Starpix[star->radius] = pixtmp;
  501.             break;
  502.         case RINGED:
  503.             XSetFunction(dpy,gc,GXxor);
  504.             XSetLineAttributes(dpy,gc,1,LineSolid,CapRound,JoinBevel);
  505.             XFillArc(dpy,pixtmp,gc,r,0,p_height,p_height,0,360*64);
  506.             XDrawArc(dpy,pixtmp,gc,0,r/2,p_width,p_height/2,
  507.                 0,360*64);
  508.             Ringpix[star->radius] = pixtmp;
  509.             break;
  510.         case PLANETX:
  511.             XSetFunction(dpy,gc,GXxor);
  512.             XFillArc(dpy,pixtmp,gc,0,0,p_width,p_height,0,360*64);
  513.             i = r/4;
  514.             if(i < 1) i = 1;
  515.             XSetLineAttributes(dpy,gc,i,LineSolid,CapRound,JoinBevel);
  516.             XSetFunction(dpy,gc,GXclear);
  517.             XDrawLine(dpy,pixtmp,gc,r/2,r/2,r+r/2,r+r/2);
  518.             XDrawLine(dpy,pixtmp,gc,r+r/2,r/2,r/2,r+r/2);
  519.             Xpix[star->radius] = pixtmp;
  520.             break;
  521.     }
  522.  
  523. }
  524.  
  525. static void initstar()
  526. {
  527.     int i;
  528.  
  529.     for(i=0;i<NUM_STARS;i++) {
  530.         NewStar(&Stars[i]);
  531.     }
  532.  
  533. }
  534.  
  535. static void initpix()
  536. {
  537.     int i;
  538.  
  539.     for(i=0;i<STAR_MAXSIZE;i++) {
  540.         Starpix[i] = 0;
  541.         Ringpix[i] = 0;
  542.         Xpix[i] = 0;
  543.     }
  544. }
  545.  
  546. static void initpos()
  547. {
  548.     if(fullscreen == TRUE) {
  549.         winwidth = XDisplayWidth(dpy,screen);
  550.         winheight = XDisplayHeight(dpy,screen);
  551.         winx = 0;
  552.         winy = 0;
  553.     } else {
  554.         winwidth = INT_WIDTH;
  555.         winheight = INT_HEIGHT;
  556.         winx = INT_X;
  557.         winy = INT_Y;
  558.     }
  559. }
  560.  
  561. extern int XCheckTypedEvent();
  562.  
  563. static void ProcEvent()
  564. {
  565.     XEvent ev;
  566.     int block = FALSE;
  567.     
  568.     while((XPending(dpy) > 0) || (block == TRUE)) {
  569.         XNextEvent(dpy,&ev);
  570.         switch(ev.type) {
  571.             case ReparentNotify:
  572.                 if(ev.xreparent.window != SpaceWin ) break;
  573.                 XSelectInput(dpy,ev.xreparent.parent,
  574.                     StructureNotifyMask);
  575.                 XSelectInput(dpy,parent,0);
  576.                 parent = ev.xreparent.parent;
  577.                 break;
  578.  
  579.             case UnmapNotify:
  580.                 if((ev.xunmap.window != SpaceWin) &&
  581.                    (ev.xunmap.window != parent)) break;
  582.                 block = TRUE;
  583.                 break;
  584.                     
  585.             case VisibilityNotify:
  586.                 if(ev.xvisibility.window != SpaceWin) break;
  587.                 if(ev.xvisibility.state == 
  588.                      VisibilityFullyObscured) {
  589.                     block = TRUE;
  590.                     break;
  591.                 }
  592.                 if((ev.xvisibility.state == 
  593.                       VisibilityUnobscured) && 
  594.                       (SpaceVis == OBSCURED)) {
  595.                     ReStart();
  596.                     SpaceVis = UNOBSCURED;
  597.                     block = FALSE;
  598.                     break;
  599.                 }
  600.                 if(ev.xvisibility.state == 
  601.                      VisibilityPartiallyObscured) {
  602.                     SpaceVis = OBSCURED;
  603.                     block = FALSE;
  604.                 }
  605.                 break;
  606.                             
  607.             case Expose:
  608.                 ReStart();
  609.                 block = FALSE;
  610.                 break;
  611.  
  612.             case MapNotify:
  613.                 if((ev.xmap.window != SpaceWin) &&
  614.                    (ev.xmap.window != parent)) break;
  615.                 ReStart();
  616.                 block = FALSE;
  617.                 break;
  618.  
  619.             case ConfigureNotify:
  620.                 if(ev.xconfigure.window != SpaceWin) break;
  621.                 ReStart();
  622.                 if((winwidth == ev.xconfigure.width) && 
  623.                    (winheight == ev.xconfigure.height))
  624.                     break;
  625.                 winwidth = ev.xconfigure.width;
  626.                 winheight = ev.xconfigure.height;
  627.                 ReDrawStars();
  628.                 block = FALSE;
  629.                 break;
  630.  
  631.             default:
  632.                 break;
  633.         }
  634.     }
  635. }
  636.  
  637. static void ReDrawStars()
  638. {
  639.     int i;
  640.  
  641.     for(i=0;i<NUM_STARS;i++) {
  642.         DisplayStar(&Stars[i]);
  643.     }
  644. }
  645.  
  646. static void ReStart()
  647. {
  648.     XClearWindow(dpy,SpaceWin);
  649.     XSync(dpy, 0);
  650.     ClearStars();
  651. }
  652.  
  653. static void ClearStars()
  654. {
  655.     int i;
  656.  
  657.     for(i=0;i<NUM_STARS;i++) {
  658.         Stars[i].displayed = FALSE;
  659.     }
  660. }
  661.  
  662. static void initcolors()
  663. {
  664.     unsigned long backgrd;
  665.  
  666.     if( reverse == TRUE )
  667.         backgrd = WhitePixel(dpy,screen);
  668.     else
  669.         backgrd = BlackPixel(dpy,screen);
  670.  
  671.     starColors[WHITE].pixel = WhitePixel(dpy,screen);
  672.  
  673.     setcolor(color1,&starColors[COLOR1]);
  674.     starColors[COLOR1].pixel ^= backgrd;
  675.  
  676.     setcolor(color2,&starColors[COLOR2]);
  677.     starColors[COLOR2].pixel ^= backgrd;
  678.  
  679.     setcolor(color3,&starColors[COLOR3]);
  680.     starColors[COLOR3].pixel ^= backgrd;
  681.  
  682.     setcolor(color4,&starColors[COLOR4]);
  683.     starColors[COLOR4].pixel ^= backgrd;
  684.  
  685.     setcolor(color5,&starColors[COLOR5]);
  686.     starColors[COLOR5].pixel ^= backgrd;
  687.  
  688.     setcolor(color6,&starColors[COLOR6]);
  689.     starColors[COLOR6].pixel ^= backgrd;
  690.  
  691.  
  692. }
  693.  
  694. static void setcolor(cnames,xcolor)
  695. char *cnames[];
  696. XColor *xcolor;
  697. {
  698.     int i = 0;
  699.     int count = 0;
  700.     XColor ecolor;
  701.  
  702.     while( *cnames[count] != NULL ) {
  703.          count++;
  704.     }
  705.  
  706.     i = RNDRANGE(0,count-1);
  707.  
  708.     if( XAllocNamedColor(dpy,cmap,cnames[i],xcolor,&ecolor) > 0) 
  709.         return;
  710.     else {
  711.         i = 0;
  712.  
  713.         while( *cnames[i] != NULL ) {
  714.             if( XAllocNamedColor(dpy,cmap,cnames[i],
  715.                         xcolor,&ecolor) > 0) 
  716.                 return;
  717.             else
  718.                 continue;
  719.         }
  720.     }
  721.     
  722.     xcolor->pixel = starColors[WHITE].pixel;
  723. }
  724.  
  725. static void initwin(hostmon)
  726. char *hostmon;
  727. {
  728.     XSetWindowAttributes attr;
  729.     char *name;
  730.  
  731.     if((dpy = XOpenDisplay(hostmon)) == NULL) {
  732.         fprintf(stderr,"Connection not made.\n");
  733.         exit(1);
  734.     }
  735.  
  736.     screen = DefaultScreen(dpy);
  737.     cmap = DefaultColormap(dpy,screen);
  738.     parent = root = RootWindow(dpy,screen);
  739.     depth = DefaultDepth(dpy,screen);
  740.  
  741.     if( color == TRUE ) 
  742.         initcolors();
  743.  
  744.     initpos();
  745.  
  746.     XSelectInput(dpy,root,SubstructureNotifyMask);
  747.  
  748.     attr.event_mask = StructureNotifyMask
  749.             | SubstructureNotifyMask
  750.             | VisibilityChangeMask
  751.             | ExposureMask;
  752.  
  753.     if(reverse == TRUE) 
  754.         attr.background_pixel = WhitePixel(dpy,screen);
  755.     else
  756.         attr.background_pixel = BlackPixel(dpy,screen);
  757.  
  758.     if(rootwin == TRUE) {
  759.         SpaceWin = root;
  760.         XChangeWindowAttributes(dpy,SpaceWin,
  761.                     CWEventMask|CWBackPixel,&attr);
  762.     } else {
  763.  
  764.         SpaceWin = XCreateWindow(dpy,root,winx,winy,winwidth,winheight,
  765.                     0,depth,InputOutput,
  766.                     DefaultVisual(dpy,screen),
  767.                     CWEventMask|CWBackPixel,&attr);
  768.  
  769.         if( color == TRUE )
  770.             name = "Color SpaceOut";
  771.         else
  772.             name = "SpaceOut";
  773.  
  774.         XChangeProperty(dpy,SpaceWin,XA_WM_NAME,XA_STRING,8,
  775.             PropModeReplace,name,strlen(name));
  776.         XMapWindow(dpy,SpaceWin);
  777.     }
  778.  
  779.     XClearWindow(dpy,SpaceWin);
  780.     XSync(dpy, 0);
  781.  
  782. }
  783.  
  784. static void usage()
  785. {
  786.     fprintf(stderr,"usage: space [host:screen] [-frCc]\n");
  787.     exit(0);
  788. }
  789.  
  790. static int randomBit()
  791. {
  792. register unsigned long int tmp ;
  793. static unsigned long int state = 0x10 ;
  794.  
  795. /* Linear Feedback Shift Register
  796.  * Length = 31 Bits
  797.  * Taps at 3 & 31
  798.  * Period = ( 2 ** 31 ) - 1
  799.  */
  800. tmp = state ;
  801. /* Next State */
  802. state = ( ( ( tmp << 2 ) ^ ( tmp << 30 ) ) & 0x80000000 ) | ( tmp >> 1 ) ;
  803.  
  804. return tmp >> 31 ;
  805. }
  806.  
  807. /* Random Number Generator State Variables */
  808. static unsigned long int lastRandom = 1 ;
  809. static unsigned long int oldRandom ;
  810.  
  811. static unsigned long int randomLong()
  812. {
  813. register unsigned long int tmpA ;
  814. register unsigned long int tmpB ;
  815.  
  816. register unsigned long int tmpH ;
  817. register unsigned long int tmpM1 ;
  818. register unsigned long int tmpM2 ;
  819. register unsigned long int tmpL ;
  820.  
  821. register long int SumOldL ;
  822. register long int SumOldH ;
  823. register long int SumLastL ;
  824. register long int SumLastH ;
  825.  
  826. /*
  827.  * Modular Congruence Method
  828.  * Period >= ( 2 ** 31 ) - 1
  829.  * X(n) =
  830.  *   ( 271,828,183 * X(n-1) - 314,159,269 * X(n-2) ) mod ( ( 2 ** 31 ) - 1 )
  831.  * = (  0x1033C4D7 * X(n-1) -  0x12B9B0A5 * X(n-2) ) mod ( ( 2 ** 31 ) - 1 )
  832.  * To Avoid OverFlow USE:
  833.  *    ( ( x1 * ( 2 ** 16 ) ) + x2 ) * ( ( y1 * ( 2 ** 15 ) ) + y2 ) =
  834.  *           ( x1 * y1 * ( 2 ** 31 ) )
  835.  *         + ( x2 * y1 * ( 2 ** 15 ) )
  836.  *         + ( x1 * y2 * ( 2 ** 16 ) )
  837.  *         + ( x2 * y2  )
  838.  * NOTE:
  839.  * 0x1033C4D7 == ( 0x2067 * 0x08000 ) + 0x44D7
  840.  *            == ( 0x1033 * 0x10000 ) + 0xC4D7
  841.  * 0x12B9B0A5 == ( 0x2573 * 0x08000 ) + 0x30A5
  842.  *            == ( 0x12B9 * 0x10000 ) + 0xB0A5
  843.  */
  844. tmpA   = oldRandom ;
  845.  
  846. tmpB   = tmpA & 0xFFFF ;    /* Low Order 16 Bits of X(n-2) */
  847. tmpM1  = tmpB * 0x2573 ;    /* Offset 16 Bits */
  848. tmpL   = tmpB * 0x30A5 ;    /* Offset  0 Bits */
  849. tmpA   >>= 16 ;            /* High Order 15 Bits of X(n-2)
  850. tmpH   = tmpA * 0x2573 ;    /* Offset 31 Bits */
  851. tmpM2  = tmpA * 0x30A5 ;    /* Offset 15 Bits */
  852. /* Now Sum Them */
  853. SumOldL  = tmpL & 0x7FFF ;
  854. tmpA     = ( tmpL >> 15 ) + tmpM2 + ( tmpM1 << 1 ) ;
  855. SumOldL |= ( ( tmpA << 15 ) & 0x7FFF8000 ) ;    /* Bottom 31 Bits */
  856. SumOldH  = ( ( tmpA >> 16 ) + tmpH ) ;        /* Top 31 Bits */
  857.  
  858. oldRandom = tmpA = lastRandom ;
  859.  
  860. tmpB   = tmpA & 0xFFFF ;    /* Low Order 16 Bits of X(n-1) */
  861. tmpM1  = tmpB * 0x2067 ;    /* Offset 15 bits */
  862. tmpL   = tmpB * 0x44D7 ;    /* Offset  0 bits */
  863. tmpA   >>= 16 ;            /* High Order 15 Bits of X(n-1)*/
  864. tmpH   = tmpA * 0x2067 ;    /* Offset 31 bits */
  865. tmpM2  = tmpA * 0x44D7 ;    /* Offset 16 bits */
  866. /* Now Sum Them */
  867. SumLastL  = tmpL & 0x7FFF ;
  868. tmpA      = ( tmpL >> 15 ) + tmpM2 + ( tmpM1 << 1 ) ;
  869. SumLastL |= ( ( tmpA << 15 ) & 0x7FFF8000 ) ;    /* Bottom 31 Bits */
  870. SumLastH  = ( ( tmpA >> 16 ) + tmpH ) ;        /* Top 31 Bits */
  871.  
  872. tmpA = SumLastH - SumOldH ;
  873. tmpB = SumLastL - SumOldL ;
  874. if ( SumLastL < SumOldL ) {
  875.     tmpB &= 0x7FFFFFFF ;
  876.     tmpA-- ;
  877. }
  878.  
  879. return lastRandom = ( ( tmpA + tmpB ) & 0x7FFFFFFF ) ;
  880. }
  881.  
  882. static void prev(ev)
  883. XEvent *ev;
  884. {
  885.     printf("event->type: %d\n",ev->type);
  886. }
  887.