aboutsummaryrefslogtreecommitdiffstats
path: root/util/alevt/main.c
blob: 31fe9c392abfb162f5d54eec3ce640ab07fe7f59 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include "vt.h"
#include "misc.h"
#include "fdset.h"
#include "xio.h"
#include "vbi.h"
#include "lang.h"
#include "cache.h"
#include "ui.h"

static char *geometry;
static char *dpy_name;
static char *vbi_name = NULL;
static struct xio *xio;
static struct vbi *vbi;
static int erc = 1;
char *outfile = "";
static char *channel;
static int ttpid = -1;
u_int16_t sid = 0;


static void usage(FILE *fp, int exitval)
{
    fprintf(fp, "\nUsage: %s [options]\n", prgname);
    fprintf(fp,
	    "\n"
	    "  Valid options:\t\tDefault:\n"
	    "    -c <channel name>\t\t(none;dvb only)\n"
	    "    -ch -child <ppp.ss>\t\t(none)\n"
	    "    -cs -charset\t\tlatin-1\n"
	    "    <latin-1/2/koi8-r/iso8859-7>\n"
	    "    -h -help\n"
	    "    -o <outfile>\t\t(none;dvb only)\n"
	    "    -p -parent <ppp.ss>\t\t900\n"
	    "    -s -sid <sid>\t\t(none;dvb only)\n"
	    "    -t -ttpid <ttpid>\t\t(none;dvb only)\n"
	    "    -v -vbi <vbidev>\t\t/dev/vbi\n"
		"                 \t\t/dev/vbi0\n"
		"                 \t\t/dev/video0\n"
		"                 \t\t/dev/dvb/adapter0/demux0\n"
	    "\n"
	    "  ppp.ss stands for a page number and an\n"
	    "  optional subpage number (Example: 123.4).\n"
	    "\n"
	    "  The -child option requires a parent\n"
	    "  window. So it must be preceded by\n"
	    "  a parent or another child window.\n"
	);
    exit(exitval);
}


static int arg_pgno(char *p, int *subno)
{
    char *end;
    int pgno;

    *subno = ANY_SUB;
    if (*p)
    {
	pgno = strtol(p, &end, 16);
	if ((*end == ':' || *end == '/' || *end == '.') && end[1])
	    *subno = strtol(end + 1, &end, 16);
	if (*end == 0)
	    if (pgno >= 0x100 && pgno <= 0x999)
		if (*subno == ANY_SUB || (*subno >= 0x00 && *subno <= 0x3f7f))
		    return pgno;
    }
    fatal("%s: invalid page number", p);
}


static struct vtwin * start(int argc, char **argv, struct vtwin *parent,
	int pgno, int subno)
{
    static int valid_vbi_name = 1;

    if (!valid_vbi_name)
	return parent;

    if (vbi == 0)
	vbi = vbi_open(vbi_name, cache_open(), channel, outfile, sid, ttpid);
    if (vbi == 0)
    {
	if (vbi_name)
	    error("cannot open device: %s", vbi_name);
    	valid_vbi_name = 0;
    	vbi = open_null_vbi(cache_open());
    }
    if (vbi->cache)
	vbi->cache->op->mode(vbi->cache, CACHE_MODE_ERC, erc);

    if (xio == 0)
	xio = xio_open_dpy(dpy_name, argc, argv);
    if (xio == 0)
	fatal("cannot open display");

    parent = vtwin_new(xio, vbi, geometry, parent, pgno, subno);
    if (parent == 0)
	fatal("cannot create window");

    if (!valid_vbi_name)
    {
	if (vbi_name)
	    send_errmsg(vbi, "cannot open device: %s", vbi_name);
	else
	    send_errmsg(vbi, "cannot open any device", vbi_name);
    }

    return parent;
}


static int option(int argc, char **argv, int *ind, char **arg)
{
    static struct { char *nam, *altnam; int arg; } opts[] = {
	{ "-channel", "-c", 1 },
	{ "-child", "-ch", 1 },
	{ "-charset", "-cs", 1 },
	{ "-help", "-h", 0 },
	{ "-outfile", "-o", 1 },
	{ "-parent", "-p", 1 },
	{ "-sid", "-s", 1 },
	{ "-ttpid", "-t", 1 },
	{ "-vbi", "-v", 1 },
    };
    int i;
    if (*ind >= argc)
	return 0;
    *arg = argv[(*ind)++];
    for (i = 0; i < NELEM(opts); ++i)
	if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
	{
	    if (opts[i].arg)
		if (*ind < argc)
		    *arg = argv[(*ind)++];
		else
		    fatal("option %s requires an argument", *arg);
	    return i+1;
	}
    if (**arg == '-')
    {
	fatal("%s: invalid option", *arg);
	usage(stderr, 1);
    }
    return -1;
}


int main(int argc, char **argv)
{
    struct vtwin *parent = 0;
    int pgno, subno;
    int opt, ind;
    char *arg;
    sid = 0;

    setprgname(argv[0]);
    fdset_init(fds);

    ind = 1;
    while (opt = option(argc, argv, &ind, &arg))
	switch (opt)
	{

	    case 1: // channel
		channel = arg;
		break;
	    case 2: // child
		if (parent == 0)
		    fatal("-child requires a parent window");
		pgno = arg_pgno(arg, &subno);
		parent = start(argc, argv, parent, pgno, subno);
		geometry = 0;
		break;
	    case 3: // charset
		if (streq(arg, "latin-1") || streq(arg, "1"))
		    latin1 = LATIN1;
		else if (streq(arg, "latin-2") || streq(arg, "2"))
		    latin1 = LATIN2;
		else if (streq(arg, "koi8-r") || streq(arg, "koi"))
		    latin1 = KOI8;
		else if (streq(arg, "iso8859-7") || streq(arg, "el"))
		    latin1 = GREEK;
		else
		    fatal("bad charset (not latin-1/2/koi8-r/iso8859-7)");
		break;
	    case 4: // help
		usage(stdout, 0);
		break;
	    case 5: // outfile
		outfile = arg;
		break;
	    case 6: // parent
	    case -1: // non-option arg
		pgno = arg_pgno(arg, &subno);
		parent = start(argc, argv, 0, pgno, subno);
		geometry = 0;
		break;
	    case 7: // sid
		sid = strtoul(arg, NULL, 0);
		break;
	    case 8: // ttpid
		ttpid = strtoul(arg, NULL, 0);
		break;
	    case 9: // vbi
		vbi_name = arg;
		vbi = 0;
		parent = 0;
		break;
	}

    if (parent == 0)
    start(argc, argv, 0, 0x900, ANY_SUB);
    xio_event_loop();
    exit(0);
}