FreeWRL/FreeX3D  3.0.0
main.c
1 /*
2 
3  FreeWRL main program.
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 // OLD_IPHONE_AQUA #if !defined(TARGET_AQUA)
28 
29 
30 #include <config.h>
31 #include <system.h>
32 #include <internal.h>
33 
34 #include <libFreeWRL.h>
35 
36 #include "../lib/main/MainLoop.h"
37 
38 #include "main.h"
39 #include "options.h"
40 #ifdef _MSC_VER
41 #include "getopt.h"
42 #endif
43 
48 /* file/url to start FreeWRL with */
49 char *start_url;
50 
54 static int CaughtSEGV = FALSE;
55 void fv_catch_SIGQUIT();
56 void fv_catch_SIGSEGV();
57 
58 #if !defined(CYGWIN)
59 void fv_catch_SIGALRM(int);
60 void fv_catch_SIGHUP();
61 #endif
62 
63 #if defined(_MSC_VER)
64 #include <shlwapi.h>
65 char *get_current_dir();
66 char *strBackslash2fore(char *str);
67 
68 #endif
69 
70 void fwExit(int iret){
71  //printf("press Enter to exit:");
72  //getchar();
73  //exit(iret);
74  fw_exit(iret); //in libfreewrl
75 }
76 void fwg_register_consolemessage_callback(void(*callback)(char *));
77 void fw_printstring(char *str){
78  printf("%s",str);
79 }
83 int main (int argc, char **argv)
84 {
85  const char *libver;
86  const char *progver;
87 
88 //#if defined(_ANDROID)
89 // int tempIsAndroid = 1 ;
90 //#else
91 // int tempIsAndroid = 0 ;
92 //#endif
93 
94  freewrl_params_t *fv_params = NULL;
95 
96  char consoleBuffer[200];
97  fwl_init_instance(); //before setting any structs we need a struct allocated
98  fwg_register_consolemessage_callback(fw_printstring);
99  fwg_setConsoleParam_maxLines(30);
100  fwg_setConsoleParam_maxLineLength(70);
101  fwg_setConsoleParam_replaceTabs(1);
102 
103  /* first, get the FreeWRL shared lib, and verify the version. */
104  libver = libFreeWRL_get_version();
105  progver = freewrl_get_version();
106  if (strcmp(progver, libver)) {
107  sprintf(consoleBuffer ,"FreeWRL expected library version %s, got %s...\n",progver, libver);
108  fwl_StringConsoleMessage(consoleBuffer);
109  }else{
110  sprintf(consoleBuffer, "libfreewrl version %s\n", libver);
111  fwl_StringConsoleMessage(consoleBuffer);
112  }
113 
114 #ifdef _MSC_VER
115  /*
116  Set fonts directory
117  ideally we would check if we are in a) projectfiles go ../../fonts b) else c:/windows/Fonts
118  */
119  if(strstr(argv[0],"projectfiles"))
120  {
121  /* we are testing - use local fonts (may be obsolete someday) - and Vera might not be installed in C:/windows/fonts */
122  static char *fdir;
123  char *xdir, *xe;
124  fdir = malloc(MAX_PATH);
125  xdir = malloc(MAX_PATH);
126  strcpy(fdir,"FREEWRL_FONTS_DIR=");
127  //strcat(fdir,"C:/Windows/Fonts");
128  strcpy(xdir,argv[0]);
129  xe = strstr(xdir,"projectfiles");
130  if(xe) xe[0] = '\0';
131  xdir = strcat(xdir,"appleOSX/OSX_Specific/fonts");
132  strcat(fdir,xdir);
133  _putenv( fdir );
134  }
135  else
136  {
137  /* deployed system (with intalled fonts) - use system fonts
138  we plan to use a professional installer to install the fonts to %windir%\Fonts directory
139  where all the system fonts already are.
140  Then in this program we will get the %windir%\Fonts directory, and set it as temporary
141  environment variable for InputFunctions.C > makeFontsDirectory() to fetch.
142  */
143  static char *fdir;
144  char *syspath;
145  syspath = getenv("windir");
146  //printf("windir path=[%s]\n",syspath);
147  fdir = malloc(MAX_PATH);
148  strcpy(fdir,"FREEWRL_FONTS_DIR=");
149  strcat(fdir,syspath);
150  strcat(fdir,"/Fonts");
151  _putenv( fdir );
152  }
153  //get_current_dir();
154  /* VBO preference - comment out for vbos (vertex buffer objects - a rendering optimization) */
155  _putenv("FREEWRL_NO_VBOS=1");
156  //_putenv("FREEWRL_USE_VBOS=1");
157 
158 
159 #endif
160 
161  /* install the signal handlers
162  win32 > for debugging its better not to install them: the debugger will then take you to the crash line
163  if signal handlers are installed, then all you see is a SIGSEG printf
164  */
165 #ifdef WANT_SIGNALHANDLERS
166  signal(SIGTERM, (void(*)(int)) fv_catch_SIGQUIT);
167  signal(SIGSEGV, (void(*)(int)) fv_catch_SIGSEGV);
168 
169  #if !defined(CYGWIN)
170  signal(SIGQUIT, (void(*)(int)) fv_catch_SIGQUIT);
171  signal(SIGALRM, (void(*)(int)) fv_catch_SIGALRM);
172  signal(SIGHUP, (void(*)(int)) fv_catch_SIGHUP);
173  #endif
174 #endif
175  /* Before we parse the command line, setup the FreeWRL default parameters */
176  fv_params = calloc(1, sizeof(freewrl_params_t));
177 
178  /* Default values */
179  fv_params->width = 640;
180  fv_params->height = 480;
181 
182  fv_params->fullscreen = FALSE;
183  fv_params->winToEmbedInto = INT_ID_UNDEFINED;
184  fv_params->verbose = FALSE;
185  // fv_params->collision = 1; // if you set it, you need to update ui button with a call
186  //setMenuButton_collision(fv_params->collision);
187  //fwl_init_StereoDefaults();
188 
189  /* parse command line arguments */
190  if (fv_parseCommandLine(argc, argv,fv_params)) {
191  if(argc > 1){
192  start_url = argv[optind];
193 #ifdef _MSC_VER
194  if(start_url)
195  start_url = strBackslash2fore(start_url);
196 #endif
197  }else{
198  start_url = NULL;
199  }
200  }
201 
202 
203  /* doug- redirect stdout to a file - works, useful for sending bug reports */
204  /*freopen("freopen.txt", "w", stdout ); */
205 
206  /* Put env parse here, because this gives systems that do not have env vars the chance to do it their own way. */
207  fv_parseEnvVars();
208  /* start threads, parse initial scene, etc */
209  if ( 1 ) {
210  /* give control to the library */
211  if (!fwl_initFreeWRL(fv_params)) {
212  ERROR_MSG("main: aborting during initialization.\n");
213  fwExit(1);
214  }
215  fwl_startFreeWRL(start_url);
216  } else {
217  /* keep control
218  if (!fv_initFreeWRL(fv_params)) {
219  ERROR_MSG("main: aborting during initialization.\n");
220  fwExit(1);
221  }
222  fv_startFreeWRL(start_url); */
223  }
224  return 0;
225 }
226 /* #include "src/bin/main-inc.c" */
227 
228 /* SIGQUIT handler - plugin code sends a SIGQUIT... */
229 void fv_catch_SIGQUIT()
230 {
231  /* fwl_StringConsoleMessage("FreeWRL got a sigquit signal"); */
232  /* shut up any SIGSEGVs we might get now. */
233  CaughtSEGV = TRUE;
234  //fwl_doQuit();
235  fwExit(-2);
236 }
237 
238 void fv_catch_SIGSEGV()
239 {
240  if (!CaughtSEGV) {
241 
242  //fwl_StringConsoleMessage("FreeWRL got a SIGSEGV - please feel free to report this - http://freewrl.sf.net.\n");
243  //bombs H: its coming in on an annonymous thread and console message needs TSD (thread-specific-data via gglobal)
244  printf("FreeWRL got a SIGSEGV - please feel free to report this - http://freewrl.sf.net.\n");
245  CaughtSEGV = TRUE;
246  }
247  fwExit(1);
248 }
249 
250 #if !defined(CYGWIN)
251 
252 void fv_catch_SIGHUP()
253 {
254  /* fwl_StringConsoleMessage ("FreeWRL got a SIGHUP signal - reloading"); */
255  /* MBFILES Anchor_ReplaceWorld(BrowserFullPath); */
256 }
257 
258 void fv_catch_SIGALRM(int sig)
259 {
260  signal(SIGALRM, SIG_IGN);
261 
262  /* stuffs to do on alarm */
263  /* fprintf(stderr,"An alarm signal just arrived ...IT WAS IGNORED!\n"); */
264  /* end of alarm actions */
265 
266 #if defined(_MSC_VER)
267  printf("\a");
268 #else
269  alarm(0);
270 #endif
271  signal(SIGALRM, fv_catch_SIGALRM);
272 }
273 
274 #endif
275 
276 // OLD_IPHONE_AQUA #endif // AQUA
Initialization.
Definition: libFreeWRL.h:71