1 /* vfeedback, Copyright (c) 2018 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Simulates video feedback: pointing a video camera at an NTSC television.
13 * Created: 4-Aug-2018.
17 * - Figure out better UI gestures on mobile to pan, zoom and rotate.
19 * - When zoomed in really far, grab_rectangle should decompose pixels
20 * into RGB phosphor dots.
22 * - Maybe load an image and chroma-key it, letting transparency bleed,
23 * for that Amiga Genlock, Cabaret Voltaire look.
26 #include "screenhack.h"
35 # include "ximage-loader.h"
36 # include "images/gen/testcard_bbcf_png.h"
42 XWindowAttributes xgwa;
46 double start, last_time;
49 double value, svalue, speed, dx, dy, ds, dth;
51 struct { double x, y, w, h, th; } rect, orect;
52 struct { int x, y, s; } specular;
54 enum { POWERUP, IDLE, MOVE } state;
56 analogtv_reception rec;
70 twiddle_knobs (struct state *st)
72 st->rec.ofs = random() % ANALOGTV_SIGNAL_LEN; /* roll picture once */
73 st->rec.level = 0.8 + frand(1.0); /* signal strength (low = dark, static) */
74 st->tv->color_control = frand(1.0) * RANDSIGN();
75 st->tv->contrast_control = 0.4 + frand(1.0);
76 st->tv->tint_control = frand(360);
81 twiddle_camera (struct state *st)
90 st->rect.x = frand(0.1) * RANDSIGN();
91 st->rect.y = frand(0.1) * RANDSIGN();
92 st->rect.w = st->rect.h = 1 + frand(0.4) * RANDSIGN();
93 st->rect.th = 0.2 + frand(1.0) * RANDSIGN();
99 vfeedback_init (Display *dpy, Window window)
101 struct state *st = (struct state *) calloc (1, sizeof(*st));
106 st->tv = analogtv_allocate (st->dpy, st->window);
107 analogtv_set_defaults (st->tv, "");
108 st->tv->need_clear = 1;
109 st->rec.input = analogtv_input_allocate();
110 analogtv_setup_sync (st->rec.input, 1, 0);
111 st->tv->use_color = 1;
113 st->rec.multipath = 0;
116 st->noise = get_float_resource (st->dpy, "noise", "Float");
117 st->speed = get_float_resource (st->dpy, "speed", "Float");
119 XGetWindowAttributes (dpy, window, &st->xgwa);
126 gcv.foreground = get_pixel_resource (st->dpy, st->xgwa.colormap,
127 "foreground", "Foreground");
128 st->gc = XCreateGC (dpy, st->window, GCForeground, &gcv);
130 st->orect = st->rect;
136 p = image_data_to_pixmap (dpy, window,
137 testcard_bbcf_png, sizeof(testcard_bbcf_png),
139 st->tcimg = XGetImage (dpy, p, 0, 0, w, h, ~0L, ZPixmap);
140 XFreePixmap (dpy, p);
145 XSelectInput (dpy, window,
146 PointerMotionMask | st->xgwa.your_event_mask);
156 return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
161 ease_ratio (double r)
164 if (r <= 0) return 0;
165 else if (r >= 1) return 1;
166 else if (r <= ease) return ease * ease_fn (r / ease);
167 else if (r > 1-ease) return 1 - ease * ease_fn ((1 - r) / ease);
173 grab_rectangle (struct state *st)
177 /* Under XQuartz we can't just do XGetImage on the Window, we have to
178 go through an intermediate Pixmap first. I don't understand why.
181 st->pix = XCreatePixmap (st->dpy, st->window,
182 st->xgwa.width, st->xgwa.height, st->xgwa.depth);
184 XCopyArea (st->dpy, st->window, st->pix, st->gc, 0, 0,
185 st->xgwa.width, st->xgwa.height, 0, 0);
190 double r = (st->svalue < p ? st->svalue/p :
191 st->svalue >= 1-p ? (1-st->svalue)/p :
193 double s = st->specular.s * ease_ratio (r * 2);
194 XFillArc (st->dpy, st->pix, st->gc,
195 st->specular.x - s/2,
196 st->specular.y - s/2,
203 in = XGetImage (st->dpy, st->pix,
204 0, 0, st->xgwa.width, st->xgwa.height,
206 /* Could actually use st->tv->image here, except we don't have the
207 subrectangle being used (overall_top, usewidth, etc.) */
210 out = XCreateImage (st->dpy, st->xgwa.visual, st->xgwa.depth,
216 out->data = (char *) calloc (out->height, out->bytes_per_line);
217 if (! out->data) abort();
220 double C = cos (st->rect.th);
221 double S = sin (st->rect.th);
222 unsigned long black = BlackPixelOfScreen (st->xgwa.screen);
224 for (oy = 0; oy < out->height; oy++)
226 double doy = (double) oy / out->height;
227 double diy = st->rect.h * doy + st->rect.y - 0.5;
229 float dix_mul = (float) st->rect.w / out->width;
230 float dix_add = (-0.5 + st->rect.x) * st->rect.w;
231 float ix_add = (-diy * S + 0.5) * in->width;
232 float iy_add = ( diy * C + 0.5) * in->height;
233 float ix_mul = C * in->width;
234 float iy_mul = S * in->height;
236 ix_add += dix_add * ix_mul;
237 iy_add += dix_add * iy_mul;
241 if (in->bits_per_pixel == 32 &&
242 out->bits_per_pixel == 32)
244 /* Unwrapping XGetPixel and XPutPixel gains us several FPS here */
246 (uint32_t *) (out->data + out->bytes_per_line * oy);
247 for (ox = 0; ox < out->width; ox++)
250 int ix = dix * ix_mul + ix_add;
251 int iy = dix * iy_mul + iy_add;
252 unsigned long p = (ix >= 0 && ix < in->width &&
253 iy >= 0 && iy < in->height
255 (in->data + in->bytes_per_line * iy))[ix]
258 p |= black; /* We get 0 instead of BlackPixel... */
264 for (ox = 0; ox < out->width; ox++)
267 int ix = dix * ix_mul + ix_add;
268 int iy = dix * iy_mul + iy_add;
269 unsigned long p = (ix >= 0 && ix < in->width &&
270 iy >= 0 && iy < in->height
271 ? XGetPixel (in, ix, iy)
274 p |= black; /* We get 0 instead of BlackPixel... */
276 XPutPixel (out, ox, oy, p);
293 # ifdef GETTIMEOFDAY_TWO_ARGS
295 gettimeofday(&now, &tzp);
300 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
305 vfeedback_draw (Display *dpy, Window window, void *closure)
307 struct state *st = (struct state *) closure;
308 const analogtv_reception *rec = &st->rec;
309 double then = double_time(), now, timedelta;
313 case POWERUP: case IDLE: break;
315 st->rect.x = st->orect.x + st->dx * ease_ratio (st->value);
316 st->rect.y = st->orect.y + st->dy * ease_ratio (st->value);
317 st->rect.th = st->orect.th + st->dth * ease_ratio (st->value);
318 st->rect.w = st->orect.w * (1 + (st->ds * ease_ratio (st->value)));
319 st->rect.h = st->orect.h * (1 + (st->ds * ease_ratio (st->value)));
326 if (! st->button_down_p)
328 st->value += 0.03 * st->speed;
329 if (st->value > 1 || st->state == POWERUP)
331 st->orect = st->rect;
333 st->dx = st->dy = st->ds = st->dth = 0;
337 /* Wait until the monitor has warmed up before turning on
339 /* if (st->tv->powerup > 4.0) */
344 if (! (random() % 5))
345 st->ds = frand(0.2) * RANDSIGN(); /* zoom */
346 if (! (random() % 3))
347 st->dth = frand(0.2) * RANDSIGN(); /* rotate */
348 if (! (random() % 8))
349 st->dx = frand(0.05) * RANDSIGN(), /* pan */
350 st->dy = frand(0.05) * RANDSIGN();
351 if (! (random() % 2000))
354 if (! (random() % 10))
368 /* Draw a specular reflection somewhere on the screen, to mix it up
369 with a little noise from environmental light.
373 st->svalue += 0.01 * st->speed;
380 else if (! (random() % 300))
383 /* Center on the monitor's screen, depth 1 */
384 int cx = st->xgwa.width / 2;
385 int cy = st->xgwa.height / 2;
387 /* Center on the monitor's screen, depth 0 -- but this clips. */
388 int cx = (st->rect.x + st->rect.w / 2) * st->xgwa.width;
389 int cy = (st->rect.y + st->rect.h / 2) * st->xgwa.height;
391 int ww = 4 + (st->rect.h * st->xgwa.height) / 12;
392 st->specular.x = cx + (random() % ww) * RANDSIGN();
393 st->specular.y = cy + (random() % ww) * RANDSIGN();
394 st->specular.s = ww * (0.8 + frand(0.4));
399 if (st->last_time == 0)
402 if (st->state != POWERUP)
404 img = grab_rectangle (st);
405 analogtv_load_ximage (st->tv, st->rec.input, img, 0, 0, 0, 0, 0);
408 analogtv_reception_update (&st->rec);
409 analogtv_draw (st->tv, st->noise, &rec, 1);
414 timedelta = (1 / 29.97) - (now - then);
416 st->tv->powerup = then - st->start;
417 st->last_time = then;
419 return timedelta > 0 ? timedelta * 1000000 : 0;
424 vfeedback_reshape (Display *dpy, Window window, void *closure,
425 unsigned int w, unsigned int h)
427 struct state *st = (struct state *) closure;
428 analogtv_reconfigure (st->tv);
429 XGetWindowAttributes (dpy, window, &st->xgwa);
433 XFreePixmap (dpy, st->pix);
440 vfeedback_event (Display *dpy, Window window, void *closure, XEvent *event)
442 struct state *st = (struct state *) closure;
445 /* Pan with left button and no modifier keys.
446 Rotate with other buttons, or left-with-modifiers.
448 if (event->xany.type == ButtonPress &&
449 (event->xbutton.button == Button1 ||
450 event->xbutton.button == Button2 ||
451 event->xbutton.button == Button3))
453 st->button_down_p = True;
454 st->mouse_x = event->xbutton.x;
455 st->mouse_y = event->xbutton.y;
456 st->mouse_th = st->rect.th;
457 st->dragmode = (event->xbutton.button == Button1 &&
458 !event->xbutton.state);
461 else if (event->xany.type == ButtonRelease &&
462 (event->xbutton.button == Button1 ||
463 event->xbutton.button == Button2 ||
464 event->xbutton.button == Button3))
466 st->button_down_p = False;
469 else if (event->xany.type == MotionNotify && st->button_down_p)
473 double dx = st->mouse_x - event->xmotion.x;
474 double dy = st->mouse_y - event->xmotion.y;
475 st->rect.x += dx / st->xgwa.width * st->rect.w;
476 st->rect.y += dy / st->xgwa.height * st->rect.h;
477 st->mouse_x = event->xmotion.x;
478 st->mouse_y = event->xmotion.y;
482 /* Angle between center and initial click */
483 double a1 = -atan2 (st->mouse_y - st->xgwa.height / 2,
484 st->mouse_x - st->xgwa.width / 2);
485 /* Angle between center and drag position */
486 double a2 = -atan2 (event->xmotion.y - st->xgwa.height / 2,
487 event->xmotion.x - st->xgwa.width / 2);
488 /* Cumulatively rotate by difference between them */
489 st->rect.th = a2 - a1 + st->mouse_th;
494 /* Zoom with mouse wheel */
496 else if (event->xany.type == ButtonPress &&
497 (event->xbutton.button == Button4 ||
498 event->xbutton.button == Button6))
503 else if (event->xany.type == ButtonPress &&
504 (event->xbutton.button == Button5 ||
505 event->xbutton.button == Button7))
510 else if (event->type == KeyPress)
514 XLookupString (&event->xkey, &c, 1, &keysym, 0);
516 /* pan with arrow keys */
517 case XK_Up: st->rect.y += i; goto OK; break;
518 case XK_Down: st->rect.y -= i; goto OK; break;
519 case XK_Left: st->rect.x += i; goto OK; break;
520 case XK_Right: st->rect.x -= i; goto OK; break;
526 case '<': case ',': st->rect.th += i; goto OK; break;
527 case '>': case '.': st->rect.th -= i; goto OK; break;
536 st->orect = st->rect;
539 st->rect.x += (st->orect.w - st->rect.w) / 2;
540 st->rect.y += (st->orect.h - st->rect.h) / 2;
544 /* tv controls with T, C, B, O */
545 case 't': st->tv->tint_control += 5; goto OK; break;
546 case 'T': st->tv->tint_control -= 5; goto OK; break;
547 case 'c': st->tv->color_control += 0.1; goto OK; break;
548 case 'C': st->tv->color_control -= 0.1; goto OK; break;
549 case 'b': st->tv->brightness_control += 0.01; goto OK; break;
550 case 'B': st->tv->brightness_control -= 0.01; goto OK; break;
551 case 'o': st->tv->contrast_control += 0.1; goto OK; break;
552 case 'O': st->tv->contrast_control -= 0.1; goto OK; break;
553 case 'r': st->rec.level += 0.01; goto OK; break;
554 case 'R': st->rec.level -= 0.01; goto OK; break;
560 fprintf (stderr, " %.6f x %.6f @ %.6f, %.6f %.6f\t",
561 st->rect.w, st->rect.h,
562 st->rect.x, st->rect.y, st->rect.th);
563 fprintf (stderr," T=%.2f C=%.2f B=%.2f O=%.2f R=%.3f\n",
564 st->tv->tint_control,
565 st->tv->color_control/* * 100*/,
566 st->tv->brightness_control/* * 100*/,
567 st->tv->contrast_control/* * 100*/,
572 st->orect = st->rect;
577 if (screenhack_event_helper (dpy, window, event))
579 /* SPC or RET re-randomize the TV controls. */
589 vfeedback_free (Display *dpy, Window window, void *closure)
591 struct state *st = (struct state *) closure;
592 analogtv_release (st->tv);
593 free (st->rec.input);
595 XFreePixmap (dpy, st->pix);
596 XFreeGC (dpy, st->gc);
601 static const char *vfeedback_defaults [] = {
603 ".foreground: #CCCC44",
604 ".background: #000000",
608 "*TVBrightness: 1.5",
613 static XrmOptionDescRec vfeedback_options [] = {
614 { "-noise", ".noise", XrmoptionSepArg, 0 },
615 { "-speed", ".speed", XrmoptionSepArg, 0 },
620 XSCREENSAVER_MODULE ("VFeedback", vfeedback)