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