summaryrefslogtreecommitdiffstats
path: root/hacks/helix.c
blob: 7c1051cc93c24c8bfcba7d6fb38481a912858d01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* xscreensaver, Copyright (c) 1992-2008 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* Algorithm from a Mac program by Chris Tate, written in 1988 or so. */

/* 18-Sep-97: Johannes Keukelaar (johannes@nada.kth.se): Improved screen
 *            eraser.
 * 10-May-97: merged ellipse code by Dan Stromberg <strombrg@nis.acs.uci.edu>
 *            as found in xlockmore 4.03a10.
 * 1992:      jwz created.
 */

/* 25 April 2002: Matthew Strait <straitm@mathcs.carleton.edu> added
-subdelay option so the drawing process can be watched */

#include <math.h>
#include "screenhack.h"
#include "erase.h"

enum draw_state { HELIX, DRAW_HELIX, TRIG, DRAW_TRIG, LINGER, ERASE };

struct state {
  enum draw_state dstate;
  double sins [360];
  double coss [360];

  GC draw_gc;
  unsigned int default_fg_pixel;
  int sleep_time;
  int subdelay;
  eraser_state *eraser;

  int width, height;
  Colormap cmap;

  int x1, y1, x2, y2, angle, i;

  int radius1, radius2, d_angle, factor1, factor2, factor3, factor4;
  int d_angle_offset;
  int offset, dir, density;
};

static void *
helix_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  int i;
  XGCValues gcv;
  XWindowAttributes xgwa;

  st->sleep_time = get_integer_resource(dpy, "delay", "Integer");
  st->subdelay = get_integer_resource(dpy, "subdelay", "Integer");

  XGetWindowAttributes (dpy, window, &xgwa);
  st->width = xgwa.width;
  st->height = xgwa.height;
  st->cmap = xgwa.colormap;
  gcv.foreground = st->default_fg_pixel =
    get_pixel_resource (dpy, st->cmap, "foreground", "Foreground");
  st->draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
  gcv.foreground = get_pixel_resource (dpy, st->cmap, "background", "Background");

  for (i = 0; i < 360; i++)
    {
      st->sins [i] = sin ((((double) i) / 180.0) * M_PI);
      st->coss [i] = cos ((((double) i) / 180.0) * M_PI);
    }

  st->dstate = (random() & 1) ? HELIX : TRIG;

  return st;
}

static int
gcd (int a, int b)
{
  while (b > 0)
    {
      int tmp;
      tmp = a % b;
      a = b;
      b = tmp;
    }
  return (a < 0 ? -a : a);
}

static void
helix (Display *dpy, Window window, struct state *st)
{
  int xmid = st->width / 2;
  int ymid = st->height / 2;
  int limit = 1 + (360 / gcd (360, st->d_angle));

  if (st->i == 0)
    {
      st->x1 = xmid;
      st->y1 = ymid + st->radius2;
      st->x2 = xmid;
      st->y2 = ymid + st->radius1;
      st->angle = 0;
    }
  
/*  for (st->i = 0; st->i < limit; st->i++)*/
    {
      int tmp;
#define pmod(x,y) (tmp=((x) % (y)), (tmp >= 0 ? tmp : (tmp + (y))))

      st->x1 = xmid + (((double) st->radius1) * st->sins [pmod ((st->angle * st->factor1), 360)]);
      st->y1 = ymid + (((double) st->radius2) * st->coss [pmod ((st->angle * st->factor2), 360)]);
      XDrawLine (dpy, window, st->draw_gc, st->x1, st->y1, st->x2, st->y2);
      st->x2 = xmid + (((double) st->radius2) * st->sins [pmod ((st->angle * st->factor3), 360)]);
      st->y2 = ymid + (((double) st->radius1) * st->coss [pmod ((st->angle * st->factor4), 360)]);
      XDrawLine (dpy, window, st->draw_gc, st->x1, st->y1, st->x2, st->y2);
      st->angle += st->d_angle;
    }
    st->i++;

    if (st->i >= limit)
      st->dstate = LINGER;
}

static void
trig (Display *dpy, Window window, struct state *st)
{
  int xmid = st->width / 2;
  int ymid = st->height / 2;

/*  while (st->d_angle >= -360 && st->d_angle <= 360)*/
    {
      int tmp;
      int angle = st->d_angle + st->d_angle_offset;
      st->x1 = (st->sins [pmod(angle * st->factor1, 360)] * xmid) + xmid;
      st->y1 = (st->coss [pmod(angle * st->factor1, 360)] * ymid) + ymid;
      st->x2 = (st->sins [pmod(angle * st->factor2 + st->offset, 360)] * xmid) + xmid;
      st->y2 = (st->coss [pmod(angle * st->factor2 + st->offset, 360)] * ymid) + ymid;
      XDrawLine(dpy, window, st->draw_gc, st->x1, st->y1, st->x2, st->y2);
      tmp = (int) 360 / (2 * st->density * st->factor1 * st->factor2);
      if (tmp == 0)	/* Do not want it getting stuck... */
	tmp = 1;	/* Would not need if floating point */
      st->d_angle += st->dir * tmp;
    }

  if (st->d_angle < -360 || st->d_angle > 360)
    st->dstate = LINGER;
}


#define min(a,b) ((a)<(b)?(a):(b))

static void
random_helix (Display *dpy, Window window, struct state *st,
              XColor *color, Bool *got_color)
{
  int radius;
  double divisor;

  radius = min (st->width, st->height) / 2;

  st->i = 0;
  st->d_angle = 0;
  st->factor1 = 2;
  st->factor2 = 2;
  st->factor3 = 2;
  st->factor4 = 2;

  divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));

  if ((random () & 1) == 0)
    {
      st->radius1 = radius;
      st->radius2 = radius / divisor;
    }
  else
    {
      st->radius2 = radius;
      st->radius1 = radius / divisor;
    }

  while (gcd (360, st->d_angle) >= 2)
    st->d_angle = random () % 360;

#define random_factor()				\
  (((random() % 7) ? ((random() & 1) + 1) : 3)	\
   * (((random() & 1) * 2) - 1))

  while (gcd (gcd (gcd (st->factor1, st->factor2), st->factor3), st->factor4) != 1)
    {
      st->factor1 = random_factor ();
      st->factor2 = random_factor ();
      st->factor3 = random_factor ();
      st->factor4 = random_factor ();
    }

  if (mono_p)
    XSetForeground (dpy, st->draw_gc, st->default_fg_pixel);
  else
    {
      hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
		  &color->red, &color->green, &color->blue);
      if ((*got_color = XAllocColor (dpy, st->cmap, color)))
	XSetForeground (dpy, st->draw_gc, color->pixel);
      else
	XSetForeground (dpy, st->draw_gc, st->default_fg_pixel);
    }

  XClearWindow (dpy, window);
}

static void
random_trig (Display *dpy, Window window, struct state *st,
             XColor *color, Bool *got_color)
{
  st->d_angle = 0;
  st->factor1 = (random() % 8) + 1;
  do {
    st->factor2 = (random() % 8) + 1;
  } while (st->factor1 == st->factor2);

  st->dir = (random() & 1) ? 1 : -1;
  st->d_angle_offset = random() % 360;
  st->offset = ((random() % ((360 / 4) - 1)) + 1) / 4;
  st->density = 1 << ((random() % 4) + 4);	/* Higher density, higher angles */

  if (mono_p)
    XSetForeground (dpy, st->draw_gc, st->default_fg_pixel);
  else
    {
      hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
		  &color->red, &color->green, &color->blue);
      if ((*got_color = XAllocColor (dpy, st->cmap, color)))
	XSetForeground (dpy, st->draw_gc, color->pixel);
      else
	XSetForeground (dpy, st->draw_gc, st->default_fg_pixel);
    }

  XClearWindow (dpy, window);
}


/* random_helix_or_trig */
static unsigned long
helix_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  Bool free_color = False;
  XColor color;
  int delay = st->subdelay;
  int erase_delay = 10000;
  int ii;

  if (st->eraser) {
    st->eraser = erase_window (dpy, window, st->eraser);
    if (st->eraser) 
      delay = erase_delay;
    goto END;
  }

  switch (st->dstate) 
    {
    case LINGER:
      delay = st->sleep_time * 1000000;
      st->dstate = ERASE;
      break;

    case ERASE:
      st->eraser = erase_window (dpy, window, st->eraser);
      delay = erase_delay;
      if (free_color) XFreeColors (dpy, st->cmap, &color.pixel, 1, 0);
      st->dstate = (random() & 1) ? HELIX : TRIG;
      break;

    case DRAW_HELIX:
      for (ii = 0; ii < 10; ii++) {
        helix (dpy, window, st);
        if (st->dstate != DRAW_HELIX)
          break;
      }
      break;

    case DRAW_TRIG:
      for (ii = 0; ii < 5; ii++) {
        trig (dpy, window, st);
        if (st->dstate != DRAW_TRIG)
          break;
      }
      break;

    case HELIX:
      random_helix (dpy, window, st, &color, &free_color);
      st->dstate = DRAW_HELIX;
      break;

    case TRIG:
      random_trig(dpy, window, st, &color, &free_color);
      st->dstate = DRAW_TRIG;
      break;

    default: 
      abort();
    }

 END:
  return delay;
}

static void
helix_reshape (Display *dpy, Window window, void *closure, 
                 unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  st->width = w;
  st->height = h;
}

static Bool
helix_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  return False;
}

static void
helix_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  XFreeGC (dpy, st->draw_gc);
  free (st);
}



static const char *helix_defaults [] = {
  ".background: black",
  ".foreground: white",
  "*fpsSolid:	true",
  "*delay:      5",
  "*subdelay:	20000",
#ifdef HAVE_MOBILE
  "*ignoreRotation: True",
#endif
  0
};

static XrmOptionDescRec helix_options [] = {   
  { "-delay",           ".delay",               XrmoptionSepArg, 0 },
  { "-subdelay",        ".subdelay",            XrmoptionSepArg, 0 },
  { 0,			0,			0,		 0 },
};

XSCREENSAVER_MODULE ("Helix", helix)