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