~ubuntu-branches/ubuntu/breezy/xscreensaver/breezy

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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* twang, twist around screen bits, v1.3
 * by Dan Bornstein, danfuzz@milk.com
 * Copyright (c) 2003 Dan Bornstein. All rights reserved.
 *
 * 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.
 *
 * See the included man page for more details.
 */

#include <math.h>
#include "screenhack.h"
#include <X11/Xutil.h>

#ifdef HAVE_XSHM_EXTENSION
#include "xshm.h"
#endif

#define FLOAT double

/* random float in the range (-1..1) */
#define RAND_FLOAT_PM1 \
        (((FLOAT) ((random() >> 8) & 0xffff)) / ((FLOAT) 0x10000) * 2 - 1)

/* random float in the range (0..1) */
#define RAND_FLOAT_01 \
        (((FLOAT) ((random() >> 8) & 0xffff)) / ((FLOAT) 0x10000))



/* parameters that are user configurable */

/* whether or not to use xshm */
#ifdef HAVE_XSHM_EXTENSION
static Bool useShm;
#endif

/* delay (usec) between iterations */
static int delay;

/* the maximum number of columns of tiles */
static int maxColumns;

/* the maximum number of rows of tiles */
static int maxRows;

/* the size (width and height) of a tile */
static int tileSize;

/* the width of the border around each tile */
static int borderWidth;

/* the chance, per iteration, of an interesting event happening */
static FLOAT eventChance;

/* friction: the fraction (0..1) by which velocity decreased per iteration */
static FLOAT friction;

/* springiness: the fraction (0..1) of the orientation that turns into 
 * velocity towards the center */
static FLOAT springiness;

/* transference: the fraction (0..1) of the orientations of orthogonal
 * neighbors that turns into velocity (in the same direction as the
 * orientation) */
static FLOAT transference;



/* non-user-modifiable immutable definitions */

/* width and height of the window */
static int windowWidth;
static int windowHeight;

static Display *display;        /* the display to draw on */
static Window window;           /* the window to draw on */
static Screen *screen;          /* the screen to draw on */

static XImage *sourceImage;     /* image source of stuff to draw */
static XImage *workImage;       /* work area image, used when rendering */
static XImage *backgroundImage; /* image filled with background pixels */

static GC backgroundGC;         /* GC for the background color */
static GC foregroundGC;         /* GC for the foreground color */
static GC borderGC;             /* GC for the border color */
unsigned long backgroundPixel;  /* background color as a pixel value */
unsigned long borderPixel;      /* border color as a pixel value */

#ifdef HAVE_XSHM_EXTENSION
XShmSegmentInfo shmInfo;
#endif



/* the model */

typedef struct
{
    int x;        /* x coordinate of the center of the tile */
    int y;        /* y coordinate of the center of the tile */
    FLOAT angle;  /* angle of the tile (-pi..pi) */
    FLOAT zoom;   /* log of the zoom of the tile (-1..1) */
    FLOAT vAngle; /* angular velocity (-pi/4..pi/4) */
    FLOAT vZoom;  /* zoomular velocity (-0.25..0.25) */
}
Tile;

static Tile *tiles;  /* array of tiles (left->right, top->bottom, row major) */
static int rows;     /* number of rows of tiles */
static int columns;  /* number of columns of tiles */

static Tile **sortedTiles; /* array of tile pointers, sorted by zoom */
static int tileCount;     /* total number of tiles */

#define TILE_AT(col,row) (&tiles[(row) * columns + (col)])

#define MAX_VANGLE (M_PI / 4.0)
#define MAX_VZOOM 0.25

#define RAND_ANGLE (RAND_FLOAT_PM1 * M_PI)
#define RAND_ZOOM (RAND_FLOAT_PM1)
#define RAND_VANGLE (RAND_FLOAT_PM1 * MAX_VANGLE)
#define RAND_VZOOM (RAND_FLOAT_PM1 * MAX_VZOOM)



/*
 * overall setup stuff
 */

/* grab the source image */
static void grabImage (XWindowAttributes *xwa)
{
    XFillRectangle (display, window, backgroundGC, 0, 0, 
		    windowWidth, windowHeight);
    backgroundImage = 
	XGetImage (display, window, 0, 0, windowWidth, windowHeight,
		   ~0L, ZPixmap);

    load_random_image (screen, window, window, NULL, NULL);
    sourceImage = XGetImage (display, window, 0, 0, windowWidth, windowHeight,
			     ~0L, ZPixmap);

#ifdef HAVE_XSHM_EXTENSION
    workImage = NULL;
    if (useShm) 
    {
	workImage = create_xshm_image (display, xwa->visual, xwa->depth,
				       ZPixmap, 0, &shmInfo, 
				       windowWidth, windowHeight);
	if (!workImage) 
	{
	    useShm = False;
	    fprintf (stderr, "create_xshm_image failed\n");
	}
    }

    if (workImage == NULL)
#endif /* HAVE_XSHM_EXTENSION */

	/* just use XSubImage to acquire the right visual, depth, etc;
	 * easier than the other alternatives */
	workImage = XSubImage (sourceImage, 0, 0, windowWidth, windowHeight);
}

/* set up the system */
static void setup (void)
{
    XWindowAttributes xgwa;
    XGCValues gcv;

    XGetWindowAttributes (display, window, &xgwa);

    screen = xgwa.screen;
    windowWidth = xgwa.width;
    windowHeight = xgwa.height;

    gcv.line_width = borderWidth;
    gcv.foreground = get_pixel_resource ("borderColor", "BorderColor",
					 display, xgwa.colormap);
    borderPixel = gcv.foreground;
    borderGC = XCreateGC (display, window, GCForeground | GCLineWidth, 
			  &gcv);

    gcv.foreground = get_pixel_resource ("background", "Background",
					 display, xgwa.colormap);
    backgroundPixel = gcv.foreground;
    backgroundGC = XCreateGC (display, window, GCForeground, &gcv);

    gcv.foreground = get_pixel_resource ("foreground", "Foreground",
					 display, xgwa.colormap);
    foregroundGC = XCreateGC (display, window, GCForeground, &gcv);

    grabImage (&xgwa);
}



/*
 * the simulation
 */

/* event: randomize all the angular velocities */
static void randomizeAllAngularVelocities (void)
{
    int c;
    int r;

    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    TILE_AT (c, r)->vAngle = RAND_VANGLE;
	}
    }
}

/* event: randomize all the zoomular velocities */
static void randomizeAllZoomularVelocities (void)
{
    int c;
    int r;

    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    TILE_AT (c, r)->vZoom = RAND_VZOOM;
	}
    }
}

/* event: randomize all the velocities */
static void randomizeAllVelocities (void)
{
    randomizeAllAngularVelocities ();
    randomizeAllZoomularVelocities ();
}

/* event: randomize all the angular orientations */
static void randomizeAllAngularOrientations (void)
{
    int c;
    int r;

    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    TILE_AT (c, r)->angle = RAND_ANGLE;
	}
    }
}

/* event: randomize all the zoomular orientations */
static void randomizeAllZoomularOrientations (void)
{
    int c;
    int r;

    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    TILE_AT (c, r)->zoom = RAND_ZOOM;
	}
    }
}

/* event: randomize all the orientations */
static void randomizeAllOrientations (void)
{
    randomizeAllAngularOrientations ();
    randomizeAllZoomularOrientations ();
}

/* event: randomize everything */
static void randomizeEverything (void)
{
    randomizeAllVelocities ();
    randomizeAllOrientations ();
}

/* event: pick one tile and randomize all its stats */
static void randomizeOneTile (void)
{
    int c = RAND_FLOAT_01 * columns;
    int r = RAND_FLOAT_01 * rows;

    Tile *t = TILE_AT (c, r);
    t->angle = RAND_ANGLE;
    t->zoom = RAND_ZOOM;
    t->vAngle = RAND_VANGLE;
    t->vZoom = RAND_VZOOM;
}

/* event: pick one row and randomize everything about each of its tiles */
static void randomizeOneRow (void)
{
    int c;
    int r = RAND_FLOAT_01 * rows;

    for (c = 0; c < columns; c++)
    {
	Tile *t = TILE_AT (c, r);
	t->angle = RAND_ANGLE;
	t->zoom = RAND_ZOOM;
	t->vAngle = RAND_VANGLE;
	t->vZoom = RAND_VZOOM;
    }
}

/* event: pick one column and randomize everything about each of its tiles */
static void randomizeOneColumn (void)
{
    int c = RAND_FLOAT_01 * columns;
    int r;

    for (r = 0; r < rows; r++)
    {
	Tile *t = TILE_AT (c, r);
	t->angle = RAND_ANGLE;
	t->zoom = RAND_ZOOM;
	t->vAngle = RAND_VANGLE;
	t->vZoom = RAND_VZOOM;
    }
}

/* do model event processing */
static void modelEvents (void)
{
    int which;

    if (RAND_FLOAT_01 > eventChance)
    {
	return;
    }

    which = RAND_FLOAT_01 * 10;

    switch (which)
    {
	case 0: randomizeAllAngularVelocities ();    break;
	case 1: randomizeAllZoomularVelocities ();   break;
	case 2: randomizeAllVelocities ();           break;
	case 3: randomizeAllAngularOrientations ();  break;
	case 4: randomizeAllZoomularOrientations (); break;
	case 5: randomizeAllOrientations ();         break;
	case 6: randomizeEverything ();              break;
	case 7: randomizeOneTile ();                 break;
	case 8: randomizeOneColumn ();               break;
	case 9: randomizeOneRow ();                  break;
    }
}

/* update the model for one iteration */
static void updateModel (void)
{
    int r;
    int c;

    /* for each tile, decrease its velocities according to the friction,
     * and increase them based on its current orientation and the orientations
     * of its orthogonal neighbors */
    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    Tile *t = TILE_AT (c, r);
	    FLOAT a = t->angle;
	    FLOAT z = t->zoom;
	    FLOAT va = t->vAngle;
	    FLOAT vz = t->vZoom;

	    va -= t->angle * springiness;
	    vz -= t->zoom * springiness;

	    if (c > 0)
	    {
		Tile *t2 = TILE_AT (c - 1, r);
		va += (t2->angle - a) * transference;
		vz += (t2->zoom - z) * transference;
	    }

	    if (c < (columns - 1))
	    {
		Tile *t2 = TILE_AT (c + 1, r);
		va += (t2->angle - a) * transference;
		vz += (t2->zoom - z) * transference;
	    }

	    if (r > 0)
	    {
		Tile *t2 = TILE_AT (c, r - 1);
		va += (t2->angle - a) * transference;
		vz += (t2->zoom - z) * transference;
	    }

	    if (r < (rows - 1))
	    {
		Tile *t2 = TILE_AT (c, r + 1);
		va += (t2->angle - a) * transference;
		vz += (t2->zoom - z) * transference;
	    }

	    va *= (1.0 - friction);
	    vz *= (1.0 - friction);

	    if (va > MAX_VANGLE) va = MAX_VANGLE;
	    else if (va < -MAX_VANGLE) va = -MAX_VANGLE;
	    t->vAngle = va;

	    if (vz > MAX_VZOOM) vz = MAX_VZOOM;
	    else if (vz < -MAX_VZOOM) vz = -MAX_VZOOM;
	    t->vZoom = vz;
	}
    }

    /* for each tile, update its orientation based on its velocities */
    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    Tile *t = TILE_AT (c, r);
	    FLOAT a = t->angle + t->vAngle;
	    FLOAT z = t->zoom + t->vZoom;

	    if (a > M_PI) a = M_PI;
	    else if (a < -M_PI) a = -M_PI;
	    t->angle = a;

	    if (z > 1.0) z = 1.0;
	    else if (z < -1.0) z = -1.0;
	    t->zoom = z;
	}
    }
}

/* the comparator to us to sort the tiles (used immediately below); it'd
 * sure be nice if C allowed inner functions (or jeebus-forbid *real
 * closures*!) */
static int sortTilesComparator (const void *v1, const void *v2)
{
    Tile *t1 = *(Tile **) v1;
    Tile *t2 = *(Tile **) v2;
    
    if (t1->zoom < t2->zoom)
    {
	return -1;
    }

    if (t1->zoom > t2->zoom)
    {
	return 1;
    }

    return 0;
}

/* sort the tiles in sortedTiles by zoom */
static void sortTiles (void)
{
    qsort (sortedTiles, tileCount, sizeof (Tile *), sortTilesComparator);
}

/* render the given tile */
static void renderTile (Tile *t)
{
    /* note: the zoom as stored per tile is log-based (centered on 0, with
     * 0 being no zoom, but the range for zoom-as-drawn is 0.4..2.5,
     * hence the alteration of t->zoom, below */

    int x, y;

    int tx = t->x;
    int ty = t->y;

    FLOAT zoom = pow (2.5, t->zoom);
    FLOAT ang = -t->angle;
    FLOAT sinAng = sin (ang);
    FLOAT cosAng = cos (ang);

    FLOAT innerBorder = (tileSize - borderWidth) / 2.0;
    FLOAT outerBorder = innerBorder + borderWidth;

    int maxCoord = outerBorder * zoom * (fabs (sinAng) + fabs (cosAng));
    int minX = tx - maxCoord;
    int maxX = tx + maxCoord;
    int minY = ty - maxCoord;
    int maxY = ty + maxCoord;

    FLOAT prey;

    if (minX < 0) minX = 0;
    if (maxX > windowWidth) maxX = windowWidth;
    if (minY < 0) minY = 0;
    if (maxY > windowHeight) maxY = windowHeight;

    sinAng /= zoom;
    cosAng /= zoom;

    for (y = minY, prey = y - ty; y < maxY; y++, prey++)
    {
	FLOAT prex = minX - tx;
	FLOAT srcx = prex * cosAng - prey * sinAng;
	FLOAT srcy = prex * sinAng + prey * cosAng;

	for (x = minX; 
	     x < maxX; 
	     x++, srcx += cosAng, srcy += sinAng)
	{
	    if ((srcx < -innerBorder) || (srcx >= innerBorder) ||
		(srcy < -innerBorder) || (srcy >= innerBorder))
	    {
		if ((srcx < -outerBorder) || (srcx >= outerBorder) ||
		    (srcy < -outerBorder) || (srcy >= outerBorder))
		{
		    continue;
		}
		XPutPixel (workImage, x, y, borderPixel);
	    }
	    else
	    {
		unsigned long p = 
		    XGetPixel (sourceImage, srcx + tx, srcy + ty);
		XPutPixel (workImage, x, y, p);
	    }
	}
    }
}

/* render and display the current model */
static void renderFrame (void)
{
    int n;

    memcpy (workImage->data, backgroundImage->data, 
	    workImage->bytes_per_line * workImage->height);

    sortTiles ();

    for (n = 0; n < tileCount; n++)
    {
	renderTile (sortedTiles[n]);
    }

#ifdef HAVE_XSHM_EXTENSION
    if (useShm)
	XShmPutImage (display, window, backgroundGC, workImage, 0, 0, 0, 0,
		      windowWidth, windowHeight, False);
    else
#endif /* HAVE_XSHM_EXTENSION */
	XPutImage (display, window, backgroundGC, workImage, 
		   0, 0, 0, 0, windowWidth, windowHeight);
}

/* set up the model */
static void setupModel (void)
{
    int c;
    int r;

    int leftX; /* x of the center of the top-left tile */
    int topY;  /* y of the center of the top-left tile */

    if (tileSize > (windowWidth / 2))
    {
	tileSize = windowWidth / 2;
    }

    if (tileSize > (windowHeight / 2))
    {
	tileSize = windowHeight / 2;
    }

    columns = windowWidth / tileSize;
    rows = windowHeight / tileSize;

    if ((maxColumns != 0) && (columns > maxColumns))
    {
	columns = maxColumns;
    }

    if ((maxRows != 0) && (rows > maxRows))
    {
	rows = maxRows;
    }

    tileCount = rows * columns;

    leftX = (windowWidth - (columns * tileSize) + tileSize) / 2;
    topY = (windowHeight - (rows * tileSize) + tileSize) / 2;

    tiles = calloc (tileCount, sizeof (Tile));
    sortedTiles = calloc (tileCount, sizeof (Tile *));

    for (r = 0; r < rows; r++)
    {
	for (c = 0; c < columns; c++)
	{
	    Tile *t = TILE_AT (c, r);
	    t->x = leftX + c * tileSize;
	    t->y = topY + r * tileSize;
	    sortedTiles[c + r * columns] = t;
	}
    }

    randomizeEverything ();
}

/* do one iteration */
static void oneIteration (void)
{
    modelEvents ();
    updateModel ();
    renderFrame ();
}



/* main and options and stuff */

char *progclass = "Twang";

char *defaults [] = {
    ".background:	black",
    ".foreground:	white",
    "*borderColor:      blue",
    "*borderWidth:	3",
    "*delay:		10000",
    "*eventChance:      0.01",
    "*friction:		0.05",
    "*maxColumns:       0",
    "*maxRows:          0",
    "*springiness:	0.1",
    "*tileSize:		120",
    "*transference:	0.025",
#ifdef HAVE_XSHM_EXTENSION
    "*useSHM: True",
#endif
    0
};

XrmOptionDescRec options [] = {
  { "-border-color",     ".borderColor",    XrmoptionSepArg, 0 },
  { "-border-width",     ".borderWidth",    XrmoptionSepArg, 0 },
  { "-delay",            ".delay",          XrmoptionSepArg, 0 },
  { "-event-chance",     ".eventChance",    XrmoptionSepArg, 0 },
  { "-friction",         ".friction",       XrmoptionSepArg, 0 },
  { "-max-columns",      ".maxColumns",     XrmoptionSepArg, 0 },
  { "-max-rows",         ".maxRows",        XrmoptionSepArg, 0 },
  { "-springiness",      ".springiness",    XrmoptionSepArg, 0 },
  { "-tile-size",        ".tileSize",       XrmoptionSepArg, 0 },
  { "-transference",     ".transference",   XrmoptionSepArg, 0 },
#ifdef HAVE_XSHM_EXTENSION
  { "-shm",              ".useSHM",         XrmoptionNoArg, "True" },
  { "-no-shm",           ".useSHM",         XrmoptionNoArg, "False" },
#endif
  { 0, 0, 0, 0 }
};

/* initialize the user-specifiable params */
static void initParams (void)
{
    int problems = 0;

    borderWidth = get_integer_resource ("borderWidth", "Integer");
    if (borderWidth < 0)
    {
	fprintf (stderr, "error: border width must be at least 0\n");
	problems = 1;
    }

    delay = get_integer_resource ("delay", "Delay");
    if (delay < 0)
    {
	fprintf (stderr, "error: delay must be at least 0\n");
	problems = 1;
    }

    eventChance = get_float_resource ("eventChance", "Double");
    if ((eventChance < 0.0) || (eventChance > 1.0))
    {
	fprintf (stderr, "error: eventChance must be in the range 0..1\n");
	problems = 1;
    }

    friction = get_float_resource ("friction", "Double");
    if ((friction < 0.0) || (friction > 1.0))
    {
	fprintf (stderr, "error: friction must be in the range 0..1\n");
	problems = 1;
    }

    maxColumns = get_integer_resource ("maxColumns", "Integer");
    if (maxColumns < 0)
    {
	fprintf (stderr, "error: max columns must be at least 0\n");
	problems = 1;
    }

    maxRows = get_integer_resource ("maxRows", "Integer");
    if (maxRows < 0)
    {
	fprintf (stderr, "error: max rows must be at least 0\n");
	problems = 1;
    }

    springiness = get_float_resource ("springiness", "Double");
    if ((springiness < 0.0) || (springiness > 1.0))
    {
	fprintf (stderr, "error: springiness must be in the range 0..1\n");
	problems = 1;
    }

    tileSize = get_integer_resource ("tileSize", "Integer");
    if (tileSize < 1)
    {
	fprintf (stderr, "error: tile size must be at least 1\n");
	problems = 1;
    }
    
    transference = get_float_resource ("transference", "Double");
    if ((transference < 0.0) || (transference > 1.0))
    {
	fprintf (stderr, "error: transference must be in the range 0..1\n");
	problems = 1;
    }

#ifdef HAVE_XSHM_EXTENSION
    useShm = get_boolean_resource ("useSHM", "Boolean");
#endif

    if (problems)
    {
	exit (1);
    }
}

/* main function */
void screenhack (Display *dpy, Window win)
{
    display = dpy;
    window = win;

    initParams ();
    setup ();
    setupModel ();

    for (;;) 
    {
	oneIteration ();
        XSync (dpy, False);
        screenhack_handle_events (dpy);
	usleep (delay);
    }
}