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
47char consoleBuffer[200];
48
49void new_root();
50// OLD_IPHONE_AQUA #if defined (TARGET_AQUA) || defined(_ANDROID)
51#if defined(TARGET_AQUA) || 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
56void 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
140#endif // _ANDROID
141
142
143
144
145/* OSX plugin is telling us the id to refer to */
146void setInstance(uintptr_t instance) {
147 /* printf ("setInstance, setting to %u\n",instance); */
148 _fw_instance = instance;
149}
150
151/* osx Safari plugin is telling us where the initial file is */
152void setFullPath(const char* file) {
153 /* turn collision on?
154 if (!fwl_getp_collision()) {
155 char ks = 'c';
156 do_keyPress(ks, KeyPress);
157 }
158 */
159
160 /* remove a FILE:// or file:// off of the front */
161 file = stripLocalFileName((char *) file);
162 FREE_IF_NZ(BrowserFullPath);
163 BrowserFullPath = STRDUP((char *) file);
164 /*
165 sprintf(consoleBuffer , "setBrowserFullPath is %s (%d)",BrowserFullPath,strlen(BrowserFullPath));
166 fwl_StringConsoleMessage(consoleBuffer);
167 */
168}
169
170char *strForeslash2back(char *str) {
171#ifdef _MSC_VER
172 int jj;
173 for( jj=0;jj<(int)strlen(str);jj++)
174 if(str[jj] == '/' ) str[jj] = '\\';
175#endif
176 return str;
177}
178
179#ifdef OLDCODE
180
181Sept 23 2013
182With Doug Sanden (correctly) moving FreeWRL to multi-invocation, the global parameter "fwl_params"
183is now local, and options are set within this.
184
185OLDCODEvoid fwl_setp_width (int foo) { fwl_params.width = foo; }
186OLDCODEvoid fwl_setp_height (int foo) { fwl_params.height = foo; }
187OLDCODEvoid fwl_setp_winToEmbedInto (void* foo) { fwl_params.winToEmbedInto = foo; }
188OLDCODEvoid fwl_setp_fullscreen (bool foo) { fwl_params.fullscreen = foo; }
189OLDCODEvoid fwl_setp_multithreading (bool foo) { fwl_params.multithreading = foo; }
190OLDCODEvoid fwl_setp_eai (bool foo) { fwl_params.enableEAI = foo; }
191OLDCODEvoid fwl_setp_verbose (bool foo) { fwl_params.verbose = foo; }
192OLDCODE//void fwl_setp_collision (int foo) { fwl_params.collision = foo; }
193OLDCODE
194OLDCODEint fwl_getp_width (void) { return fwl_params.width; }
195OLDCODEint fwl_getp_height (void) { return fwl_params.height; }
196OLDCODElong int fwl_getp_winToEmbedInto (void) { return fwl_params.winToEmbedInto; }
197OLDCODEbool fwl_getp_fullscreen (void) { return fwl_params.fullscreen; }
198OLDCODEbool fwl_getp_multithreading (void) { return fwl_params.multithreading; }
199OLDCODEbool fwl_getp_eai (void) { return fwl_params.enableEAI; }
200OLDCODEbool fwl_getp_verbose (void) { return fwl_params.verbose; }
201OLDCODE//int fwl_getp_collision (void) { return fwl_params.collision; }
202OLDCODE
203OLDCODE//static ttglobal fwl_instance_parameters = NULL;
204#endif //OLDCODE
205
206void* fwl_init_instance() {
207
208 ttglobal tg;
209
210 fwl_setCurrentHandle(NULL, __FILE__, __LINE__); //added aug 29, 2015
211 /* commented aug 29, 2015
212 tg = gglobal0();
213 if (NULL != tg)
214 {
215 fwl_doQuitInstance(tg); //what scenario was this for? anchor? browser plugin backbutton + forebutton? Do we stil need it? Aug 29, 2015
216 }
217 */
218
219 //ConsoleMessage ("called fwl_init_instance");
220
221 tg = iglobal_constructor();
222
223 fwl_setCurrentHandle(tg, __FILE__, __LINE__);
224 return (void *) tg;
225}
226
227bool fwl_initFreeWRL(freewrl_params_t *params) {
228 ttglobal tg;
229 tg = (ttglobal) fwl_getCurrentHandle(__FILE__, __LINE__);
230 //ConsoleMessage ("fwl_initFreeWRL, tg %p params %p where %s\n",tg,params,where);
231
232 if (tg == NULL)
233 tg = fwl_init_instance();
234 TRACE_MSG("FreeWRL: initializing...\n");
235
236 //ConsoleMessage ("fwl_initFreeWRL, mainThread %p",tg->threads.mainThread);
237
238 tg->threads.mainThread = pthread_self();
239
240 /* dug9 Aug 23, 2013
241 For the main UI thread that's shared between multiple
242 libfreewrl instances within a single process
243 -ie 2 ActiveX controls on a single web page or gui app, or
244 a console program that pops up 2 separate freewrl instances-
245 we use fwl_setCurrentHandle(ttglobal) from the calling application
246 process to switch gglobals for this UI/main thread.
247 For the worker threads, we lookup their ttglobal based on their
248 threadID.
249 */
250#if !defined(_ANDROID)
251 /* Android does not have stdout nor stderr */
252 /* Initialize console (log, error, ...) */
253 setbuf(stdout,0);
254 setbuf(stderr,0);
255#endif
256 /* Check parameters */
257 if (params) {
258 DEBUG_MSG("copying application supplied params...\n");
259 memcpy(tg->display.params, params, sizeof(freewrl_params_t));
260 //tg->display.win_height = params->height;// = 0; /* window */
261 //tg->display.win_width = params->width;// = 0;
262 //tg->display.winToEmbedInto = params->winToEmbedInto;// = -1;
263 //tg->display.fullscreen = params->fullscreen;// = FALSE;
264 }
265
266#if !defined(EXCLUDE_EAI)
267 /* do we require EAI? */
268 if (params->enableEAI) {
269 fwlio_RxTx_control(CHANNEL_EAI, RxTx_START);
270 // set_thread2global(tglobal* fwl, pthread_t any );
271
272 }
273#endif
274
275 /* Initialize parser */
276 fwl_initialize_parser();
277 fwl_initializeInputParseThread();
278 fwl_initializeTextureThread();
279
280 return TRUE;
281}
282
283
290void splitpath_local_suffix(const char *url, char **local_name, char **suff) {
291 //takes a http or file path, and gives back just the scene name and suffix
292 //ie file://E:/tests/1.wrl -> local_name = "1" suff = "wrl"
293 *local_name = NULL;
294 *suff = NULL;
295 if (url) {
296 int i, len;
297 char *localname;
298 len = (int) strlen(url);
299 localname = NULL;
300 for (i = len - 1; i >= 0; i--) {
301 if (url[i] == '/')
302 break;
303 localname = (char*) &url[i];
304 }
305 if (localname) {
306 *local_name = STRDUP(localname);
307 localname = *local_name;
308 len = (int) strlen(localname);
309 *suff = NULL;
310 for (i = len - 1; i >= 0; i--) {
311 if (localname[i] == '.') {
312 localname[i] = '\0';
313 *suff = STRDUP(&localname[i+1]);
314 break;
315 }
316 }
317 }
318 }
319}
320
321int checkExitRequest();
322
323
327void closeFreeWRL() {
328}
Initialization.
Definition: libFreeWRL.h:72