FreeWRL/FreeX3D  3.0.0
main.c
1 /*
2 
3  FreeWRL support library.
4  Resources handling: URL, files, ...
5 
6  */
7 
8 /****************************************************************************
9  This file is part of the FreeWRL/FreeX3D Distribution.
10 
11  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
12 
13  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
14  it under the terms of the GNU Lesser Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
25  ****************************************************************************/
26 
27 
28 
29 #include <config.h>
30 #include <system.h>
31 #include <system_threads.h>
32 #include <display.h>
33 #include <internal.h>
34 #include <signal.h>
35 #include <libFreeWRL.h>
36 #include <list.h>
37 #include <io_files.h>
38 #include <threads.h>
39 #include <libFreeWRL.h>
40 
41 #include "vrml_parser/Structs.h"
42 #include "main/ProdCon.h"
43 #include "input/InputFunctions.h"
44 
45 #include "ui/common.h"
46 
47 char consoleBuffer[200];
48 
49 void new_root();
50 // OLD_IPHONE_AQUA #if defined (TARGET_AQUA) || defined(_ANDROID)
51 #if defined(_ANDROID)
52 
53 /* put some config stuff here, as that way the Objective-C Standalone OSX front end does not
54  need to worry about specific structures and calls */
55 
56 void fwl_OSX_initializeParameters(const char* initialURL) {
57  resource_item_t *res;
58  freewrl_params_t myParams;
59 
60  ttglobal tg = gglobal();
61 
62  //printf ("fwl_OSX_initializeParameters, sending in %s\n",initialURL);
63 
64  /* have we been through once already (eg, plugin loading new file)? */
65 
66  //ConsoleMessage("fwl_OSX_initializeParameters - loadThread %p, pcThread %p", tg->threads.loadThread, tg->threads.PCthread);
67  if ((tg->threads.loadThread == 0 ) || (tg->threads.PCthread == 0 )) {
68  //ConsoleMessage("fwl_OSX_initializeParameters, qParamsInit is FALSE");
69 
70  myParams.width = 600;
71  myParams.height = 400;
72  myParams.xpos = 0;
73  myParams.ypos = 0;
74  myParams.winToEmbedInto = INT_ID_UNDEFINED;
75  myParams.fullscreen = FALSE;
76  myParams.multithreading = TRUE;
77  myParams.enableEAI = FALSE;
78  myParams.verbose = FALSE;
79 
80  /* Default values */
81 #ifdef OLDCODE
82  OLDCODE fwl_setp_height(400);
83  OLDCODE fwl_setp_eai(FALSE);
84  OLDCODE fwl_setp_fullscreen(FALSE);
85 
86 #endif //OLDCODE
87  ConsoleMessage("forcing EAI");
88  myParams.enableEAI = TRUE;
89 
90  /* start threads, parse initial scene, etc */
91 
92  //ConsoleMessage ("calling fwl_initFreeWRL from within fwl_OSX_initializeParameters");
93  if (!fwl_initFreeWRL(&myParams)) {
94  ERROR_MSG("main: aborting during initialization.\n");
95  exit(1);
96  }
97  }
98 
99  res = resource_create_single(initialURL);
100 
101  new_root();
102 
103  send_resource_to_parser_async(res);
104 
105  while ((!res->complete) && (res->status != ress_failed)
106  && (res->status != ress_not_loaded)) {
107  usleep(100);
108  }
109 
110  /* did this load correctly? */
111  if (res->status == ress_not_loaded) {
112  sprintf(consoleBuffer, "FreeWRL: Problem loading file \"%s\"",
113  res->URLrequest);
114  fwl_StringConsoleMessage(consoleBuffer);
115  }
116 
117  if (res->status == ress_failed) {
118  printf("load failed %s\n", initialURL);
119  sprintf(consoleBuffer, "FreeWRL: unknown data on command line: \"%s\"",
120  res->URLrequest);
121  fwl_StringConsoleMessage(consoleBuffer);
122  } else {
123 
124  /* tell the new world which viewpoint to go to */
125  if (res->afterPoundCharacters != NULL) {
126  fwl_gotoViewpoint(res->afterPoundCharacters);
127  /* Success!
128  printf("loaded %s\n", initialURL); */
129  }
130 
131  }
132 
133  if (tg->ProdCon._frontEndOnX3DFileLoadedListener) {
134  char *URLRequest = STRDUP(res->URLrequest);
135  tg->ProdCon._frontEndOnX3DFileLoadedListener(URLRequest);
136  }
137 }
138 
139 #endif // _ANDROID
140 
141 
142 
143 
144 /* OSX plugin is telling us the id to refer to */
145 void setInstance(uintptr_t instance) {
146  /* printf ("setInstance, setting to %u\n",instance); */
147  _fw_instance = instance;
148 }
149 
150 /* osx Safari plugin is telling us where the initial file is */
151 void setFullPath(const char* file) {
152  /* turn collision on?
153  if (!fwl_getp_collision()) {
154  char ks = 'c';
155  do_keyPress(ks, KeyPress);
156  }
157  */
158 
159  /* remove a FILE:// or file:// off of the front */
160  file = stripLocalFileName((char *) file);
161  FREE_IF_NZ(BrowserFullPath);
162  BrowserFullPath = STRDUP((char *) file);
163  /*
164  sprintf(consoleBuffer , "setBrowserFullPath is %s (%d)",BrowserFullPath,strlen(BrowserFullPath));
165  fwl_StringConsoleMessage(consoleBuffer);
166  */
167 }
168 
169 char *strForeslash2back(char *str) {
170 #ifdef _MSC_VER
171  int jj;
172  for( jj=0;jj<(int)strlen(str);jj++)
173  if(str[jj] == '/' ) str[jj] = '\\';
174 #endif
175  return str;
176 }
177 
178 #ifdef OLDCODE
179 
180 Sept 23 2013
181 With Doug Sanden (correctly) moving FreeWRL to multi-invocation, the global parameter "fwl_params"
182 is now local, and options are set within this.
183 
184 OLDCODEvoid fwl_setp_width (int foo) { fwl_params.width = foo; }
185 OLDCODEvoid fwl_setp_height (int foo) { fwl_params.height = foo; }
186 OLDCODEvoid fwl_setp_winToEmbedInto (void* foo) { fwl_params.winToEmbedInto = foo; }
187 OLDCODEvoid fwl_setp_fullscreen (bool foo) { fwl_params.fullscreen = foo; }
188 OLDCODEvoid fwl_setp_multithreading (bool foo) { fwl_params.multithreading = foo; }
189 OLDCODEvoid fwl_setp_eai (bool foo) { fwl_params.enableEAI = foo; }
190 OLDCODEvoid fwl_setp_verbose (bool foo) { fwl_params.verbose = foo; }
191 OLDCODE//void fwl_setp_collision (int foo) { fwl_params.collision = foo; }
192 OLDCODE
193 OLDCODEint fwl_getp_width (void) { return fwl_params.width; }
194 OLDCODEint fwl_getp_height (void) { return fwl_params.height; }
195 OLDCODElong int fwl_getp_winToEmbedInto (void) { return fwl_params.winToEmbedInto; }
196 OLDCODEbool fwl_getp_fullscreen (void) { return fwl_params.fullscreen; }
197 OLDCODEbool fwl_getp_multithreading (void) { return fwl_params.multithreading; }
198 OLDCODEbool fwl_getp_eai (void) { return fwl_params.enableEAI; }
199 OLDCODEbool fwl_getp_verbose (void) { return fwl_params.verbose; }
200 OLDCODE//int fwl_getp_collision (void) { return fwl_params.collision; }
201 OLDCODE
202 OLDCODE//static ttglobal fwl_instance_parameters = NULL;
203 #endif //OLDCODE
204 
205 void* fwl_init_instance() {
206 
207  ttglobal tg;
208 
209  fwl_setCurrentHandle(NULL, __FILE__, __LINE__); //added aug 29, 2015
210  /* commented aug 29, 2015
211  tg = gglobal0();
212  if (NULL != tg)
213  {
214  fwl_doQuitInstance(tg); //what scenario was this for? anchor? browser plugin backbutton + forebutton? Do we stil need it? Aug 29, 2015
215  }
216  */
217 
218  //ConsoleMessage ("called fwl_init_instance");
219 
220  tg = iglobal_constructor();
221 
222  fwl_setCurrentHandle(tg, __FILE__, __LINE__);
223  return (void *) tg;
224 }
225 
226 bool fwl_initFreeWRL(freewrl_params_t *params) {
227  ttglobal tg;
228  tg = (ttglobal) fwl_getCurrentHandle(__FILE__, __LINE__);
229  //ConsoleMessage ("fwl_initFreeWRL, tg %p params %p where %s\n",tg,params,where);
230 
231  if (tg == NULL)
232  tg = fwl_init_instance();
233  TRACE_MSG("FreeWRL: initializing...\n");
234 
235  //ConsoleMessage ("fwl_initFreeWRL, mainThread %p",tg->threads.mainThread);
236 
237  tg->threads.mainThread = pthread_self();
238 
239  /* dug9 Aug 23, 2013
240  For the main UI thread that's shared between multiple
241  libfreewrl instances within a single process
242  -ie 2 ActiveX controls on a single web page or gui app, or
243  a console program that pops up 2 separate freewrl instances-
244  we use fwl_setCurrentHandle(ttglobal) from the calling application
245  process to switch gglobals for this UI/main thread.
246  For the worker threads, we lookup their ttglobal based on their
247  threadID.
248  */
249 #if !defined(_ANDROID)
250  /* Android does not have stdout nor stderr */
251  /* Initialize console (log, error, ...) */
252  setbuf(stdout,0);
253  setbuf(stderr,0);
254 #endif
255  /* Check parameters */
256  if (params) {
257  DEBUG_MSG("copying application supplied params...\n");
258  memcpy(tg->display.params, params, sizeof(freewrl_params_t));
259  //tg->display.win_height = params->height;// = 0; /* window */
260  //tg->display.win_width = params->width;// = 0;
261  //tg->display.winToEmbedInto = params->winToEmbedInto;// = -1;
262  //tg->display.fullscreen = params->fullscreen;// = FALSE;
263  }
264 
265 #if !defined(EXCLUDE_EAI)
266  /* do we require EAI? */
267  if (params->enableEAI) {
268  fwlio_RxTx_control(CHANNEL_EAI, RxTx_START);
269  // set_thread2global(tglobal* fwl, pthread_t any );
270 
271  }
272 #endif
273 
274  /* Initialize parser */
275  fwl_initialize_parser();
276  fwl_initializeInputParseThread();
277  fwl_initializeTextureThread();
278 
279  return TRUE;
280 }
281 
282 
289 void splitpath_local_suffix(const char *url, char **local_name, char **suff) {
290  //takes a http or file path, and gives back just the scene name and suffix
291  //ie file://E:/tests/1.wrl -> local_name = "1" suff = "wrl"
292  *local_name = NULL;
293  *suff = NULL;
294  if (url) {
295  int i, len;
296  char *localname;
297  len = (int) strlen(url);
298  localname = NULL;
299  for (i = len - 1; i >= 0; i--) {
300  if (url[i] == '/')
301  break;
302  localname = (char*) &url[i];
303  }
304  if (localname) {
305  *local_name = STRDUP(localname);
306  localname = *local_name;
307  len = (int) strlen(localname);
308  *suff = NULL;
309  for (i = len - 1; i >= 0; i--) {
310  if (localname[i] == '.') {
311  localname[i] = '\0';
312  *suff = STRDUP(&localname[i+1]);
313  break;
314  }
315  }
316  }
317  }
318 }
319 
320 int checkExitRequest();
321 
322 
326 void closeFreeWRL() {
327 }
Initialization.
Definition: libFreeWRL.h:71