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