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"
34 #include <X11/Xutil.h>
35 #include <X11/Intrinsic.h>
37 #include "images/logo-50.xpm"
40 /* #define USE_TEST_PATTERNS */
42 #define countof(x) (sizeof((x))/sizeof((*x)))
44 static analogtv *tv=NULL;
46 analogtv_font ugly_font;
49 update_smpte_colorbars(analogtv_input *input)
56 SMPTE is the society of motion picture and television engineers, and
57 these are the standard color bars in the US. Following the partial spec
58 at http://broadcastengineering.com/ar/broadcasting_inside_color_bars/
59 These are luma, chroma, and phase numbers for each of the 7 bars.
61 double top_cb_table[7][3]={
62 {75, 0, 0.0}, /* gray */
63 {69, 31, 167.0}, /* yellow */
64 {56, 44, 283.5}, /* cyan */
65 {48, 41, 240.5}, /* green */
66 {36, 41, 60.5}, /* magenta */
67 {28, 44, 103.5}, /* red */
68 {15, 31, 347.0} /* blue */
70 double mid_cb_table[7][3]={
71 {15, 31, 347.0}, /* blue */
72 {7, 0, 0}, /* black */
73 {36, 41, 60.5}, /* magenta */
74 {7, 0, 0}, /* black */
75 {56, 44, 283.5}, /* cyan */
76 {7, 0, 0}, /* black */
77 {75, 0, 0.0} /* gray */
80 analogtv_lcp_to_ntsc(0.0, 0.0, 0.0, black_ntsc);
82 analogtv_setup_sync(input, 1, 0);
83 analogtv_setup_teletext(input);
85 for (col=0; col<7; col++) {
86 analogtv_draw_solid_rel_lcp(input, col*(1.0/7.0), (col+1)*(1.0/7.0), 0.00, 0.68,
88 top_cb_table[col][1], top_cb_table[col][2]);
90 analogtv_draw_solid_rel_lcp(input, col*(1.0/7.0), (col+1)*(1.0/7.0), 0.68, 0.75,
92 mid_cb_table[col][1], mid_cb_table[col][2]);
95 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0/6.0,
96 0.75, 1.00, 7, 40, 303); /* -I */
97 analogtv_draw_solid_rel_lcp(input, 1.0/6.0, 2.0/6.0,
98 0.75, 1.00, 100, 0, 0); /* white */
99 analogtv_draw_solid_rel_lcp(input, 2.0/6.0, 3.0/6.0,
100 0.75, 1.00, 7, 40, 33); /* +Q */
101 analogtv_draw_solid_rel_lcp(input, 3.0/6.0, 4.0/6.0,
102 0.75, 1.00, 7, 0, 0); /* black */
103 analogtv_draw_solid_rel_lcp(input, 12.0/18.0, 13.0/18.0,
104 0.75, 1.00, 3, 0, 0); /* black -4 */
105 analogtv_draw_solid_rel_lcp(input, 13.0/18.0, 14.0/18.0,
106 0.75, 1.00, 7, 0, 0); /* black */
107 analogtv_draw_solid_rel_lcp(input, 14.0/18.0, 15.0/18.0,
108 0.75, 1.00, 11, 0, 0); /* black +4 */
109 analogtv_draw_solid_rel_lcp(input, 5.0/6.0, 6.0/6.0,
110 0.75, 1.00, 7, 0, 0); /* black */
114 xpos=ANALOGTV_VIS_START + ANALOGTV_VIS_LEN/2;
118 if (gethostname (localname, sizeof (localname))==0) {
119 localname[sizeof(localname)-1]=0; /* "The returned name is null-
120 terminated unless insufficient
121 space is provided" */
122 localname[24]=0; /* limit length */
124 analogtv_draw_string_centered(input, &ugly_font, localname,
125 xpos, ypos, black_ntsc);
128 ypos += ugly_font.char_h*5/2;
130 analogtv_draw_xpm(tv, input,
131 logo_50_xpm, xpos - 100, ypos);
136 analogtv_draw_string_centered(input, &ugly_font, "Please Stand By", xpos, ypos);
137 ypos += ugly_font.char_h*4;
142 time_t t = time ((time_t *) 0);
143 struct tm *tm = localtime (&t);
145 /* Y2K: It is OK for this to use a 2-digit year because it's simulating a
146 TV display and is purely decorative. */
147 strftime(timestamp, sizeof(timestamp)-1, "%y.%m.%d %H:%M:%S ", tm);
148 analogtv_draw_string_centered(input, &ugly_font, timestamp,
149 xpos, ypos, black_ntsc);
153 input->next_update_time += 1.0;
158 draw_color_square(analogtv_input *input)
162 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0, 0.0, 1.0,
165 for (xs=0.0; xs<0.9999; xs+=1.0/15.0) {
166 analogtv_draw_solid_rel_lcp(input, xs, xs, 0.0, 1.0,
170 for (ys=0.0; ys<0.9999; ys+=1.0/11.0) {
171 analogtv_draw_solid_rel_lcp(input, 0.0, 1.0, ys, ys,
175 for (ys=0.0; ys<0.9999; ys+=0.01) {
177 analogtv_draw_solid_rel_lcp(input, 0.0/15, 1.0/15, ys, ys+0.01,
178 40.0, 45.0, 103.5*(1.0-ys) + 347.0*ys);
180 analogtv_draw_solid_rel_lcp(input, 14.0/15, 15.0/15, ys, ys+0.01,
181 40.0, 45.0, 103.5*(ys) + 347.0*(1.0-ys));
184 for (ys=0.0; ys<0.9999; ys+=0.02) {
185 analogtv_draw_solid_rel_lcp(input, 1.0/15, 2.0/15, ys*2.0/11.0+1.0/11.0,
186 (ys+0.01)*2.0/11.0+1.0/11.0,
187 100.0*(1.0-ys), 0.0, 0.0);
194 char *progclass = "XAnalogTV";
196 char *defaults [] = {
202 XrmOptionDescRec options [] = {
203 { "-delay", ".delay", XrmoptionSepArg, 0 },
209 #ifdef USE_TEST_PATTERNS
211 #include "images/earth.xpm"
213 char **test_patterns[] = {
221 N_CHANNELS=12, /* Channels 2 through 13 on VHF */
225 typedef struct chansetting_s {
227 analogtv_reception recs[MAX_MULTICHAN];
233 static struct timeval basetime;
239 gettimeofday(&tv,NULL);
240 return ((tv.tv_sec - basetime.tv_sec)*1000 +
241 (tv.tv_usec - basetime.tv_usec)/1000);
245 analogtv_load_random_image(analogtv *it, analogtv_input *input)
249 int width=ANALOGTV_PIC_LEN;
250 int height=width*3/4;
253 pixmap=XCreatePixmap(it->dpy, it->window, width, height, it->visdepth);
254 XSync(it->dpy, False);
255 load_random_image(it->screen, it->window, pixmap, NULL);
256 image = XGetImage(it->dpy, pixmap, 0, 0, width, height, ~0L, ZPixmap);
257 XFreePixmap(it->dpy, pixmap);
259 /* Make sure the window's background is not set to None, and get the
260 grabbed bits (if any) off it as soon as possible. */
261 XSetWindowBackground (it->dpy, it->window,
262 get_pixel_resource ("background", "Background",
263 it->dpy, it->xgwa.colormap));
264 XClearWindow (it->dpy, it->window);
266 analogtv_setup_sync(input, 1, (random()%20)==0);
267 rc=analogtv_load_ximage(it, input, image);
268 if (image) XDestroyImage(image);
269 XSync(it->dpy, False);
274 analogtv_load_xpm(analogtv *it, analogtv_input *input, char **xpm)
281 pixmap=xpm_data_to_pixmap (it->dpy, it->window, xpm,
282 &width, &height, NULL);
283 image = XGetImage(it->dpy, pixmap, 0, 0, width, height, ~0L, ZPixmap);
284 XFreePixmap(it->dpy, pixmap);
285 rc=analogtv_load_ximage(it, input, image);
286 if (image) XDestroyImage(image);
287 XSync(it->dpy, False);
291 enum { MAX_STATIONS = 6 };
292 static int n_stations;
293 static analogtv_input *stations[MAX_STATIONS];
296 void add_stations(void)
298 while (n_stations < MAX_STATIONS) {
299 analogtv_input *input=analogtv_input_allocate();
300 stations[n_stations++]=input;
303 input->updater = update_smpte_colorbars;
304 input->do_teletext=1;
306 #ifdef USE_TEST_PATTERNS
307 else if (random()%5==0) {
308 j=random()%countof(test_patterns);
309 analogtv_setup_sync(input);
310 analogtv_load_xpm(tv, input, test_patterns[j]);
311 analogtv_setup_teletext(input);
315 analogtv_load_random_image(tv, input);
316 input->do_teletext=1;
322 screenhack (Display *dpy, Window window)
329 chansetting chansettings[N_CHANNELS];
332 int delay = get_integer_resource("delay", "Integer");
333 if (delay < 1) delay = 1;
335 analogtv_make_font(dpy, window, &ugly_font, 7, 10, "6x10");
337 tv=analogtv_allocate(dpy, window);
338 tv->event_handler = screenhack_handle_event;
342 analogtv_set_defaults(tv, "");
346 tv->tint_control += pow(frand(2.0)-1.0, 7) * 180.0;
349 tv->color_control += frand(0.3);
352 for (i=0; i<N_CHANNELS; i++) {
353 memset(&chansettings[i], 0, sizeof(chansetting));
355 chansettings[i].noise_level = 0.06;
356 chansettings[i].dur = 1000*delay;
359 chansettings[i].dur=600;
363 for (stati=0; stati<MAX_MULTICHAN; stati++) {
364 analogtv_reception *rec=&chansettings[i].recs[stati];
367 station=random()%n_stations;
368 if (station!=last_station) break;
369 if ((random()%10)==0) break;
371 last_station=station;
372 rec->input = stations[station];
373 rec->level = pow(frand(1.0), 3.0) * 2.0 + 0.05;
374 rec->ofs=random()%ANALOGTV_SIGNAL_LEN;
376 rec->multipath = frand(1.0);
381 /* We only set a frequency error for ghosting stations,
382 because it doesn't matter otherwise */
383 rec->freqerr = (frand(2.0)-1.0) * 3.0;
386 if (rec->level > 0.3) break;
387 if (random()%4) break;
392 gettimeofday(&basetime,NULL);
395 cs=&chansettings[curinputi];
396 change_ticks = cs->dur + 1500;
400 int curticks=getticks();
401 double curtime=curticks*0.001;
404 if (analogtv_handle_events(tv)) {
408 if (change_now || (!using_mouse && curticks>=change_ticks
409 && tv->powerup > 10.0)) {
410 curinputi=(curinputi+1)%N_CHANNELS;
411 cs=&chansettings[curinputi];
412 change_ticks = curticks + cs->dur;
413 /* Set channel change noise flag */
414 tv->channel_change_cycles=200000;
417 for (i=0; i<MAX_MULTICHAN; i++) {
418 analogtv_reception *rec=&cs->recs[i];
419 analogtv_input *inp=rec->input;
423 inp->next_update_time = curtime;
426 rec->ofs += rec->freqerr;
431 analogtv_init_signal(tv, cs->noise_level);
432 for (i=0; i<MAX_MULTICHAN; i++) {
433 analogtv_reception *rec=&cs->recs[i];
434 analogtv_input *inp=rec->input;
437 analogtv_reception_update(rec);
438 analogtv_add_signal(tv, rec);
444 XClearWindow(dpy, window);
446 if (tv) analogtv_release(tv);