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 ConsoleMessage("forcing EAI");
83 myParams.enableEAI = TRUE;
84
85 /* start threads, parse initial scene, etc */
86
87 //ConsoleMessage ("calling fwl_initFreeWRL from within fwl_OSX_initializeParameters");
88 if (!fwl_initFreeWRL(&myParams)) {
89 ERROR_MSG("main: aborting during initialization.\n");
90 exit(1);
91 }
92 }
93
94 res = resource_create_single(initialURL);
95
96 new_root();
97
98 send_resource_to_parser_async(res);
99
100 while ((!res->complete) && (res->status != ress_failed)
101 && (res->status != ress_not_loaded)) {
102 usleep(100);
103 }
104
105 /* did this load correctly? */
106 if (res->status == ress_not_loaded) {
107 sprintf(consoleBuffer, "FreeWRL: Problem loading file \"%s\"",
108 res->URLrequest);
109 fwl_StringConsoleMessage(consoleBuffer);
110 }
111
112 if (res->status == ress_failed) {
113 printf("load failed %s\n", initialURL);
114 sprintf(consoleBuffer, "FreeWRL: unknown data on command line: \"%s\"",
115 res->URLrequest);
116 fwl_StringConsoleMessage(consoleBuffer);
117 } else {
118
119 /* tell the new world which viewpoint to go to */
120 if (res->afterPoundCharacters != NULL) {
121 fwl_gotoViewpoint(res->afterPoundCharacters);
122 /* Success!
123 printf("loaded %s\n", initialURL); */
124 }
125
126 }
127/*
128 if (tg->ProdCon._frontEndOnX3DFileLoadedListener) {
129 char *URLRequest = STRDUP(res->URLrequest);
130 tg->ProdCon._frontEndOnX3DFileLoadedListener(URLRequest);
131 }
132 */
133}
134
135#endif // _ANDROID
136
137
138
139
140/* OSX plugin is telling us the id to refer to */
141void setInstance(uintptr_t instance) {
142 /* printf ("setInstance, setting to %u\n",instance); */
143 _fw_instance = instance;
144}
145
146/* osx Safari plugin is telling us where the initial file is */
147void setFullPath(const char* file) {
148 /* turn collision on?
149 if (!fwl_getp_collision()) {
150 char ks = 'c';
151 do_keyPress(ks, KeyPress);
152 }
153 */
154
155 /* remove a FILE:// or file:// off of the front */
156 file = stripLocalFileName((char *) file);
157 FREE_IF_NZ(BrowserFullPath);
158 BrowserFullPath = STRDUP((char *) file);
159 /*
160 sprintf(consoleBuffer , "setBrowserFullPath is %s (%d)",BrowserFullPath,strlen(BrowserFullPath));
161 fwl_StringConsoleMessage(consoleBuffer);
162 */
163}
164
165char *strForeslash2back(char *str) {
166#ifdef _MSC_VER
167 int jj;
168 for( jj=0;jj<(int)strlen(str);jj++)
169 if(str[jj] == '/' ) str[jj] = '\\';
170#endif
171 return str;
172}
173
174void* fwl_init_instance() {
175
176 ttglobal tg;
177
178 fwl_setCurrentHandle(NULL, __FILE__, __LINE__); //added aug 29, 2015
179 /* commented aug 29, 2015
180 tg = gglobal0();
181 if (NULL != tg)
182 {
183 fwl_doQuitInstance(tg); //what scenario was this for? anchor? browser plugin backbutton + forebutton? Do we stil need it? Aug 29, 2015
184 }
185 */
186
187 //ConsoleMessage ("called fwl_init_instance");
188
189 tg = iglobal_constructor();
190
191 fwl_setCurrentHandle(tg, __FILE__, __LINE__);
192 return (void *) tg;
193}
194
195bool fwl_initFreeWRL(freewrl_params_t *params) {
196 ttglobal tg;
197 tg = (ttglobal) fwl_getCurrentHandle(__FILE__, __LINE__);
198 //ConsoleMessage ("fwl_initFreeWRL, tg %p params %p where %s\n",tg,params,where);
199
200 if (tg == NULL)
201 tg = fwl_init_instance();
202 TRACE_MSG("FreeWRL: initializing...\n");
203
204 //ConsoleMessage ("fwl_initFreeWRL, mainThread %p",tg->threads.mainThread);
205
206 tg->threads.mainThread = pthread_self();
207
208 /* dug9 Aug 23, 2013
209 For the main UI thread that's shared between multiple
210 libfreewrl instances within a single process
211 -ie 2 ActiveX controls on a single web page or gui app, or
212 a console program that pops up 2 separate freewrl instances-
213 we use fwl_setCurrentHandle(ttglobal) from the calling application
214 process to switch gglobals for this UI/main thread.
215 For the worker threads, we lookup their ttglobal based on their
216 threadID.
217 */
218#if !defined(_ANDROID)
219 /* Android does not have stdout nor stderr */
220 /* Initialize console (log, error, ...) */
221 setbuf(stdout,0);
222 setbuf(stderr,0);
223#endif
224 /* Check parameters */
225 if (params) {
226 DEBUG_MSG("copying application supplied params...\n");
227 memcpy(tg->display.params, params, sizeof(freewrl_params_t));
228 //tg->display.win_height = params->height;// = 0; /* window */
229 //tg->display.win_width = params->width;// = 0;
230 //tg->display.winToEmbedInto = params->winToEmbedInto;// = -1;
231 //tg->display.fullscreen = params->fullscreen;// = FALSE;
232 }
233
234#if !defined(EXCLUDE_EAI)
235 /* do we require EAI? */
236 if (params->enableEAI) {
237 fwlio_RxTx_control(CHANNEL_EAI, RxTx_START);
238 // set_thread2global(tglobal* fwl, pthread_t any );
239
240 }
241#endif
242
243 /* Initialize parser */
244 fwl_initialize_parser();
245 fwl_initializeInputParseThread();
246 fwl_initializeTextureThread();
247
248 return TRUE;
249}
250
251
258void splitpath_local_suffix(const char *url, char **local_name, char **suff) {
259 //takes a http or file path, and gives back just the scene name and suffix
260 //ie file://E:/tests/1.wrl -> local_name = "1" suff = "wrl"
261 *local_name = NULL;
262 *suff = NULL;
263 if (url) {
264 int i, len;
265 char *localname;
266 len = (int) strlen(url);
267 localname = NULL;
268 for (i = len - 1; i >= 0; i--) {
269 if (url[i] == '/')
270 break;
271 localname = (char*) &url[i];
272 }
273 if (localname) {
274 *local_name = STRDUP(localname);
275 localname = *local_name;
276 len = (int) strlen(localname);
277 *suff = NULL;
278 for (i = len - 1; i >= 0; i--) {
279 if (localname[i] == '.') {
280 localname[i] = '\0';
281 *suff = STRDUP(&localname[i+1]);
282 break;
283 }
284 }
285 }
286 }
287}
288void splitpath3(const char* url, char** folder, char** local_name, char** suff) {
289 //takes a http or file path, and gives back just the scene name and suffix
290 //ie file://E:/tests/1.wrl -> local_name = "1" suff = "wrl"
291 *local_name = NULL;
292 *suff = NULL;
293 if (url) {
294 int i, len;
295 char* localname;
296 len = (int)strlen(url);
297 localname = NULL;
298 for (i = len - 1; i >= 0; i--) {
299 if (url[i] == '/')
300 break;
301 localname = (char*)&url[i];
302 }
303 *folder = strndup(url, i);
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
320int checkExitRequest();
321
322
326void closeFreeWRL() {
327}