FreeWRL/FreeX3D  3.0.0
internal.h
1 /*
2 
3 
4 ???
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 //<<<<<<< internal.h
28 #include <config.h>
29 #include <system.h>
30 #include <display.h>
31 
32 #include <libFreeWRL.h>
33 #include <list.h>
34 #include <resources.h>
35 #include <io_files.h>
36 
37 
38 #include "vrml_parser/Structs.h"
39 #include "main/headers.h"
40 #include "vrml_parser/CParseGeneral.h"
41 #include "scenegraph/Vector.h"
42 #include "vrml_parser/CFieldDecls.h"
43 #include "world_script/JScript.h"
44 #include "world_script/CScripts.h"
45 #include "world_script/fieldSet.h"
46 #include "vrml_parser/CParseParser.h"
47 #include "vrml_parser/CParseLexer.h"
48 #include "vrml_parser/CParse.h"
49 #include "input/InputFunctions.h" /* resolving implicit declarations */
50 #include "input/EAIHeaders.h" /* resolving implicit declarations */
51 #include "input/EAIHelpers.h" /* resolving implicit declarations */
52 
53 #include "x3d_parser/X3DParser.h"
54 #include <iglobal.h>
55 
56 //#ifndef STATIC_ONCE
57 //static int currentProtoDeclare = INT_ID_UNDEFINED;
58 //static int MAXProtos = 0;
59 //static int curProDecStackInd = 0;
61 //static int currentProtoInstance[PROTOINSTANCE_MAX_LEVELS];
62 //#else
63 //extern int currentProtoDeclare;
64 //extern int MAXProtos;
65 //extern int curProDecStackInd;
66 //static int currentProtoInstance = INT_ID_UNDEFINED;
67 //extern int currentProtoInstance[PROTOINSTANCE_MAX_LEVELS];
68 //#endif
69 #define STATIC_ONCE 1
70 
71 //#define CPI ProtoInstanceTable[curProtoInsStackInd]
72 //#define CPD PROTONames[currentProtoDeclare]
73 
75 //#define MP_NAME 0
76 //#define MP_ACCESSTYPE 1
77 //#define MP_TYPE 2
78 //#define MP_VALUE 3
79 //#define MPFIELDS 4 /* MUST be the highest MP* plus one - array size */
80 //
81 //#define UNIQUE_NUMBER_HOLDER "-fReeWrl-UniqueNumH"
82 
83 /* ProtoInstance table This table is a dynamic table that is used for keeping track of ProtoInstance field values... */
84 //#ifndef CURPROINSTSTACK
85 //static int curProtoInsStackInd = -1;
86 //#define CURPROINSTSTACK 1
87 //
88 //struct PROTOInstanceEntry {
89 // char *name[PROTOINSTANCE_MAX_PARAMS];
90 // char *value[PROTOINSTANCE_MAX_PARAMS];
91 // int type[PROTOINSTANCE_MAX_PARAMS]; //0-string 1-itoa(DEF index) 10-(FIELDTYPE_SFNODE) union anyVrml* or X3D_Node* 11-(FIELDTYPE_MFNODE) union anyVrml* or Multi_Node*
92 // char *defName;
93 // int container;
94 // int paircount;
95 // int uniqueNumber;
96 //};
97 //static struct PROTOInstanceEntry ProtoInstanceTable[PROTOINSTANCE_MAX_LEVELS];
98 //
100 //struct PROTOnameStruct {
101 // char *definedProtoName;
102 // char *url;
103 // FILE *fileDescriptor;
104 // char *fileName;
105 // int charLen;
106 // int fileOpen;
107 // int isExternProto;
108 // struct Shader_Script *fieldDefs;
109 //};
110 //static struct PROTOnameStruct *PROTONames = NULL;
111 //#else
112 //extern int curProtoInsStackInd;
113 
114 // JAS extern struct PROTOInstanceEntry ProtoInstanceTable[PROTOINSTANCE_MAX_LEVELS];
115 
116 /* PROTO table */
117 //extern struct PROTOnameStruct *PROTONames;
118 //#endif
119 
120 //=======
121 #ifndef __LIBFREEWRL_DECL_H__
122 #define __LIBFREEWRL_DECL_H__
123 
124 
125 #ifdef FREEWRL_THREAD_COLORIZED
126 
127 /* FreeWRL will try to output color is stdout is a terminal */
128 #define PRINTF printf_with_colored_threads
129 extern int printf_with_colored_threads(const char *format, ...);
130 #define FPRINTF fprintf_with_colored_threads
131 extern int fprintf_with_colored_threads(FILE *stream, const char *format, ...);
132 
133 #else /* FREEWRL_THREAD_COLORIZED */
134 
135 #define PRINTF printf
136 #define FPRINTF fprintf
137 
138 #endif /* FREEWRL_THREAD_COLORIZED */
139 
140 #define BOOL_STR(b) (b ? "TRUE" : "FALSE")
141 
142 /* Useful to suppress things from non-debug builds */
143 #if defined(FW_DEBUG)
144 # define DEBUG_(_expr) _expr
145 #else
146 # define DEBUG_(_expr)
147 #endif
148 
149 #include <stdio.h>
150 
151 void fw_perror(FILE *f, const char *format, ...);
152 #ifdef DISABLER
153 #if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
154 void freewrlFreeAllRegisteredAllocations();
155 bool freewrlIsRegisteringAllocation();
156 void freewrlSetShouldRegisterAllocation(bool shouldRegisterAllocation);
157 void freewrlInitMemTable();
158 void freewrlDisposeMemTable();
159 #endif
160 #endif
161 
162 /* To conform C99 ISO C (do not use GCC extension) */
163 #if defined(_MSC_VER)
164 void usleep(int);
165 void sleep(int ms);
166 #if _MSC_VER < 1500
167 //vc7 cant seem to do the ... thing or the __VAR_ARGS__ thing.
168 int DEBUG_FPRINTF(const char *fmt, ...); //almost stubs it out - a function call and return
169 #define DEBUG_MSG DEBUG_FPRINTF
170 #define TRACE_MSG DEBUG_FPRINTF
171 #define WARN_MSG DEBUG_FPRINTF
172 #define ERROR_MSG DEBUG_FPRINTF
173 #define PERROR_MSG DEBUG_FPRINTF
174 #define DEBUG_MEM DEBUG_FPRINTF
175 #define DEBUG_RENDER DEBUG_FPRINTF
176 #define DEBUG_TEX DEBUG_FPRINTF
177 #define DEBUG_X3DPARSER DEBUG_FPRINTF
178 #define DEBUG_SHADER DEBUG_FPRINTF
179 #define DEBUG_RES DEBUG_FPRINTF
180 #define DEBUG_CPARSER DEBUG_FPRINTF
181 
182 /* //would implement it for vc7 but long winded
183 int DEBUG_MSG(const char *fmt, ...)
184 {
185  int ret = 0;
186 #ifdef VERBOSE
187  va_list args;
188  va_start( args, fmt );
189 
190  ret = ConsoleMessage("FW:");
191  ret = ConsoleMessage(fmt,args);
192 #endif
193  return ret;
194 }
195 */
196 #define MSVC_7 1
197 #endif
198 #endif
199 
200 #if !defined(MSVC_7) // _MSC_VER >= 1500
201 #define DEBUG_MSG(...) DEBUG_(FPRINTF(stdout, __VA_ARGS__))
202 #define TRACE_MSG(...) DEBUG_(FPRINTF(stdout, __VA_ARGS__))
203 #define WARN_MSG(...) DEBUG_(FPRINTF(stdout, __VA_ARGS__))
204 /* Error message should always be printed */
205 #define ERROR_MSG(...) FPRINTF(stderr, __VA_ARGS__)
206 #define PERROR_MSG(...) fw_perror(stderr, __VA_ARGS__)
207 #ifdef VERBOSE
208 #define DEBUG_FW(...) DEBUG_(PRINTF("FW: " __VA_ARGS__))
209 #else
210 #define DEBUG_FW(...)
211 #endif
212 #ifdef RESVERBOSE
213 #define DEBUG_RES(...) DEBUG_(PRINTF("RES: " __VA_ARGS__))
214 #else
215 #define DEBUG_RES(...)
216 #endif
217 
218 #ifdef TEXVERBOSE
219 #define DEBUG_TEX(...) DEBUG_(PRINTF("TEXTURE: " __VA_ARGS__))
220 #else
221 #define DEBUG_TEX(...)
222 #endif
223 
224 #ifdef MEMVERBOSE
225 #define DEBUG_MEM(...) DEBUG_(PRINTF("MEM: " __VA_ARGS__))
226 #else
227 #define DEBUG_MEM(...)
228 #endif
229 
230 #ifdef CPARSERVERBOSE
231 #define DEBUG_CPARSER(...) DEBUG_(PRINTF("CPARSER: " __VA_ARGS__))
232 #else
233 #define DEBUG_CPARSER(...)
234 #endif
235 
236 #ifdef CPROTOVERBOSE
237 #define DEBUG_CPROTO(...) DEBUG_(PRINTF("CPROTO: " __VA_ARGS__))
238 #else
239 #define DEBUG_CPROTO(...)
240 #endif
241 
242 #ifdef PLUGINSOCKETVERBOSE
243 #define DEBUG_PLUGINSOCKET(...) DEBUG_(PRINTF("PLUGINSOCKET: " __VA_ARGS__))
244 #else
245 #define DEBUG_PLUGINSOCKET(...)
246 #endif
247 
248 /* FIXME: replace CR with CROUTE for clarity */
249 #ifdef CRVERBOSE
250 #define DEBUG_CR(...) DEBUG_(PRINTF("CR: " __VA_ARGS__))
251 #else
252 #define DEBUG_CR(...)
253 #endif
254 
255 /* FIXME: maybe shorten the def here to make it more practical */
256 #ifdef JSVRMLCLASSESVERBOSE
257 #define DEBUG_JSVRMLCLASSES(...) DEBUG_(PRINTF("JSVRMLCLASSES: " __VA_ARGS__))
258 #else
259 #define DEBUG_JSVRMLCLASSES(...)
260 #endif
261 
262 #ifdef JAVASCRIPTVERBOSE
263 #define DEBUG_JS(...) DEBUG_(PRINTF("JS: " __VA_ARGS__))
264 #else
265 #define DEBUG_JS(...)
266 #endif
267 
268 #ifdef SETFIELDVERBOSE
269 #define DEBUG_SETFIELD(...) DEBUG_(PRINTF("SETFIELD: " __VA_ARGS__))
270 #else
271 #define DEBUG_SETFIELD(...)
272 #endif
273 
274 #ifdef RENDERVERBOSE
275 #define DEBUG_RENDER(...) DEBUG_(PRINTF("RENDER: " __VA_ARGS__))
276 #else
277 #define DEBUG_RENDER(...)
278 #endif
279 
280 #ifdef CHILDVERBOSE
281 #define DEBUG_CHILD(...) DEBUG_(PRINTF("CHILD: " __VA_ARGS__))
282 #else
283 #define DEBUG_CHILD(...)
284 #endif
285 
286 /* FIXME: maybe replace SE with SENSOR for clarity */
287 #ifdef SEVERBOSE
288 #define DEBUG_SE(...) DEBUG_(PRINTF("SE: " __VA_ARGS__))
289 #else
290 #define DEBUG_SE(...)
291 #endif
292 
293 #ifdef STREAM_POLY_VERBOSE
294 #define DEBUG_STREAM_POLY(...) DEBUG_(PRINTF("STREAM_POLY: " __VA_ARGS__))
295 #else
296 #define DEBUG_STREAM_POLY(...)
297 #endif
298 
299 #ifdef OCCLUSIONVERBOSE
300 #define DEBUG_OCCLUSION(...) DEBUG_(PRINTF("OCCLUSION: " __VA_ARGS__))
301 #else
302 #define DEBUG_OCCLUSION(...)
303 #endif
304 
305 #ifdef FRUSTUMVERBOSE
306 #define DEBUG_FRUSTUM(...) DEBUG_(PRINTF("FRUSTUM: " __VA_ARGS__))
307 #else
308 #define DEBUG_FRUSTUM(...)
309 #endif
310 
311 #ifdef SHADERVERBOSE
312 #define DEBUG_SHADER(...) DEBUG_(PRINTF("SHADER: " __VA_ARGS__))
313 #else
314 #define DEBUG_SHADER(...)
315 #endif
316 
317 #ifdef BINDVERBOSE
318 #define DEBUG_BIND(...) DEBUG_(PRINTF("BIND: " __VA_ARGS__))
319 #else
320 #define DEBUG_BIND(...)
321 #endif
322 //>>>>>>> 1.44
323 
324 #ifdef X3DPARSERVERBOSE
325 #define DEBUG_X3DPARSER(...) DEBUG_(PRINTF("X3DPARSER: " __VA_ARGS__))
326 #else
327 #define DEBUG_X3DPARSER(...)
328 #endif
329 
330 /* FIXME: maybe change CAPABILITIES with X3DCAPS */
331 #ifdef CAPABILITIESVERBOSE
332 #define DEBUG_CAPABILITIES(...) DEBUG_(PRINTF("CAPABILITIES: " __VA_ARGS__))
333 #else
334 #define DEBUG_CAPABILITIES(...)
335 #endif
336 
337 /* FIXME: does this has to do with SENSOR or SEVERBOSE ? */
338 #ifdef SENSVERBOSE
339 #define DEBUG_SENS(...) DEBUG_(PRINTF("SENS: " __VA_ARGS__))
340 #else
341 #define DEBUG_SENS(...)
342 #endif
343 
344 #ifdef SOUNDVERBOSE
345 #define DEBUG_SOUND(...) DEBUG_(PRINTF("SOUND: " __VA_ARGS__))
346 #else
347 #define DEBUG_SOUND(...)
348 #endif
349 
350 #ifdef FILLVERBOSE
351 #define DEBUG_FILL(...) DEBUG_(PRINTF("FILL: " __VA_ARGS__))
352 #else
353 #define DEBUG_FILL(...)
354 #endif
355 
356 #ifdef TEXTVERBOSE
357 #define DEBUG_TEXT(...) DEBUG_(PRINTF("TEXT: " __VA_ARGS__))
358 #else
359 #define DEBUG_TEXT(...)
360 #endif
361 
362 #ifdef ARGSVERBOSE
363 #define DEBUG_ARGS(...) DEBUG_(PRINTF("TEXT: " __VA_ARGS__))
364 #else
365 #define DEBUG_ARGS(...)
366 #endif
367 
368 #ifdef XEVENT_VERBOSE
369 #define DEBUG_XEV(...) DEBUG_(PRINTF("XEV: " __VA_ARGS__))
370 #else
371 #define DEBUG_XEV(...)
372 #endif
373 #endif //_MSC_VER
374 
375 void *mallocn_debug(int line, char *file, void *node,size_t size);
376 void *reallocn_debug(int line, char *file, void *node, void *pold, size_t newsize);
377 void *mallocn(void *node,size_t size);
378 void *reallocn(void *node, void *pold, size_t newsize);
379 
383 #if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
384 
385 void *freewrlMalloc(int line, char *file, size_t sz, int zeroData);
386 void *freewrlRealloc(int line, char *file, void *ptr, size_t size);
387 void freewrlFree(int line, char *file, void *a);
388 void *freewrlStrdup(int line, char *file, char *str);
389 void *freewrlStrndup(int line, char *file, const char *str, size_t n);
390 #define MALLOCN(_node,_sz) (mallocn_debug(__LINE__,__FILE__,_node,_sz))
391 #define REALLOCN(_node,_oldp,_newsz) (reallocn_debug(__LINE__,__FILE__,_node,_oldp,_newsz))
392 # define MALLOCV(_sz) (freewrlMalloc(__LINE__, __FILE__, _sz, FALSE))
393 # define MALLOC(t,_sz) ((t)freewrlMalloc(__LINE__, __FILE__, _sz, FALSE))
394 # define CALLOC(_fill, _sz) freewrlMalloc(__LINE__, __FILE__, _fill * _sz, TRUE);
395 # define REALLOC(_a,_b) freewrlRealloc(__LINE__, __FILE__, _a, _b)
396 # define FREE(_ptr) freewrlFree(__LINE__, __FILE__, _ptr)
397 
398 # define XALLOC(_type) (_type *) CALLOC(1, sizeof(_type))
399 # define XFREE(_ptr) {if (_ptr) { FREE(_ptr); _ptr = NULL; }}
400 
401 # define STRDUP(_a) freewrlStrdup(__LINE__, __FILE__, _a)
402 
403 # define STRNDUP(_a, _n) freewrlStrndup(__LINE__, __FILE__, _a, _n)
404 #ifdef FW_DEBUG
405 # define UNLINK(_fdd) do { \
406  TRACE_MSG("TRACE: unlink %s at %s:%d\n",_fdd,__FILE__,__LINE__); \
407  unlink (_fdd); \
408  } while (0);
409 
410 # define TEMPNAM(_dir,_pfx) tempnam(_dir, _pfx); do { \
411  TRACE_MSG("TRACE: tempnam %s/%s at %s:%d\n", _dir, _pfx, __FILE__, __LINE__); \
412  } while (0);
413 
414 # define ASSERT(_ptr) do { if (!(_ptr)) { \
415  ERROR_MSG("ERROR: assert failed: %s (%s:%d)\n", #_ptr, __FILE__, __LINE__); } \
416  } while (0);
417 #else
418 # define UNLINK unlink
419 # define TEMPNAM tempnam
420 
421 
422 # define ASSERT(_ptr) do { if (!(_ptr)) { \
423  ERROR_MSG("ERROR: assert failed: %s (%s:%d)\n", #_ptr, __FILE__, __LINE__); } \
424  } while (0);
425 
426 #endif // FW_DEBUG
427 /* JAS */
428 #ifndef TEMPNAM
429 #if defined(_MSC_VER)
430 # define TEMPNAM _tempnam
431 #else
432 # define TEMPNAM tempnam
433 #endif
434 #endif
435 
436 
437 #else /* defined(WRAP_MALLOC) || defined(DEBUG_MALLOC) */
438 #define MALLOCN(_node,_sz) (mallocn(_node,_sz))
439 #define REALLOCN(_node,_oldp,_newsz) (reallocn(_node,_oldp,_newsz))
440 
441 # define MALLOCV(_sz) (malloc(_sz))
442 # define MALLOC(t,_sz) ((_sz > 0) ? (t)malloc(_sz) : NULL)
443 # define REALLOC realloc
444 # define FREE free
445 
446 # define XALLOC(_type) (_type *) calloc(1, sizeof(_type))
447 # define XFREE(_ptr) {if (_ptr) { free(_ptr); _ptr = NULL; }}
448 
449 # define STRDUP strdup
450 # define STRNDUP strndup
451 # define UNLINK unlink
452 # define TEMPNAM tempnam
453 
454 # define ASSERT(_whatever)
455 
456 #endif /* defined(WRAP_MALLOC) || defined(DEBUG_MALLOC) */
457 
458 /* This get always defined, but ERROR_MSG is no-op without _DEBUG */
459 
460 #define FREE_IF_NZ(_ptr) {if (_ptr) { \
461  FREE(_ptr); \
462  _ptr = NULL; } \
463  else { \
464  DEBUG_MEM("double free: %s:%d\n", __FILE__, __LINE__); \
465  }}
466 
467 
468 /* New ptr/string guarded code:
469  this macro free the original pointed ptr (void* or char*)
470 */
471 #define PTR_REPLACE(_ptr,_newptr) do { \
472  if (_ptr != _newptr) { \
473  if (_ptr) { \
474  DEBUG_MSG("replacing ptr %p with %p\n", _ptr, _newptr); \
475  FREE(_ptr); \
476  _ptr = _newptr; \
477  } else { \
478  DEBUG_MSG("ptr newly assigned value %p\n", _newptr); \
479  _ptr = _newptr; \
480  } \
481  } else { \
482  DEBUG_MSG("replacing ptr with the same value (warning)\n"); \
483  } } while (0);
484 
485 #define PTR_REPLACE_DUP(_ptr,_newptr) do { \
486  if (_ptr != _newptr) { \
487  if (_ptr) { \
488  DEBUG_MSG("replacing ptr %p with %p\n", _ptr, _newptr); \
489  FREE(_ptr); \
490  _ptr = STRDUP(_newptr); \
491  } else { \
492  DEBUG_MSG("ptr newly assigned value %p\n", _newptr); \
493  _ptr = STRDUP(_newptr); \
494  } \
495  } else { \
496  DEBUG_MSG("replacing ptr with the same value (warning)\n"); \
497  } } while (0);
498 
499 
500 void register_node_gc(void *node, void *p); //registers in node->_gc vector* for freeing
501 void unregister_node_gc(void *node, void *p); //unregister old on realloc
502 void free_registered_node_gc(void *node); //free when freeing node ie freeMallocedNodeFields
503 
504 /* THIS HAS TO BE FIXED TOO :) */
505 
506 #if defined(_MSC_VER)
507 /* FIXME: investigate on this... (michel) */
508 #include <stddef.h> /* for offsetof(...) */
509 /* textures.c > jpeg > jmorecfg.h tries to redefine booleand but you can say you have it */
510 #define HAVE_BOOLEAN 1
511 
512 #endif
513 
514 #ifdef IPHONE
515 #include <stddef.h>
516 #define HAVE_BOOLEAN 1
517 #endif
518 
519 #ifndef M_PI
520 #define M_PI 3.14159265358979323846 //acos(-1.0)
521 #endif
522 /* Move those to a better place: */
523 /* OLDCODE: void initialize_parser(); */
524 
525 /* Global FreeWRL options (will become profiles ?) */
526 
527 //extern bool global_strictParsing; /* are we doing "strict" parsing,
528 // as per FreeX3D, or "loose" parsing,
529 // as per FreeWRL ? */
530 
531 //extern bool global_plugin_print; /* are we printing messages to a file
532 // because we are running as a plugin ? */
533 
534 //extern bool global_occlusion_disable; /* do we disable all occlusion query
535 // calls in the renderer ? */
536 
537 //extern unsigned global_texture_size; /* do we manually set up the texture
538 // size ? */
539 
540 //extern bool global_print_opengl_errors; /* print OpenGL errors as they come ? */
541 
542 //extern bool global_trace_threads; /* trace thread creation / switch ... ? */
543 
544 #endif /* __LIBFREEWRL_DECL_H__ */