summaryrefslogtreecommitdiffstats
path: root/hacks/vfeedback.c
blob: 5b482a7b947a70c778571d9d74eaf2a3d1c986fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/* vfeedback, Copyright (c) 2018 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * Simulates video feedback: pointing a video camera at an NTSC television.
 *
 * Created: 4-Aug-2018.
 *
 * TODO:
 *
 * - Figure out better UI gestures on mobile to pan, zoom and rotate.
 *
 * - When zoomed in really far, grab_rectangle should decompose pixels
 *   into RGB phosphor dots.
 *
 * - Maybe load an image and chroma-key it, letting transparency bleed,
 *   for that Amiga Genlock, Cabaret Voltaire look.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include "screenhack.h"
#include "analogtv.h"

#include <time.h>

#undef DEBUG
#undef DARKEN

#ifdef DEBUG
# include "ximage-loader.h"
# include "images/gen/testcard_bbcf_png.h"
#endif

#undef countof
#define countof(x) (sizeof((x))/sizeof((*x)))

#define RANDSIGN() ((random() & 1) ? 1 : -1)

struct state {
  Display *dpy;
  Window window;
  XWindowAttributes xgwa;
  int w, h;
  Pixmap pix;
  GC gc;
  double start, last_time;
  double noise;
  double zoom, rot;
  double value, svalue, speed, dx, dy, ds, dth;

  struct { double x, y, w, h, th; } rect, orect;
  struct { int x, y, s; } specular;

  enum { POWERUP, IDLE, MOVE } state;
  analogtv *tv;
  analogtv_reception rec;
  Bool button_down_p;
  int mouse_x, mouse_y;
  double mouse_th;
  Bool dragmode;

# ifdef DEBUG
  XImage *tcimg;
# endif

};


static void
twiddle_knobs (struct state *st)
{
  st->rec.ofs = random() % ANALOGTV_SIGNAL_LEN;		/* roll picture once */
  st->rec.level = 0.8 + frand(1.0);  /* signal strength (low = dark, static) */
  st->tv->color_control    = frand(1.0) * RANDSIGN();
  st->tv->contrast_control = 0.4 + frand(1.0);
  st->tv->tint_control     = frand(360);
}


static void
twiddle_camera (struct state *st)
{
# if 0
  st->rect.x  = 0;
  st->rect.y  = 0;
  st->rect.w  = 1;
  st->rect.h  = 1;
  st->rect.th = 0;
# else
  st->rect.x = frand(0.1) * RANDSIGN();
  st->rect.y = frand(0.1) * RANDSIGN();
  st->rect.w = st->rect.h = 1 + frand(0.4) * RANDSIGN();
  st->rect.th = 0.2 + frand(1.0) * RANDSIGN();
# endif
}


static void *
vfeedback_init (Display *dpy, Window window)
{
  struct state *st = (struct state *) calloc (1, sizeof(*st));
  XGCValues gcv;

  st->dpy = dpy;
  st->window = window;
  st->tv = analogtv_allocate (st->dpy, st->window);
  analogtv_set_defaults (st->tv, "");
  st->tv->need_clear = 1;
  st->rec.input = analogtv_input_allocate();
  analogtv_setup_sync (st->rec.input, 1, 0);
  st->tv->use_color = 1;
  st->tv->powerup = 0;
  st->rec.multipath = 0;
  twiddle_camera (st);
  twiddle_knobs (st);
  st->noise = get_float_resource (st->dpy, "noise", "Float");
  st->speed = get_float_resource (st->dpy, "speed", "Float");

  XGetWindowAttributes (dpy, window, &st->xgwa);

  st->state = POWERUP;
  st->value = 0;

  st->w = 640;
  st->h = 480;
  gcv.foreground = get_pixel_resource (st->dpy, st->xgwa.colormap,
                                       "foreground", "Foreground");
  st->gc = XCreateGC (dpy, st->window, GCForeground, &gcv);

  st->orect = st->rect;

# ifdef DEBUG
  {
    int w, h;
    Pixmap p;
    p = image_data_to_pixmap (dpy, window,
                              testcard_bbcf_png, sizeof(testcard_bbcf_png),
                              &w, &h, 0);
    st->tcimg = XGetImage (dpy, p, 0, 0, w, h, ~0L, ZPixmap);
    XFreePixmap (dpy, p);
  }
# endif

# ifndef HAVE_JWXYZ
  XSelectInput (dpy, window,
                PointerMotionMask | st->xgwa.your_event_mask);
# endif

  return st;
}


static double
ease_fn (double r)
{
  return cos ((r/2 + 1) * M_PI) + 1; /* Smooth curve up, end at slope 1. */
}


static double
ease_ratio (double r)
{
  double ease = 0.5;
  if      (r <= 0)     return 0;
  else if (r >= 1)     return 1;
  else if (r <= ease)  return     ease * ease_fn (r / ease);
  else if (r > 1-ease) return 1 - ease * ease_fn ((1 - r) / ease);
  else                 return r;
}


static XImage *
grab_rectangle (struct state *st)
{
  XImage *in, *out;

  /* Under XQuartz we can't just do XGetImage on the Window, we have to
     go through an intermediate Pixmap first.  I don't understand why.
   */
  if (! st->pix)
    st->pix = XCreatePixmap (st->dpy, st->window, 
                             st->xgwa.width, st->xgwa.height, st->xgwa.depth);

  XCopyArea (st->dpy, st->window, st->pix, st->gc, 0, 0,
             st->xgwa.width, st->xgwa.height, 0, 0);

  if (st->specular.s)
    {
      double p = 0.2;
      double r = (st->svalue <    p ? st->svalue/p :
                  st->svalue >= 1-p ? (1-st->svalue)/p :
                  1);
      double s = st->specular.s * ease_ratio (r * 2);
      XFillArc (st->dpy, st->pix, st->gc,
                st->specular.x - s/2,
                st->specular.y - s/2,
                s, s, 0, 360*64);
    }

# ifdef DEBUG
  in = st->tcimg;
# else
  in = XGetImage (st->dpy, st->pix,
                  0, 0, st->xgwa.width, st->xgwa.height,
                  ~0L, ZPixmap);
  /* Could actually use st->tv->image here, except we don't have the
     subrectangle being used (overall_top, usewidth, etc.) */
# endif

  out = XCreateImage (st->dpy, st->xgwa.visual, st->xgwa.depth,
                      ZPixmap, 0, NULL,
                      st->w, st->h, 8, 0);

  if (! in) abort();
  if (! out) abort();
  out->data = (char *) calloc (out->height, out->bytes_per_line);
  if (! out->data) abort();

  {
    double C = cos (st->rect.th);
    double S = sin (st->rect.th);
    unsigned long black = BlackPixelOfScreen (st->xgwa.screen);
    int ox, oy;
    for (oy = 0; oy < out->height; oy++)
      {
        double doy = (double) oy / out->height;
        double diy = st->rect.h * doy + st->rect.y - 0.5;

        float dix_mul = (float) st->rect.w / out->width;
        float dix_add = (-0.5 + st->rect.x) * st->rect.w;
        float ix_add = (-diy * S + 0.5) * in->width;
        float iy_add = ( diy * C + 0.5) * in->height;
        float ix_mul = C * in->width;
        float iy_mul = S * in->height;

        ix_add += dix_add * ix_mul;
        iy_add += dix_add * iy_mul;
        ix_mul *= dix_mul;
        iy_mul *= dix_mul;

        if (in->bits_per_pixel == 32 &&
            out->bits_per_pixel == 32)
          {
            /* Unwrapping XGetPixel and XPutPixel gains us several FPS here */
            uint32_t *out_line =
              (uint32_t *) (out->data + out->bytes_per_line * oy);
            for (ox = 0; ox < out->width; ox++)
              {
                float dix = ox;
                int ix = dix * ix_mul + ix_add;
                int iy = dix * iy_mul + iy_add;
                unsigned long p = (ix >= 0 && ix < in->width &&
                                   iy >= 0 && iy < in->height
                                   ? ((uint32_t *)
                                      (in->data + in->bytes_per_line * iy))[ix]
                                   : black);
# ifdef HAVE_JWXYZ
                p |= black;   /* We get 0 instead of BlackPixel... */
# endif
                out_line[ox] = p;
              }
          }
        else
          for (ox = 0; ox < out->width; ox++)
            {
              float dix = ox;
              int ix = dix * ix_mul + ix_add;
              int iy = dix * iy_mul + iy_add;
              unsigned long p = (ix >= 0 && ix < in->width &&
                                 iy >= 0 && iy < in->height
                                 ? XGetPixel (in, ix, iy)
                                 : black);
# ifdef HAVE_JWXYZ
              p |= black;   /* We get 0 instead of BlackPixel... */
# endif
              XPutPixel (out, ox, oy, p);
            }
      }
  }

# ifndef DEBUG
  XDestroyImage (in);
# endif

  return out;
}


static double
double_time (void)
{
  struct timeval now;
# ifdef GETTIMEOFDAY_TWO_ARGS
  struct timezone tzp;
  gettimeofday(&now, &tzp);
# else
  gettimeofday(&now);
# endif

  return (now.tv_sec + ((double) now.tv_usec * 0.000001));
}


static unsigned long
vfeedback_draw (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  const analogtv_reception *rec = &st->rec;
  double then = double_time(), now, timedelta;
  XImage *img = 0;

  switch (st->state) {
  case POWERUP: case IDLE: break;
  case MOVE:
    st->rect.x  = st->orect.x  + st->dx  * ease_ratio (st->value);
    st->rect.y  = st->orect.y  + st->dy  * ease_ratio (st->value);
    st->rect.th = st->orect.th + st->dth * ease_ratio (st->value);
    st->rect.w  = st->orect.w * (1 + (st->ds * ease_ratio (st->value)));
    st->rect.h  = st->orect.h * (1 + (st->ds * ease_ratio (st->value)));
    break;
  default:
    abort();
    break;
  }

  if (! st->button_down_p)
    {
      st->value  += 0.03 * st->speed;
      if (st->value > 1 || st->state == POWERUP)
        {
          st->orect = st->rect;
          st->value = 0;
          st->dx = st->dy = st->ds = st->dth = 0;

          switch (st->state) {
          case POWERUP:
            /* Wait until the monitor has warmed up before turning on
               the camcorder? */
            /* if (st->tv->powerup > 4.0) */
              st->state = IDLE;
            break;
          case IDLE:
            st->state = MOVE;
            if (! (random() % 5))
              st->ds = frand(0.2) * RANDSIGN();		/* zoom */
            if (! (random() % 3))
              st->dth = frand(0.2) * RANDSIGN();	/* rotate */
            if (! (random() % 8))
              st->dx = frand(0.05) * RANDSIGN(),	/* pan */
              st->dy = frand(0.05) * RANDSIGN();
            if (! (random() % 2000))
              {
                twiddle_knobs (st);
                if (! (random() % 10))
                  twiddle_camera (st);
              }
            break;
          case MOVE:
            st->state = IDLE;
            st->value = 0.3;
            break;
          default:
            abort();
            break;
          }
        }

      /* Draw a specular reflection somewhere on the screen, to mix it up
         with a little noise from environmental light.
       */
      if (st->specular.s)
        {
          st->svalue += 0.01 * st->speed;
          if (st->svalue > 1)
            {
              st->svalue = 0;
              st->specular.s = 0;
            }
        }
      else if (! (random() % 300))
        {
# if 1
          /* Center on the monitor's screen, depth 1 */
          int cx = st->xgwa.width / 2;
          int cy = st->xgwa.height / 2;
# else
          /* Center on the monitor's screen, depth 0 -- but this clips. */
          int cx = (st->rect.x + st->rect.w / 2) * st->xgwa.width;
          int cy = (st->rect.y + st->rect.h / 2) * st->xgwa.height;
# endif
          int ww = 4 + (st->rect.h * st->xgwa.height) / 12;
          st->specular.x = cx + (random() % ww) * RANDSIGN();
          st->specular.y = cy + (random() % ww) * RANDSIGN();
          st->specular.s = ww * (0.8 + frand(0.4));
          st->svalue = 0;
        }
    }

  if (st->last_time == 0)
    st->start = then;

  if (st->state != POWERUP)
    {
      img = grab_rectangle (st);
      analogtv_load_ximage (st->tv, st->rec.input, img, 0, 0, 0, 0, 0);
    }

  analogtv_reception_update (&st->rec);
  analogtv_draw (st->tv, st->noise, &rec, 1);
  if (img)
    XDestroyImage (img);

  now = double_time();
  timedelta = (1 / 29.97) - (now - then);

  st->tv->powerup = then - st->start;
  st->last_time = then;

  return timedelta > 0 ? timedelta * 1000000 : 0;
}


static void
vfeedback_reshape (Display *dpy, Window window, void *closure, 
                    unsigned int w, unsigned int h)
{
  struct state *st = (struct state *) closure;
  analogtv_reconfigure (st->tv);
  XGetWindowAttributes (dpy, window, &st->xgwa);

  if (st->pix)
    {
      XFreePixmap (dpy, st->pix);
      st->pix = 0;
    }
}


static Bool
vfeedback_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  struct state *st = (struct state *) closure;
  double i = 0.02;

  /* Pan with left button and no modifier keys.
     Rotate with other buttons, or left-with-modifiers.
   */
  if (event->xany.type == ButtonPress &&
      (event->xbutton.button == Button1 ||
       event->xbutton.button == Button2 ||
       event->xbutton.button == Button3))
    {
      st->button_down_p = True;
      st->mouse_x = event->xbutton.x;
      st->mouse_y = event->xbutton.y;
      st->mouse_th = st->rect.th;
      st->dragmode = (event->xbutton.button == Button1 && 
                      !event->xbutton.state);
      return True;
    }
  else if (event->xany.type == ButtonRelease &&
           (event->xbutton.button == Button1 ||
            event->xbutton.button == Button2 ||
            event->xbutton.button == Button3))
    {
      st->button_down_p = False;
      return True;
    }
  else if (event->xany.type == MotionNotify && st->button_down_p)
    {
      if (st->dragmode)
        {
          double dx = st->mouse_x - event->xmotion.x;
          double dy = st->mouse_y - event->xmotion.y;
          st->rect.x += dx / st->xgwa.width  * st->rect.w;
          st->rect.y += dy / st->xgwa.height * st->rect.h;
          st->mouse_x = event->xmotion.x;
          st->mouse_y = event->xmotion.y;
        }
      else
        {
          /* Angle between center and initial click */
          double a1 = -atan2 (st->mouse_y - st->xgwa.height / 2,
                              st->mouse_x - st->xgwa.width  / 2);
          /* Angle between center and drag position */
          double a2 = -atan2 (event->xmotion.y - st->xgwa.height / 2,
                              event->xmotion.x - st->xgwa.width  / 2);
          /* Cumulatively rotate by difference between them */
          st->rect.th = a2 - a1 + st->mouse_th;
        }
      goto OK;
    }

  /* Zoom with mouse wheel */

  else if (event->xany.type == ButtonPress &&
           (event->xbutton.button == Button4 ||
            event->xbutton.button == Button6))
    {
      i = 1-i;
      goto ZZ;
    }
  else if (event->xany.type == ButtonPress &&
           (event->xbutton.button == Button5 ||
            event->xbutton.button == Button7))
    {
      i = 1+i;
      goto ZZ;
    }
  else if (event->type == KeyPress)
    {
      KeySym keysym;
      char c = 0;
      XLookupString (&event->xkey, &c, 1, &keysym, 0);
      switch (keysym) {
        /* pan with arrow keys */
      case XK_Up:    st->rect.y += i; goto OK; break;
      case XK_Down:  st->rect.y -= i; goto OK; break;
      case XK_Left:  st->rect.x += i; goto OK; break;
      case XK_Right: st->rect.x -= i; goto OK; break;
      default: break;
      }
      switch (c) {

        /* rotate with <> */
      case '<': case ',': st->rect.th += i; goto OK; break;
      case '>': case '.': st->rect.th -= i; goto OK; break;

        /* zoom with += */
      case '-': case '_':
        i = 1+i;
        goto ZZ;
      case '=': case '+':
        i = 1-i;
      ZZ:
        st->orect = st->rect;
        st->rect.w *= i;
        st->rect.h *= i;
        st->rect.x += (st->orect.w - st->rect.w) / 2;
        st->rect.y += (st->orect.h - st->rect.h) / 2;
        goto OK;
        break;

        /* tv controls with T, C, B, O */
      case 't': st->tv->tint_control       += 5;    goto OK; break;
      case 'T': st->tv->tint_control       -= 5;    goto OK; break;
      case 'c': st->tv->color_control      += 0.1;  goto OK; break;
      case 'C': st->tv->color_control      -= 0.1;  goto OK; break;
      case 'b': st->tv->brightness_control += 0.01; goto OK; break;
      case 'B': st->tv->brightness_control -= 0.01; goto OK; break;
      case 'o': st->tv->contrast_control   += 0.1;  goto OK; break;
      case 'O': st->tv->contrast_control   -= 0.1;  goto OK; break;
      case 'r': st->rec.level              += 0.01; goto OK; break;
      case 'R': st->rec.level              -= 0.01; goto OK; break;
      default: break;
      }
      goto NOPE;
    OK:
# if 0
      fprintf (stderr, " %.6f x %.6f @ %.6f, %.6f %.6f\t",
               st->rect.w, st->rect.h,
               st->rect.x, st->rect.y, st->rect.th);
      fprintf (stderr," T=%.2f C=%.2f B=%.2f O=%.2f R=%.3f\n",
               st->tv->tint_control,
               st->tv->color_control/* * 100*/,
               st->tv->brightness_control/* * 100*/,
               st->tv->contrast_control/* * 100*/,
               st->rec.level);
# endif
      st->value = 0;
      st->state = IDLE;
      st->orect = st->rect;
      return True;
    }

 NOPE:
  if (screenhack_event_helper (dpy, window, event))
    {
      /* SPC or RET re-randomize the TV controls. */
      twiddle_knobs (st);
      goto OK;
    }

  return False;
}


static void
vfeedback_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  analogtv_release (st->tv);
  if (st->pix)
    XFreePixmap (dpy, st->pix);
  XFreeGC (dpy, st->gc);
  free (st);
}


static const char *vfeedback_defaults [] = {

  ".foreground:  #CCCC44",
  ".background:  #000000",
  "*noise:       0.02",
  "*speed:       1.0",
  ANALOGTV_DEFAULTS
  "*TVBrightness: 1.5",
  "*TVContrast:   150",
  0
};

static XrmOptionDescRec vfeedback_options [] = {
  { "-noise",           ".noise",     XrmoptionSepArg, 0 },
  { "-speed",           ".speed",     XrmoptionSepArg, 0 },
  ANALOGTV_OPTIONS
  { 0, 0, 0, 0 }
};

XSCREENSAVER_MODULE ("VFeedback", vfeedback)