home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xlock / part01 / qix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-24  |  4.1 KB  |  208 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)qix.c 22.4 89/09/23";
  3. #endif
  4. /*-
  5.  * qix.c - The old standby vector swirl for the xlock X11 terminal locker.
  6.  *
  7.  * Copyright (c) 1989 by Sun Microsystems Inc.
  8.  *
  9.  * Permission to use, copy, modify, and distribute this software and its
  10.  * documentation for any purpose and without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and that
  12.  * both that copyright notice and this permission notice appear in
  13.  * supporting documentation.
  14.  *
  15.  * This file is provided AS IS with no warranties of any kind.  The author
  16.  * shall have no liability with respect to the infringement of copyrights,
  17.  * trade secrets or any patents by this file or any part thereof.  In no
  18.  * event will the author be liable for any lost revenue or profits or
  19.  * other special, indirect and consequential damages.
  20.  *
  21.  * Comments and additions should be sent to the author:
  22.  *
  23.  *               naughton@sun.com
  24.  *
  25.  *               Patrick J. Naughton
  26.  *               Window Systems Group, MS 14-40
  27.  *               Sun Microsystems, Inc.
  28.  *               2550 Garcia Ave
  29.  *               Mountain View, CA  94043
  30.  *
  31.  * Revision History:
  32.  * 23-Sep-89: Switch to random() and fixed bug w/ less than 4 lines.
  33.  * 20-Sep-89: Lint.
  34.  * 24-Mar-89: Written.
  35.  */
  36.  
  37. #include <X11/Xos.h>
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40.  
  41. static Display *Dsp;
  42. static Window Win;
  43. static GC   Gc,
  44.         eraseGC = (GC) 0;
  45. static int  timeout;
  46. static int  color;
  47. static unsigned long pix = 0;
  48. static long startTime;
  49. static int  first,
  50.         last,
  51.         dx1,
  52.         dy1,
  53.         dx2,
  54.         dy2,
  55.         x1,
  56.         y1,
  57.         x2,
  58.         y2,
  59.         offset,
  60.         delta,
  61.         width,
  62.         height;
  63.  
  64. typedef struct {
  65.     int        x,
  66.         y;
  67. }        point;
  68.  
  69. static int  Nlines = 0;
  70. static point *lineq = (point *) 0;
  71.  
  72. static long
  73. seconds()
  74. {
  75.     struct timeval foo;
  76.  
  77.     gettimeofday(&foo, (struct timezone *) 0);
  78.     return (foo.tv_sec);
  79. }
  80.  
  81. void
  82. initqix(d, w, g, c, t, n)
  83.     Display    *d;
  84.     Window    w;
  85.     GC        g;
  86.     int        c,
  87.         t,
  88.         n;
  89. {
  90.     XWindowAttributes xgwa;
  91.     XGCValues    xgcv;
  92.  
  93.     startTime = seconds();
  94.     if (n < 4)
  95.     n = 4;
  96.  
  97.     if (n != Nlines) {
  98.     if (lineq)
  99.         free((char *) lineq);
  100.     lineq = (point *) malloc(n * sizeof(point));
  101.     Nlines = n;
  102.     }
  103.     Dsp = d;
  104.     Win = w;
  105.     Gc = g;
  106.     color = c;
  107.     timeout = t;
  108.  
  109.     if (eraseGC == (GC) 0) {
  110.     xgcv.foreground = BlackPixel(Dsp, 0);
  111.     eraseGC = XCreateGC(Dsp, Win, GCForeground, &xgcv);
  112.     }
  113.     if (!color)
  114.     XSetForeground(Dsp, Gc, WhitePixel(Dsp, 0));
  115.  
  116.     XGetWindowAttributes(Dsp, Win, &xgwa);
  117.     width = xgwa.width;
  118.     height = xgwa.height;
  119.  
  120.     if (width < 100)        /* icon window */
  121.     delta = 2;
  122.     else
  123.     delta = 15;
  124.     offset = delta / 3;
  125.     last = 0;
  126.  
  127.     srandom(time((long *) 0));
  128.     dx1 = random() & (width - 1) + 50;
  129.     dy1 = random() & (height - 1) + 50;
  130.     dx2 = random() & (width - 1) + 50;
  131.     dy2 = random() & (height - 1) + 50;
  132.     x1 = random() & width;
  133.     y1 = random() & height;
  134.     x2 = random() & width;
  135.     y2 = random() & height;
  136.     XFillRectangle(Dsp, Win, eraseGC, 0, 0, width, height);
  137. }
  138.  
  139. int
  140. qixdone()
  141. {
  142.     return (seconds() - startTime > timeout);
  143. }
  144.  
  145. void
  146. drawqix()
  147. {
  148.     register int n = Nlines;
  149.  
  150.     while (n--) {
  151.     first = (last + 2) % Nlines;
  152.  
  153.     x1 += dx1;
  154.     y1 += dy1;
  155.     x2 += dx2;
  156.     y2 += dy2;
  157.     check_bounds_x(x1, &dx1);
  158.     check_bounds_y(y1, &dy1);
  159.     check_bounds_x(x2, &dx2);
  160.     check_bounds_y(y2, &dy2);
  161.     if (color) {
  162.         XSetForeground(Dsp, Gc, pix++);
  163.         if (pix > 253)
  164.         pix = 0;
  165.     }
  166.     XDrawLine(Dsp, Win, eraseGC,
  167.           lineq[first].x, lineq[first].y,
  168.           lineq[first + 1].x, lineq[first + 1].y);
  169.     XDrawLine(Dsp, Win, Gc, x1, y1, x2, y2);
  170.  
  171.     lineq[last].x = x1;
  172.     lineq[last].y = y1;
  173.     last += 1;
  174.     if (last >= Nlines)
  175.         last = 0;
  176.  
  177.     lineq[last].x = x2;
  178.     lineq[last].y = y2;
  179.     last += 1;
  180.     if (last >= Nlines)
  181.         last = 0;
  182.     }
  183. }
  184.  
  185. static
  186. check_bounds_y(y, dy)
  187.     int        y,
  188.            *dy;
  189. {
  190.     if (y < 0) {
  191.     *dy = (random() & delta) + offset;
  192.     } else if (y > height) {
  193.     *dy = -(random() & delta) - offset;
  194.     }
  195. }
  196.  
  197. static
  198. check_bounds_x(x, dx)
  199.     int        x,
  200.            *dx;
  201. {
  202.     if (x < 0) {
  203.     *dx = (random() & delta) + offset;
  204.     } else if (x > width) {
  205.     *dx = -(random() & delta) - offset;
  206.     }
  207. }
  208.