1 /* rotzoomer - creates a collage of rotated and scaled portions of the screen
2 * Copyright (C) 2001 Claudio Matsuoka <claudio@helllabs.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
13 /* (circle-mode by jwz, 4-Jun-2014; not finished yet.) */
18 * -shm enable MIT shared memory extension
19 * -no-shm disable MIT shared memory extension
20 * -n <num> number of zoomboxes
21 * -move enable mobile zoomboxes
22 * -sweep enable sweep mode
23 * -circle enable circle mode
24 * -anim enable snapshot mode
25 * -no-anim enable snapshot mode
26 * -delay delay in milliseconds
30 #include "screenhack.h"
32 #ifdef HAVE_XSHM_EXTENSION
37 int w, h; /* rectangle width and height */
38 int inc1, inc2; /* rotation and zoom angle increments */
39 int dx, dy; /* translation increments */
40 int a1, a2; /* rotation and zoom angular variables */
41 int ox, oy; /* origin in the background copy */
42 int xx, yy; /* left-upper corner position (* 256) */
43 int x, y; /* left-upper corner position */
44 int ww, hh; /* valid area to place left-upper corner */
45 int n; /* number of iteractions */
46 int count; /* current iteraction */
55 XImage *orig_map, *buffer_map;
59 struct zoom_area **zoom_box;
69 async_load_state *img_loader;
71 #ifdef HAVE_XSHM_EXTENSION
73 XShmSegmentInfo shm_info;
79 rotzoom (struct state *st, struct zoom_area *za)
81 int x, y, c, s, zoom, z;
82 int x2 = za->x + za->w - 1, y2 = za->y + za->h - 1;
84 int w2 = (za->w/2) * (za->w/2);
86 z = 8100 * sin (M_PI * za->a2 / 8192);
89 for (y = za->y; y <= y2; y++) {
90 for (x = za->x; x <= x2; x++) {
91 c = zoom * cos (M_PI * za->a1 / 8192);
92 s = zoom * sin (M_PI * za->a1 / 8192);
94 int cx = za->x + za->w / 2;
95 int cy = za->y + za->h / 2;
98 int d2 = (dx*dx) + (dy*dy);
104 double r = sqrt ((double) d2);
105 double th = atan ((double)dy / (double) (dx == 0 ? 1 : dx));
106 th += M_PI * (za->a1 / 300.0);
107 ox = 10 + cx + (int) (r * cos(th));
108 oy = 10 + cy + (int) (r * sin(th));
111 ox = (x * c + y * s) >> 13;
112 oy = (-x * s + y * c) >> 13;
119 while (ox >= st->width)
121 while (oy >= st->height)
124 XPutPixel (st->buffer_map, x, y, XGetPixel (st->orig_map, ox, oy));
128 za->a1 += za->inc1; /* Rotation angle */
131 za->a2 += za->inc2; /* Zoom */
134 za->ox = ox; /* Save state for next iteration */
142 reset_zoom (struct state *st, struct zoom_area *za)
145 int speed = random () % 100 + 100;
146 switch (random () % 4) {
154 za->n = (st->height - 10) * 256 / speed;
159 za->x = st->width - 10;
163 za->n = (st->width - 10) * 256 / speed;
169 za->y = st->height - 10;
172 za->n = (st->height - 10) * 256 / speed;
181 za->n = (st->width - 10) * 256 / speed;
184 za->ww = st->width - za->w;
185 za->hh = st->height - za->h;
187 /* We want smaller angle increments in sweep mode (looks better) */
191 za->inc1 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
192 za->inc2 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
193 } else if (st->circle) {
195 za->w = 50 + random() % 300;
196 if (za->w > st->width / 3)
197 za->w = st->width / 3;
198 if (za->w > st->height / 3)
199 za->w = st->height / 3;
202 za->ww = st->width - za->w;
203 za->hh = st->height - za->h;
205 za->x = (za->ww ? random() % za->ww : 0);
206 za->y = (za->hh ? random() % za->hh : 0);
210 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
213 za->n = 50 + random() % 1000;
217 za->n = 5 + random() % 10;
222 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
223 za->inc2 = ((2 * (random() & 1)) - 1) * (random () % 30);
226 za->w = 50 + random() % 300;
227 za->h = 50 + random() % 300;
229 if (za->w > st->width / 3)
230 za->w = st->width / 3;
231 if (za->h > st->height / 3)
232 za->h = st->height / 3;
234 za->ww = st->width - za->w;
235 za->hh = st->height - za->h;
237 za->x = (za->ww ? random() % za->ww : 0);
238 za->y = (za->hh ? random() % za->hh : 0);
240 za->dx = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
241 za->dy = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
244 za->n = 50 + random() % 1000;
248 za->n = 5 + random() % 10;
253 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
254 za->inc2 = ((2 * (random() & 1)) - 1) * (random () % 30);
257 za->xx = za->x * 256;
258 za->yy = za->y * 256;
264 static struct zoom_area *
265 create_zoom (struct state *st)
267 struct zoom_area *za;
269 za = calloc (1, sizeof (struct zoom_area));
277 update_position (struct zoom_area *za)
287 za->dx = 100 + random() % 100;
292 za->dy = 100 + random() % 100;
295 if (za->x > za->ww) {
297 za->dx = -(100 + random() % 100);
300 if (za->y > za->hh) {
302 za->dy = -(100 + random() % 100);
308 DisplayImage (struct state *st, int x, int y, int w, int h)
310 #ifdef HAVE_XSHM_EXTENSION
312 XShmPutImage (st->dpy, st->window, st->gc, st->buffer_map, x, y, x, y,
315 #endif /* HAVE_XSHM_EXTENSION */
316 XPutImage(st->dpy, st->window, st->gc, st->buffer_map, x, y, x, y, w, h);
321 init_hack (struct state *st)
325 st->start_time = time ((time_t *) 0);
326 st->zoom_box = calloc (st->num_zoom, sizeof (struct zoom_area *));
327 for (i = 0; i < st->num_zoom; i++) {
328 st->zoom_box[i] = create_zoom (st);
331 if (st->height && st->orig_map->data)
332 memcpy (st->buffer_map->data, st->orig_map->data,
333 st->height * st->buffer_map->bytes_per_line);
335 DisplayImage(st, 0, 0, st->width, st->height);
340 rotzoomer_draw (Display *disp, Window win, void *closure)
342 struct state *st = (struct state *) closure;
343 int delay = st->delay;
346 if (st->img_loader) /* still loading */
348 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
349 if (! st->img_loader) { /* just finished */
350 st->orig_map = XGetImage (st->dpy, st->window, 0, 0,
351 st->width, st->height, ~0L, ZPixmap);
357 if (!st->img_loader &&
358 st->start_time + st->duration < time ((time_t *) 0)) {
359 XWindowAttributes xgwa;
360 XGetWindowAttributes(st->dpy, st->window, &xgwa);
361 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
363 st->start_time = time ((time_t *) 0);
367 for (i = 0; i < st->num_zoom; i++) {
368 if (st->move || st->sweep)
369 update_position (st->zoom_box[i]);
371 if (st->zoom_box[i]->n > 0) {
372 if (st->anim || st->zoom_box[i]->count == 0) {
373 rotzoom (st, st->zoom_box[i]);
377 st->zoom_box[i]->n--;
379 reset_zoom (st, st->zoom_box[i]);
383 for (i = 0; i < st->num_zoom; i++) {
384 DisplayImage(st, st->zoom_box[i]->x, st->zoom_box[i]->y,
385 st->zoom_box[i]->w, st->zoom_box[i]->h);
393 setup_X (struct state *st)
395 XWindowAttributes xgwa;
400 XGetWindowAttributes (st->dpy, st->window, &xgwa);
402 st->colormap = xgwa.colormap;
403 st->width = xgwa.width;
404 st->height = xgwa.height;
405 st->visual = xgwa.visual;
412 gcv.function = GXcopy;
413 gcv.subwindow_mode = IncludeInferiors;
414 gcflags = GCFunction;
415 if (use_subwindow_mode_p (xgwa.screen, st->window)) /* see grabscreen.c */
416 gcflags |= GCSubwindowMode;
417 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
418 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
423 #ifdef HAVE_XSHM_EXTENSION
425 st->buffer_map = create_xshm_image(st->dpy, xgwa.visual, depth,
426 ZPixmap, 0, &st->shm_info, st->width, st->height);
427 if (!st->buffer_map) {
429 fprintf(stderr, "create_xshm_image failed\n");
432 #endif /* HAVE_XSHM_EXTENSION */
434 if (!st->buffer_map) {
435 st->buffer_map = XCreateImage(st->dpy, xgwa.visual,
436 depth, ZPixmap, 0, 0, st->width, st->height, 8, 0);
437 st->buffer_map->data = (char *)calloc (st->buffer_map->height,
438 st->buffer_map->bytes_per_line);
444 rotzoomer_init (Display *dpy, Window window)
446 struct state *st = (struct state *) calloc (1, sizeof(*st));
450 #ifdef HAVE_XSHM_EXTENSION
451 st->use_shm = get_boolean_resource (st->dpy, "useSHM", "Boolean");
453 st->num_zoom = get_integer_resource (st->dpy, "numboxes", "Integer");
455 s = get_string_resource (dpy, "mode", "Mode");
456 if (!s || !*s || !strcasecmp (s, "stationary"))
458 else if (!strcasecmp (s, "move"))
460 else if (!strcasecmp (s, "sweep"))
462 else if (!strcasecmp (s, "circle"))
465 fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s);
467 st->anim = get_boolean_resource (st->dpy, "anim", "Boolean");
468 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
469 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
470 if (st->delay < 0) st->delay = 0;
471 if (st->duration < 1) st->duration = 1;
473 /* In sweep or static mode, we want only one box */
474 if (st->sweep || !st->anim)
477 /* Can't have static sweep mode */
486 st->start_time = time ((time_t *) 0);
494 rotzoomer_reshape (Display *dpy, Window window, void *closure,
495 unsigned int w, unsigned int h)
500 rotzoomer_event (Display *dpy, Window window, void *closure, XEvent *event)
502 struct state *st = (struct state *) closure;
503 if (screenhack_event_helper (dpy, window, event))
512 rotzoomer_free (Display *dpy, Window window, void *closure)
514 struct state *st = (struct state *) closure;
519 static const char *rotzoomer_defaults[] = {
520 ".background: black",
521 ".foreground: white",
523 #ifdef HAVE_XSHM_EXTENSION
534 "*ignoreRotation: True",
535 "*rotateImages: True",
541 static XrmOptionDescRec rotzoomer_options[] = {
542 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
543 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
544 { "-mode", ".mode", XrmoptionSepArg, 0 },
545 { "-move", ".mode", XrmoptionNoArg, "move" },
546 { "-sweep", ".mode", XrmoptionNoArg, "sweep" },
547 { "-circle", ".mode", XrmoptionNoArg, "circle"},
548 { "-anim", ".anim", XrmoptionNoArg, "True" },
549 { "-no-anim", ".anim", XrmoptionNoArg, "False" },
550 { "-delay", ".delay", XrmoptionSepArg, 0 },
551 {"-duration", ".duration", XrmoptionSepArg, 0 },
552 { "-n", ".numboxes", XrmoptionSepArg, 0 },
557 XSCREENSAVER_MODULE ("RotZoomer", rotzoomer)