~ubuntu-branches/debian/sid/xscreensaver/sid

« back to all changes in this revision

Viewing changes to hacks/pedal.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas, Tormod Volden, Jose Luis Rivas
  • Date: 2008-07-15 14:48:48 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080715144848-c6c6mhyxij0dk2p7
Tags: 5.05-3
[ Tormod Volden ]
* debian/patches/10_jwz-xscreensaver-randr-patch-3.patch:
  from upstream, addresses issues with xrandr/xinerama
  (Closes: #482385, #428797, #471920, #453708, #473681, #479715, #480231)
* fixed typo "screen < real_nscreens" in driver/lock:1527 from above patch
* drop 61_DualHead-nVidia_bug471920.patch (obsolete)
* drop 67_XineRama-mode_bug473681.patch (obsolete)
* fix m6502.o typo in hacks/Makefile.in
* refresh 53_XScreenSaver.ad.in.patch
* refresh (disabled) 60_add-ant-hack.patch

[ Jose Luis Rivas ]
* add xscreensaver-demo desktop file, thanks to Daniel Dickinson
  (Closes: #480592)
* update package descriptions (thanks jwz)
* fix categories in xscreensaver.menu
* change build-deps from xlibmesa-gl-dev to libgl1-mesa-dev,
  xutils to xutils-dev, x-dev to x11proto-core-dev.
* bump Standards-Version to 3.8.0
* add Vcs fields and Homepage to debian/control
* Flurry is not installed until the bug get fixed (Closes: #484112)

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <math.h>
23
23
#include <stdlib.h>
24
24
#include "screenhack.h"
 
25
#include "erase.h"
25
26
 
26
27
/* If MAXLINES is too big, we might not be able to get it
27
28
 * to the X server in the 2byte length field. Must be less
29
30
 */
30
31
#define MAXLINES (16 * 1024)
31
32
#define MAXPOINTS MAXLINES
32
 
XPoint *points;
33
33
 
34
34
/* 
35
35
 * If the pedal has only this many lines, it must be ugly and we dont
37
37
 */
38
38
#define MINLINES 7
39
39
 
40
 
static int sizex, sizey;
41
 
static int delay;
42
 
static int fadedelay;
43
 
static int maxlines;
44
 
static GC gc;
45
 
static XColor foreground, background;
46
 
static Colormap cmap;
47
 
 
48
 
static Bool fade_p;
 
40
 
 
41
struct state {
 
42
  Display *dpy;
 
43
  Window window;
 
44
 
 
45
  XPoint *points;
 
46
 
 
47
  int sizex, sizey;
 
48
  int delay;
 
49
  int maxlines;
 
50
  GC gc;
 
51
  XColor foreground, background;
 
52
  Colormap cmap;
 
53
 
 
54
  eraser_state *eraser;
 
55
  int erase_p;
 
56
};
49
57
 
50
58
 
51
59
/*
116
124
}
117
125
 
118
126
static int
119
 
compute_pedal(XPoint *points, int maxpoints)
 
127
compute_pedal(struct state *st, XPoint *points, int maxpoints)
120
128
/*
121
129
 * Description:
122
130
 *
150
158
 
151
159
    double r;
152
160
    int theta = 0;
153
 
    XPoint *pp = points;
 
161
    XPoint *pp = st->points;
154
162
    int count;
155
163
    int numpoints;
156
164
 
157
165
    /* Just to make sure that this division is not done inside the loop */
158
 
    int h_width = sizex / 2, h_height = sizey / 2 ;
 
166
    int h_width = st->sizex / 2, h_height = st->sizey / 2 ;
159
167
 
160
168
    for (;;) {
161
 
        d = rand_range (MINLINES, maxlines);
 
169
        d = rand_range (MINLINES, st->maxlines);
162
170
 
163
171
        a = rand_range (1, d);
164
172
        b = rand_range (1, d);
189
197
    return(numpoints);
190
198
}
191
199
 
192
 
static void
193
 
init_pedal (Display *dpy, Window window)
 
200
static void *
 
201
pedal_init (Display *dpy, Window window)
194
202
{
 
203
  struct state *st = (struct state *) calloc (1, sizeof(*st));
195
204
  XGCValues gcv;
196
205
  XWindowAttributes xgwa;
197
206
 
198
 
  fade_p = !mono_p;
199
 
 
200
 
  delay = get_integer_resource ("delay", "Integer");
201
 
  if (delay < 0) delay = 0;
202
 
 
203
 
  fadedelay = get_integer_resource ("fadedelay", "Integer");
204
 
  if (fadedelay < 0) fadedelay = 0;
205
 
 
206
 
  maxlines = get_integer_resource ("maxlines", "Integer");
207
 
  if (maxlines < MINLINES) maxlines = MINLINES;
208
 
  else if (maxlines > MAXLINES) maxlines = MAXLINES;
209
 
 
210
 
  points = (XPoint *)malloc(sizeof(XPoint) * maxlines);
211
 
 
212
 
  XGetWindowAttributes (dpy, window, &xgwa);
213
 
  sizex = xgwa.width;
214
 
  sizey = xgwa.height;
215
 
 
216
 
  if ((xgwa.visual->class != GrayScale) && (xgwa.visual->class != PseudoColor))
217
 
    fade_p = False;
218
 
 
219
 
  cmap = xgwa.colormap;
 
207
  st->dpy = dpy;
 
208
  st->window = window;
 
209
 
 
210
  st->delay = get_integer_resource (st->dpy, "delay", "Integer");
 
211
  if (st->delay < 0) st->delay = 0;
 
212
 
 
213
  st->maxlines = get_integer_resource (st->dpy, "maxlines", "Integer");
 
214
  if (st->maxlines < MINLINES) st->maxlines = MINLINES;
 
215
  else if (st->maxlines > MAXLINES) st->maxlines = MAXLINES;
 
216
 
 
217
  st->points = (XPoint *)malloc(sizeof(XPoint) * st->maxlines);
 
218
 
 
219
  XGetWindowAttributes (st->dpy, st->window, &xgwa);
 
220
  st->sizex = xgwa.width;
 
221
  st->sizey = xgwa.height;
 
222
 
 
223
  st->cmap = xgwa.colormap;
220
224
 
221
225
  gcv.function = GXcopy;
222
 
  gcv.foreground = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
223
 
  gcv.background = get_pixel_resource ("background", "Background", dpy, cmap);
224
 
  gc = XCreateGC (dpy, window, GCForeground | GCBackground |GCFunction, &gcv);
225
 
 
226
 
  if (fade_p)
227
 
  {
228
 
      int status;
229
 
      foreground.pixel = gcv.foreground;
230
 
      XQueryColor (dpy, cmap, &foreground);
231
 
 
232
 
      status = XAllocColorCells (
233
 
                        dpy,
234
 
                        cmap,
235
 
                        0,
236
 
                        NULL,
237
 
                        0,
238
 
                        &foreground.pixel,
239
 
                        1);
240
 
      if (status)
241
 
      {
242
 
          XStoreColor ( dpy, cmap, &foreground);
243
 
          XSetForeground (dpy, gc, foreground.pixel);
244
 
 
245
 
          background.pixel = gcv.background;
246
 
          XQueryColor (dpy, cmap, &background);
247
 
      }
248
 
      else
249
 
      {
250
 
          /* If we cant allocate a color cell, then just forget the
251
 
           * whole fade business.
252
 
           */
253
 
          fade_p = False;
254
 
      }
255
 
  }
256
 
}
257
 
 
258
 
static void
259
 
fade_foreground (Display *dpy, Colormap cmap,
260
 
                 XColor from, XColor to, int steps)
261
 
/*
262
 
 * This routine assumes that we have a writeable colormap.
263
 
 * That means that the default colormap is not full, and that
264
 
 * the visual class is PseudoColor or GrayScale.
265
 
 */
266
 
{
267
 
    int i;
268
 
    XColor inbetween;
269
 
    int udelay = fadedelay / (steps + 1);
270
 
 
271
 
    inbetween = foreground;
272
 
    for (i = 0; i <= steps; i++ )
273
 
    {
274
 
      inbetween.red   = from.red   + (to.red   - from.red)   * i / steps ;
275
 
      inbetween.green = from.green + (to.green - from.green) * i / steps ;
276
 
      inbetween.blue  = from.blue  + (to.blue  - from.blue)  * i / steps ;
277
 
      XStoreColor (dpy, cmap, &inbetween);
278
 
      /* If we don't sync, these can bunch up */
279
 
      XSync(dpy, False);
280
 
      screenhack_handle_events (dpy);
281
 
      usleep(udelay);
282
 
    }
283
 
}
284
 
 
285
 
static void
286
 
pedal (Display *dpy, Window window)
 
226
  gcv.foreground = get_pixel_resource (st->dpy, st->cmap, "foreground", "Foreground");
 
227
  gcv.background = get_pixel_resource (st->dpy, st->cmap, "background", "Background");
 
228
  st->gc = XCreateGC (st->dpy, st->window, GCForeground | GCBackground |GCFunction, &gcv);
 
229
 
 
230
  return st;
 
231
}
 
232
 
287
233
/*
288
234
 *    Since the XFillPolygon doesn't require that the last
289
235
 *    point == first point, the number of points is the same
291
237
 *    the line from the last point to the first point.
292
238
 *
293
239
 */
294
 
{
295
 
   int numpoints;
296
 
 
297
 
   numpoints = compute_pedal(points, maxlines);
298
 
 
299
 
   /* Fade out, make foreground the same as background */
300
 
   if (fade_p)
301
 
     fade_foreground (dpy, cmap, foreground, background, 32);
302
 
 
303
 
    /* Clear the window of previous garbage */
304
 
    XClearWindow (dpy, window);
305
 
 
306
 
    XFillPolygon (
307
 
                dpy,
308
 
                window,
309
 
                gc,
310
 
                points,
311
 
                numpoints,
312
 
                Complex,
313
 
                CoordModeOrigin);
314
 
 
315
 
   /* Pick a new foreground color (added by jwz) */
316
 
   if (! mono_p)
317
 
     {
318
 
       XColor color;
319
 
       hsv_to_rgb (random()%360, 1.0, 1.0,
320
 
                   &color.red, &color.green, &color.blue);
321
 
       XSync(dpy, False);
322
 
       if (fade_p)
323
 
         {
324
 
           foreground.red = color.red;
325
 
           foreground.green = color.green;
326
 
           foreground.blue = color.blue;
327
 
           /* don't do this here -- let fade_foreground() bring it up! */
328
 
           /* XStoreColor (dpy, cmap, &foreground); */
329
 
         }
330
 
       else if (XAllocColor (dpy, cmap, &color))
331
 
         {
332
 
           XSetForeground (dpy, gc, color.pixel);
333
 
           XFreeColors (dpy, cmap, &foreground.pixel, 1, 0);
334
 
           foreground.red = color.red;
335
 
           foreground.green = color.green;
336
 
           foreground.blue = color.blue;
337
 
           foreground.pixel = color.pixel;
338
 
         }
339
 
       XSync(dpy, False);
340
 
     }
341
 
 
342
 
    /* Fade in by bringing the foreground back from background */
343
 
    if (fade_p)
344
 
       fade_foreground (dpy, cmap, background, foreground, 32);
345
 
}
 
240
static unsigned long
 
241
pedal_draw (Display *dpy, Window window, void *closure)
 
242
{
 
243
  struct state *st = (struct state *) closure;
 
244
  int numpoints;
 
245
  int erase_delay = 10000;
 
246
  int long_delay = 1000000 * st->delay;
 
247
 
 
248
  if (st->erase_p || st->eraser) {
 
249
    st->eraser = erase_window (dpy, window, st->eraser);
 
250
    st->erase_p = 0;
 
251
    return (st->eraser ? erase_delay : 1000000);
 
252
  }
 
253
 
 
254
  numpoints = compute_pedal(st, st->points, st->maxlines);
 
255
 
 
256
  /* Pick a new foreground color (added by jwz) */
 
257
  if (! mono_p)
 
258
    {
 
259
      XColor color;
 
260
      hsv_to_rgb (random()%360, 1.0, 1.0,
 
261
                  &color.red, &color.green, &color.blue);
 
262
      if (XAllocColor (st->dpy, st->cmap, &color))
 
263
        {
 
264
          XSetForeground (st->dpy, st->gc, color.pixel);
 
265
          XFreeColors (st->dpy, st->cmap, &st->foreground.pixel, 1, 0);
 
266
          st->foreground.red = color.red;
 
267
          st->foreground.green = color.green;
 
268
          st->foreground.blue = color.blue;
 
269
          st->foreground.pixel = color.pixel;
 
270
        }
 
271
    }
 
272
 
 
273
  XFillPolygon (st->dpy, st->window, st->gc, st->points, numpoints,
 
274
                Complex, CoordModeOrigin);
 
275
 
 
276
  st->erase_p = 1;
 
277
  return long_delay;
 
278
}
 
279
 
 
280
static void
 
281
pedal_reshape (Display *dpy, Window window, void *closure, 
 
282
                 unsigned int w, unsigned int h)
 
283
{
 
284
  struct state *st = (struct state *) closure;
 
285
  st->sizex = w;
 
286
  st->sizey = h;
 
287
}
 
288
 
 
289
static Bool
 
290
pedal_event (Display *dpy, Window window, void *closure, XEvent *event)
 
291
{
 
292
  return False;
 
293
}
 
294
 
 
295
static void
 
296
pedal_free (Display *dpy, Window window, void *closure)
 
297
{
 
298
  struct state *st = (struct state *) closure;
 
299
  free (st);
 
300
}
 
301
 
346
302
 
347
303
 
348
 
char *progclass = "Pedal";
349
304
 
350
305
/*
351
306
 * If we are trying to save the screen, the background
352
307
 * should be dark.
353
308
 */
354
 
char *defaults [] = {
 
309
static const char *pedal_defaults [] = {
355
310
  ".background:                 black",
356
311
  ".foreground:                 white",
357
312
  "*delay:                      5",
358
 
  "*fadedelay:                  200000",
359
313
  "*maxlines:                   1000",
360
314
  0
361
315
};
362
316
 
363
 
XrmOptionDescRec options [] = {
 
317
static XrmOptionDescRec pedal_options [] = {
364
318
  { "-delay",           ".delay",               XrmoptionSepArg, 0 },
365
 
  { "-fadedelay",       ".fadedelay",           XrmoptionSepArg, 0 },
366
319
  { "-maxlines",        ".maxlines",            XrmoptionSepArg, 0 },
367
320
  { "-foreground",      ".foreground",          XrmoptionSepArg, 0 },
368
321
  { "-background",      ".background",          XrmoptionSepArg, 0 },
369
322
  { 0, 0, 0, 0 }
370
323
};
371
324
 
372
 
void
373
 
screenhack (Display *dpy, Window window)
374
 
{
375
 
    init_pedal (dpy, window);
376
 
    for (;;) {
377
 
        pedal (dpy, window);
378
 
        XSync(dpy, False);
379
 
        screenhack_handle_events (dpy);
380
 
        if (delay) sleep (delay);
381
 
    }
382
 
}
 
325
XSCREENSAVER_MODULE ("Pedal", pedal)