Project

General

Profile

TUIO Scol plugin
scolplugin.cpp
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2013 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
25 /*
26  TUIO interface for Scol
27  First version : MAY 2013
28  Author : Bastien BOURINEAU
29 */
30 
31 #include "sTuio.h"
32 #include <scolPlugin.h>
33 #include <scolMemoryHelper.hpp>
34 
36 #ifdef SCOL_STATIC
37 extern cbmachine ww;
38 extern mmachine mm;
39 #else
40 cbmachine ww;
41 mmachine mm;
42 #endif
43 
44 int OBJTUIOSCOL;
45 int TUIO_CURSOR_ADD_CB;
46 int SCOL_TUIO_CURSOR_ADD_CB = 0;
47 int TUIO_CURSOR_REMOVE_CB;
48 int SCOL_TUIO_CURSOR_REMOVE_CB = 1;
49 int TUIO_CURSOR_UPDATE_CB;
50 int SCOL_TUIO_CURSOR_UPDATE_CB = 2;
51 
52 int TUIO_OBJECT_ADD_CB;
53 int SCOL_TUIO_OBJECT_ADD_CB = 3;
54 int TUIO_OBJECT_REMOVE_CB;
55 int SCOL_TUIO_OBJECT_REMOVE_CB = 4;
56 int TUIO_OBJECT_UPDATE_CB;
57 int SCOL_TUIO_OBJECT_UPDATE_CB = 5;
58 
59 
81 int destroyTuioObj(mmachine m, SCOL_PTR_TYPE handsys, int handscol) {
82  // Read the first element of a TAB element (table of object)
83  STuioClient* tuioobj = MMgetPointer<STuioClient*>(m, MTOP(handscol));
84  SAFE_DELETE(tuioobj);
85 
86  MMsetPointer(m, MTOP(handscol), NULL);
87 
88  // Display debug message
89  MMechostr(MSKDEBUG, "ObjTUIO destroyed.\n");
90  return 0;
91 }
92 
93 
103 int _CRTuioClient(mmachine m)
104 {
105 #ifdef _SCOL_DEBUG_
106  MMechostr(0,"_CRTuioClient\n");
107 #endif
108  // Declare local variables
109  int k = 0;
110 
111  int sPort = MMpull(m);
112 
113  // Get the channel without pulling it (first element in the stack)
114  int channel = MMget(m, 0);
115 
116  // Test the channel
117  if (channel == NIL)
118  {
119  MMechostr(MSKDEBUG, "Channel NIL\n");
120  MMset(m, 0, NIL);
121  return 0;
122  }
123 
124  int port = 3333;
125  if (sPort != NIL)
126  port = MTOI(sPort);
127 
128  STuioClient* tuioObj = new STuioClient(port);
129  if (tuioObj == NULL)
130  {
131  MMechostr(MSKDEBUG, "tuioObj failed\n");
132  SAFE_DELETE(tuioObj);
133  MMset(m, 0, NIL);
134  return 0;
135  }
136 
137  // Allocate a space in the stack for a table of object
138  if ((MMpushPointer(m, tuioObj) != 0))
139  {
140  SAFE_DELETE(tuioObj);
141  MMset(m, 0, NIL);
142  return MERRMEM;
143  }
144 
145  // Create a new scol joypad object
146  k = OBJcreate(m, OBJTUIOSCOL, SCOL_PTR tuioObj, NULL, NULL);
147 
148 #ifdef _SCOL_DEBUG_
149  MMechostr(0,"ok\n");
150 #endif
151 
152  return k;
153 }
154 
162 int _DSTuioClient(mmachine m)
163 {
164 #ifdef _SCOL_DEBUG_
165  MMechostr(MSKDEBUG,"_DSTuioClient\n");
166 #endif
167 
168  int objTab = MMget(m, 0);
169  if (objTab == NIL)
170  {
171  MMset(m, 0, NIL);
172  return 0;
173  }
174 
175  OBJdelTM(m, OBJTUIOSCOL, objTab);
176  MMset(m, 0, ITOM(0));
177 
178 #ifdef _SCOL_DEBUG_
179  MMechostr(MSKDEBUG,"ok\n");
180 #endif
181  return 0;
182 }
183 
191 int _CONNECTTuioClient(mmachine m)
192 {
193 #ifdef _SCOL_DEBUG_
194  MMechostr(MSKDEBUG, "_CONNECTTuioClient\n");
195 #endif
196 
197  int objTab = MMget(m, 0);
198  if (objTab == NIL)
199  {
200  MMset(m, 0, NIL);
201  return 0;
202  }
203 
204  STuioClient* tuioObj = MMgetPointer<STuioClient*>(m, MTOP(objTab));
205  if (tuioObj == NULL)
206  {
207  MMset(m, 0, NIL);
208  return 0;
209  }
210 
211  if (tuioObj->Connect())
212  MMset(m, 0, ITOM(1));
213  else
214  MMset(m, 0, ITOM(0));
215 
216 #ifdef _SCOL_DEBUG_
217  MMechostr(MSKDEBUG, "ok\n");
218 #endif
219  return 0;
220 }
221 
229 int _DISCONNECTTuioClient(mmachine m)
230 {
231 #ifdef _SCOL_DEBUG_
232  MMechostr(MSKDEBUG, "_DISCONNECTTuioClient\n");
233 #endif
234 
235  int objTab = MMget(m, 0);
236  if (objTab == NIL)
237  {
238  MMset(m, 0, NIL);
239  return 0;
240  }
241 
242  STuioClient* tuioObj = MMgetPointer<STuioClient*>(m, MTOP(objTab));
243  if (tuioObj == NULL)
244  {
245  MMset(m, 0, NIL);
246  return 0;
247  }
248 
249  tuioObj->Disconnect();
250  MMset(m, 0, ITOM(1));
251 
252 #ifdef _SCOL_DEBUG_
253  MMechostr(MSKDEBUG, "ok\n");
254 #endif
255  return 0;
256 }
257 
258 
274 int _CBTuioAddCursor(mmachine m)
275 {
276  return OBJaddreflex (m, OBJTUIOSCOL, SCOL_TUIO_CURSOR_ADD_CB);
277 }
278 
279 
283 int getTUIOCursorAddCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
284 {
285  SCbData* tuioData = (SCbData*)lParam;
286 
287  //call the scol callback
288  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_CURSOR_ADD_CB))
289  {
290  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
291  return 0;
292  }
293 
294  // push params
295  MMpush(m, ITOM(tuioData->id));
296  MMpush(m, FTOM(tuioData->x));
297  MMpush(m, FTOM(tuioData->y));
298  MMpush(m, FTOM(tuioData->xvel));
299  MMpush(m, FTOM(tuioData->yvel));
300 
301  // Call reflex previously defined
302  int k = OBJcallreflex(m, 5 /*nb param after obj and u0*/);
303 
304  // free data
305  delete(tuioData);
306  return k;
307 }
308 
309 
325 int _CBTuioUpdateCursor(mmachine m)
326 {
327  return OBJaddreflex (m, OBJTUIOSCOL, SCOL_TUIO_CURSOR_UPDATE_CB);
328 }
329 
330 
334 int getTUIOCursorUpdateCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
335 {
336  SCbData* tuioData = (SCbData*)lParam;
337 
338  //call the scol callback
339  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_CURSOR_UPDATE_CB))
340  {
341  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
342  return 0;
343  }
344 
345  // push params
346  MMpush(m, ITOM(tuioData->id));
347  MMpush(m, FTOM(tuioData->x));
348  MMpush(m, FTOM(tuioData->y));
349  MMpush(m, FTOM(tuioData->xvel));
350  MMpush(m, FTOM(tuioData->yvel));
351 
352  // Call reflex previously defined
353  int k = OBJcallreflex(m, 5 /*nb param after obj and u0*/);
354 
355  // free data
356  delete(tuioData);
357  return k;
358 }
359 
371 int _CBTuioRemoveCursor(mmachine m)
372 {
373  return OBJaddreflex (m, OBJTUIOSCOL, SCOL_TUIO_CURSOR_REMOVE_CB);
374 }
375 
376 
380 int getTUIOCursorRemoveCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
381 {
382  int id = (int)lParam;
383 
384  //call the scol callback
385  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_CURSOR_REMOVE_CB))
386  {
387  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
388  return 0;
389  }
390 
391  // push the id
392  MMpush(m, ITOM(id));
393 
394  // Call reflex previously defined
395  int k = OBJcallreflex(m, 1 /*nb param after obj and u0*/);
396 
397  return k;
398 }
399 
417 int _CBTuioAddObject(mmachine m)
418 {
419  return OBJaddreflex (m, OBJTUIOSCOL, SCOL_TUIO_OBJECT_ADD_CB);
420 }
421 
422 
426 int getTUIOObjectAddCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
427 {
428  SCbData* tuioData = (SCbData*)lParam;
429 
430  //call the scol callback
431  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_OBJECT_ADD_CB))
432  {
433  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
434  return 0;
435  }
436 
437  // push params
438  MMpush(m, ITOM(tuioData->id));
439  MMpush(m, FTOM(tuioData->x));
440  MMpush(m, FTOM(tuioData->y));
441  MMpush(m, FTOM(tuioData->ang));
442  MMpush(m, FTOM(tuioData->xvel));
443  MMpush(m, FTOM(tuioData->yvel));
444  MMpush(m, FTOM(tuioData->angvel));
445 
446  // Call reflex previously defined
447  int k = OBJcallreflex(m, 7 /*nb param after obj and u0*/);
448 
449  // free data
450  delete(tuioData);
451  return k;
452 }
453 
454 
472 int _CBTuioUpdateObject(mmachine m)
473 {
474  return OBJaddreflex(m, OBJTUIOSCOL, SCOL_TUIO_OBJECT_UPDATE_CB);
475 }
476 
477 
481 int getTUIOObjectUpdateCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
482 {
483  SCbData* tuioData = (SCbData*)lParam;
484 
485  //call the scol callback
486  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_OBJECT_UPDATE_CB))
487  {
488  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
489  return 0;
490  }
491 
492  // push params
493  MMpush(m, ITOM(tuioData->id));
494  MMpush(m, FTOM(tuioData->x));
495  MMpush(m, FTOM(tuioData->y));
496  MMpush(m, FTOM(tuioData->ang));
497  MMpush(m, FTOM(tuioData->xvel));
498  MMpush(m, FTOM(tuioData->yvel));
499  MMpush(m, FTOM(tuioData->angvel));
500 
501  // Call reflex previously defined
502  int k = OBJcallreflex(m, 7 /*nb param after obj and u0*/);
503 
504  // free data
505  delete(tuioData);
506  return k;
507 }
508 
520 int _CBTuioRemoveObject(mmachine m)
521 {
522  return OBJaddreflex(m, OBJTUIOSCOL, SCOL_TUIO_OBJECT_REMOVE_CB);
523 }
524 
525 
529 int getTUIOObjectRemoveCb(mmachine m, SCOL_PTR_TYPE wParam, LONG lParam)
530 {
531  int id = (int)lParam;
532 
533  //call the scol callback
534  if (OBJbeginreflex(m, OBJTUIOSCOL, SCOL_PTR wParam, SCOL_TUIO_OBJECT_REMOVE_CB))
535  {
536  MMechostr(MSKDEBUG, "ObjTUIO callback not found\n");
537  return 0;
538  }
539 
540  // push the id
541  MMpush(m, ITOM(id));
542 
543  // Call reflex previously defined
544  int k = OBJcallreflex(m, 1 /*nb param after obj and u0*/);
545 
546  return k;
547 }
548 
552 char* mTUIOname[]=
553 {
554  "ObjTUIO",
555 
556  "_CRTuioClient",
557  "_DSTuioClient",
558  "_CONNECTTuioClient",
559  "_DISCONNECTTuioClient",
560  "_CBTuioAddCursor",
561  "_CBTuioUpdateCursor",
562  "_CBTuioRemoveCursor",
563  "_CBTuioAddObject",
564  "_CBTuioUpdateObject",
565  "_CBTuioRemoveObject"
566 };
567 
571 #define NTUIOPKG (sizeof(mTUIOname)/sizeof(char*))
572 
576 int (*mTUIOfun[NTUIOPKG])(mmachine m)=
577 {
578  NULL, //ObjTUIO
579 
590 
591 };
592 
596 int mTUIOnarg[NTUIOPKG]=
597 {
598  TYPTYPE, //ObjTUIO
599 
600  2, //_CRTuioClient
601  1, //_DSTuioClient
602  1, //_CONNECTTuioClient
603  1, //_DISCONNECTTuioClient
604  3, //_CBTuioAddCursor
605  3, //_CBTuioUpdateCursor
606  3, //_CBTuioRemoveCursor
607  3, //_CBTuioAddObject
608  3, //_CBTuioUpdateObject
609  3 //_CBTuioRemoveObject
610 };
611 
615 char* mTUIOtype[NTUIOPKG]=
616 {
617  NULL, //ObjTUIO
618 
619  "fun [Chn I] ObjTUIO", //_CRTuioClient
620  "fun [ObjTUIO] I", //_DSTuioClient
621  "fun [ObjTUIO] I", //_CONNECTTuioClient
622  "fun [ObjTUIO] I", //_DISCONNECTTuioClient
623  "fun [ObjTUIO fun [ObjTUIO u0 I F F F F] u1 u0] ObjTUIO", //_CBTuioAddCursor
624  "fun [ObjTUIO fun [ObjTUIO u0 I F F F F] u1 u0] ObjTUIO", //_CBTuioUpdateCursor
625  "fun [ObjTUIO fun [ObjTUIO u0 I] u1 u0] ObjTUIO", //_CBTuioRemoveCursor
626  "fun [ObjTUIO fun [ObjTUIO u0 I F F F F F F] u1 u0] ObjTUIO", //_CBTuioAddObject
627  "fun [ObjTUIO fun [ObjTUIO u0 I F F F F F F] u1 u0] ObjTUIO", //_CBTuioUpdateObject
628  "fun [ObjTUIO fun [ObjTUIO u0 I] u1 u0] ObjTUIO" //_CBTuioRemoveObject
629 };
630 
631 
632 // Everything inside _cond and _endcond is ignored by doxygen
634 
639 int LoadTUIO(mmachine m)
640 {
641  int k = 0;
642 
643  MMechostr(MSKDEBUG,"\n" );
644  MMechostr(MSKDEBUG," > Loading TUIO Support\n");
645 
646  // Declare a new type of object ("ObjTUIO")
647  OBJTUIOSCOL = OBJregister(6 /*nb of callback*/, 0, destroyTuioObj, "OBJTUIOSCOL");
648  k = PKhardpak(m, "mTUIO.pkg-1.0", NTUIOPKG, mTUIOname, mTUIOfun, mTUIOnarg, mTUIOtype);
649 
650  // Register data callback
651  TUIO_CURSOR_ADD_CB = OBJgetUserEvent();
652  OBJdefEvent(TUIO_CURSOR_ADD_CB, getTUIOCursorAddCb);
653 
654  TUIO_CURSOR_REMOVE_CB = OBJgetUserEvent();
655  OBJdefEvent(TUIO_CURSOR_REMOVE_CB, getTUIOCursorRemoveCb);
656 
657  TUIO_CURSOR_UPDATE_CB = OBJgetUserEvent();
658  OBJdefEvent(TUIO_CURSOR_UPDATE_CB, getTUIOCursorUpdateCb);
659 
660  TUIO_OBJECT_ADD_CB = OBJgetUserEvent();
661  OBJdefEvent(TUIO_OBJECT_ADD_CB, getTUIOObjectAddCb);
662 
663  TUIO_OBJECT_REMOVE_CB = OBJgetUserEvent();
664  OBJdefEvent(TUIO_OBJECT_REMOVE_CB, getTUIOObjectRemoveCb);
665 
666  TUIO_OBJECT_UPDATE_CB = OBJgetUserEvent();
667  OBJdefEvent(TUIO_OBJECT_UPDATE_CB, getTUIOObjectUpdateCb);
668 
669  MMechostr(MSKDEBUG," > Successfully Loaded\n\n");
670  return k;
671 }
672 
673 int CloseTUIO()
674 {
675  MMechostr(MSKDEBUG,"\n" );
676  MMechostr(MSKDEBUG," > Unloading TUIO Support\n");
677  MMechostr(MSKDEBUG," > Successfully unloaded\n\n");
678  return 0;
679 }
680 
682 
683 
688 #ifndef SCOL_STATIC
689 extern "C" SCOL_EXPORT int ScolLoadPlugin(mmachine m, cbmachine w)
690 #else
691 extern "C" SCOL_EXPORT int ScolStuioLoadPlugin(mmachine m, cbmachine w)
692 #endif
693 {
694  SCOLinitplugin(w);
695  return LoadTUIO(m);
696 }
697 
702 #ifndef SCOL_STATIC
703 extern "C" SCOL_EXPORT int ScolUnloadPlugin()
704 #else
705 extern "C" SCOL_EXPORT int ScolStuioUnloadPlugin()
706 #endif
707 {
708  return CloseTUIO();
709 }
int _CBTuioAddCursor(mmachine m)
_CBTuioAddCursor : defines callback for TUIO add cursor event
Definition: scolplugin.cpp:274
int _DSTuioClient(mmachine m)
_DSTuioClient : Destroy TUIO object
Definition: scolplugin.cpp:162
void Disconnect()
Definition: sTuio.cpp:55
int _CBTuioRemoveObject(mmachine m)
_CBTuioRemoveObject : defines callback for TUIO remove object event
Definition: scolplugin.cpp:520
bool Connect()
Definition: sTuio.cpp:49
int _CONNECTTuioClient(mmachine m)
_CONNECTTuioClient : This function connect the TUIO client
Definition: scolplugin.cpp:191
int _CBTuioAddObject(mmachine m)
_CBTuioAddObject : defines callback for TUIO add object event
Definition: scolplugin.cpp:417
int _CBTuioUpdateObject(mmachine m)
_CBTuioUpdateObject : defines callback for TUIO update cursor event
Definition: scolplugin.cpp:472
int _CBTuioRemoveCursor(mmachine m)
_CBTuioRemoveCursor : defines callback for TUIO remove cursor event
Definition: scolplugin.cpp:371
Definition: sTuio.h:39
int _DISCONNECTTuioClient(mmachine m)
_DISCONNECTTuioClient : This function disconnect the TUIO client
Definition: scolplugin.cpp:229
int _CBTuioUpdateCursor(mmachine m)
_CBTuioUpdateCursor : defines callback for TUIO update cursor event
Definition: scolplugin.cpp:325
int _CRTuioClient(mmachine m)
_CRTuioClient : This function create a TUIO object
Definition: scolplugin.cpp:103