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);
107 double r = sqrt ((double) d2);
108 double th = atan ((double)dy / (double) (dx == 0 ? 1 : dx));
109 th += M_PI * (za->a1 / 300.0);
110 ox = 10 + cx + (int) (r * cos(th));
111 oy = 10 + cy + (int) (r * sin(th));
114 ox = (x * c + y * s) >> 13;
115 oy = (-x * s + y * c) >> 13;
122 while (ox >= st->width)
124 while (oy >= st->height)
127 XPutPixel (st->buffer_map, x, y, XGetPixel (st->orig_map, ox, oy));
131 za->a1 += za->inc1; /* Rotation angle */
134 za->a2 += za->inc2; /* Zoom */
137 za->ox = ox; /* Save state for next iteration */
145 reset_zoom (struct state *st, struct zoom_area *za)
148 int speed = random () % 100 + 100;
149 switch (random () % 4) {
157 za->n = (st->height - 10) * 256 / speed;
162 za->x = st->width - 10;
166 za->n = (st->width - 10) * 256 / speed;
172 za->y = st->height - 10;
175 za->n = (st->height - 10) * 256 / speed;
184 za->n = (st->width - 10) * 256 / speed;
187 za->ww = st->width - za->w;
188 za->hh = st->height - za->h;
190 /* We want smaller angle increments in sweep mode (looks better) */
194 za->inc1 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
195 za->inc2 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
196 } else if (st->circle) {
198 za->w = 50 + random() % 300;
199 if (za->w > st->width / 3)
200 za->w = st->width / 3;
201 if (za->w > st->height / 3)
202 za->w = st->height / 3;
205 za->ww = st->width - za->w;
206 za->hh = st->height - za->h;
208 za->x = (za->ww ? random() % za->ww : 0);
209 za->y = (za->hh ? random() % za->hh : 0);
213 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
216 za->n = 50 + random() % 1000;
220 za->n = 5 + random() % 10;
225 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
226 za->inc2 = ((2 * (random() & 1)) - 1) * (random () % 30);
229 za->w = 50 + random() % 300;
230 za->h = 50 + random() % 300;
232 if (za->w > st->width / 3)
233 za->w = st->width / 3;
234 if (za->h > st->height / 3)
235 za->h = st->height / 3;
237 za->ww = st->width - za->w;
238 za->hh = st->height - za->h;
240 za->x = (za->ww ? random() % za->ww : 0);
241 za->y = (za->hh ? random() % za->hh : 0);
243 za->dx = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
244 za->dy = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
247 za->n = 50 + random() % 1000;
251 za->n = 5 + random() % 10;
256 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
257 za->inc2 = ((2 * (random() & 1)) - 1) * (random () % 30);
260 za->xx = za->x * 256;
261 za->yy = za->y * 256;
267 static struct zoom_area *
268 create_zoom (struct state *st)
270 struct zoom_area *za;
272 za = malloc (sizeof (struct zoom_area));
280 update_position (struct zoom_area *za)
290 za->dx = 100 + random() % 100;
295 za->dy = 100 + random() % 100;
298 if (za->x > za->ww) {
300 za->dx = -(100 + random() % 100);
303 if (za->y > za->hh) {
305 za->dy = -(100 + random() % 100);
311 DisplayImage (struct state *st, int x, int y, int w, int h)
313 #ifdef HAVE_XSHM_EXTENSION
315 XShmPutImage (st->dpy, st->window, st->gc, st->buffer_map, x, y, x, y,
318 #endif /* HAVE_XSHM_EXTENSION */
319 XPutImage(st->dpy, st->window, st->gc, st->buffer_map, x, y, x, y, w, h);
324 init_hack (struct state *st)
328 st->start_time = time ((time_t) 0);
329 st->zoom_box = calloc (st->num_zoom, sizeof (struct zoom_area *));
330 for (i = 0; i < st->num_zoom; i++) {
331 st->zoom_box[i] = create_zoom (st);
334 if (st->height && st->orig_map->data)
335 memcpy (st->buffer_map->data, st->orig_map->data,
336 st->height * st->buffer_map->bytes_per_line);
338 DisplayImage(st, 0, 0, st->width, st->height);
343 rotzoomer_draw (Display *disp, Window win, void *closure)
345 struct state *st = (struct state *) closure;
346 int delay = st->delay;
349 if (st->img_loader) /* still loading */
351 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
352 if (! st->img_loader) { /* just finished */
353 st->orig_map = XGetImage (st->dpy, st->window, 0, 0,
354 st->width, st->height, ~0L, ZPixmap);
360 if (!st->img_loader &&
361 st->start_time + st->duration < time ((time_t) 0)) {
362 XWindowAttributes xgwa;
363 XGetWindowAttributes(st->dpy, st->window, &xgwa);
364 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
366 st->start_time = time ((time_t) 0);
370 for (i = 0; i < st->num_zoom; i++) {
371 if (st->move || st->sweep)
372 update_position (st->zoom_box[i]);
374 if (st->zoom_box[i]->n > 0) {
375 if (st->anim || st->zoom_box[i]->count == 0) {
376 rotzoom (st, st->zoom_box[i]);
380 st->zoom_box[i]->n--;
382 reset_zoom (st, st->zoom_box[i]);
386 for (i = 0; i < st->num_zoom; i++) {
387 DisplayImage(st, st->zoom_box[i]->x, st->zoom_box[i]->y,
388 st->zoom_box[i]->w, st->zoom_box[i]->h);
396 setup_X (struct state *st)
398 XWindowAttributes xgwa;
403 XGetWindowAttributes (st->dpy, st->window, &xgwa);
405 st->colormap = xgwa.colormap;
406 st->width = xgwa.width;
407 st->height = xgwa.height;
408 st->visual = xgwa.visual;
415 gcv.function = GXcopy;
416 gcv.subwindow_mode = IncludeInferiors;
417 gcflags = GCFunction;
418 if (use_subwindow_mode_p (xgwa.screen, st->window)) /* see grabscreen.c */
419 gcflags |= GCSubwindowMode;
420 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
421 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
426 #ifdef HAVE_XSHM_EXTENSION
428 st->buffer_map = create_xshm_image(st->dpy, xgwa.visual, depth,
429 ZPixmap, 0, &st->shm_info, st->width, st->height);
430 if (!st->buffer_map) {
432 fprintf(stderr, "create_xshm_image failed\n");
435 #endif /* HAVE_XSHM_EXTENSION */
437 if (!st->buffer_map) {
438 st->buffer_map = XCreateImage(st->dpy, xgwa.visual,
439 depth, ZPixmap, 0, 0, st->width, st->height, 8, 0);
440 st->buffer_map->data = (char *)calloc (st->buffer_map->height,
441 st->buffer_map->bytes_per_line);
447 rotzoomer_init (Display *dpy, Window window)
449 struct state *st = (struct state *) calloc (1, sizeof(*st));
453 #ifdef HAVE_XSHM_EXTENSION
454 st->use_shm = get_boolean_resource (st->dpy, "useSHM", "Boolean");
456 st->num_zoom = get_integer_resource (st->dpy, "numboxes", "Integer");
458 s = get_string_resource (dpy, "mode", "Mode");
459 if (!s || !*s || !strcasecmp (s, "stationary"))
461 else if (!strcasecmp (s, "move"))
463 else if (!strcasecmp (s, "sweep"))
465 else if (!strcasecmp (s, "circle"))
468 fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s);
470 st->anim = get_boolean_resource (st->dpy, "anim", "Boolean");
471 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
472 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
473 if (st->delay < 0) st->delay = 0;
474 if (st->duration < 1) st->duration = 1;
476 /* In sweep or static mode, we want only one box */
477 if (st->sweep || !st->anim)
480 /* Can't have static sweep mode */
489 st->start_time = time ((time_t) 0);
497 rotzoomer_reshape (Display *dpy, Window window, void *closure,
498 unsigned int w, unsigned int h)
503 rotzoomer_event (Display *dpy, Window window, void *closure, XEvent *event)
505 struct state *st = (struct state *) closure;
506 if (screenhack_event_helper (dpy, window, event))
515 rotzoomer_free (Display *dpy, Window window, void *closure)
517 struct state *st = (struct state *) closure;
522 static const char *rotzoomer_defaults[] = {
523 ".background: black",
524 ".foreground: white",
526 #ifdef HAVE_XSHM_EXTENSION
537 "*ignoreRotation: True",
538 "*rotateImages: True",
544 static XrmOptionDescRec rotzoomer_options[] = {
545 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
546 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
547 { "-mode", ".mode", XrmoptionSepArg, 0 },
548 { "-move", ".mode", XrmoptionNoArg, "move" },
549 { "-sweep", ".mode", XrmoptionNoArg, "sweep" },
550 { "-circle", ".mode", XrmoptionNoArg, "circle"},
551 { "-anim", ".anim", XrmoptionNoArg, "True" },
552 { "-no-anim", ".anim", XrmoptionNoArg, "False" },
553 { "-delay", ".delay", XrmoptionSepArg, 0 },
554 {"-duration", ".duration", XrmoptionSepArg, 0 },
555 { "-n", ".numboxes", XrmoptionSepArg, 0 },
560 XSCREENSAVER_MODULE ("RotZoomer", rotzoomer)