aboutsummaryrefslogtreecommitdiffstats
path: root/console/scantv.c
blob: 5d96772f32a34b0fe264bfeab6e55bd872241ed8 (plain)
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
/*
 * (c) 2000-2002 Gerd Knorr <kraxel@goldbach.in-berlin.de>
 *
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>

/* xawtv */
#include "frequencies.h"
#include "grab-ng.h"
#include "commands.h"
#include "channel.h"

#include "vbi-data.h"

int debug = 0;
int have_dga = 0;

int timeout = 3;
char *tvname;

/*---------------------------------------------------------------------*/

static void
grabber_init(void)
{
    drv = ng_vid_open(ng_dev.video,NULL,NULL,0,&h_drv);
    if (NULL == drv) {
	fprintf(stderr,"no grabber device available\n");
	exit(1);
    }
    f_drv = drv->capabilities(h_drv);
    add_attrs(drv->list_attrs(h_drv));
}

static void
event(struct vbi_event *ev, void *user)
{
    switch (ev->type) {
    case VBI_EVENT_NETWORK:
	if (NULL != tvname)
	    free(tvname);
	if (strlen(ev->ev.network.name) > 0)
	    tvname = strdup(ev->ev.network.name);
	break;
    }
}

static char*
get_vbi_name(struct vbi_state *vbi)
{
    int start;

    vbi_hasdata(vbi);
    if (NULL != tvname)
	free(tvname);
    tvname = NULL;
    start = time(NULL);
    for (;;) {
	vbi_hasdata(vbi);
        if (time(NULL) > start+timeout)
            break;
        if (tvname)
            break;
    }
    return tvname;
}

static int
menu(char *intro, struct STRTAB *tab, char *opt)
{
    int i,ret;
    char line[80];

    if (NULL != opt) {
	/* cmd line option -- non-interactive mode */
	for (i = 0; tab[i].str != NULL; i++)
	    if (0 == strcasecmp(tab[i].str,opt))
		return tab[i].nr;
	fprintf(stderr,"%s: not found\n",opt);
	exit(1);
    }

    fprintf(stderr,"\n%s\n",intro);
    for (i = 0; tab[i].str != NULL; i++)
	fprintf(stderr,"  %2ld: %s\n",tab[i].nr,tab[i].str);

    for (;;) {
	fprintf(stderr,"nr ? ");
	fgets(line,79,stdin);
	ret = atoi(line);
	for (i = 0; tab[i].str != NULL; i++)
	    if (ret == tab[i].nr)
		return ret;
	fprintf(stderr,"invalid choice\n");
    }
}

static void
usage(FILE *out, char *prog, char *outfile)
{
    fprintf(out,
	    "This tool scan for tv stations and writes "
	    "a initial xawtv config file.\n"
	    "usage: %s [ options ]\n"
	    "options:\n"
	    "   -h           print this text\n"
	    "   -o outfile   set output file.        [%s]\n"
	    "   -i input     set input.\n"
	    "   -n norm      set tv norm.\n"
	    "   -f table     set frequency table.\n"
	    "   -c device    set video device file.  [%s]\n"
	    "   -C device    set vbi device file.    [%s]\n"
	    "   -s           skip channel scan\n"
	    "   -a           full scan (all frequencies, not just\n"
	    "                the ones from the frequency table)\n",
	    prog,
	    outfile ? outfile : "stdout",
	    ng_dev.video, ng_dev.vbi);
}

int
main(int argc, char **argv)
{
    struct vbi_state *vbi;
    struct ng_attribute *attr;
    int c,fi,on,tuned,i,j,scan=1,fullscan=0;
    unsigned int f,f1,f2,fc;
    char *name,dummy[32];
    char *tvnorm  = NULL;
    char *tvinput  = NULL;
    char *freqtab = NULL;
    char *outfile = NULL;
    FILE *conf = stdout;

    /* parse options */
    ng_init();
    for (;;) {
	if (-1 == (c = getopt(argc, argv, "hsadi:n:f:o:c:C:")))
	    break;
	switch (c) {
	case 'd':
	    debug++;
	    break;
	case 's':
	    scan=0;
	    break;
	case 'a':
	    fullscan=1;
	    break;
	case 'i':
	    tvinput = optarg;
	    break;
	case 'n':
	    tvnorm = optarg;
	    break;
	case 'f':
	    freqtab = optarg;
	    break;
	case 'o':
	    outfile = optarg;
	    break;
	case 'c':
	    ng_dev.video = optarg;
	    break;
	case 'C':
	    ng_dev.vbi = optarg;
	    break;
	case 'h':
	    usage(stdout,argv[0],outfile);
	    exit(0);
	default:
	    usage(stderr,argv[0],outfile);
	    exit(1);
	}
    }

    if (outfile) {
	if (NULL == (conf = fopen(outfile,"w"))) {
	    fprintf(stderr,"open %s: %s\n",argv[1],strerror(errno));
	    exit(1);
	}
    }

    /* video */
    if (NULL == drv)
	grabber_init();
    if (NULL == drv) {
	fprintf(stderr,"can't open video device\n");
	exit(1);
    }

    attr_init();
    audio_init();
    freq_init();

    /* ask the user some questions ... */
    attr = ng_attr_byid(attrs,ATTR_ID_NORM);
    i = menu("please select your TV norm",attr->choices,tvnorm);
    j = menu("please select a frequency table",chanlist_names,freqtab);
    if (tvinput == NULL) {
       tvinput = "Television";
    }

    fprintf(conf,"[global]\n");
    fprintf(conf,"freqtab = %s\n",chanlist_names[j].str);
    fprintf(conf,"\n");
    fprintf(conf,"[defaults]\n");
    fprintf(conf,"input = %s\n", tvinput);
    fprintf(conf,"norm = %s\n",ng_attr_getstr(attr,i));
    fprintf(conf,"\n");
    fflush(conf);
    if (!scan)
	exit(0);

    if (!(f_drv & CAN_TUNE)) {
	fprintf(stderr,"device has no tuner, exiting\n");
	exit(0);
    }
    set_defaults();
    do_va_cmd(2,"setinput",tvinput);
    do_va_cmd(2,"setnorm",ng_attr_getstr(attr,i));
    do_va_cmd(2,"setfreqtab",chanlist_names[j].str);

    /* vbi */
    vbi = vbi_open(ng_dev.vbi,debug,0);
    if (NULL == vbi) {
	fprintf(stderr,"open %s: %s\n",ng_dev.vbi,strerror(errno));
	exit(1);
    }
    vbi_event_handler_add(vbi->dec,~0,event,vbi);
    
    if (!fullscan) {
	/* scan channels */
	fprintf(stderr,"\nscanning channel list %s...\n",
		chanlist_names[j].str);
	for (i = 0; i < chancount; i++) {
	    fprintf(stderr,"%-4s (%6.2f MHz): ",chanlist[i].name,
		    (float)chanlist[i].freq/1000);
	    do_va_cmd(2,"setchannel",chanlist[i].name);
	    usleep(200000); /* 0.2 sec */
	    if (0 == drv->is_tuned(h_drv)) {
		fprintf(stderr,"no station\n");
		continue;
	    }
	    name = get_vbi_name(vbi);
	    fprintf(stderr, "%s\n", name ? name : "???");
	    if (NULL == name) {
		sprintf(dummy,"unknown (%s)",chanlist[i].name);
		name = dummy;
	    }
	    fprintf(conf,"[%s]\nchannel = %s\n\n",name,chanlist[i].name);
	    fflush(conf);
	}
    } else {
	/* scan freqnencies */
	fprintf(stderr,"\nscanning freqencies...\n");
	on = 0;
	fc = 0;
	f1 = 0;
	f2 = 0;
	fi = -1;
	for (f = 44*16; f <= 958*16; f += 4) {
	    for (i = 0; i < chancount; i++)
		if (chanlist[i].freq * 16 == f * 1000)
		    break;
	    fprintf(stderr,"?? %6.2f MHz (%-4s): ",f/16.0,
		    (i == chancount) ? "-" : chanlist[i].name);
	    drv->setfreq(h_drv,f);
	    usleep(200000); /* 0.2 sec */
	    tuned = drv->is_tuned(h_drv);

	    /* state machine */
	    if (0 == on && 0 == tuned) {
		fprintf(stderr,"|   no\n");
		continue;
	    }
	    if (0 == on && 0 != tuned) {
		fprintf(stderr," \\  raise\n");
		f1 = f;
		if (i != chancount) {
		    fi = i;
		    fc = f;
		}
		on = 1;
		continue;
	    }
	    if (0 != on && 0 != tuned) {
		fprintf(stderr,"  | yes\n");
		if (i != chancount) {
		    fi = i;
		    fc = f;
		}
		continue;
	    }
	    /* if (on != 0 && 0 == tuned)  --  found one, read name from vbi */
	    fprintf(stderr," /  fall\n");
	    f2 = f;
	    if (0 == fc)
		fc = (f1+f2)/2;

	    fprintf(stderr,"=> %6.2f MHz (%-4s): ", fc/16.0,
		    (-1 != fi) ? chanlist[fi].name : "-");
	    drv->setfreq(h_drv,fc);
	    
	    name = get_vbi_name(vbi);
	    fprintf(stderr,"%s\n",name ? name : "???");
	    if (NULL == name) {
		if (-1 == fi)
		    sprintf(dummy,"unknown (%s)",chanlist[fi].name);
		else
		    sprintf(dummy,"unknown (%.2f)",fc/16.0);
		name = dummy;
	    }
	    if (-1 != fi) {
		if (NULL == name) {
		    sprintf(dummy,"unknown (%s)",chanlist[fi].name);
		    name = dummy;
		}
		fprintf(conf,"[%s]\nchannel = %s\n\n",name,chanlist[fi].name);
	    } else {
		if (NULL == name) {
		    sprintf(dummy,"unknown (%.3f)", fc/16.0);
		    name = dummy;
		}
		fprintf(conf,"[%s]\nfreq = %.3f\n\n", name, fc/16.0);
	    }
	    fflush(conf);

	    on = 0;
	    fc = 0;
	    f1 = 0;
	    f2 = 0;
	    fi = -1;
	}
    }

    /* cleanup */
    audio_off();
    drv->close(h_drv);

    exit(0);
}

Privacy Policy