~ubuntu-branches/ubuntu/karmic/xscreensaver/karmic

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
/* mismunch.c
 * Munch Errors
 * Copyright (c) 2004 Steven Hazel <sah@thalassocracy.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.
 *
 * Based on munch.c
 * A munching squares implementation for X
 * Copyright 1997, Tim Showalter <tjs@andrew.cmu.edu>
 *
 * Some code stolen from / This is meant to work with
 * xscreensaver, Copyright (c) 1992, 1995, 1996 Jamie Zawinski <jwz@jwz.org>
 *
 */

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

char *progclass = "Mismunch";

char *defaults [] = {
  ".background:       black",
  ".foreground:       white",
  "*delay:            2500",
  "*simul:            5",
  "*clear:            65",
  "*xor:              True",
  0
};

XrmOptionDescRec options [] = {
  { "-delay",         ".delay",       XrmoptionSepArg,  0 },
  { "-simul",         ".simul",       XrmoptionSepArg,  0 },
  { "-clear",         ".clear",       XrmoptionSepArg, "true" },
  { "-xor",           ".xor",         XrmoptionNoArg,  "true" },
  { "-no-xor",        ".xor",         XrmoptionNoArg,  "false" },
  { 0, 0, 0, 0 }
};


static GC gc;
static int delay, simul, clear, xor;
static int logminwidth, logmaxwidth;
static int restart, window_width, window_height;

typedef struct _muncher {
  int width;
  int atX, atY;
  int kX, kT, kY;
  int grav;
  XColor fgc;
  int yshadow, xshadow;
  int x, y, t;
  int doom;
  int done;
} muncher;


/*
 * dumb way to get # of digits in number.  Probably faster than actually
 * doing a log and a division, maybe.
 */
static int dumb_log_2(int k) {
  int r = -1;
  while (k > 0) {
    k >>= 1; r++;
  }
  return r;
}


void calc_logwidths (void) {
  /* Choose a range of square sizes based on the window size.  We want
     a power of 2 for the square width or the munch doesn't fill up.
     Also, if a square doesn't fit inside an area 20% smaller than the
     window, it's too big.  Mismunched squares that big make things
     look too noisy. */

  if (window_height < window_width) {
    logmaxwidth = (int)dumb_log_2(window_height * 0.8);
  } else {
    logmaxwidth = (int)dumb_log_2(window_width * 0.8);
  }

  if (logmaxwidth < 2) {
    logmaxwidth = 2;
  }

  /* we always want three sizes of squares */
  logminwidth = logmaxwidth - 2;

  if (logminwidth < 2) {
    logminwidth = 2;
  }
}


void mismunch_handle_events (Display *dpy) {
  XEvent e;

  while (XPending(dpy)) {
    XNextEvent(dpy, &e);

    switch (e.type) {
      case ConfigureNotify:
        if (e.xconfigure.width != window_width ||
            e.xconfigure.height != window_height) {
          window_width = e.xconfigure.width;
          window_height = e.xconfigure.height;
          calc_logwidths();
          restart = 1;
        }
        break;

      default:
        screenhack_handle_event(dpy, &e);
    }
  }
}


static muncher *make_muncher (Display *dpy, Window w) {
  int logwidth;
  XWindowAttributes xgwa;
  muncher *m = (muncher *) malloc(sizeof(muncher));

  XGetWindowAttributes(dpy, w, &xgwa);

  /* choose size -- power of two */
  logwidth = (logminwidth +
              (random() % (1 + logmaxwidth - logminwidth)));

  m->width = 1 << logwidth;

  /* draw at this location */
  m->atX = (random() % (xgwa.width <= m->width ? 1
                        : xgwa.width - m->width));
  m->atY = (random() % (xgwa.height <= m->width ? 1
                        : xgwa.width - m->width));

  /* wrap-around by these values; no need to % as we end up doing that
     later anyway */
  m->kX = ((random() % 2)
           ? (random() % m->width) : 0);
  m->kT = ((random() % 2)
           ? (random() % m->width) : 0);
  m->kY = ((random() % 2)
           ? (random() % m->width) : 0);

  /* set the gravity of the munch, or rather, which direction we draw
     stuff in. */
  m->grav = random() % 2;

  /* I like this color scheme better than random colors. */
  switch (random() % 4) {
    case 0:
      m->fgc.red = random() % 65536;
      m->fgc.blue = random() % 32768;
      m->fgc.green = random() % 16384;
      break;

    case 1:
      m->fgc.red = 0;
      m->fgc.blue = random() % 65536;
      m->fgc.green = random() % 16384;
      break;

    case 2:
      m->fgc.red = random() % 8192;
      m->fgc.blue = random() % 8192;
      m->fgc.green = random() % 49152;
      break;

    case 3:
      m->fgc.red = random() % 65536;
      m->fgc.green = m->fgc.red;
      m->fgc.blue = m->fgc.red;
      break;
  }

  /* Sometimes draw a mostly-overlapping copy of the square.  This
     generates all kinds of neat blocky graphics when drawing in xor
     mode. */
  if (random() % 4) {
    m->xshadow = 0;
    m->yshadow = 0;
  } else {
    m->xshadow = (random() % (m->width/3)) - (m->width/6);
    m->yshadow = (random() % (m->width/3)) - (m->width/6);
  }

  /* Start with a random y value -- this sort of controls the type of
     deformities seen in the squares. */
  m->y = random() % 256;

  m->t = 0;

  /*
    Doom each square to be aborted at some random point.
    (When doom == (width - 1), the entire square will be drawn.)
  */
  m->doom = random() % m->width;

  m->done = 0;

  return m;
}


static void munch (Display *dpy, Window w, muncher *m) {
  int drawX, drawY;
  XWindowAttributes xgwa;
  static Colormap cmap;

  if (m->done) {
    return;
  }

  XGetWindowAttributes(dpy, w, &xgwa);

  if (!mono_p) {
    /* XXX there are probably bugs with this. */
    cmap = xgwa.colormap;

    if (XAllocColor(dpy, cmap, &m->fgc)) {
      XSetForeground(dpy, gc, m->fgc.pixel);
    }
  }

  /* Finally draw this pass of the munching error. */

  for(m->x = 0; m->x < m->width; m->x++) {
    /* figure out the next point */

    /*
      The ordinary Munching Squares calculation is:
      m->y = ((m->x ^ ((m->t + m->kT) % m->width)) + m->kY) % m->width;

      We create some feedback by plugging in y in place of x, and
      make a couple of values negative so that some parts of some
      squares get drawn in the wrong place.
    */
    m->y = ((-m->y ^ ((-m->t + m->kT) % m->width)) + m->kY) % m->width;

    drawX = ((m->x + m->kX) % m->width) + m->atX;
    drawY = (m->grav ? m->y + m->atY : m->atY + m->width - 1 - m->y);

    XDrawPoint(dpy, w, gc, drawX, drawY);
    if ((m->xshadow != 0) || (m->yshadow != 0)) {
      /* draw the corresponding shadow point */
      XDrawPoint(dpy, w, gc, drawX + m->xshadow, drawY + m->yshadow);
    }
    /* XXX may want to change this to XDrawPoints,
       but it's fast enough without it for the moment. */

  }

  /*
    we've drawn one pass' worth of points.  let the server catch up or
    this won't be interesting to watch at all.  we call this here in
    the hopes of having one whole sequence of dots appear at the same
    time (one for each value of x, surprisingly enough)
  */
  XSync(dpy, False);
  mismunch_handle_events(dpy);

  if (delay) usleep(delay);

  m->t++;
  if (m->t > m->doom) {
    m->done = 1;
    return;
  }

  return;
}


void screenhack (Display *dpy, Window w)
{
  XWindowAttributes xgwa;
  Colormap cmap;
  XGCValues gcv;
  int n = 0;  /* number of squares before we have to clear */
  muncher **munchers;
  int i;

  restart = 0;

  /* get the dimensions of the window */
  XGetWindowAttributes(dpy, w, &xgwa);

  /* create the gc */
  cmap = xgwa.colormap;
  gcv.foreground= get_pixel_resource("foreground","Foreground",
                                     dpy, cmap);
  gcv.background= get_pixel_resource("background","Background",
                                     dpy, cmap);

  gc = XCreateGC(dpy, w, GCForeground|GCBackground, &gcv);

  delay = get_integer_resource("delay", "Integer");
  if (delay < 0) delay = 0;

  simul = get_integer_resource("simul", "Integer");
  if (simul < 1) simul = 1;

  clear = get_integer_resource("clear", "Integer");
  if (clear < 0) clear = 0;

  xor = get_boolean_resource("xor", "Boolean");

  window_width = xgwa.width;
  window_height = xgwa.height;

  calc_logwidths();

  /* always draw xor on mono. */
  if (mono_p || xor) {
    XSetFunction(dpy, gc, GXxor);
  }

  munchers = (muncher **) calloc(simul, sizeof(muncher *));
  for (i = 0; i < simul; i++) {
    munchers[i] = make_muncher(dpy, w);
  }

  for(;;) {
    for (i = 0; i < simul; i++) {
      munch(dpy, w, munchers[i]);

      if (munchers[i]->done) {
        n++;

        free(munchers[i]);
        munchers[i] = make_muncher(dpy, w);

        mismunch_handle_events(dpy);
      }
    }

    if (restart || (clear && n >= clear)) {
      for (i = 0; i < simul; i++) {
        free(munchers[i]);
        munchers[i] = make_muncher(dpy, w);
      }

      XClearWindow(dpy, w);
      n = 0;
      restart = 0;
    }

    XSync(dpy, False);
  }
}