35#include "../vrml_parser/Structs.h"
36#include "../vrml_parser/CRoutes.h"
37#include "../main/headers.h"
39#include "../input/EAIHeaders.h"
40#include "../input/EAIHelpers.h"
41#include "../opengl/Frustum.h"
42#include "../opengl/OpenGL_Utils.h"
43#include "../opengl/Textures.h"
45#include "Component_Networking.h"
47#include "../scenegraph/RenderFuncs.h"
49#include <libFreeWRL.h>
56 #define TRACK_OSC_MSG 0
62#include "../vrml_parser/CParseGeneral.h"
63#include "../scenegraph/Vector.h"
64#include "../vrml_parser/CFieldDecls.h"
65#include "../world_script/JScript.h"
66#include "../world_script/CScripts.h"
67#include "../world_script/fieldSet.h"
68#include "../vrml_parser/CParseParser.h"
69#include "../vrml_parser/CParseLexer.h"
70#include "../vrml_parser/CParse.h"
78void error(
int num,
const char *m,
const char *path);
79void utilOSCcounts(
char *types ,
int *intCount,
int *fltCount,
int *strCount,
int *blobCount,
int *midiCount,
int *otherCount);
82#include "OSCcallbacks.c"
85#define MAX_OSC_SERVERS 32
86int serverPort[MAX_OSC_SERVERS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
87lo_server_thread oscThread[MAX_OSC_SERVERS] ;
89static uintptr_t *OSC_Nodes = NULL;
90static int num_OSC_Nodes = 0;
92int active_OSC_Nodes = FALSE;
94void utilOSCcounts(
char *types ,
int *intCount,
int *fltCount,
int *strCount,
int *blobCount,
int *midiCount,
int *otherCount) {
106 for (i=0 ; i < j ; i++) {
130void activate_OSCsensors() {
132 active_OSC_Nodes = TRUE ;
144 while (active_OSC_Nodes && curr_OSC_Node < num_OSC_Nodes) {
146 if (checkNode(realnode,__FILE__,__LINE__)) {
148 printf(
"activate_OSCsensors : %s,%d node=%p name=%s\n", __FILE__,__LINE__,realnode,realnode->description->strptr) ;
150 if (realnode->_status < 0) {
151 printf(
"activate_OSCsensors : %s,%d Moving %s to ready.\n", __FILE__,__LINE__,realnode->description->strptr) ;
152 realnode->_status = 0 ;
153 }
else if (realnode->_status == 0) {
154 printf(
"activate_OSCsensors : %s,%d\n", __FILE__,__LINE__) ;
155 printf(
"activate_OSCsensors : enabled=%d\n", realnode->enabled) ;
156 printf(
"activate_OSCsensors : gotEvents=%d\n", realnode->gotEvents) ;
157 printf(
"activate_OSCsensors : description=%s\n",realnode->description->strptr) ;
158 printf(
"activate_OSCsensors : protocol=%s\n", realnode->protocol->strptr) ;
159 printf(
"activate_OSCsensors : port=%d\n", realnode->port) ;
160 printf(
"activate_OSCsensors : filter=%s\n", realnode->filter->strptr) ;
161 printf(
"activate_OSCsensors : handler=%s\n", realnode->handler->strptr) ;
179 printf(
"activate_OSCsensors : talksTo=[ ");
180 for (i=0; i < realnode->talksTo.n; i++) {
181 printf(
"\"%s\" ",realnode->talksTo.p[i]->strptr);
185 myNode = parser_getNodeFromName(realnode->talksTo.p[i]->strptr);
186 if (myNode != NULL) {
187 printf(
"(%p) ",(
void *)myNode);
192 printf(
"] (%d nodes) (Need to fix %s,%d)\n",realnode->talksTo.n , __FILE__,__LINE__);
193 printf(
"activate_OSCsensors : listenfor=%s , expect %d parameters\n", realnode->listenfor->strptr , (
int)strlen(realnode->listenfor->strptr)) ;
194 printf(
"activate_OSCsensors : FIFOsize=%d\n", realnode->FIFOsize) ;
195 printf(
"activate_OSCsensors : _status=%d\n", realnode->_status) ;
197 if (realnode->FIFOsize > 0) {
199 utilOSCcounts(realnode->listenfor->strptr,&intCount,&fltCount,&strCount,&blobCount,&midiCount,&otherCount);
200 intCount = realnode->FIFOsize * (intCount + midiCount);
201 fltCount = realnode->FIFOsize * fltCount;
202 strCount = realnode->FIFOsize * (strCount + blobCount + otherCount);
203 printf(
"Allocate %d floats, %d ints for '%s'\n",fltCount,intCount,realnode->description->strptr);
205 realnode->_int32InpFIFO = (
void *) NewRingBuffer (intCount) ;
206 realnode->_floatInpFIFO = (
void *) NewRingBuffer (fltCount) ;
207 realnode->_stringInpFIFO = (
void *) NewRingBuffer (strCount) ;
212 int foundCurrentPort = -1 ;
213 for ( i=0 ; i < num_OSC_Nodes ; i++) {
214 if(realnode->port == serverPort[i]) {
219 if (foundCurrentPort < 0) {
220 foundCurrentPort = serverCount ;
221 serverPort[foundCurrentPort] = realnode->port ;
224 sprintf (buf,
"%d",realnode->port);
226 if (strcmp(
"TCP",realnode->protocol->strptr)==0) {
228 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
229 }
else if (strcmp(
"UNIX",realnode->protocol->strptr)==0) {
231 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
234 oscThread[foundCurrentPort] = lo_server_thread_new(buf, error);
236 lo_server_thread_start(oscThread[foundCurrentPort]);
239 printf(
"%d servers; current server is running in slot %d on port %d\n",serverCount,foundCurrentPort,serverPort[foundCurrentPort]) ;
244 int foundHandler = 0 ;
245 for (i=0 ; i < OSCfuncCount ; i++) {
246 printf(
"%d/%d : Check %s against %s\n",i,OSCfuncCount,realnode->handler->strptr,OSCfuncNames[i]);
247 if (0 == strcmp(realnode->handler->strptr,OSCfuncNames[i])) {foundHandler = i;}
249 if (OSCcallbacks[foundHandler] != NULL) {
250 printf(
"Going to hook '%s' to '%s' handler\n",realnode->description->strptr ,OSCfuncNames[foundHandler]) ;
251 lo_server_thread_add_method(oscThread[foundCurrentPort], realnode->filter->strptr, realnode->listenfor->strptr,
252 (OSCcallbacks[foundHandler]), realnode);
255 realnode->_status = 1 ;
257 active_OSC_Nodes = FALSE ;
265void error(
int num,
const char *msg,
const char *path)
267 printf(
"liblo server error %d in path %s: %s\n", num, path, msg);
270void add_OSCsensor(
struct X3D_Node * node) {
274 printf (
"error in registerOSCNode; somehow the node datastructure is zero \n");
278 if (node->_nodeType != NODE_OSC_Sensor)
return;
280 OSC_Nodes = (uintptr_t *) REALLOC (OSC_Nodes,
sizeof (uintptr_t *) * (num_OSC_Nodes+1));
284 *myptr = (uintptr_t) node;
288void remove_OSCsensor(
struct X3D_Node * node) {}
291void add_OSCsensor(
struct X3D_Node * node) {}
292void remove_OSCsensor(
struct X3D_Node * node) {}
296int loadstatus_Script(
struct X3D_Script *script);
297int getFieldFromNodeAndNameC(
struct X3D_Node* node,
const char *fieldname,
int *type,
int *kind,
int *iifield,
int *builtIn,
union anyVrml **value,
const char **cname);
302 struct X3D_Node *cnode, **watchlist;
307 if (node->__oldEnabled != node->enabled) {
308 node->__oldEnabled = node->enabled;
309 MARK_EVENT(X3D_NODE(node),offsetof (
struct X3D_LoadSensor, enabled));
311 if (!node->enabled)
return;
315 if (!renderstate()->render_sensitive)
return;
318 if NODE_NEEDS_COMPILING {
321 node->__finishedloading = 0;
322 node->progress = (float) 0.0;
323 node->__StartLoadTime = 0.0;
328 if (node->watchList.n) {
329 nwatch = node->watchList.n;
330 watchlist = node->watchList.p;
332 else if (node->children.n) {
333 nwatch = node->children.n;
334 watchlist = node->children.p;
336 if (nwatch <=0)
return;
339 if (node->__finishedloading == nwatch)
return;
347 for (count = 0; count < nwatch; count ++) {
349 cnode = watchlist[count];
352 switch (cnode->_nodeType) {
353 case NODE_ImageTexture:
360 if (fwl_isTextureLoaded(tnode->__textureTableIndex)) {
370 if(inode->__loadstatus > INLINE_INITIAL_STATE && inode->__loadstatus < INLINE_STABLE)
372 if(inode->__loadstatus == INLINE_STABLE)
379 if(loadstatus_Script(X3D_SCRIPT(cnode)))
383 case NODE_ShaderProgram:
386 shader=(
struct Shader_Script *)(X3D_SHADERPROGRAM(cnode)->_shaderUserDefinedFields);
387 if(shader->loaded) nowFinished++;
390 case NODE_PackagedShader:
393 shader=(
struct Shader_Script *)(X3D_PACKAGEDSHADER(cnode)->_shaderUserDefinedFields);
394 if(shader->loaded) nowFinished++;
397 case NODE_ComposedShader:
400 shader=(
struct Shader_Script *)(X3D_COMPOSEDSHADER(cnode)->_shaderUserDefinedFields);
401 if(shader->loaded) nowFinished++;
408 shader=(
struct Shader_Script *)(X3D_EFFECT(cnode)->_shaderUserDefinedFields);
409 if(shader->loaded) nowFinished++;
413 case NODE_MovieTexture:
420 istate = loadstatus_AudioClip(anode);
434 if (nowFinished == nwatch) {
436 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
439 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isLoaded));
441 node->progress = (float) 1.0;
442 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, progress));
444 node->loadTime = TickTime();
445 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, loadTime));
449 if ((nowLoading > 0) && (node->__loading == 0)) {
452 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
455 node->__StartLoadTime = TickTime();
459 if (node->isActive == 1) {
460 node->progress = (float)(nowFinished)/(float)(nwatch);
461 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, progress));
465 node->__loading = nowLoading;
466 node->__finishedloading = nowFinished;
469 if (node->timeOut > 0.0001) {
470 if (node->__StartLoadTime > 0.001) {
473 if ((TickTime() - node->__StartLoadTime) > node->timeOut) {
475 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isLoaded));
478 MARK_EVENT (X3D_NODE(node), offsetof (
struct X3D_LoadSensor, isActive));
481 node->__finishedloading = nwatch;
489 int nc = (node->children).n;
496 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
497 prep_BBox((
struct BBoxFields*)&node->bboxCenter);
499 normalChildren(node->children);
501 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
504struct X3D_Node *broto_search_DEFname(
struct X3D_Proto *context,
const char *name);
505struct IMEXPORT *broto_search_IMPORTname(
struct X3D_Proto *context,
const char *name);
506struct IMEXPORT *broto_search_EXPORTname(
struct X3D_Proto *context,
const char *name);
508struct X3D_Node * broto_search_ALLnames(
struct X3D_Proto *context,
const char *name,
int *source){
520 node = broto_search_DEFname(context,name);
524 im = broto_search_IMPORTname(context,name);
529 nlinenode = broto_search_DEFname(context,im->inlinename);
530 if(nlinenode && nlinenode->_nodeType == NODE_Inline ){
531 struct X3D_Inline *nline = X3D_INLINE(nlinenode);
532 if(nline->__loadstatus == INLINE_IMPORTING || nline->__loadstatus == INLINE_STABLE){
534 struct IMEXPORT *ex = broto_search_EXPORTname(X3D_PROTO(nline),im->mxname);
543 node = broto_search_DEFname(X3D_PROTO(nline),ex->mxname);
561 struct X3D_Node* newnodef, *newnodet;
562 int source, type, kind, ifield, builtIn;
567 newnodef = route->from.node;
568 newnodet = route->to.node;
569 if(route->from.weak){
571 newnodef = broto_search_ALLnames(context,route->from.cnode,&source);
572 ic = newnodef != route->from.node;
573 changed = changed || ic;
575 route->from.weak = 3;
576 getFieldFromNodeAndNameC(newnodef,route->from.cfield,&type,&kind,&ifield,&builtIn, &value, &cname);
577 if(ifield < 0) ConsoleMessage(
"bad FROM field ROUTE %s.%s TO %s.%s\n",route->from.cnode,route->from.cfield,route->to.cnode,route->to.cfield);
578 route->from.ifield = ifield;
579 route->from.builtIn = builtIn;
580 route->from.ftype = type;
583 else route->from.weak = 1;
587 newnodet = broto_search_ALLnames(context,route->to.cnode,&source);
588 ic = newnodet != route->to.node;
589 changed = changed || ic;
592 getFieldFromNodeAndNameC(newnodet,route->to.cfield,&type,&kind,&ifield,&builtIn,&value,&cname);
594 ConsoleMessage(
"bad TO field ROUTE %s.%s TO %s.%s\n",route->from.cnode,route->from.cfield,route->to.cnode,route->to.cfield);
595 route->to.ifield = ifield;
596 route->to.builtIn = builtIn;
597 route->to.ftype = type;
600 else route->to.weak = 1;
603 if(route->lastCommand){
605 CRoutes_RemoveSimpleB(route->from.node,route->from.ifield,route->from.builtIn,route->to.node,route->to.ifield,route->to.builtIn,route->ft);
606 route->lastCommand = 0;
608 route->from.node = newnodef;
609 route->to.node = newnodet;
610 if(route->from.node && route->to.node && route->from.ifield > -1 && route->to.ifield > -1){
611 route->lastCommand = 1;
612 CRoutes_RegisterSimpleB(route->from.node,route->from.ifield,route->from.builtIn,route->to.node,route->to.ifield,route->to.builtIn,route->ft);
616void update_weakRoutes(
struct X3D_Proto *context){
626 if(context && context->__ROUTES){
632 for(k=0;k<vectorSize(context->__ROUTES);k++){
634 if(route->from.weak || route->to.weak){
635 update_weakRoute(context,route);
645 resource_item_t *res;
650 switch (node->__loadstatus) {
651 case INLINE_INITIAL_STATE:
653 if (node->url.n == 0) {
654 node->__loadstatus = INLINE_STABLE;
659 res = resource_create_multi(&(node->url));
660 res->media_type = resm_unknown;
661 node->__loadstatus = INLINE_REQUEST_RESOURCE;
662 node->__loadResource = res;
667 case INLINE_REQUEST_RESOURCE:
668 res = node->__loadResource;
669 resource_identify(node->_parentResource, res);
672 res->actions = resa_download | resa_load;
674 resitem_enqueue(ml_new(res));
676 node->__loadstatus = INLINE_FETCHING_RESOURCE;
679 case INLINE_FETCHING_RESOURCE:
680 res = node->__loadResource;
684 if (res->status == ress_loaded) {
686 res->ectx = (
void*)node;
687 res->whereToPlaceData = X3D_NODE(node);
688 res->offsetFromWhereToPlaceData = offsetof (
struct X3D_Inline, __children);
689 res->actions = resa_process;
690 node->__loadstatus = INLINE_PARSING;
692 node->__protoFlags = ciflag_set(node->__protoFlags,1,0);
693 res->complete = FALSE;
696 resitem_enqueue(ml_new(res));
698 }
else if ((res->status == ress_failed) || (res->status == ress_invalid)) {
701 node->__loadstatus = INLINE_STABLE;
707 res = node->__loadResource;
712 if (res->status == ress_parsed) {
714 node->__loadstatus = INLINE_IMPORTING;
719 case INLINE_IMPORTING:
721 context = hasContext(node->_executionContext);
723 update_weakRoutes(context);
724 node->__loadstatus = INLINE_STABLE;
729 node->__loadstatus = INLINE_UN_IMPORTING;
733 case INLINE_UN_IMPORTING:
735 context = hasContext(node->_executionContext);
737 update_weakRoutes(context);
738 node->__loadstatus = INLINE_UNLOADING;
740 case INLINE_UNLOADING:
765 unload_broto(X3D_PROTO(node));
766 node->__loadstatus = INLINE_INITIAL_STATE;
775 if(0)printf(
"in prep_inline\n");
778 if ((node->__loadstatus != INLINE_STABLE && node->load) || (node->__loadstatus != INLINE_INITIAL_STATE && !node->load)) {
784 if(0)printf(
"in compile_inline\n");
788 REINITIALIZE_SORTED_NODES_FIELD(node->__children,node->_sortedChildren);
791 int loadchanged, urlchanged;
796 loadchanged = urlchanged = 0;
797 loadchanged = node->load != node->__oldload;
798 urlchanged = node->url.n != node->__oldurl.n || node->url.p != node->__oldurl.p;
799 if(loadchanged || urlchanged){
801 node->__loadstatus = INLINE_UN_IMPORTING;
802 if(loadchanged) node->__oldload = node->load;
803 if(urlchanged) node->__oldurl = node->url;
809void prep_unitscale (
struct X3D_Proto *ec);
810void fin_unitscale (
struct X3D_Proto *ec);
819 RETURN_FROM_CHILD_IF_NOT_FOR_ME
820 push_executionContext(X3D_NODE(node));
821 prep_unitscale(X3D_PROTO(node));
822 prep_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
824 prep_BBox((
struct BBoxFields*)&node->bboxCenter);
826 normalChildren(node->_sortedChildren);
829 fin_sibAffectors((
struct X3D_Node*)node,&node->__sibAffectors);
830 fin_unitscale(X3D_PROTO(node));
831 pop_executionContext();