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