1 /*
2  * mtt - teletext test app.
3  *
4  * This is just main() for the standalone version, the actual
5  * code is in vbi-gui.c (motif) and vbi-tty.c (terminal).
6  *
7  *   (c) 2002 Gerd Knorr <kraxel@bytesex.org>
8  *
9  */
10 
11 #include "config.h"
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <iconv.h>
19 #include <locale.h>
20 #include <langinfo.h>
21 #include <termios.h>
22 #include <fcntl.h>
23 #include <pthread.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 
27 #include <X11/Xlib.h>
28 #include <X11/Intrinsic.h>
29 #include <Xm/Xm.h>
30 #include <Xm/Form.h>
31 #include <Xm/Label.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/CascadeB.h>
34 #include <Xm/PushB.h>
35 #include <Xm/DrawingA.h>
36 #include <Xm/Protocols.h>
37 
38 #include "icons.h"
39 #include "atoms.h"
40 #include "vbi-data.h"
41 #include "vbi-gui.h"
42 #include "vbi-tty.h"
43 
44 #include "grab-ng.h"
45 #include "channel.h"
46 #include "frequencies.h"
47 #include "commands.h"
48 
49 /* --------------------------------------------------------------------- */
50 
51 XtAppContext  app_context;
52 Widget        app_shell;
53 Display       *dpy;
54 
55 static String fallback_ressources[] = {
56 #include "mtt.h"
57     NULL
58 };
59 
60 struct ARGS {
61     char  *device;
62     int   help;
63     int   tty;
64     int   debug;
65     int   sim;
66 } args;
67 
68 XtResource args_desc[] = {
69     /* name, class, type, size, offset, default_type, default_addr */
70     {
71 	/* Strings */
72 	"device",
73 	XtCString, XtRString, sizeof(char*),
74 	XtOffset(struct ARGS*,device),
75 	XtRString, "/dev/vbi0",
76     },{
77 	/* Integer */
78 	"help",
79 	XtCValue, XtRInt, sizeof(int),
80 	XtOffset(struct ARGS*,help),
81 	XtRString, "0"
82     },{
83 	"tty",
84 	XtCValue, XtRInt, sizeof(int),
85 	XtOffset(struct ARGS*,tty),
86 	XtRString, "0"
87     },{
88 	"debug",
89 	XtCValue, XtRInt, sizeof(int),
90 	XtOffset(struct ARGS*,debug),
91 	XtRString, "0"
92     },{
93 	"sim",
94 	XtCValue, XtRInt, sizeof(int),
95 	XtOffset(struct ARGS*,sim),
96 	XtRString, "0"
97     }
98 };
99 const int args_count = XtNumber(args_desc);
100 
101 XrmOptionDescRec opt_desc[] = {
102     { "-c",          "device",      XrmoptionSepArg, NULL },
103     { "-device",     "device",      XrmoptionSepArg, NULL },
104 
105     { "-tty",        "tty",         XrmoptionNoArg,  "1" },
106     { "-sim",        "sim",         XrmoptionNoArg,  "1" },
107     { "-debug",      "debug",       XrmoptionNoArg,  "1" },
108 
109     { "-h",          "help",        XrmoptionNoArg,  "1" },
110     { "-help",       "help",        XrmoptionNoArg,  "1" },
111     { "--help",      "help",        XrmoptionNoArg,  "1" },
112 };
113 const int opt_count = (sizeof(opt_desc)/sizeof(XrmOptionDescRec));
114 
115 /* --------------------------------------------------------------------- */
116 
117 static void
debug_action(Widget widget,XEvent * event,String * params,Cardinal * num_params)118 debug_action(Widget widget, XEvent *event,
119 	     String *params, Cardinal *num_params)
120 {
121     fprintf(stderr,"debug_action: called\n");
122 }
123 
124 static XtActionsRec actionTable[] = {
125     { "debug", debug_action },
126 };
127 
128 /* --------------------------------------------------------------------- */
129 
usage(void)130 static void usage(void)
131 {
132     fprintf(stderr,
133 	    "\n"
134 	    "mtt -- teletext application\n"
135 	    "\n"
136 	    "usage: mtt [ options ]\n"
137 	    "options:\n"
138 	    "  -help         print this text\n"
139 	    "  -debug        enable debug messages\n"
140 	    "  -device <dev> use vbi device <dev> instead of /dev/vbi0\n"
141 	    "  -tty          use terminal mode\n"
142 	    "\n"
143 	    "--\n"
144 	    "Gerd Knorr <kraxel@bytesex.org>\n");
145 }
146 
vbi_data(XtPointer data,int * fd,XtInputId * iproc)147 static void vbi_data(XtPointer data, int *fd, XtInputId *iproc)
148 {
149     struct vbi_state *vbi = data;
150     vbi_hasdata(vbi);
151 }
152 
main_tty(int argc,char ** argv)153 static int main_tty(int argc, char **argv)
154 {
155     char *dev = "/dev/vbi0";
156     int debug = 0;
157     int sim   = 0;
158 
159     argc--;
160     argv++;
161     for (;argc;) {
162 	if (0 == strcmp(argv[0],"-c") ||
163 	    0 == strcmp(argv[0],"-device")) {
164 	    dev = argv[1];
165 	    argc -= 2;
166 	    argv += 2;
167 
168 	} else if (0 == strcmp(argv[0],"-tty")) {
169 	    argc -= 1;
170 	    argv += 1;
171 
172 	} else if (0 == strcmp(argv[0],"-debug")) {
173 	    debug = 1;
174 	    argc -= 1;
175 	    argv += 1;
176 
177 	} else if (0 == strcmp(argv[0],"-sim")) {
178 	    sim   = 1;
179 	    argc -= 1;
180 	    argv += 1;
181 
182 	} else if (0 == strcmp(argv[0],"-h") &&
183 		   0 == strcmp(argv[0],"-help") &&
184 		   0 == strcmp(argv[0],"--help")) {
185 	    usage();
186 	    exit(0);
187 
188 	} else
189 	    break;
190     }
191     vbi_tty(dev,debug,sim);
192     exit(0);
193 }
194 
195 int
main(int argc,char ** argv)196 main(int argc, char **argv)
197 {
198     struct vbi_state *vbi;
199     char **av;
200     int ac;
201 
202     ac = argc;
203     av = malloc(sizeof(char*)*(argc+1));
204     memcpy(av,argv,sizeof(char*)*(argc+1));
205 
206     XtSetLanguageProc(NULL,NULL,NULL);
207     XtToolkitInitialize();
208     app_context = XtCreateApplicationContext();
209     XtAppSetFallbackResources(app_context,fallback_ressources);
210     dpy = XtOpenDisplay(app_context, NULL,
211 			NULL,"mtt",
212 			opt_desc, opt_count,
213 			&argc, argv);
214     if (NULL == dpy)
215 	main_tty(ac,av);
216     app_shell = XtVaAppCreateShell(NULL,"mtt",
217 				   applicationShellWidgetClass,dpy,NULL);
218     XtAppAddActions(app_context,actionTable,
219 		    sizeof(actionTable)/sizeof(XtActionsRec));
220     x11_icons_init(dpy,0);
221     init_atoms(dpy);
222     XtGetApplicationResources(app_shell,&args,
223 			      args_desc,args_count,
224 			      NULL,0);
225     if (args.help) {
226 	usage();
227 	exit(1);
228     }
229     if (args.tty)
230 	main_tty(ac,av);
231 
232     freq_init();
233     read_config(NULL, &argc, argv);
234     parse_config(1);
235     do_va_cmd(2,"setfreqtab",(-1 != chantab)
236 	      ? chanlist_names[chantab].str : "europe-west");
237 
238     vbi = vbi_open(args.device,args.debug,args.sim);
239     if (NULL == vbi)
240 	exit(1);
241     if (args.debug)
242 	vbi_event_handler_add(vbi->dec,~0,vbi_dump_event,vbi);
243     XtAppAddInput(app_context, vbi->fd, (XtPointer) XtInputReadMask,
244 		  vbi_data, vbi);
245 
246     vbi_create_widgets(app_shell,vbi);
247     XtRealizeWidget(app_shell);
248     XtAppMainLoop(app_context);
249     return 0;
250 }
251