1 /* rotzoomer - creates a collage of rotated and scaled portions of the screen
2 * Copyright (C) 2001-2016 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, 2014, 2016. */
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"
34 int w, h; /* rectangle width and height */
35 int inc1, inc2; /* rotation and zoom angle increments */
36 int dx, dy; /* translation increments */
37 int a1, a2; /* rotation and zoom angular variables */
38 int ox, oy; /* origin in the background copy */
39 int xx, yy; /* left-upper corner position (* 256) */
40 int x, y; /* left-upper corner position */
41 int ww, hh; /* valid area to place left-upper corner */
42 int n; /* number of iteractions */
43 int count; /* current iteraction */
52 XImage *orig_map, *buffer_map;
56 struct zoom_area **zoom_box;
66 async_load_state *img_loader;
69 XShmSegmentInfo shm_info;
74 rotzoom (struct state *st, struct zoom_area *za)
76 int x, y, c, s, zoom, z;
77 int x2 = za->x + za->w - 1, y2 = za->y + za->h - 1;
79 int w2 = (za->w/2) * (za->w/2);
81 z = 8100 * sin (M_PI * za->a2 / 8192);
84 for (y = za->y; y <= y2; y++) {
85 for (x = za->x; x <= x2; x++) {
87 double a = M_PI * za->a1 / 8192;
91 int cx = za->x + za->w / 2;
92 int cy = za->y + za->h / 2;
95 int d2 = (dx*dx) + (dy*dy);
100 double r = sqrt ((double) d2);
101 double th = atan ((double)dy / (double) (dx == 0 ? 1 : dx));
103 if (dx < 0) th += M_PI;
104 th += M_PI * (za->a1 / 600.0);
105 ox = cx + (int) (r * cos(th));
106 oy = cy + (int) (r * sin(th));
109 ox = (x * c + y * s) >> 13;
110 oy = (-x * s + y * c) >> 13;
118 while (ox >= st->width)
120 while (oy >= st->height)
123 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 */
137 if (st->circle && za->n <= 1)
139 /* Done rotating the circle: copy the bits from the working set back
140 into the origin, so that subsequent rotations pick up these changes.
142 int cx = za->x + za->w / 2;
143 int cy = za->y + za->h / 2;
144 int w2 = (za->w/2) * (za->w/2);
145 for (y = za->y; y < za->y + za->h; y++)
146 for (x = za->x; x < za->x + za->w; x++)
150 int d2 = (dx*dx) + (dy*dy);
152 XPutPixel (st->orig_map, x, y, XGetPixel (st->buffer_map, x, y));
161 reset_zoom (struct state *st, struct zoom_area *za)
164 int speed = random () % 100 + 100;
165 switch (random () % 4) {
173 za->n = (st->height - 10) * 256 / speed;
178 za->x = st->width - 10;
182 za->n = (st->width - 10) * 256 / speed;
188 za->y = st->height - 10;
191 za->n = (st->height - 10) * 256 / speed;
200 za->n = (st->width - 10) * 256 / speed;
203 za->ww = st->width - za->w;
204 za->hh = st->height - za->h;
206 /* We want smaller angle increments in sweep mode (looks better) */
210 za->inc1 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
211 za->inc2 = ((2 * (random() & 1)) - 1) * (1 + random () % 7);
212 } else if (st->circle) {
214 za->w = 50 + random() % 300;
215 if (za->w > st->width / 3)
216 za->w = st->width / 3;
217 if (za->w > st->height / 3)
218 za->w = st->height / 3;
221 za->ww = st->width - za->w;
222 za->hh = st->height - za->h;
224 za->x = (za->ww ? random() % za->ww : 0);
225 za->y = (za->hh ? random() % za->hh : 0);
232 /* #### If we go clockwise, it doesn't start rotating from 0.
233 So only go counter-clockwise for now. Sigh. */
234 za->inc1 = (random () % 30);
236 za->n = 50 + random() % 100;
239 za->count = random() % (za->n / 2);
244 za->w = 50 + random() % 300;
245 za->h = 50 + random() % 300;
247 if (za->w > st->width / 3)
248 za->w = st->width / 3;
249 if (za->h > st->height / 3)
250 za->h = st->height / 3;
252 za->ww = st->width - za->w;
253 za->hh = st->height - za->h;
255 za->x = (za->ww ? random() % za->ww : 0);
256 za->y = (za->hh ? random() % za->hh : 0);
258 za->dx = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
259 za->dy = ((2 * (random() & 1)) - 1) * (100 + random() % 300);
262 za->n = 50 + random() % 1000;
266 za->n = 5 + random() % 10;
271 za->inc1 = ((2 * (random() & 1)) - 1) * (random () % 30);
272 za->inc2 = ((2 * (random() & 1)) - 1) * (random () % 30);
275 za->xx = za->x * 256;
276 za->yy = za->y * 256;
282 static struct zoom_area *
283 create_zoom (struct state *st)
285 struct zoom_area *za;
287 za = calloc (1, sizeof (struct zoom_area));
295 update_position (struct zoom_area *za)
305 za->dx = 100 + random() % 100;
310 za->dy = 100 + random() % 100;
313 if (za->x > za->ww) {
315 za->dx = -(100 + random() % 100);
318 if (za->y > za->hh) {
320 za->dy = -(100 + random() % 100);
326 DisplayImage (struct state *st, int x, int y, int w, int h)
328 put_xshm_image (st->dpy, st->window, st->gc, st->buffer_map, x, y, x, y,
329 w, h, &st->shm_info);
334 set_mode(struct state *st)
336 char *s = get_string_resource (st->dpy, "mode", "Mode");
337 if (!s || !*s || !strcasecmp (s, "random"))
339 switch (random() % 4) {
340 case 0: s = "stationary"; break;
341 case 1: s = "move"; break;
342 case 2: s = "sweep"; break;
343 case 3: s = "circle"; break;
352 if (!strcasecmp (s, "stationary"))
354 else if (!strcasecmp (s, "move"))
356 else if (!strcasecmp (s, "sweep"))
358 else if (!strcasecmp (s, "circle"))
361 fprintf (stderr, "%s: bogus mode: \"%s\"\n", progname, s);
366 init_hack (struct state *st)
372 st->start_time = time ((time_t *) 0);
373 st->zoom_box = calloc (st->num_zoom, sizeof (struct zoom_area *));
374 for (i = 0; i < st->num_zoom; i++) {
375 st->zoom_box[i] = create_zoom (st);
378 if (st->height && st->orig_map->data)
379 memcpy (st->buffer_map->data, st->orig_map->data,
380 st->height * st->buffer_map->bytes_per_line);
382 DisplayImage(st, 0, 0, st->width, st->height);
387 rotzoomer_draw (Display *disp, Window win, void *closure)
389 struct state *st = (struct state *) closure;
390 int delay = st->delay;
393 if (st->img_loader) /* still loading */
395 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
396 if (! st->img_loader) { /* just finished */
397 if (! st->pm) abort();
398 st->orig_map = XGetImage (st->dpy, st->pm,
399 0, 0, st->width, st->height,
406 if (!st->img_loader &&
407 st->start_time + st->duration < time ((time_t *) 0)) {
408 XWindowAttributes xgwa;
409 XGetWindowAttributes(st->dpy, st->window, &xgwa);
410 /* On MacOS X11, XGetImage on a Window often gets an inexplicable BadMatch,
411 possibly due to the window manager having occluded something? It seems
412 nondeterministic. Loading the image into a pixmap instead fixes it. */
413 if (st->pm) XFreePixmap (st->dpy, st->pm);
414 st->pm = XCreatePixmap (st->dpy, st->window,
415 xgwa.width, xgwa.height, xgwa.depth);
416 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
418 st->start_time = time ((time_t *) 0);
422 for (i = 0; i < st->num_zoom; i++) {
423 if (st->move || st->sweep)
424 update_position (st->zoom_box[i]);
426 if (st->zoom_box[i]->n > 0) {
427 if (st->anim || st->zoom_box[i]->count == 0) {
428 rotzoom (st, st->zoom_box[i]);
432 st->zoom_box[i]->n--;
434 reset_zoom (st, st->zoom_box[i]);
438 for (i = 0; i < st->num_zoom; i++) {
439 DisplayImage(st, st->zoom_box[i]->x, st->zoom_box[i]->y,
440 st->zoom_box[i]->w, st->zoom_box[i]->h);
448 setup_X (struct state *st)
450 XWindowAttributes xgwa;
455 XGetWindowAttributes (st->dpy, st->window, &xgwa);
457 st->colormap = xgwa.colormap;
458 st->width = xgwa.width;
459 st->height = xgwa.height;
460 st->visual = xgwa.visual;
467 gcv.function = GXcopy;
468 gcv.subwindow_mode = IncludeInferiors;
469 gcflags = GCFunction;
470 if (use_subwindow_mode_p (xgwa.screen, st->window)) /* see grabscreen.c */
471 gcflags |= GCSubwindowMode;
472 st->gc = XCreateGC (st->dpy, st->window, gcflags, &gcv);
473 if (st->pm) XFreePixmap (st->dpy, st->pm);
474 st->pm = XCreatePixmap (st->dpy, st->window,
475 xgwa.width, xgwa.height, xgwa.depth);
476 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
479 st->buffer_map = create_xshm_image(st->dpy, xgwa.visual, depth,
480 ZPixmap, &st->shm_info, st->width, st->height);
485 rotzoomer_init (Display *dpy, Window window)
487 struct state *st = (struct state *) calloc (1, sizeof(*st));
490 st->num_zoom = get_integer_resource (st->dpy, "numboxes", "Integer");
494 st->anim = get_boolean_resource (st->dpy, "anim", "Boolean");
495 st->delay = get_integer_resource (st->dpy, "delay", "Integer");
496 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
497 if (st->delay < 0) st->delay = 0;
498 if (st->duration < 1) st->duration = 1;
500 /* In sweep or static mode, we want only one box */
501 if (st->sweep || !st->anim)
504 /* Can't have static sweep mode */
513 st->start_time = time ((time_t *) 0);
521 rotzoomer_reshape (Display *dpy, Window window, void *closure,
522 unsigned int w, unsigned int h)
527 rotzoomer_event (Display *dpy, Window window, void *closure, XEvent *event)
529 struct state *st = (struct state *) closure;
530 if (screenhack_event_helper (dpy, window, event))
539 rotzoomer_free (Display *dpy, Window window, void *closure)
541 struct state *st = (struct state *) closure;
542 if (st->pm) XFreePixmap (dpy, st->pm);
547 static const char *rotzoomer_defaults[] = {
548 ".background: black",
549 ".foreground: white",
551 #ifdef HAVE_XSHM_EXTENSION
562 "*ignoreRotation: True",
563 "*rotateImages: True",
569 static XrmOptionDescRec rotzoomer_options[] = {
570 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
571 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
572 { "-mode", ".mode", XrmoptionSepArg, 0 },
573 { "-move", ".mode", XrmoptionNoArg, "move" },
574 { "-sweep", ".mode", XrmoptionNoArg, "sweep" },
575 { "-circle", ".mode", XrmoptionNoArg, "circle"},
576 { "-anim", ".anim", XrmoptionNoArg, "True" },
577 { "-no-anim", ".anim", XrmoptionNoArg, "False" },
578 { "-delay", ".delay", XrmoptionSepArg, 0 },
579 {"-duration", ".duration", XrmoptionSepArg, 0 },
580 { "-n", ".numboxes", XrmoptionSepArg, 0 },
585 XSCREENSAVER_MODULE ("RotZoomer", rotzoomer)