FreeWRL / FreeX3D 4.3.0
fieldGet.c
1/*
2
3
4Javascript C language binding.
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
35#include <libFreeWRL.h>
36
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/EAIHeaders.h"
46#include "../input/EAIHelpers.h" /* resolving implicit declarations */
47#include "../input/SensInterps.h"
48#include "../x3d_parser/Bindable.h"
49
50#include "JScript.h"
51#include "CScripts.h"
52#include "fieldSet.h"
53#include "fieldGet.h"
54
55
56/********************************************************************
57
58getField_ToJavascript.
59
60this sends events to scripts that have eventIns defined.
61
62********************************************************************/
63
64void getField_ToJavascript (int num, int fromoffset) {
65 int ignored;
66 struct CRjsnameStruct *JSparamnames = getJSparamnames();
67
68 UNUSED(ignored); // compiler warning mitigation
69
70 #ifdef SETFIELDVERBOSE
71 printf ("getField_ToJavascript, from offset %d name %s type %d num=%d\n",
72 fromoffset,JSparamnames[fromoffset].name,JSparamnames[fromoffset].type,num);
73 #endif
74 /* set the parameter */
75 /* see comments in gatherScriptEventOuts to see exact formats */
76
77 switch (JSparamnames[fromoffset].type) {
78 case FIELDTYPE_SFBool:
79 case FIELDTYPE_SFFloat:
80 case FIELDTYPE_SFTime:
81 case FIELDTYPE_SFDouble:
82 case FIELDTYPE_SFInt32:
83 case FIELDTYPE_SFString:
84 setScriptECMAtype(num);
85 break;
86 case FIELDTYPE_SFColor:
87 case FIELDTYPE_SFColorRGBA:
88 case FIELDTYPE_SFNode:
89 case FIELDTYPE_SFVec2f:
90 case FIELDTYPE_SFVec3f:
91 case FIELDTYPE_SFVec4f:
92 case FIELDTYPE_SFVec2d:
93 case FIELDTYPE_SFVec3d:
94 case FIELDTYPE_SFVec4d:
95 case FIELDTYPE_SFRotation:
96 case FIELDTYPE_SFImage:
97 case FIELDTYPE_SFMatrix3f:
98 case FIELDTYPE_SFMatrix4f:
99 case FIELDTYPE_SFMatrix3d:
100 case FIELDTYPE_SFMatrix4d:
101 setScriptMultiElementtype(num);
102 break;
103 case FIELDTYPE_MFFloat:
104 case FIELDTYPE_MFBool:
105 case FIELDTYPE_MFInt32:
106 case FIELDTYPE_MFTime:
107 case FIELDTYPE_MFDouble:
108 case FIELDTYPE_MFColor:
109 case FIELDTYPE_MFColorRGBA:
110 case FIELDTYPE_MFVec2f:
111 case FIELDTYPE_MFVec3f:
112 case FIELDTYPE_MFVec4f:
113 case FIELDTYPE_MFVec2d:
114 case FIELDTYPE_MFVec3d:
115 case FIELDTYPE_MFVec4d:
116 case FIELDTYPE_MFString:
117 case FIELDTYPE_MFNode:
118 case FIELDTYPE_MFRotation:
119 case FIELDTYPE_MFImage:
120 case FIELDTYPE_MFMatrix3f:
121 case FIELDTYPE_MFMatrix4f:
122 case FIELDTYPE_MFMatrix3d:
123 case FIELDTYPE_MFMatrix4d:
124 ignored = setMFElementtype(num);
125 break;
126 default : {
127 printf("WARNING: sendScriptEventIn type %s not handled yet\n",
128 FIELDTYPES[JSparamnames[fromoffset].type]);
129 }
130 }
131}
132
133//void getField_ToJavascript_B(int num, int fromoffset) {
134void getField_ToJavascript_B(int shader_num, int fieldOffset, int type, union anyVrml *any, int len) {
135
136 #ifdef SETFIELDVERBOSE
137 struct CRjsnameStruct *JSparamnames = getJSparamnames();
138
139 printf ("getField_ToJavascript, from offset %d type %d num=%d\n",
140 fieldOffset,JSparamnames[fieldOffset].type,shader_num);
141 #endif
142
143 /* set the parameter */
144 /* see comments in gatherScriptEventOuts to see exact formats */
145
146 switch (type) {
147 case FIELDTYPE_SFBool:
148 case FIELDTYPE_SFFloat:
149 case FIELDTYPE_SFTime:
150 case FIELDTYPE_SFDouble:
151 case FIELDTYPE_SFInt32:
152 case FIELDTYPE_SFString:
153 //setScriptECMAtype(num);
154 set_one_ECMAtype(shader_num, fieldOffset, type, any, len);
155 break;
156 case FIELDTYPE_SFColor:
157 case FIELDTYPE_SFColorRGBA:
158 case FIELDTYPE_SFNode:
159 case FIELDTYPE_SFVec2f:
160 case FIELDTYPE_SFVec3f:
161 case FIELDTYPE_SFVec4f:
162 case FIELDTYPE_SFVec2d:
163 case FIELDTYPE_SFVec3d:
164 case FIELDTYPE_SFVec4d:
165 case FIELDTYPE_SFMatrix3f:
166 case FIELDTYPE_SFMatrix4f:
167 case FIELDTYPE_SFMatrix3d:
168 case FIELDTYPE_SFMatrix4d:
169 case FIELDTYPE_SFRotation:
170 case FIELDTYPE_SFImage:
171 set_one_MultiElementType(shader_num, fieldOffset, any, len);
172 break;
173 case FIELDTYPE_MFFloat:
174 case FIELDTYPE_MFInt32:
175 case FIELDTYPE_MFBool:
176 case FIELDTYPE_MFTime:
177 case FIELDTYPE_MFDouble:
178 case FIELDTYPE_MFColor:
179 case FIELDTYPE_MFColorRGBA:
180 case FIELDTYPE_MFVec2f:
181 case FIELDTYPE_MFVec3f:
182 case FIELDTYPE_MFVec4f:
183 case FIELDTYPE_MFVec2d:
184 case FIELDTYPE_MFVec3d:
185 case FIELDTYPE_MFVec4d:
186 case FIELDTYPE_MFString:
187 case FIELDTYPE_MFNode:
188 case FIELDTYPE_MFRotation:
189 case FIELDTYPE_MFImage:
190 case FIELDTYPE_MFMatrix3f:
191 case FIELDTYPE_MFMatrix4f:
192 case FIELDTYPE_MFMatrix3d:
193 case FIELDTYPE_MFMatrix4d:
194
195 set_one_MFElementType(shader_num, fieldOffset, type, (void *)any,len);
196
197 break;
198 default : {
199 printf("WARNING: sendScriptEventIn type %s not handled yet\n",
200 FIELDTYPES[type]);
201 }
202 }
203}
204
205
206
207
208/* setMFElementtype called by getField_ToJavascript for
209 case FIELDTYPE_MFColor:
210 case FIELDTYPE_MFVec3f:
211 case FIELDTYPE_MFVec2f:
212 case FIELDTYPE_MFFloat:
213 case FIELDTYPE_MFTime:
214 case FIELDTYPE_MFInt32:
215 case FIELDTYPE_MFString:
216 case FIELDTYPE_MFNode:
217 case FIELDTYPE_MFRotation:
218 case FIELDTYPE_SFImage:
219*/
220
221
222int setMFElementtype (int num) {
223 void * fn;
224 int fptr;
225 int len;
226 int to_counter;
227 CRnodeStruct *to_ptr = NULL;
228 char *pptr;
229 struct Multi_Node *mfp;
230 struct CRStruct *CRoutes = getCRoutes();
231 struct CRjsnameStruct *JSparamnames = getJSparamnames();
232
233
234 #ifdef SETFIELDVERBOSE
235 printf("------------BEGIN setMFElementtype ---------------\n");
236 #endif
237
238
239 fn = (void *)CRoutes[num].routeFromNode;
240 fptr = CRoutes[num].fnptr;
241
242 /* we can do arithmetic on character pointers; so we have to cast void *
243 to char * here */
244 pptr = offsetPointer_deref (char *, fn, fptr);
245
246 len = CRoutes[num].len;
247
248 /* is this from a MFElementType? positive lengths in routing table == easy memcpy types */
249 if (len <= 0) {
250 mfp = (struct Multi_Node *) pptr;
251
252 /* check Multimemcpy for C to C routing for this type */
253 /* get the number of elements */
254 len = mfp->n;
255 pptr = (char *) mfp->p; /* pptr is a char * just for math stuff */
256 #ifdef SETFIELDVERBOSE
257 printf ("setMFElementtype, len now %d, from %d\n",len,fn);
258 #endif
259 } else {
260 /* SFImages will have a length of greater than zero */
261 /* printf ("setMFElementtype, length is greater than 0 (%d), how can this be?\n",len); */
262 }
263
264 /* go through all the nodes that this script sends to for this entry in the CRoutes table */
265 for (to_counter = 0; to_counter < CRoutes[num].tonode_count; to_counter++) {
266 struct Shader_Script *myObj;
267
268 to_ptr = &(CRoutes[num].tonodes[to_counter]);
269 myObj = X3D_SCRIPT(to_ptr->routeToNode)->__scriptObj;
270
271 #ifdef SETFIELDVERBOSE
272 printf ("got a script event! index %d type %d\n",
273 num, CRoutes[num].direction_flag);
274/*
275 printf ("\tfrom %#x from ptr %#x\n\tto %#x toptr %#x\n",fn,fptr,tn,to_ptr->foffset);
276 printf ("\tfrom %d from ptr %d\n\tto %d toptr %d\n",fn,fptr,tn,to_ptr->foffset);
277*/
278 printf ("\tdata length %d\n",len);
279 printf ("and, sending it to %s as type %d\n",JSparamnames[to_ptr->foffset].name,
280 JSparamnames[to_ptr->foffset].type);
281 #endif
282
283 set_one_MFElementType(myObj->num, to_ptr->foffset, JSparamnames[to_ptr->foffset].type, (void *)pptr,len);
284 }
285
286
287 #ifdef SETFIELDVERBOSE
288 printf("------------END setMFElementtype ---------------\n");
289 #endif
290 return FALSE; /* return value never checked; #defines expect a return value */
291}
292
293
294/* setScriptMultiElementtype called by getField_ToJavascript for
295 case FIELDTYPE_SFColor:
296 case FIELDTYPE_SFNode:
297 case FIELDTYPE_SFVec2f:
298 case FIELDTYPE_SFVec3f:
299 case FIELDTYPE_SFRotation:
300*/
301
302void setScriptMultiElementtype (int num)
303{
304 int tptr, fptr;
305 int len;
306 int to_counter;
307 void *fn;
308
309 CRnodeStruct *to_ptr = NULL;
310 struct CRStruct *CRoutes = getCRoutes();
311
312
313 fn = (void *)CRoutes[num].routeFromNode;
314 fptr = CRoutes[num].fnptr;
315 if (CRoutes[num].len == ROUTING_SFNODE) len = returnElementLength(FIELDTYPE_SFNode);
316 else if (CRoutes[num].len < 0) {
317 ConsoleMessage ("setScriptMultiElementtype - len of %d unhandled\n",CRoutes[num].len);
318 return;
319 } else {
320 len = CRoutes[num].len;
321 }
322
323 for (to_counter = 0; to_counter < CRoutes[num].tonode_count; to_counter++) {
324 struct Shader_Script *myObj;
325
326 to_ptr = &(CRoutes[num].tonodes[to_counter]);
327 myObj = X3D_SCRIPT(to_ptr->routeToNode)->__scriptObj;
328
329 /* the to_node should be a script number; it will be a small integer */
330 tptr = to_ptr->foffset;
331
332 #ifdef SETFIELDVERBOSE
333 printf ("got a script event! index %d type %d\n",
334 num, CRoutes[num].direction_flag);
335 printf ("\tfrom %#x from ptr %#x\n\tto %#x toptr %#x\n",fn,fptr,myObj->num,tptr);
336 printf ("\tdata length %d\n",len);
337 printf ("setScriptMultiElementtype here script number %d tptr %d len %d\n",myObj->num, tptr,len);
338 #endif
339
340 fn = offsetPointer_deref(void*,fn,fptr); /*fn += fptr;*/
341
342 set_one_MultiElementType (myObj->num, tptr, fn, len);
343 }
344}
345
346
347
348
349/* convert a number in memory to a printable type. Used to send back EVents, or replies to
350 the SAI/EAI client program. */
351
352void EAI_Convert_mem_to_ASCII (int id, char *reptype, int type, char *memptr, char *buf) {
353
354 char utilBuf[EAIREADSIZE];
355 int errcount;
356 memset(utilBuf,'\0',sizeof(utilBuf));
357
358 errcount = UtilEAI_Convert_mem_to_ASCII (type,memptr, utilBuf);
359 if (0 == errcount) {
360 sprintf (buf,"%s\n%f\n%d\n%s",reptype,TickTime(),id, utilBuf);
361 } else {
362 sprintf (buf,"%s\n%f\n%d\n%s",reptype,TickTime(),id, "indeterminate....");
363 }
364}
365
366/* Utility routine to convert a value in memory to a printable type. */
367
368int UtilEAI_Convert_mem_to_ASCII (int type, char *memptr, char *buf) { /* Returns errcount */
369
370 double dval;
371 float fl[4];
372 double dl[4];
373 float *fp;
374 int *ip;
375 int ival;
376 struct X3D_Node *uval;
377 int row; /* MF* counter */
378 struct Multi_String *MSptr; /* MFString pointer */
379 struct Multi_Node *MNptr; /* MFNode pointer */
380 struct Multi_Color *MCptr; /* MFColor pointer */
381 char *ptr; /* used for building up return string */
382 struct Uni_String *svptr;
383 char *retSFString;
384
385 int numPerRow; /* 1, 2, 3 or 4 floats per row of this MF? */
386 int i, errcount;
387
388 /* used because of endian problems... */
389 int *intptr;
390 int eaiverbose;
391 eaiverbose = gglobal()->EAI_C_CommonFunctions.eaiverbose;
392 intptr = (int *) memptr;
393
394/* printf("%s,%d UtilEAI_Convert_mem_to_ASCII (type=%d , memptr=%p intptr=%p ....)\n",__FILE__,__LINE__,type,memptr,intptr); */
395
396 errcount=0;
397 switch (type) {
398 case FIELDTYPE_SFBool: {
399 if (eaiverbose) {
400 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFBOOL - value %d; TRUE %d false %d\n",*intptr,TRUE,FALSE);
401 }
402
403 if (*intptr == 1) sprintf (buf,"TRUE");
404 else sprintf (buf,"FALSE");
405 break;
406 }
407
408 case FIELDTYPE_SFDouble:
409 case FIELDTYPE_SFTime: {
410 if (eaiverbose) {
411 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFTIME\n");
412 }
413 memcpy(&dval,memptr,sizeof(double));
414 sprintf (buf, "%lf",dval);
415 break;
416 }
417
418 case FIELDTYPE_SFInt32: {
419 if (eaiverbose) {
420 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFINT32\n");
421 }
422 memcpy(&ival,memptr,sizeof(int));
423 sprintf (buf, "%d",ival);
424 break;
425 }
426
427 case FIELDTYPE_SFNode: {
428 if (eaiverbose) {
429 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFNODE\n");
430 }
431 memcpy((void *)&uval,(void *)memptr,sizeof(void *));
432 sprintf (buf, "%u",registerEAINodeForAccess(X3D_NODE(uval)));
433 break;
434 }
435
436 case FIELDTYPE_SFFloat: {
437 if (eaiverbose) {
438 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFFLOAT\n");
439 }
440
441 memcpy(fl,memptr,sizeof(float));
442 sprintf (buf, "%f",fl[0]);
443 break;
444 }
445
446 case FIELDTYPE_SFVec3f:
447 case FIELDTYPE_SFColor: {
448 if (eaiverbose) {
449 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFCOLOR or EAI_SFVEC3F\n");
450 }
451 memcpy(fl,memptr,sizeof(float)*3);
452 sprintf (buf, "%f %f %f",fl[0],fl[1],fl[2]);
453 break;
454 }
455
456 case FIELDTYPE_SFVec3d: {
457 if (eaiverbose) {
458 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFVEC3D\n");
459 }
460 memcpy(dl,memptr,sizeof(double)*3);
461 sprintf (buf, "%lf %lf %lf",dl[0],dl[1],dl[2]);
462 break;
463 }
464
465 case FIELDTYPE_SFVec2f: {
466 if (eaiverbose) {
467 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFVEC2F\n");
468 }
469 memcpy(fl,memptr,sizeof(float)*2);
470 sprintf (buf, "%f %f",fl[0],fl[1]);
471 break;
472 }
473
474 case FIELDTYPE_SFColorRGBA:
475 case FIELDTYPE_SFRotation: {
476 if (eaiverbose) {
477 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFROTATION\n");
478 }
479
480 memcpy(fl,memptr,sizeof(float)*4);
481 sprintf (buf, "%f %f %f %f",fl[0],fl[1],fl[2],fl[3]);
482 break;
483 }
484
485 case FIELDTYPE_SFImage:
486 case FIELDTYPE_SFString: {
487 uintptr_t *xx;
488
489 if (eaiverbose) {
490 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_SFSTRING\n");
491 }
492
493 /* get the pointer to the string, do this in a couple of steps... */
494 svptr = (struct Uni_String *)memptr;
495 xx= (uintptr_t *) memptr;
496 svptr = (struct Uni_String *) *xx;
497
498 retSFString = (char *)svptr->strptr;
499 sprintf (buf, "\"%s\"",retSFString);
500 break;
501 }
502
503 case FIELDTYPE_MFString: {
504 if (eaiverbose) {
505 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_MFSTRING\n");
506 }
507
508 /* make the Multi_String pointer */
509 MSptr = (struct Multi_String *) memptr;
510
511 /* printf ("UtilEAI_Convert_mem_to_ASCII: EAI_MFString, there are %d strings\n",(*MSptr).n);*/
512 ptr = buf + strlen(buf);
513
514 for (row=0; row<(*MSptr).n; row++) {
515 /* printf ("UtilEAI_Convert_mem_to_ASCII: String %d is %s\n",row,(*MSptr).p[row]->strptr); */
516 if (strlen ((*MSptr).p[row]->strptr) == 0) {
517 sprintf (ptr, "\"\" "); /* encode junk for Java side.*/
518 } else {
519 sprintf (ptr, "\"%s\" ",(*MSptr).p[row]->strptr);
520 }
521 /* printf ("UtilEAI_Convert_mem_to_ASCII: buf now is %s\n",buf); */
522 ptr = buf + strlen (buf);
523 }
524
525 break;
526 }
527
528 case FIELDTYPE_MFNode: {
529 MNptr = (struct Multi_Node *) memptr;
530
531 if (eaiverbose) {
532 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_MFNode, there are %d nodes at %p\n",(*MNptr).n,memptr);
533 }
534
535 ptr = buf + strlen(buf);
536
537 for (row=0; row<(*MNptr).n; row++) {
538 sprintf (ptr, "%d ",registerEAINodeForAccess(X3D_NODE((*MNptr).p[row])));
539 ptr = buf + strlen (buf);
540 }
541 break;
542 }
543
544 case FIELDTYPE_MFInt32: {
545 MCptr = (struct Multi_Color *) memptr;
546 if (eaiverbose) {
547 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_MFColor, there are %d nodes at %p\n",(*MCptr).n,memptr);
548 }
549
550 sprintf (buf, "%d \n",(*MCptr).n);
551 ptr = buf + strlen(buf);
552
553 ip = (int *) (*MCptr).p;
554 for (row=0; row<(*MCptr).n; row++) {
555 sprintf (ptr, "%d \n",*ip);
556 ip++;
557 /* printf ("UtilEAI_Convert_mem_to_ASCII: line %d is ",row,ptr); */
558 ptr = buf + strlen (buf);
559 }
560
561 break;
562 }
563
564 case FIELDTYPE_MFFloat:
565 case FIELDTYPE_MFVec2f:
566 case FIELDTYPE_MFVec3f:
567 case FIELDTYPE_MFRotation:
568 case FIELDTYPE_MFColorRGBA:
569 case FIELDTYPE_MFColor: {
570 numPerRow=3;
571 if (type==FIELDTYPE_MFFloat) {numPerRow=1;}
572 else if (type==FIELDTYPE_MFVec2f) {numPerRow=2;}
573 else if (type==FIELDTYPE_MFRotation) {numPerRow=4;}
574 else if (type==FIELDTYPE_MFColorRGBA) {numPerRow=4;}
575
576 MCptr = (struct Multi_Color *) memptr;
577 if (eaiverbose) {
578 printf ("UtilEAI_Convert_mem_to_ASCII: EAI_MFColor, there are %d nodes at %p\n",(*MCptr).n,memptr);
579 }
580
581 sprintf (buf, "%d \n",(*MCptr).n);
582 ptr = buf + strlen(buf);
583
584
585 fp = (float *) (*MCptr).p;
586 for (row=0; row<(*MCptr).n; row++) {
587 for (i=0; i<numPerRow; i++) {
588 fl[i] = *fp; fp++;
589 }
590 switch (numPerRow) {
591 case 1:
592 sprintf (ptr, "%f \n",fl[0]); break;
593 case 2:
594 sprintf (ptr, "%f %f \n",fl[0],fl[1]); break;
595 case 3:
596 sprintf (ptr, "%f %f %f \n",fl[0],fl[1],fl[2]); break;
597 case 4:
598 sprintf (ptr, "%f %f %f %f \n",fl[0],fl[1],fl[2],fl[3]); break;
599 }
600 /* printf ("UtilEAI_Convert_mem_to_ASCII: line %d is ",row,ptr); */
601 ptr = buf + strlen (buf);
602 }
603
604 break;
605 }
606 default: {
607 errcount++;
608 printf ("UtilEAI_Convert_mem_to_ASCII: EAI, type %d (%s) not handled yet\n",type,stringFieldtypeType (type));
609 }
610
611
612 }
613 return errcount ;
614}