FreeWRL / FreeX3D 4.3.0
options.c
1/*
2
3 FreeWRL command line arguments.
4
5*/
6
7/****************************************************************************
8 This file is part of the FreeWRL/FreeX3D Distribution.
9
10 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
11
12 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
24****************************************************************************/
25
26
27#include <config.h>
28#include <system.h>
29//#include <internal.h>
30#define ERROR_MSG
31#define DEBUG_MSG
32#define TRACE_MSG
33#include <libFreeWRL.h>
34
35#include "main.h"
36#include "options.h"
37
38#if HAVE_GETOPT_H
39#include <getopt.h>
40#endif
41
42#if !defined(min)
43 #define min(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
44#endif
45
46#if !defined(max)
47 #define max(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
48#endif
49
50void fv_print_version()
51{
52 const char *libver, *progver;
53
54 libver = libFreeWRL_get_version();
55 progver = freewrl_get_version();
56
57 printf("Program version: %s\nLibrary version: %s\n", progver, libver);
58 printf("\nFreeWRL VRML/X3D browser from (http://freewrl.sf.net)\n");
59 printf(" type \"man freewrl\" to view man pages\n\n");
60}
61
62void fv_usage()
63{
64 printf( "usage: freewrl [options] <VRML or X3D file|URL>\n\n"
65 " -h|--help This help.\n"
66 " -v|--version Print version.\n"
67 "\nWindow options:\n"
68 " -c|--fullscreen Set window fullscreen\n"
69 " -g|--geometry <WxH> Set window geometry (W width, H height).\n"
70 " -b|--big Set window size to 800x600.\n"
71 "\nGeneral options:\n"
72 " -e|--eai Enable EAI.\n"
73 // " -f|--fast Set global texture size to -256 (fast).\n"
74 " -W|--linewidth <float> Set line width.\n"
75 //" -Q|--nocollision Disable collision management.\n"
76 "\nSnapshot options:\n"
77 " -p|--gif Set file format to GIF (default is PNG).\n"
78 " -n|--snapfile <string> Set output file name pattern with <string>,\n"
79 " (use %%n for iteration number).\n"
80 " -o|--snaptmp <string> Set output directory for snap files.\n"
81 "\nMisc options:\n"
82 " -V|--eaiverbose Set EAI subsystem messages.\n"
83 " -r|--screendist <float> Set screen distance.\n"
84 " -y|--eyedist <float> Set eye distance.\n"
85 " -u|--shutter Set shutter glasses.\n"
86 " -t|--stereo <float> Set stereo parameter (angle factor).\n"
87 " -A|--anaglyph <string> Set anaglyph color pair ie: RB for left red, right blue. any of RGBCAM.\n"
88 " -B|--sidebyside Set side-by-side stereo.\n"
89 " -U|--updown Set updown stereo.\n"
90 " -K|--keypress <string> Set immediate key pressed when ready.\n"
91#ifdef USE_SNAPSHOT_TESTING
92 " -R|--record Record to /recording/<scene>.fwplay.\n"
93 " -F|--fixture Playback from /recording/<scene>.fwplay to /fixture.\n"
94 " -P|--playback Playback from /recording/<scene>.fwplay to /playback\n"
95 " -N|--nametest <string> Set name of .fwplay test file\n"
96 " -Y|--testpath <string> Set path to recording directory\n"
97#endif
98 " -G|--colorscheme <string> UI colorscheme by builtin name: {original,angry,\n"
99 " aqua,favicon,midnight,neon:lime,neon:yellow,neon:cyan,neon:pink}\n"
100 " -H|--colors <string> UI colorscheme by 4 html colors in order: \n"
101 " panel,menuIcon,statusText,messageText ie \"#3D4557,#00FFFF,#00FFFF.#00FFFF\" \n"
102 " -I|--pin TF Pin statusbar(T/F) menubar(T/F)\n"
103 " -w|--want TF Want statusbar(T/F) menubar(T/F)\n"
104 " -E|--FPS <int> Target Maximum Frames Per Second\n"
105 " =^|--shadingStyle <int> 0=Flat 1=gouraud 2=phong 3=wire\n"
106 //" -N|--nametest <string> Set name of .fwplay test file\n"
107 " -D|--DIS Allow Distributed Interactive Simulation\n"
108 " -J|--javascript <string> SM spidermonkey, DUK duktape, NONE stubs\n"
109 " -x|--boxes Draw bounding boxes\n"
110 "\nInternal options:\n"
111 " -i|--plugin <string> Called from plugin.\n"
112 " -j|--fd <number> Pipe to command the program.\n"
113 " -k|--instance <number> Instance of plugin.\n"
114 " -L|--logfile <filename> Log file where all messages should go.\n"
115#ifdef HAVE_LIBCURL
116 " -C|--curl Use libcurl instead of wget.\n"
117#endif
118 ""
119 );
120}
121
122const char * fv_validate_string_arg(const char *optarg)
123{
124 return NULL; /* TODO: implement validate_* functions */
125}
126 static struct option long_options[] = {
127
128/* { const char *name, int has_arg, int *flag, int val }, */
129
130 {"help", no_argument, 0, 'h'},
131 {"version", no_argument, 0, 'v'},
132
133 {"fullscreen", no_argument, 0, 'c'},
134 {"FPS", required_argument, 0, 'E'},
135 {"pin", required_argument, 0, 'I'},
136 {"want", required_argument, 0, 'w'},
137 {"geometry", required_argument, 0, 'g'},
138 {"big", no_argument, 0, 'b'},
139
140 {"eai", no_argument, 0, 'e'},
141 //{"fast", no_argument, 0, 'f'},
142 {"linewidth", required_argument, 0, 'W'},
143 //{"nocollision", no_argument, 0, 'Q'},
144
145 {"gif", no_argument, 0, 'p'},
146 {"snapfile", required_argument, 0, 'n'},
147 {"snaptmp", required_argument, 0, 'o'},
148
149 {"eaiverbose", no_argument, 0, 'V'},
150 {"screendist", required_argument, 0, 'r'},
151 {"eyedist", required_argument, 0, 'y'},
152 {"shutter", no_argument, 0, 'u'},
153 {"stereo", required_argument, 0, 't'},
154 {"anaglyph", required_argument, 0, 'A'},
155 {"sidebyside", no_argument, 0, 'B'},
156 {"updown", no_argument, 0, 'U'},
157 {"keypress", required_argument, 0, 'K'},
158 {"plugin", required_argument, 0, 'i'},
159 {"fd", required_argument, 0, 'j'},
160 {"instance", required_argument, 0, 'k'},
161 {"logfile", required_argument, 0, 'L'},
162
163 {"curl", no_argument, 0, 'C'},
164
165 {"display", required_argument, 0, 'd'}, /* Roberto Gerson */
166#ifdef USE_SNAPSHOT_TESTING
167 {"record", no_argument, 0, 'R'},
168 {"fixture", no_argument, 0, 'F'},
169 {"playback", no_argument, 0, 'P'},
170 {"nametest", required_argument, 0, 'N'},
171 {"testpath", required_argument, 0, 'Y'},
172#endif
173 {"colorscheme", required_argument, 0, 'G'},
174 {"colors", required_argument, 0, 'H'},
175 {"shadingStyle",required_argument,0,'^'},
176 {"DIS",no_argument,0,'D'},
177 {"javascript",required_argument,0,'J'},
178 {"boxes",no_argument,0,'x'},
179 {0, 0, 0, 0}
180 };
181
182int fv_find_opt_for_optopt(char c) {
183 int i;
184 struct option *p;
185
186 /* initialization */
187 i = 0;
188 p = &(long_options[i]);
189
190 while (p->name) {
191 if (!p->flag) {
192 if (p->val == c) {
193 return i;
194 }
195 }
196 p = &(long_options[++i]);
197 }
198 return -1;
199}
200
201//freewrl_params_t *fv_params = NULL;
202
203int fv_parseCommandLine (int argc, char **argv, freewrl_params_t *fv_params, int *url_index)
204{
205 int c, itmp;
206 float ftmp;
207 long int ldtmp;
208 int option_index = 0;
209 int real_option_index;
210 const char *real_option_name;
211 //char *logFileName = NULL;
212 //FILE *fp;
213
214 static const char optstring[] = "efg:hi:j:k:vVpn:o:bsQW:K:Xcr:y:utCL:d:RFPN:Y:DJ:x"; //':' means the preceding option requires an arguement
215
216
217 *url_index = -1;
218#if defined(_DEBUG) || defined(DEBUG)
219 for(c=0;c<argc;c++)
220 printf("argv[%d]=%s\n",c,argv[c]);
221#endif //DEBUG
222 optind = 1;
223 while (1) {
224
225 /* Do we want getopt to print errors by itself ? */
226 opterr = 0;
227
228# if HAVE_GETOPT_LONG
229
230#if defined(_MSC_VER)
231#define strncasecmp _strnicmp
232 c = _getopt_internal (argc, argv, optstring, long_options, &option_index, 0);
233#else //_MSC_VERF
234 c = getopt_long(argc, argv, optstring, long_options, &option_index);
235#endif
236# else //HAVE_GETOPT_LONG
237 c = getopt(argc, argv, optstring);
238
239# endif //HAVE_GETOPT_LONG
240#if defined(_DEBUG) || defined(DEBUG)
241 printf("c=%c argv[%d]=%s\n",c,optind,argv[optind]);
242#define DEBUG_ARGS printf
243#else
244#define DEBUG_ARGS
245#endif //DEBUG
246
247 if (c == -1)
248 break;
249
250
251 if ((c == '?')) {
252 real_option_index = fv_find_opt_for_optopt(optopt);
253 } else {
254 real_option_index = fv_find_opt_for_optopt(c);
255 }
256 if (real_option_index < 0) {
257 real_option_name = argv[optind-1];
258 } else {
259 real_option_name = long_options[real_option_index].name;
260 }
261 DEBUG_ARGS("option_index=%d optopt=%c option=%s\n", real_option_index, c,
262 real_option_name);
263
264 switch (c) {
265
266 /* Error handling */
267
268 case '?': /* getopt error: unknown option or missing argument */
269 ERROR_MSG("ERROR: unknown option or missing argument to option: %c (%s)\n",
270 c, real_option_name);
271 //fwExit(1);
272 return FALSE;
273 // break;
274
275 /* Options handling */
276
277 case 'h': /* --help, no argument */
278 fv_usage();
279 //fwExit(0);
280 return FALSE;
281 //break;
282
283 case 'v': /* --version, no argument */
284 fv_print_version();
285 //fwExit(0);
286 return FALSE;
287 //break;
288
289/* Window options */
290
291 case 'c': /* --fullscreen, no argument */
292
293// OLD_IPHONE_AQUA #if !defined(TARGET_AQUA)
294#ifdef _MSC_VER
295 fv_params->fullscreen = TRUE; //win32 will look at this in its internal code
296#else
297#if defined(HAVE_XF86_VMODE)
298 fv_params->fullscreen = TRUE;
299#else
300 printf("\nFullscreen mode is only available when xf86vmode extension is\n"
301 "supported by your X11 server: i.e. XFree86 version 4 or later,\n"
302 "Xorg version 1.0 or later.\n"
303 "Configure should autodetect it for you. If not please report"
304 "this problem to\n\t " PACKAGE_BUGREPORT "\n");
305 fv_params->fullscreen = FALSE;
306#endif /* HAVE_XF86_VMODE */
307#endif
308
309// OLD_IPHONE_AQUA #endif /* TARGET_AQUA */
310
311
312 break;
313
314 case 'g': /* --geometry, required argument: string (ex: 1024x768+100+50) */
315 if (!optarg) {
316 ERROR_MSG("Argument missing for option -g/--geometry\n");
317 //fwExit(1);
318 return FALSE;
319 } else {
320 if (!fwl_parse_geometry_string(optarg,
321 &fv_params->width, &fv_params->height,
322 &fv_params->xpos, &fv_params->ypos)) {
323 ERROR_MSG("Malformed geometry string: %s\n", optarg);
324 return FALSE;
325 }
326 }
327 break;
328
329 case 'b': /* --big, no argument */
330 fv_params->width = 800;
331 fv_params->height = 600;
332 break;
333
334 case 'd': /* --display, required argument int */
335 printf ("Parameter --display = %s\n", optarg);
336 sscanf(optarg,"%ld", (long int *)&ldtmp);
337 fv_params->winToEmbedInto = ldtmp;
338 break;
339
340
341
342/* General options */
343
344 case 'e': /* --eai, no argument */
345 fv_params->enableEAI = TRUE;
346 break;
347
348 //case 'f': /* --fast, no argument */
349 // /* does nothing right now */
350 // break;
351
352 case 'W': /* --linewidth, required argument: float */
353 sscanf(optarg,"%g", &ftmp);
354 fwl_set_LineWidth(ftmp);
355 break;
356
357 //case 'Q': /* --nocollision, no argument */
358 // //fv_params->collision = FALSE; //this is the default
359 // ConsoleMessage ("ignoring collision off mode on command line");
360 // break;
361
362/* Snapshot options */
363#ifndef FRONTEND_DOES_SNAPSHOTS
364 case 'p': /* --gif, no argument */
365 fwl_init_SnapGif();
366 break;
367
368 case 'n': /* --snapfile, required argument: string */
369 fwl_set_SnapFile(optarg);
370 break;
371
372 case 'o': /* --snaptmp, required argument: string */
373 fwl_set_SnapTmp(optarg);
374 break;
375#endif
376
377/* Misc options */
378
379 case 'V': /* --eaiverbose, no argument */
380 fwl_init_EaiVerbose();
381 fv_params->verbose = TRUE;
382 break;
383
384 case 'r': /* --screendist, required argument: float */
385 fwl_set_ScreenDist(optarg);
386 break;
387
388 case 'y': /* --eyedist, required argument: float */
389 fwl_set_EyeDist(optarg);
390 break;
391
392 case 'u': /* --shutter, no argument */
393 fwl_init_Shutter();
394 /*setXEventStereo();*/
395 break;
396
397 case 'x': /* bounding boxes */
398 fwl_setDrawBoundingBoxes(1);
399 break;
400
401 case 't': /* --stereo, required argument: float */
402 fwl_set_StereoParameter(optarg);
403 break;
404 case 'A': /* --anaglyph, required argument: string */
405 fwl_set_AnaglyphParameter(optarg);
406 break;
407
408 case 'B': /* --sidebyside, no argument */
409 fwl_init_SideBySide();
410 break;
411 case 'D': /* --DIS, no argument */
412 fwl_init_DIS();
413 break;
414
415 case 'U': /* --updown, no argument */
416 fwl_init_UpDown();
417 break;
418
419 case 'K': /* --keypress, required argument: string */
420 /* initial string of keypresses once main url is loaded */
421 fwl_set_KeyString(optarg);
422 break;
423
424 case 'G': /* --colorscheme string */
425 fwl_set_ui_colorscheme(optarg);
426 break;
427 case 'H': /* --colors string */
428 fwl_set_ui_colors(optarg);
429 break;
430
431 case 'I': /* --pin TF */
432 fwl_set_sbh_pin_option(optarg);
433 break;
434 case 'w': /* --want TF */
435 fwl_set_sbh_want_option(optarg);
436 break;
437 case '^': /* --shadingStyle 0=Flat 1=Gouraud 2=Phong 3=wire */
438 {
439 int ival = optarg[0] - '0';
440 fwl_setShadingStyle(max(min(ival,3),0));
441 }
442 break;
443 case 'E': /* --FPS, required argument: int */
444 sscanf(optarg,"%d", &itmp);
445 fwl_set_target_fps(itmp);
446 break;
447
448/* Internal options */
449
450 case 'i': /* --plugin, required argument: number */
451 sscanf(optarg,"pipe:%d",&_fw_pipe);
452 isBrowserPlugin = TRUE;
453 break;
454
455 case 'j': /* --fd, required argument: number */
456 sscanf(optarg,"%d",&_fw_browser_plugin);
457 break;
458
459 case 'k': /* --instance, required argument: number */
460 sscanf(optarg,"%u",(unsigned int *)(void *)(&_fw_instance));
461 break;
462
463 case 'L': /* --logfile, required argument: log filename */
464 if (optarg) {
465 //logFileName = strdup(optarg);
466 fwl_set_logfile(optarg);
467 } else {
468 ERROR_MSG("Option -L|--logfile: log filename required\n");
469 return FALSE;
470 }
471 break;
472 case 'J': /* --javascript, required argument: string */
473 fwl_setJsEngine(optarg);
474 break;
475
476#ifdef USE_SNAPSHOT_TESTING
477 // link to lib/main/SnapshotTesting.c
478 case 'R': /* --record, no arg */
479 fwl_set_modeRecord();
480 break;
481 case 'F': /* --fixture, no arg */
482 fwl_set_modeFixture();
483 break;
484 case 'P': /* --playback, no arg */
485 fwl_set_modePlayback();
486 break;
487 case 'N': /* --nametest, required arguement: "name_of_fwplay"*/
488 fwl_set_nameTest(optarg);
489 break;
490 case 'Y': /* --testPath directory where to put recording, playback */
491 fwl_set_testPath(optarg);
492 break;
493#endif
494
495#ifdef HAVE_LIBCURL
496 case 'C': /* --curl, no argument */
497 with_libcurl = TRUE;
498 break;
499#endif
500
501 default:
502 ERROR_MSG("ERROR: getopt returned character code 0%o, unknown error.\n", c);
503 //fwExit(1);
504 return FALSE;
505 break;
506 }
507 }
508
509 // moved to fwl_set_logfile(char*)/* Quick hack: redirect stdout and stderr to logFileName if supplied */
510 // if (logFileName) {
511 //if (strncasecmp(logFileName, "-", 1) == 0) {
512 // printf("FreeWRL: output to stdout/stderr\n");
513 //} else {
514 // printf ("FreeWRL: redirect stdout and stderr to %s\n", logFileName);
515 // fp = freopen(logFileName, "a", stdout);
516 // if (NULL == fp) {
517 // WARN_MSG("WARNING: Unable to reopen stdout to %s\n", logFileName) ;
518 // }
519 // fp = freopen(logFileName, "a", stderr);
520 // if (NULL == fp) {
521 // WARN_MSG("WARNING: Unable to reopen stderr to %s\n", logFileName) ;
522 // }
523 //}
524 // }
525
526 if (optind < argc) {
527 if (optind != (argc-1)) {
528 ERROR_MSG("FreeWRL accepts only one argument: we have %d\n", (argc-optind));
529 return FALSE;
530 }
531 DEBUG_MSG("Start url: %s\n", argv[optind]);
532 //start_url = STRDUP(argv[optind]);
533 *url_index = optind;
534 }
535
536 return TRUE;
537}
538
539void fv_parseEnvVars()
540{
541 /* Check environment */
542 fwl_set_strictParsing (getenv("FREEWRL_STRICT_PARSING") != NULL);
543 fwl_set_plugin_print (getenv("FREEWRL_DO_PLUGIN_PRINT") != NULL);
544 fwl_set_occlusion_disable (getenv("FREEWRL_NO_GL_ARB_OCCLUSION_QUERY") != NULL);
545 fwl_set_print_opengl_errors (getenv("FREEWRL_PRINT_OPENGL_ERRORS") != NULL);
546 fwl_set_trace_threads (getenv("FREEWRL_TRACE_THREADS") != NULL);
547 {
548 char *env_texture_size = getenv("FREEWRL_TEXTURE_SIZE");
549 if (env_texture_size) {
550 unsigned int local_texture_size ;
551 sscanf(env_texture_size, "%u", &local_texture_size);
552 TRACE_MSG("Env: TEXTURE SIZE %u.\n", local_texture_size);
553 fwl_set_texture_size(local_texture_size);
554 }
555 }
556}
Initialization.
Definition: libFreeWRL.h:72