1 /* xanalogtv, Copyright (c) 2003 Trevor Blackwell <tlb@tlb.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
12 * Simulate test patterns on an analog TV. Concept similar to xteevee
13 * in this distribution, but a totally different implementation based
14 * on the simulation of an analog TV set in utils/analogtv.c. Much
15 * more realistic, but needs more video card bandwidth.
17 * It flips around through simulated channels 2 through 13. Some show
18 * pictures from your images directory, some show color bars, and some
19 * just have static. Some channels receive two stations simultaneously
20 * so you see a ghostly, misaligned image.
22 * It's easy to add some test patterns by compiling in an XPM, but I
23 * can't find any that are clearly freely redistributable.
28 #include "screenhack.h"
29 #include "xpm-pixmap.h"
35 #include <X11/Xutil.h>
36 #include <X11/Intrinsic.h>
38 #include "images/logo-50.xpm"
41 /* #define USE_TEST_PATTERNS */
43 #define countof(x) (sizeof((x))/sizeof((*x)))
45 static analogtv *tv=NULL;
47 analogtv_font ugly_font;
50 update_smpte_colorbars(analogtv_input *input)
57 SMPTE is the society of motion picture and television engineers, and
58 these are the standard color bars in the US. Following the partial spec
59 at http://broadcastengineering.com/ar/broadcasting_inside_color_bars/
60 These are luma, chroma, and phase numbers for each of the 7 bars.
62 double top_cb_table[7][3]={
63 {75, 0, 0.0}, /* gray */
64 {69, 31, 167.0}, /* yellow */
65 {56, 44, 283.5}, /* cyan */
66 {48, 41, 240.5}, /* green */
67 {36, 41, 60.5}, /* magenta */
68 {28, 44, 103.5}, /* red */
69 {15, 31, 347.0} /* blue */
71 double mid_cb_table[7][3]={
72 {15, 31, 347.0}, /* blue */
73 {7, 0, 0}, /* black */
74 {36, 41, 60.5}, /* magenta */
75 {7, 0, 0}, /* black */
76 {56, 44, 283.5}, /* cyan */
77 {7, 0, 0}, /* black */
78 {75, 0, 0.0} /* gray */
81 analogtv_lcp_to_ntsc(0.0, 0.0, 0.0, black_ntsc);
83 analogtv_setup_sync(input, 1, 0);
84 analogtv_setup_teletext(input);
86 for (col=0; col<7; col++) {
87 analogtv_draw_solid_rel_lcp(input, col*(1.0/7.0), (col+1)*(1.0/7.0), 0.00, 0.68,
89 top_cb_table[col][1], top_cb_table[col][2]);
91 analogtv_draw_solid_rel_lcp(input, col*(1.0/7.0), (col+1)*(1.0/7.0), 0.68, 0.75,
93 mid_cb_table[col][1], mid_cb_table[col][2]);
96 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0/6.0,
97 0.75, 1.00, 7, 40, 303); /* -I */
98 analogtv_draw_solid_rel_lcp(input, 1.0/6.0, 2.0/6.0,
99 0.75, 1.00, 100, 0, 0); /* white */
100 analogtv_draw_solid_rel_lcp(input, 2.0/6.0, 3.0/6.0,
101 0.75, 1.00, 7, 40, 33); /* +Q */
102 analogtv_draw_solid_rel_lcp(input, 3.0/6.0, 4.0/6.0,
103 0.75, 1.00, 7, 0, 0); /* black */
104 analogtv_draw_solid_rel_lcp(input, 12.0/18.0, 13.0/18.0,
105 0.75, 1.00, 3, 0, 0); /* black -4 */
106 analogtv_draw_solid_rel_lcp(input, 13.0/18.0, 14.0/18.0,
107 0.75, 1.00, 7, 0, 0); /* black */
108 analogtv_draw_solid_rel_lcp(input, 14.0/18.0, 15.0/18.0,
109 0.75, 1.00, 11, 0, 0); /* black +4 */
110 analogtv_draw_solid_rel_lcp(input, 5.0/6.0, 6.0/6.0,
111 0.75, 1.00, 7, 0, 0); /* black */
115 xpos=ANALOGTV_VIS_START + ANALOGTV_VIS_LEN/2;
119 if (gethostname (localname, sizeof (localname))==0) {
120 localname[sizeof(localname)-1]=0; /* "The returned name is null-
121 terminated unless insufficient
122 space is provided" */
123 localname[24]=0; /* limit length */
125 analogtv_draw_string_centered(input, &ugly_font, localname,
126 xpos, ypos, black_ntsc);
129 ypos += ugly_font.char_h*5/2;
131 analogtv_draw_xpm(tv, input,
132 logo_50_xpm, xpos - 100, ypos);
137 analogtv_draw_string_centered(input, &ugly_font, "Please Stand By", xpos, ypos);
138 ypos += ugly_font.char_h*4;
143 time_t t = time ((time_t *) 0);
144 struct tm *tm = localtime (&t);
146 /* Y2K: It is OK for this to use a 2-digit year because it's simulating a
147 TV display and is purely decorative. */
148 strftime(timestamp, sizeof(timestamp)-1, "%y.%m.%d %H:%M:%S ", tm);
149 analogtv_draw_string_centered(input, &ugly_font, timestamp,
150 xpos, ypos, black_ntsc);
154 input->next_update_time += 1.0;
159 draw_color_square(analogtv_input *input)
163 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0, 0.0, 1.0,
166 for (xs=0.0; xs<0.9999; xs+=1.0/15.0) {
167 analogtv_draw_solid_rel_lcp(input, xs, xs, 0.0, 1.0,
171 for (ys=0.0; ys<0.9999; ys+=1.0/11.0) {
172 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0, ys, ys,
176 for (ys=0.0; ys<0.9999; ys+=0.01) {
178 analogtv_draw_solid_rel_lcp(input, 0.0/15, 1.0/15, ys, ys+0.01,
179 40.0, 45.0, 103.5*(1.0-ys) + 347.0*ys);
181 analogtv_draw_solid_rel_lcp(input, 14.0/15, 15.0/15, ys, ys+0.01,
182 40.0, 45.0, 103.5*(ys) + 347.0*(1.0-ys));
185 for (ys=0.0; ys<0.9999; ys+=0.02) {
186 analogtv_draw_solid_rel_lcp(input, 1.0/15, 2.0/15, ys*2.0/11.0+1.0/11.0,
187 (ys+0.01)*2.0/11.0+1.0/11.0,
188 100.0*(1.0-ys), 0.0, 0.0);
195 char *progclass = "XAnalogTV";
197 char *defaults [] = {
198 ".background: black",
199 ".foreground: white",
205 XrmOptionDescRec options [] = {
206 { "-delay", ".delay", XrmoptionSepArg, 0 },
212 #ifdef USE_TEST_PATTERNS
214 #include "images/earth.xpm"
216 char **test_patterns[] = {
224 N_CHANNELS=12, /* Channels 2 through 13 on VHF */
228 typedef struct chansetting_s {
230 analogtv_reception recs[MAX_MULTICHAN];
236 static struct timeval basetime;
242 gettimeofday(&tv,NULL);
243 return ((tv.tv_sec - basetime.tv_sec)*1000 +
244 (tv.tv_usec - basetime.tv_usec)/1000);
248 analogtv_load_random_image(analogtv *it, analogtv_input *input)
252 int width=ANALOGTV_PIC_LEN;
253 int height=width*3/4;
256 pixmap=XCreatePixmap(it->dpy, it->window, width, height, it->visdepth);
257 XSync(it->dpy, False);
258 load_random_image(it->screen, it->window, pixmap, NULL);
259 image = XGetImage(it->dpy, pixmap, 0, 0, width, height, ~0L, ZPixmap);
260 XFreePixmap(it->dpy, pixmap);
262 /* Make sure the window's background is not set to None, and get the
263 grabbed bits (if any) off it as soon as possible. */
264 XSetWindowBackground (it->dpy, it->window,
265 get_pixel_resource ("background", "Background",
266 it->dpy, it->xgwa.colormap));
267 XClearWindow (it->dpy, it->window);
269 analogtv_setup_sync(input, 1, (random()%20)==0);
270 rc=analogtv_load_ximage(it, input, image);
271 if (image) XDestroyImage(image);
272 XSync(it->dpy, False);
277 analogtv_load_xpm(analogtv *it, analogtv_input *input, char **xpm)
284 pixmap=xpm_data_to_pixmap (it->dpy, it->window, xpm,
285 &width, &height, NULL);
286 image = XGetImage(it->dpy, pixmap, 0, 0, width, height, ~0L, ZPixmap);
287 XFreePixmap(it->dpy, pixmap);
288 rc=analogtv_load_ximage(it, input, image);
289 if (image) XDestroyImage(image);
290 XSync(it->dpy, False);
294 enum { MAX_STATIONS = 6 };
295 static int n_stations;
296 static analogtv_input *stations[MAX_STATIONS];
299 void add_stations(void)
301 while (n_stations < MAX_STATIONS) {
302 analogtv_input *input=analogtv_input_allocate();
303 stations[n_stations++]=input;
306 input->updater = update_smpte_colorbars;
307 input->do_teletext=1;
309 #ifdef USE_TEST_PATTERNS
310 else if (random()%5==0) {
311 j=random()%countof(test_patterns);
312 analogtv_setup_sync(input);
313 analogtv_load_xpm(tv, input, test_patterns[j]);
314 analogtv_setup_teletext(input);
318 analogtv_load_random_image(tv, input);
319 input->do_teletext=1;
325 screenhack (Display *dpy, Window window)
332 chansetting chansettings[N_CHANNELS];
335 int delay = get_integer_resource("delay", "Integer");
336 if (delay < 1) delay = 1;
338 analogtv_make_font(dpy, window, &ugly_font, 7, 10, "6x10");
340 tv=analogtv_allocate(dpy, window);
341 tv->event_handler = screenhack_handle_event;
345 analogtv_set_defaults(tv, "");
349 tv->tint_control += pow(frand(2.0)-1.0, 7) * 180.0;
352 tv->color_control += frand(0.3);
355 for (i=0; i<N_CHANNELS; i++) {
356 memset(&chansettings[i], 0, sizeof(chansetting));
358 chansettings[i].noise_level = 0.06;
359 chansettings[i].dur = 1000*delay;
362 chansettings[i].dur=600;
366 for (stati=0; stati<MAX_MULTICHAN; stati++) {
367 analogtv_reception *rec=&chansettings[i].recs[stati];
370 station=random()%n_stations;
371 if (station!=last_station) break;
372 if ((random()%10)==0) break;
374 last_station=station;
375 rec->input = stations[station];
376 rec->level = pow(frand(1.0), 3.0) * 2.0 + 0.05;
377 rec->ofs=random()%ANALOGTV_SIGNAL_LEN;
379 rec->multipath = frand(1.0);
384 /* We only set a frequency error for ghosting stations,
385 because it doesn't matter otherwise */
386 rec->freqerr = (frand(2.0)-1.0) * 3.0;
389 if (rec->level > 0.3) break;
390 if (random()%4) break;
395 gettimeofday(&basetime,NULL);
398 cs=&chansettings[curinputi];
399 change_ticks = cs->dur + 1500;
403 int curticks=getticks();
404 double curtime=curticks*0.001;
407 if (analogtv_handle_events(tv)) {
411 if (change_now || (!using_mouse && curticks>=change_ticks
412 && tv->powerup > 10.0)) {
413 curinputi=(curinputi+1)%N_CHANNELS;
414 cs=&chansettings[curinputi];
415 change_ticks = curticks + cs->dur;
416 /* Set channel change noise flag */
417 tv->channel_change_cycles=200000;
420 for (i=0; i<MAX_MULTICHAN; i++) {
421 analogtv_reception *rec=&cs->recs[i];
422 analogtv_input *inp=rec->input;
426 inp->next_update_time = curtime;
429 rec->ofs += rec->freqerr;
434 analogtv_init_signal(tv, cs->noise_level);
435 for (i=0; i<MAX_MULTICHAN; i++) {
436 analogtv_reception *rec=&cs->recs[i];
437 analogtv_input *inp=rec->input;
440 analogtv_reception_update(rec);
441 analogtv_add_signal(tv, rec);
447 XClearWindow(dpy, window);
449 if (tv) analogtv_release(tv);