summaryrefslogtreecommitdiffstats
path: root/hacks/filmleader.c
blob: ef021e1a7ecbec84f6063b19163e1a846eaee5e2 (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
/* filmleader, 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.
 *
 * Simulate an SMPTE Universal Film Leader playing on an analog television.
 */

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

#include "xft.h" /* before screenhack.h */

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

#include <time.h>

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

struct state {
  Display *dpy;
  Window window;
  XWindowAttributes xgwa;
  int w, h;
  unsigned long bg, text_color, ring_color, trace_color;
  XftColor xft_text_color_1, xft_text_color_2;

  XftFont *font, *font2, *font3;
  XftDraw *xftdraw;
  Pixmap pix;
  GC gc;
  double start, last_time;
  double value;
  int stop;
  double noise;
  analogtv *tv;
  analogtv_input *inp;
  analogtv_reception rec;
  Bool button_down_p;
};


static void *
filmleader_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->inp = analogtv_input_allocate();
  analogtv_setup_sync (st->inp, 1, 0);
  st->rec.input = st->inp;
  st->rec.level = 2.0;
  st->tv->use_color = 1;
  st->rec.level = pow(frand(1.0), 3.0) * 2.0 + 0.05;
  st->rec.ofs = random() % ANALOGTV_SIGNAL_LEN;
  st->tv->powerup = 0;

  st->rec.multipath = 0.0;
  st->tv->color_control += frand(0.3);
  st->noise = get_float_resource (st->dpy, "noise", "Float");
  st->value = 18;  /* Leave time for powerup */
  st->stop = 2 + (random() % 5);
  XGetWindowAttributes (dpy, window, &st->xgwa);

  /* Let's render it into a 16:9 pixmap, since that's what most screens are
     these days.  That means the circle will be squashed on 4:3 screens. */
  {
    double r = 16/9.0;

# ifdef HAVE_MOBILE
    /* analogtv.c always fills whole screen on mobile, so use screen aspect. */
    r = st->xgwa.width / (double) st->xgwa.height;
    if (r < 1) r = 1/r;
# endif

    st->w = 712;
    st->h = st->w / r;
  }

  if (st->xgwa.width < st->xgwa.height)
    {
      int swap = st->w;
      st->w = st->h;
      st->h = swap;
    }

  st->pix = XCreatePixmap (dpy, window,
                           st->w > st->h ? st->w : st->h,
                           st->w > st->h ? st->w : st->h,
                           st->xgwa.depth);
  st->gc = XCreateGC (dpy, st->pix, 0, &gcv);

  st->xftdraw = XftDrawCreate (dpy, st->pix, st->xgwa.visual,
                               st->xgwa.colormap);
  st->font = load_xft_font_retry (dpy, screen_number (st->xgwa.screen),
                                  get_string_resource (dpy, "numberFont",
                                                       "Font"));
  st->font2 = load_xft_font_retry (dpy, screen_number (st->xgwa.screen),
                                   get_string_resource (dpy, "numberFont2",
                                                        "Font"));
  st->font3 = load_xft_font_retry (dpy, screen_number (st->xgwa.screen),
                                   get_string_resource (dpy, "numberFont3",
                                                        "Font"));

  st->bg = get_pixel_resource (dpy, st->xgwa.colormap,
                               "textBackground", "Background");
  st->text_color = get_pixel_resource (dpy, st->xgwa.colormap,
                                       "textColor", "Foreground");
  st->ring_color = get_pixel_resource (dpy, st->xgwa.colormap,
                                       "ringColor", "Foreground");
  st->trace_color = get_pixel_resource (dpy, st->xgwa.colormap,
                                        "traceColor", "Foreground");

  XftColorAllocName (dpy, st->xgwa.visual, st->xgwa.colormap,
                     get_string_resource (dpy, "textColor", "Foreground"),
                     &st->xft_text_color_1);
  XftColorAllocName (dpy, st->xgwa.visual, st->xgwa.colormap,
                     get_string_resource (dpy, "textBackground", "Background"),
                     &st->xft_text_color_2);

  return st;
}


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
filmleader_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;
  int i, x, y, w2, h2;
  XGlyphInfo extents;
  int lbearing, rbearing, ascent, descent;
  char s[20];
  double r = 1 - (st->value - (int) st->value);
  int ivalue = st->value;
  XftFont *xftfont;
  XftColor *xftcolor;

  /* You may ask, why use Xft for this instead of the much simpler XDrawString?
     Well, for some reason, XLoadQueryFont is giving me horribly-scaled bitmap
     fonts, but Xft works properly. So perhaps in This Modern World, if one
     expects large fonts to work, one must use Xft instead of Xlib?

     Everything is terrible.
   */

  const struct { double t; int k, f; const char * const s[4]; } blurbs[] = {
    {  9.1, 3, 1, { "PICTURE", "  START", 0, 0 }},
    { 10.0, 2, 1, { "    16", "SOUND", "START", 0 }},
    { 10.5, 2, 1, { "    32", "SOUND", "START", 0 }},
    { 11.6, 2, 0, { "PICTURE", "COMPANY", "SERIES", 0 }},
    { 11.7, 2, 0, { "XSCRNSAVER", 0, 0, 0 }},
    { 11.9, 2, 0, { "REEL No.", "PROD No.", "PLAY DATE", 0 }},
    { 12.2, 0, 0, { "    SMPTE     ", "UNIVERSAL", "   LEADER", 0 }},
    { 12.3, 0, 1, { "X           ", "X", "X", "X" }},
    { 12.4, 0, 0, { "    SMPTE     ", "UNIVERSAL", "   LEADER", 0 }},
    { 12.5, 3, 1, { "PICTURE", 0, 0, 0 }},
    { 12.7, 3, 1, { "HEAD", 0, 0, 0 }},
    { 12.8, 2, 1, { "OOOO", 0, "ASPECT", "TYPE OF" }},
    { 12.9, 2, 0, { "SOUND", 0, "RATIO", 0 }},
    { 13.2, 1, 1, { "                  ", "PICTURE", 0, 0 }},
    { 13.3, 1, 0, { "REEL No.      ", "COLOR", 0, 0 }},
    { 13.4, 1, 0, { "LENGTH        ", 0, 0, "ROLL" }},
    { 13.5, 1, 0, { "SUBJECT", 0, 0, 0 }},
    { 13.9, 1, 1, { "     \342\206\221", "SPLICE", " HERE", 0 }},
  };

  for (i = 0; i < countof(blurbs); i++)
    {
      if (st->value >= blurbs[i].t && st->value <= blurbs[i].t + 1/15.0)
        {
          int line_height;
          int j;
          xftfont = (blurbs[i].f == 1 ? st->font2 :
                     blurbs[i].f == 2 ? st->font : st->font3);

          XSetForeground (dpy, st->gc, 
                          blurbs[i].k == 3 ? st->bg : st->text_color);
          XFillRectangle (dpy, st->pix, st->gc, 0, 0, st->w, st->h);
          XSetForeground (dpy, st->gc,
                          blurbs[i].k == 3 ? st->text_color : st->bg);
          xftcolor = (blurbs[i].k == 3 ? 
                      &st->xft_text_color_1 : &st->xft_text_color_2);

          /* The height of a string of spaces is 0... */
          XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *) "My", 2, &extents);
          line_height = extents.height;

          XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *)
                              blurbs[i].s[0], strlen(blurbs[i].s[0]),
                              &extents);
          lbearing = -extents.x;
          rbearing = extents.width - extents.x;
          ascent   = extents.y;
          descent  = extents.height - extents.y;

          x = (st->w - rbearing) / 2;
          y = st->h * 0.1 + ascent;

          for (j = 0; j < countof(blurbs[i].s); j++)
            {
              if (blurbs[i].s[j])
                XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y,
                                   (FcChar8 *) blurbs[i].s[j],
                                   strlen(blurbs[i].s[j]));

              y += line_height * 1.5;

              if (blurbs[i].s[j])
                {
                  XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *)
                                      blurbs[i].s[0], strlen(blurbs[i].s[j]),
                                      &extents);
                  lbearing = -extents.x;
                  rbearing = extents.width - extents.x;
                  ascent   = extents.y;
                  descent  = extents.height - extents.y;
                }
            }

          if (blurbs[i].k == 2)  /* Rotate clockwise and flip */
            {
              int wh = st->w < st->h ? st->w : st->h;
              XImage *img1 = XGetImage (dpy, st->pix,
                                        (st->w - wh) / 2,
                                        (st->h - wh) / 2,
                                        wh, wh, ~0L, ZPixmap);
              XImage *img2 = XCreateImage (dpy, st->xgwa.visual,
                                           st->xgwa.depth, ZPixmap, 0, 0,
                                           wh, wh, 32, 0);
              img2->data = malloc (img2->bytes_per_line * img2->height);
              for (y = 0; y < wh; y++)
                for (x = 0; x < wh; x++)
                  XPutPixel (img2, y, x, XGetPixel (img1, x, y));
              XSetForeground (dpy, st->gc, 
                              blurbs[i].k == 3 ? st->bg : st->text_color);
              XFillRectangle (dpy, st->pix, st->gc, 0, 0, st->w, st->h);
              XPutImage (dpy, st->pix, st->gc, img2,
                         0, 0,
                         (st->w - wh) / 2,
                         (st->h - wh) / 2,
                         wh, wh);
              XDestroyImage (img1);
              XDestroyImage (img2);
            }
          else if (blurbs[i].k == 1)  /* Flip vertically */
            {
              XImage *img1 = XGetImage (dpy, st->pix, 0, 0,
                                        st->w, st->h, ~0L, ZPixmap);
              XImage *img2 = XCreateImage (dpy, st->xgwa.visual,
                                           st->xgwa.depth, ZPixmap, 0, 0,
                                           st->w, st->h, 32, 0);
              img2->data = malloc (img2->bytes_per_line * img2->height);
              for (y = 0; y < img2->height; y++)
                for (x = 0; x < img2->width; x++)
                  XPutPixel (img2, x, img2->height-y-1,
                             XGetPixel (img1, x, y));
              XPutImage (dpy, st->pix, st->gc, img2, 0, 0, 0, 0, st->w, st->h);
              XDestroyImage (img1);
              XDestroyImage (img2);
            }

          goto DONE;
        }
    }

  if (st->value < 2.0 || st->value >= 9.0)	/* Black screen */
    {
      XSetForeground (dpy, st->gc, st->text_color);
      XFillRectangle (dpy, st->pix, st->gc, 0, 0, st->w, st->h);
      goto DONE;
    }

  XSetForeground (dpy, st->gc, st->bg);
  XFillRectangle (dpy, st->pix, st->gc, 0, 0, st->w, st->h);

  if (r > 1/30.0)				/* Sweep line and background */
    {
      x = st->w/2 + st->w * cos (M_PI * 2 * r - M_PI/2);
      y = st->h/2 + st->h * sin (M_PI * 2 * r - M_PI/2);

      XSetForeground (dpy, st->gc, st->trace_color);
      XFillArc (dpy, st->pix, st->gc,
                -st->w, -st->h, st->w*3, st->h*3,
                90*64,
                90*64 - ((r + 0.25) * 360*64));

      XSetForeground (dpy, st->gc, st->text_color);
      XSetLineAttributes (dpy, st->gc, 1, LineSolid, CapRound, JoinRound);
      XDrawLine (dpy, st->pix, st->gc, st->w/2, st->h/2, x, y);
  
      XSetForeground (dpy, st->gc, st->text_color);
      XSetLineAttributes (dpy, st->gc, 2, LineSolid, CapRound, JoinRound);
      XDrawLine (dpy, st->pix, st->gc, st->w/2, 0, st->w/2, st->h);
      XDrawLine (dpy, st->pix, st->gc, 0, st->h/2, st->w, st->h/2);
    }

  /* Big number */

  s[0] = (char) (ivalue + '0');
  xftfont = st->font;
  xftcolor = &st->xft_text_color_1;
  XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *) s, 1, &extents);
  lbearing = -extents.x;
  rbearing = extents.width - extents.x;
  ascent   = extents.y;
  descent  = extents.height - extents.y;

  x = (st->w - (rbearing + lbearing)) / 2;
  y = (st->h + (ascent - descent)) / 2;
  XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y, (FcChar8 *) s, 1);

  /* Annotations on 7 and 4 */

  if ((st->value >= 7.75 && st->value <= 7.85) ||
      (st->value >= 4.00 && st->value <= 4.25))
    {
      XSetForeground (dpy, st->gc, st->ring_color);
      xftcolor = &st->xft_text_color_2;
      xftfont = st->font2;

      s[0] = (ivalue == 4 ? 'C' : 'M');
      s[1] = 0;

      XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *) s, strlen(s), &extents);
      lbearing = -extents.x;
      rbearing = extents.width - extents.x;
      ascent   = extents.y;
      descent  = extents.height - extents.y;

      x = st->w * 0.1;
      y = st->h * 0.1 + ascent;
      XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y,
                         (FcChar8 *) s, strlen(s));
      x = st->w * 0.9 - (rbearing + lbearing);
      XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y,
                         (FcChar8 *) s, strlen(s));

      s[0] = (ivalue == 4 ? 'F' : '3');
      s[1] = (ivalue == 4 ? 0   : '5');
      s[2] = 0;

      XftTextExtentsUtf8 (dpy, xftfont, (FcChar8 *) s, strlen(s), &extents);
      lbearing = -extents.x;
      rbearing = extents.width - extents.x;
      ascent   = extents.y;
      descent  = extents.height - extents.y;

      x = st->w * 0.1;
      y = st->h * 0.95;
      XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y, 
                         (FcChar8 *) s, strlen(s));
      x = st->w * 0.9 - (rbearing + lbearing);
      XftDrawStringUtf8 (st->xftdraw, xftcolor, xftfont, x, y,
                         (FcChar8 *) s, strlen(s));
    }

  if (r > 1/30.0)				/* Two rings around number */
    {
      double r2 = st->w / (double) st->h;
      double ss = 1;

      if (st->xgwa.width < st->xgwa.height)
        ss = 0.5;

      XSetForeground (dpy, st->gc, st->ring_color);
      XSetLineAttributes (dpy, st->gc, st->w * 0.025,
                          LineSolid, CapRound, JoinRound);

      w2 = st->w * 0.8 * ss / r2;
      h2 = st->h * 0.8 * ss;
      x = (st->w - w2) / 2;
      y = (st->h - h2) / 2;
      XDrawArc (dpy, st->pix, st->gc, x, y, w2, h2, 0, 360*64);

      w2 = w2 * 0.8;
      h2 = h2 * 0.8;
      x = (st->w - w2) / 2;
      y = (st->h - h2) / 2;
      XDrawArc (dpy, st->pix, st->gc, x, y, w2, h2, 0, 360*64);
    }

 DONE:

  img = XGetImage (dpy, st->pix, 0, 0, st->w, st->h, ~0L, ZPixmap);

  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);

  XDestroyImage (img);

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

  if (! st->button_down_p)
    {
      if (st->last_time == 0)
        st->start = then;
      else
        st->value -= then - st->last_time;

      if (st->value <= 0 ||
          (r > 0.9 && st->value <= st->stop))
        {
          st->value = (random() % 20) ? 8.9 : 15;
          st->stop = ((random() % 50) ? 2 : 1) + (random() % 5);

          if (st->value > 9)	/* Spin the knobs again */
            {
              st->rec.level = pow(frand(1.0), 3.0) * 2.0 + 0.05;
              st->rec.ofs = random() % ANALOGTV_SIGNAL_LEN;
              st->tv->color_control += frand(0.3) - 0.15;
            }
        }
    }

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

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


static void
filmleader_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->w > st->h) != (st->xgwa.width > st->xgwa.height))
    {
      int swap = st->w;
      st->w = st->h;
      st->h = swap;
    }
}


static Bool
filmleader_event (Display *dpy, Window window, void *closure, XEvent *event)
{
  struct state *st = (struct state *) closure;
  if (event->xany.type == ButtonPress)
    {
      st->button_down_p = True;
      return True;
    }
  else if (event->xany.type == ButtonRelease)
    {
      st->button_down_p = False;
      return True;
    }
  else if (screenhack_event_helper (dpy, window, event))
    {
      st->value = 15;
      st->rec.level = pow(frand(1.0), 3.0) * 2.0 + 0.05;
      st->rec.ofs = random() % ANALOGTV_SIGNAL_LEN;
      st->tv->color_control += frand(0.3) - 0.15;
      return True;
    }
  else if (event->xany.type == KeyPress)
    {
      KeySym keysym;
      char c = 0;
      XLookupString (&event->xkey, &c, 1, &keysym, 0);
      if (c >= '2' && c <= '8')
        {
          st->value = (c - '0') + (st->value - (int) st->value);
          return True;
        }
    }

  return False;
}


static void
filmleader_free (Display *dpy, Window window, void *closure)
{
  struct state *st = (struct state *) closure;
  analogtv_release (st->tv);
  XftDrawDestroy (st->xftdraw);
  XftColorFree(dpy, st->xgwa.visual, st->xgwa.colormap, &st->xft_text_color_1);
  XftColorFree(dpy, st->xgwa.visual, st->xgwa.colormap, &st->xft_text_color_2);
  XFreePixmap (dpy, st->pix);
  XFreeGC (dpy, st->gc);
  free (st);
}


static const char *filmleader_defaults [] = {

  ".background:  #000000",

# ifdef HAVE_MOBILE

  "*textBackground: #444488",  /* Need much higher contrast for some reason */
  "*textColor:      #000033",
  "*ringColor:      #DDDDFF",
  "*traceColor:     #222244",

# else /* X11 or Cocoa */

  "*textBackground: #9999DD",
  "*textColor:      #000015",
  "*ringColor:      #DDDDFF",
  "*traceColor:     #555577",

# endif

# ifdef USE_IPHONE

  "*numberFont:  Helvetica Bold 120",
  "*numberFont2: Helvetica 36",
  "*numberFont3: Helvetica 28",

# else /* X11, Cocoa or Android */

  "*numberFont:  -*-helvetica-bold-r-*-*-*-1700-*-*-*-*-*-*",
  "*numberFont2: -*-helvetica-medium-r-*-*-*-500-*-*-*-*-*-*",
  "*numberFont3: -*-helvetica-medium-r-*-*-*-360-*-*-*-*-*-*",

# endif


  "*noise:       0.04",
  ANALOGTV_DEFAULTS
  "*geometry: 1280x720",
  0
};

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

XSCREENSAVER_MODULE ("FilmLeader", filmleader)