FreeWRL / FreeX3D 4.3.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 "scenegraph/Vector.h"
39#include "vrml_parser/Structs.h"
40#include "main/headers.h"
41#include "vrml_parser/CParseGeneral.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
129extern int printf_with_colored_threads(const char *format, ...);
130#define FPRINTF fprintf_with_colored_threads
131extern 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
151void fw_perror(FILE *f, const char *format, ...);
152#ifdef DISABLER
153#if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
154void freewrlFreeAllRegisteredAllocations();
155bool freewrlIsRegisteringAllocation();
156void freewrlSetShouldRegisterAllocation(bool shouldRegisterAllocation);
157void freewrlInitMemTable();
158void freewrlDisposeMemTable();
159#endif
160#endif
161
162/* To conform C99 ISO C (do not use GCC extension) */
163#if defined(_MSC_VER)
164void usleep(int);
165void sleep(int ms);
166#if _MSC_VER < 1500
167//vc7 cant seem to do the ... thing or the __VAR_ARGS__ thing.
168int 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
183int 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
375void *mallocn_debug(int line, char *file, void *node,size_t size);
376void *reallocn_debug(int line, char *file, void *node, void *pold, size_t newsize);
377void *mallocn(void *node,size_t size);
378void *reallocn(void *node, void *pold, size_t newsize);
379
384#if defined(_tempnam)
385# define TEMPNAM _tempnam
386#else
387# define TEMPNAM tempnam
388#endif
389
390#if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
391
392void *freewrlMalloc(int line, char *file, size_t sz, int zeroData);
393void *freewrlRealloc(int line, char *file, void *ptr, size_t size);
394void freewrlFree(int line, char *file, void *a);
395void *freewrlStrdup(int line, char *file, char *str);
396void *freewrlStrndup(int line, char *file, const char *str, size_t n);
397#define MALLOCN(_node,_sz) (mallocn_debug(__LINE__,__FILE__,_node,_sz))
398#define REALLOCN(_node,_oldp,_newsz) (reallocn_debug(__LINE__,__FILE__,_node,_oldp,_newsz))
399# define MALLOCV(_sz) (freewrlMalloc(__LINE__, __FILE__, _sz, FALSE))
400# define MALLOC(t,_sz) ((t)freewrlMalloc(__LINE__, __FILE__, _sz, FALSE))
401# define CALLOC(_fill, _sz) freewrlMalloc(__LINE__, __FILE__, _fill * _sz, TRUE);
402# define REALLOC(_a,_b) freewrlRealloc(__LINE__, __FILE__, _a, _b)
403# define FREE(_ptr) freewrlFree(__LINE__, __FILE__, _ptr)
404
405# define XALLOC(_type) (_type *) CALLOC(1, sizeof(_type))
406# define XFREE(_ptr) {if (_ptr) { FREE(_ptr); _ptr = NULL; }}
407
408# define STRDUP(_a) freewrlStrdup(__LINE__, __FILE__, _a)
409
410# define STRNDUP(_a, _n) freewrlStrndup(__LINE__, __FILE__, _a, _n)
411#ifdef FW_DEBUG
412# define UNLINK(_fdd) do { \
413 TRACE_MSG("TRACE: unlink %s at %s:%d\n",_fdd,__FILE__,__LINE__); \
414 unlink (_fdd); \
415 } while (0);
416
417# define TEMPNAM(_dir,_pfx) tempnam(_dir, _pfx); do { \
418 TRACE_MSG("TRACE: tempnam %s/%s at %s:%d\n", _dir, _pfx, __FILE__, __LINE__); \
419 } while (0);
420
421# define ASSERT(_ptr) do { if (!(_ptr)) { \
422 ERROR_MSG("ERROR: assert failed: %s (%s:%d)\n", #_ptr, __FILE__, __LINE__); } \
423 } while (0);
424#else
425# define UNLINK unlink
426# define TEMPNAM tempnam
427
428
429# define ASSERT(_ptr) do { if (!(_ptr)) { \
430 ERROR_MSG("ERROR: assert failed: %s (%s:%d)\n", #_ptr, __FILE__, __LINE__); } \
431 } while (0);
432
433#endif // FW_DEBUG
434
435#else /* defined(WRAP_MALLOC) || defined(DEBUG_MALLOC) */
436#define MALLOCN(_node,_sz) (mallocn(_node,_sz))
437#define REALLOCN(_node,_oldp,_newsz) (reallocn(_node,_oldp,_newsz))
438
439# define MALLOCV(_sz) (malloc(_sz))
440# define MALLOC(t,_sz) ((_sz > 0) ? (t)malloc(_sz) : NULL)
441# define REALLOC realloc
442# define FREE free
443
444# define XALLOC(_type) (_type *) calloc(1, sizeof(_type))
445# define XFREE(_ptr) {if (_ptr) { free(_ptr); _ptr = NULL; }}
446
447# define STRDUP strdup
448# define STRNDUP strndup
449# define UNLINK unlink
450# define TEMPNAM tempnam
451
452# define ASSERT(_whatever)
453
454#endif /* defined(WRAP_MALLOC) || defined(DEBUG_MALLOC) */
455
456#if defined(_MSC_VER) && defined(W_DEBUG)
457#define CHECK_MEMORY check_memory();
458#else
459#define CHECK_MEMORY
460#endif
461
462/* This get always defined, but ERROR_MSG is no-op without _DEBUG */
463
464#define FREE_IF_NZ(_ptr) {if (_ptr) { \
465 FREE(_ptr); \
466 _ptr = NULL; } \
467 else { \
468 DEBUG_MEM("double free: %s:%d\n", __FILE__, __LINE__); \
469 }}
470
471
472/* New ptr/string guarded code:
473 this macro free the original pointed ptr (void* or char*)
474*/
475#define PTR_REPLACE(_ptr,_newptr) do { \
476 if (_ptr != _newptr) { \
477 if (_ptr) { \
478 DEBUG_MSG("replacing ptr %p with %p\n", _ptr, _newptr); \
479 FREE(_ptr); \
480 _ptr = _newptr; \
481 } else { \
482 DEBUG_MSG("ptr newly assigned value %p\n", _newptr); \
483 _ptr = _newptr; \
484 } \
485 } else { \
486 DEBUG_MSG("replacing ptr with the same value (warning)\n"); \
487 } } while (0);
488
489#define PTR_REPLACE_DUP(_ptr,_newptr) do { \
490 if (_ptr != _newptr) { \
491 if (_ptr) { \
492 DEBUG_MSG("replacing ptr %p with %p\n", _ptr, _newptr); \
493 FREE(_ptr); \
494 _ptr = STRDUP(_newptr); \
495 } else { \
496 DEBUG_MSG("ptr newly assigned value %p\n", _newptr); \
497 _ptr = STRDUP(_newptr); \
498 } \
499 } else { \
500 DEBUG_MSG("replacing ptr with the same value (warning)\n"); \
501 } } while (0);
502
503
504void register_node_gc(void *node, void *p); //registers in node->_gc vector* for freeing
505void unregister_node_gc(void *node, void *p); //unregister old on realloc
506void free_registered_node_gc(void *node); //free when freeing node ie freeMallocedNodeFields
507
508/* THIS HAS TO BE FIXED TOO :) */
509
510#if defined(_MSC_VER)
511/* FIXME: investigate on this... (michel) */
512#include <stddef.h> /* for offsetof(...) */
513/* textures.c > jpeg > jmorecfg.h tries to redefine booleand but you can say you have it */
514#define HAVE_BOOLEAN 1
515
516#endif
517
518#ifdef IPHONE
519#include <stddef.h>
520#define HAVE_BOOLEAN 1
521#endif
522
523#ifndef M_PI
524#define M_PI 3.14159265358979323846 //acos(-1.0)
525#endif
526/* Move those to a better place: */
527/* OLDCODE: void initialize_parser(); */
528
529/* Global FreeWRL options (will become profiles ?) */
530
531//extern bool global_strictParsing; /* are we doing "strict" parsing,
532// as per FreeX3D, or "loose" parsing,
533// as per FreeWRL ? */
534
535//extern bool global_plugin_print; /* are we printing messages to a file
536// because we are running as a plugin ? */
537
538//extern bool global_occlusion_disable; /* do we disable all occlusion query
539// calls in the renderer ? */
540
541//extern unsigned global_texture_size; /* do we manually set up the texture
542// size ? */
543
544//extern bool global_print_opengl_errors; /* print OpenGL errors as they come ? */
545
546//extern bool global_trace_threads; /* trace thread creation / switch ... ? */
547const char *getNodeName(struct X3D_Node *node);
548#endif /* __LIBFREEWRL_DECL_H__ */