34 #include <libFreeWRL.h>
37 #include "../vrml_parser/Structs.h"
38 #include "../vrml_parser/CRoutes.h"
39 #include "../main/headers.h"
40 #include "../vrml_parser/CParseGeneral.h"
41 #include "../main/Snapshot.h"
42 #include "../scenegraph/Collision.h"
43 #include "../scenegraph/quaternion.h"
44 #include "../scenegraph/Viewer.h"
45 #include "../input/SensInterps.h"
46 #include "../input/EAIHeaders.h"
47 #include "../input/EAIHelpers.h"
48 #include "../x3d_parser/Bindable.h"
58 void dumpOneNode(
int myptr);
59 void dumpOne_X3D_Node(
struct X3D_Node * boxptr);
60 void fudgeIfNeeded(
int myptr,
int myoffset);
73 static char *Multi_Struct_memptr (
int type,
char *memptr) {
86 case FIELDTYPE_MFInt32:
87 case FIELDTYPE_MFFloat:
88 case FIELDTYPE_MFRotation:
89 case FIELDTYPE_MFVec3f:
90 case FIELDTYPE_MFColorRGBA:
91 case FIELDTYPE_MFColor:
92 case FIELDTYPE_MFString:
93 case FIELDTYPE_MFVec2f:
96 retval = (
char *) (mp->p);
100 if (retval == memptr) printf (
"Multi_Struct_memptr, just returning original pointer...\n");
116 static int returnNumberOfRows(
int datatype,
union anyVrml *memptr) {
119 #define SF_TYPE(fttype, type, ttype) \
120 case FIELDTYPE_##fttype: return 1; break;
122 #define MF_TYPE(fttype, type, ttype) \
123 case FIELDTYPE_##fttype: return memptr->type.n; break;
125 #include "../vrml_parser/VrmlTypeList.h"
131 parseError(
"Unsupported type in defaultValue!");
150 void setField_fromJavascript (
struct X3D_Node *node,
char *field,
char *value,
int isXML) {
156 #ifdef SETFIELDVERBOSE
157 printf (
"\nsetField_fromJavascript, node %p field %s value %s\n", (
char*) node, field, value);
161 foffset = findRoutedFieldInFIELDNAMES(node,field,1);
163 ConsoleMessage (
"field %s is not a valid field of a node %s",field,stringNodeType(node->_nodeType));
164 printf (
"field %s is not a valid field of a node %s\n",field,stringNodeType(node->_nodeType));
169 #ifdef SETFIELDVERBOSE
170 printf (
"getting nodeOffsets for type %s field %s value %s\n",stringNodeType(node->_nodeType),field,value);
173 findFieldInOFFSETS(node->_nodeType, foffset, &coffset, &ctype, &ctmp);
175 #ifdef SETFIELDVERBOSE
176 printf (
"so, offset is %d, type %d value %s\n",coffset, ctype, value);
180 printf (
"setField_fromJavascript, trouble finding field %s in node %s\n",field,stringNodeType(node->_nodeType));
182 printf(
"this is a Proto...have to go through PROTO defs to get to it\n");
195 Parser_scanStringValueToMem(node, (
size_t) coffset, ctype, value, isXML);
204 static int setField_FromEAI_ToScript(
int tonode,
int toname,
205 int datatype,
void *data,
unsigned rowcount) {
210 #ifdef SETFIELDVERBOSE
211 printf (
"doing setField_FromEAI_ToScript, for script %u, nameIndex %u, type %s\n",tonode, toname, stringFieldtypeType(datatype));
215 case FIELDTYPE_SFBool:
216 case FIELDTYPE_SFFloat:
217 case FIELDTYPE_SFTime:
218 case FIELDTYPE_SFDouble:
219 case FIELDTYPE_SFInt32:
220 case FIELDTYPE_SFString:
223 datalen = returnElementLength(datatype) * returnElementRowSize(datatype);
224 #ifdef SETFIELDVERBOSE
225 printf (
"SFSingle in setField_FromEAI_ToScript, setting script, dataLength is made up of %d x %d x %d for %s\n",
226 returnElementLength(datatype), returnElementRowSize(datatype), rowcount, stringFieldtypeType(datatype));
229 set_one_ECMAtype (tonode, toname, datatype, data, datalen);
231 case FIELDTYPE_SFColor:
232 case FIELDTYPE_SFVec2f:
233 case FIELDTYPE_SFVec3f:
234 case FIELDTYPE_SFVec3d:
235 case FIELDTYPE_SFRotation:
237 datalen = returnElementLength(datatype) * returnElementRowSize(datatype);
238 #ifdef SETFIELDVERBOSE
239 printf (
"SFColor-style in setField_FromEAI_ToScript, setting script, dataLength is made up of %d x %d x %d for %s\n",
240 returnElementLength(datatype), returnElementRowSize(datatype), rowcount, stringFieldtypeType(datatype));
242 set_one_MultiElementType (tonode, toname, data, datalen);
244 case FIELDTYPE_SFNode:
245 #ifdef SETFIELDVERBOSE
246 printf (
"SFNode copy, tonode %u...\n",tonode);
249 datalen = returnElementLength(FIELDTYPE_SFNode);
250 set_one_MultiElementType (tonode, toname, data, datalen);
255 case FIELDTYPE_MFColor:
256 case FIELDTYPE_MFVec3f:
257 case FIELDTYPE_MFVec3d:
258 case FIELDTYPE_MFVec2f:
259 case FIELDTYPE_MFFloat:
260 case FIELDTYPE_MFTime:
261 case FIELDTYPE_MFInt32:
262 case FIELDTYPE_MFString:
263 case FIELDTYPE_MFNode:
264 case FIELDTYPE_MFRotation:
265 case FIELDTYPE_SFImage:
266 #ifdef SETFIELDVERBOSE
267 printf (
"going to call MF types in set_one_MFElementType rowcount %d\n",rowcount);
269 set_one_MFElementType(tonode, toname, datatype, data, rowcount);
272 printf(
"WARNING: setField_FromEAI_ToScript, type %s not handled yet\n",
273 stringFieldtypeType(datatype));
282 void fudgeIfNeeded(
int myptr,
int myoffset){
301 char *setnameIs = NULL ;
302 char *altnameIs = NULL ;
306 void *sourceNode = NULL;
307 void *destNode = NULL;
309 boxptr = getEAINodeFromTable(myptr,-1);
310 #ifdef SETFIELDVERBOSE
311 printf (
"%s,%d fudgeIfNeeded node %u -> %p\n",__FILE__,__LINE__, (
unsigned int)myptr, boxptr);
315 np = (
int *) NODE_OFFSETS[boxptr->_nodeType];
316 while (scanning && (*np != INT_ID_UNDEFINED)) {
322 if (myoffset == np[1]) {
323 #ifdef SETFIELDVERBOSE
324 printf(
"Field %d %s ", myc, stringFieldType(np[0]));
326 if (0 == strncmp(
"set_",stringFieldType(np[0]),4)) {
328 #ifdef SETFIELDVERBOSE
329 printf(
" , Found a set_ ; offset=%d\n",offset);
335 setnameIs = (
char *)stringFieldType(np[0]);
336 altnameIs = setnameIs+4;
345 relSet = returnRoutingElementLength(np[2]);
348 destNode = offsetPointer_deref(
void *, boxptr, offset);
350 #ifdef SETFIELDVERBOSE
361 if (!foundSet)
return;
363 #ifdef SETFIELDVERBOSE
364 printf(
"%s,%d setnameIs=%s , altnameIs=%s foundSet=%d relSet=%d\n",__FILE__,__LINE__,setnameIs,altnameIs,foundSet,relSet);
371 f_indx = findFieldInFIELDNAMES(altnameIs);
377 #ifdef SETFIELDVERBOSE
378 printf (
"field index %s is %d, it had better not be -1 !! \n",altnameIs,f_indx);
382 np = (
int *) NODE_OFFSETS[boxptr->_nodeType];
383 while (scanning && (*np != INT_ID_UNDEFINED)) {
385 if (0 != strncmp(stringFieldType(np[0]),
"_", 1) ) {
386 #ifdef SETFIELDVERBOSE
387 printf(
"Field %d %s", myc, stringFieldType(np[0]));
393 if (f_indx == np[0]) {
394 #ifdef SETFIELDVERBOSE
395 printf(
" , Found alternate name, ie %s f_indx=%d offset=%d\n",altnameIs,f_indx,np[1]);
400 relAlt = returnRoutingElementLength(np[2]);
403 sourceNode = offsetPointer_deref(
void *, boxptr, np[1]);
406 #ifdef SETFIELDVERBOSE
414 if (!foundAlt)
return;
416 #ifdef SETFIELDVERBOSE
417 printf(
"%s,%d setnameIs=%s , altnameIs=%s foundAlt=%d relAlt=%d\n",__FILE__,__LINE__,setnameIs,altnameIs,foundAlt,relAlt);
421 if (relAlt == relSet && relSet < 0) {
422 #ifdef SETFIELDVERBOSE
423 printf(
"%s,%d About to call Multimemcpy (boxptr=%p, boxptr=%p, destNode=%p, sourceNode=%p, relAlt=%d);\n",__FILE__, __LINE__, boxptr, boxptr, destNode, sourceNode, relAlt);
425 Multimemcpy (boxptr, boxptr, destNode, sourceNode, relAlt);
430 #ifdef SETFIELDVERBOSE
431 printf(
"=================================== Fudged %s,%d ==================================\n",__FILE__,__LINE__);
433 printf(
"====================================== %s,%d ======================================\n",__FILE__,__LINE__);
439 void dumpOneNode(
int myptr) {
441 bool eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
442 boxptr = getEAINodeFromTable(myptr,-1);
445 printf (
"GETFIELDDEFS, node %u -> %p\n",(
unsigned int)myptr, boxptr);
447 dumpOne_X3D_Node(boxptr) ;
450 void dumpOne_X3D_Node(
struct X3D_Node * boxptr) {
457 char utilBuf[EAIREADSIZE];
461 printf (
"makeFIELDDEFret have null node here \n");
465 printf (
"node type is %s\n",stringNodeType(boxptr->_nodeType));
468 np = (
int *) NODE_OFFSETS[boxptr->_nodeType];
472 if (0 != strncmp(stringFieldType(np[0]),
"_", 1) ) {
473 ctmp = (char) mapFieldTypeToEAItype(np[2]);
474 dtmp = mapEAItypeToFieldType(ctmp);
476 tmpptr = offsetPointer_deref (
char *, boxptr,np[1]);
477 printf(
"%s,%d ",__FILE__,__LINE__);
478 printf(
"Field %d %s , ", myc, stringFieldType(np[0]));
479 printf(
"offset=%d bytes , ", np[1]);
485 errcount = UtilEAI_Convert_mem_to_ASCII (dtmp,tmpptr, utilBuf);
487 printf (
"\t\tValue = %s\n",utilBuf);
489 printf (
"\t\tValue = indeterminate....\n");
500 unsigned int setField_FromEAI (
char *ptr) {
503 int nodeIndex, fieldIndex;
507 unsigned int scripttype;
519 #ifdef SETFIELDVERBOSE
520 printf (
"%s,%d setField_FromEAI, string :%s:\n",__FILE__,__LINE__,ptr);
526 while (*ptr==
' ')ptr++;
528 datatype = mapEAItypeToFieldType(nt);
534 retint=sscanf (ptr,
"%d %d %d",&nodeIndex, &fieldIndex, &scripttype);
535 if (retint != 3) ConsoleMessage (
"setField_FromEAI: error reading 3 numbers from the string :%s:\n",ptr);
536 #ifdef SETFIELDVERBOSE
537 printf(
"setField_FromEAI: nodeIndex=%d, fieldIndex=%d, scripttype=%d\n",nodeIndex, fieldIndex, scripttype);
540 while ((*ptr) >
' ') ptr++;
541 while ((*ptr) ==
' ') ptr++;
542 while ((*ptr) >
' ') ptr++;
543 while ((*ptr) ==
' ') ptr++;
544 while ((*ptr) >
' ') ptr++;
546 #ifdef SETFIELDVERBOSE
551 np = getEAINodeFromTable(nodeIndex,fieldIndex);
553 printf(
"=================================== Pre op %s,%d ==================================\n",__FILE__,__LINE__);
554 dumpOneNode(nodeIndex);
555 printf(
"====================================== %s,%d ======================================\n",__FILE__,__LINE__);
557 nt = getEAINodeTypeFromTable(nodeIndex);
559 printf (
"EAI_SendEvent, type %s, nodeptr (index %d) %u offset %d script type %d ",
560 stringFieldtypeType(datatype),nodeIndex, np->_nodeType, fieldIndex, scripttype);
561 printf (
"np->_nodeType %s\n",stringNodeType(np->_nodeType));
563 if (nt == EAI_NODETYPE_SCRIPT) printf (
"setField_FromEAI - sending to a script node!\n");
564 else if (nt == EAI_NODETYPE_PROTO) printf (
"setField_FromEAI - sending to a script node!\n");
565 else if(nt == EAI_NODETYPE_STANDARD) printf (
"setField_FromEAI - sending to a standard node!\n");
566 else printf (
"setField_FromEAI - unknown type!\n");
574 if (scripttype == EAI_NODETYPE_SCRIPT) {
576 memptr = (
char *) &myAnyValue;
578 memptr = (
char *) getEAIMemoryPointer (nodeIndex,fieldIndex);
581 offset = getEAIActualOffset(nodeIndex, fieldIndex);
583 nodeptr = getEAINodeFromTable(nodeIndex,fieldIndex);
588 while (*ptr ==
' ') ptr++;
590 #ifdef SETFIELDVERBOSE
591 printf (
"setField_FromEAI EAI_SendEvent, event string now is :%s:\n",ptr);
596 if (strncmp(
"ONEVAL ",ptr, strlen(
"ONEVAL ")) == 0) {
597 #ifdef SETFIELDVERBOSE
598 printf (
"%s,%d This is a ONEVAL operation\n",__FILE__,__LINE__);
601 fudgeIfNeeded(nodeIndex,myoffset);
603 ptr += strlen (
"ONEVAL ");
606 while (*ptr==
' ')ptr++;
607 retint=sscanf (ptr,
"%d",&valIndex);
608 #ifdef SETFIELDVERBOSE
609 printf (
"%s,%d Request to set element %d\n",__FILE__,__LINE__,valIndex);
612 if (retint != 1) ConsoleMessage (
"setField_FromEAI: error reading 1 numbers from the string :%s:\n",ptr);
613 while (*ptr>
' ')ptr++;
614 while (*ptr==
' ')ptr++;
618 #ifdef SETFIELDVERBOSE
619 printf (
"%s,%d now, we have valIndex %d, tcol->n %d\n",__FILE__,__LINE__,valIndex,tcol->n);
622 if (valIndex >= tcol->n) {
633 malSize = (valIndex+1) * returnElementLength(datatype) * returnElementRowSize(datatype);
634 nmemptr = MALLOC(
void *, malSize);
637 bzero (nmemptr,(
size_t)malSize);
641 memcpy (nmemptr,tcol->p, tcol->n * returnElementLength(datatype) * returnElementRowSize(datatype));
646 if (datatype == FIELDTYPE_MFString) {
649 for (count = 0; count <=valIndex ; count++) {
652 if ((*strarr) == NULL) *strarr = newASCIIString (
"");
661 tcol->n = valIndex+1;
665 #ifdef SETFIELDVERBOSE
666 printf (
"%s,%d Size OK, replacing element %d in %d elements\n",__FILE__,__LINE__,valIndex,tcol->n);
672 memptr = Multi_Struct_memptr(datatype, (
void *) memptr);
675 memptr += valIndex * returnElementLength(datatype) * returnElementRowSize(datatype);
678 datatype = convertToSFType(datatype);
681 myptr = X3D_NODE(memptr);
684 #ifdef SETFIELDVERBOSE
685 printf (
"%s,%d Not a ONEVAL operation\n",__FILE__,__LINE__);
690 eol = strchr (ptr,
'\n');
if (eol != NULL) *eol =
'\0';
698 Parser_scanStringValueToMem(myptr,myoffset,datatype,ptr,FALSE);
700 if (scripttype == EAI_NODETYPE_SCRIPT) {
705 sp = (
struct Shader_Script *) (X3D_SCRIPT(nodeptr)->__scriptObj);
707 mark_script (sp->num);
710 rowCount = returnNumberOfRows(datatype,(
union anyVrml *) memptr);
711 #ifdef SETFIELDVERBOSE
712 printf(
"%s,%d rowCount=%d\n",__FILE__,__LINE__,rowCount);
716 memptr = Multi_Struct_memptr(datatype, memptr);
718 setField_FromEAI_ToScript(sp->num,offset,datatype,memptr,rowCount);
728 update_node ((
void *)nodeptr);
731 MARK_EVENT (X3D_NODE(nodeptr),offset);
735 #ifdef SETFIELDVERBOSE
736 printf(
"================================== Post op %s,%d ==================================\n",__FILE__,__LINE__);
737 dumpOneNode(nodeIndex);
738 printf(
"====================================== %s,%d ======================================\n",__FILE__,__LINE__);
742 if (eol != NULL) *eol =
'\n';
749 char *findFIELDNAMESfromNodeOffset(
struct X3D_Node *node,
int offset) {
751 if (node == 0)
return "unknown";
753 np = (
int *) NODE_OFFSETS[node->_nodeType];
756 while ((*np != -1) && (*np != offset)) np +=5;
758 if (*np == -1)
return "fieldNotFound";
762 return ((
char *) FIELDNAMES[*np]);
767 int findFieldInARR(
const char* field,
const char** arr,
size_t cnt)
772 if (field == NULL)
return -1;
774 #ifdef SETFIELDVERBOSE
775 if (field[0] ==
'_') {
776 printf (
"findFieldInFIELDNAMES - internal field %s\n",field);
780 mystrlen = strlen(field);
781 for (x=0; x!=cnt; ++x) {
782 if (strlen(arr[x]) == mystrlen) {
783 if (strcmp(field, arr[x])==0)
return x;
789 #define DEF_FINDFIELD(arr) \
790 int findFieldIn##arr(const char* field) \
792 return findFieldInARR(field, arr, arr##_COUNT); \
794 DEF_FINDFIELD(FIELDNAMES)
796 DEF_FINDFIELD(EXPOSED_FIELD)
797 DEF_FINDFIELD(EVENT_IN)
798 DEF_FINDFIELD(EVENT_OUT)
799 DEF_FINDFIELD(KEYWORDS)
800 DEF_FINDFIELD(PROTOKEYWORDS)
802 DEF_FINDFIELD(PROFILES)
803 DEF_FINDFIELD(COMPONENTS)
804 DEF_FINDFIELD(FIELDTYPES)
805 DEF_FINDFIELD(X3DSPECIAL)
806 DEF_FINDFIELD(GEOSPATIAL)
807 DEF_FINDFIELD(MULTITEXTUREMODE);
808 DEF_FINDFIELD(MULTITEXTURESOURCE);
809 DEF_FINDFIELD(MULTITEXTUREFUNCTION);
814 int findRoutedFieldInARR (struct
X3D_Node * node, const
char *field,
int fromTo,
815 const
char** arr,
size_t cnt, BOOL user) {
822 #define FIELDCHECK(fld) \
824 if (user) return retval; \
825 {int fieldNamesIndex = findIndexInFIELDNAMES(retval, arr, cnt); \
826 if (fieldNamesIndex >= 0) { \
827 findFieldInOFFSETS (node->_nodeType, fieldNamesIndex,\
831 if (a >= 0) return retval; \
837 retval = findFieldInARR(field, arr, cnt);
842 strncpy (mychar, field, 100);
844 if (strlen(field) > strlen(
"set_"))
845 retval=findFieldInARR(mychar+strlen(
"set_"), arr, cnt);
847 if (strlen(field) > strlen(
"_changed")) {
848 mychar[strlen(field) - strlen(
"_changed")] =
'\0';
849 retval = findFieldInARR(mychar, arr, cnt);
858 strcpy (mychar,
"set_");
859 strncat (mychar, field,100);
860 retval=findFieldInARR(mychar, arr, cnt);
862 strncpy (mychar, field, 100);
863 strcat (mychar,
"_changed");
864 retval = findFieldInARR(mychar, arr, cnt);
872 #define DEF_FINDROUTEDFIELD(arr) \
873 int findRoutedFieldIn##arr(struct X3D_Node* node, const char* field, int fromTo) \
875 return findRoutedFieldInARR(node, field, fromTo, arr, arr##_COUNT, FALSE); \
877 DEF_FINDROUTEDFIELD(FIELDNAMES)
878 DEF_FINDROUTEDFIELD(EXPOSED_FIELD)
879 DEF_FINDROUTEDFIELD(EVENT_IN)
880 DEF_FINDROUTEDFIELD(EVENT_OUT)
884 void findFieldInOFFSETS(
int nodeType, const
int field,
int *coffset,
int *ctype,
int *ckind) {
889 x = (
int *) NODE_OFFSETS[nodeType];
891 #ifdef SETFIELDVERBOSE
892 printf (
"findFieldInOFFSETS, nodeType %s\n",stringNodeType(nodeType));
893 printf (
"findFieldInOffsets, comparing %d to %d\n",*x, field);
896 while ((*x != field) && (*x != -1)) {
900 x++; *coffset = (int)*x; x++; *ctype = (int)*x; x++; *ckind = (int)*x; x++; X3DLevel = (int)*x;
902 #ifdef SETFIELDVERBOSE
903 printf (
"found field, coffset %d ctype %d ckind %d X3DLevel %x\n",*coffset, *ctype, *ckind, X3DLevel);
907 if (gglobal()->internalc.global_strictParsing) {
908 if (inputFileVersion[0] == 2) {
909 if ((X3DLevel & SPEC_VRML) == SPEC_VRML) {
912 }
else if (inputFileVersion[0] == 3) {
913 switch (inputFileVersion[1]) {
914 case 0: mask = SPEC_X3D30;
break;
915 case 1: mask = SPEC_X3D31;
break;
916 case 2: mask = SPEC_X3D32;
break;
917 case 3: mask = SPEC_X3D33;
break;
918 case 4: mask = SPEC_X3D34;
break;
919 default: {printf (
"unknown X3D level %d\n",inputFileVersion[1]);
923 if ((X3DLevel & mask) == mask) {
927 printf (
"unknown input file version %d for strictParsing! help!\n",inputFileVersion[0]);
929 ConsoleMessage (
"strictParsing, Node %s field %s is not valid for X3D version %d.%d",
930 stringNodeType(nodeType),stringFieldType(field),inputFileVersion[0],inputFileVersion[1]);
936 #ifdef SETFIELDVERBOSE
937 printf (
"did not find field %d in OFFSETS\n",field);
940 *coffset = -1; *ctype = -1, *ckind = -1;
955 AddRemoveChildren (parent, tn, &strp, 1, ar,__FILE__,__LINE__);
961 int findIndexInFIELDNAMES(
int index,
const char** arr,
size_t arrCnt) {
969 for(i=0; i!=FIELDNAMES_COUNT; ++i) {
970 if(!strcmp(FIELDNAMES[i], arr[index]))