front_idill/extern/fajran-npTuioClient/TuioClient/TuioClient.cpp
author bastiena
Fri, 06 Apr 2012 18:32:13 +0200
changeset 25 a7b0e40bcab0
parent 24 2bdf5d51d434
child 27 6c08d4d7219e
permissions -rw-r--r--
Front IDILL : Basic JS TUIO lib loaded
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     1
/*
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     2
	TUIO C++ Library - part of the reacTIVision project
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     3
	http://reactivision.sourceforge.net/
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     4
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     5
	Copyright (c) 2005-2008 Martin Kaltenbrunner <mkalten@iua.upf.edu>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     6
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     7
    This program is free software; you can redistribute it and/or modify
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     8
    it under the terms of the GNU General Public License as published by
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     9
    the Free Software Foundation; either version 2 of the License, or
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    10
    (at your option) any later version.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    11
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    12
    This program is distributed in the hope that it will be useful,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    15
    GNU General Public License for more details.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    16
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    17
    You should have received a copy of the GNU General Public License
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    18
    along with this program; if not, write to the Free Software
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    19
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    20
*/
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    22
#include "TuioClient.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    23
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    24
#ifndef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    25
static void* ThreadFunc( void* obj )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    26
#else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    27
static DWORD WINAPI ThreadFunc( LPVOID obj )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    28
#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    29
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    30
	static_cast<TuioClient*>(obj)->socket->Run();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    31
	return 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    32
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    33
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    34
TuioClient::TuioClient() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    35
	TuioClient(3333);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    36
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    37
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    38
TuioClient::TuioClient(int port) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    39
	try {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    40
		socket = new UdpListeningReceiveSocket(IpEndpointName( IpEndpointName::ANY_ADDRESS, port ), this );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    41
	} catch (std::exception &e) { 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    42
		std::cout << "could not bind to UDP port " << port << std::endl;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    43
		socket = NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    44
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    45
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    46
	if (socket!=NULL) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    47
		if (!socket->IsBound()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    48
			delete socket;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    49
			socket = NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    50
		} else std::cout << "listening to TUIO messages on UDP port " << port << std::endl;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    51
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    52
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    53
	locked = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    54
	running = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    55
	currentFrame = lastFrame = maxFingerID = -1;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    56
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    57
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    58
TuioClient::~TuioClient() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    59
	delete socket;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    60
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    61
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    62
void TuioClient::ProcessBundle( const ReceivedBundle& b, const IpEndpointName& remoteEndpoint) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    63
	for( ReceivedBundle::const_iterator i = b.ElementsBegin(); i != b.ElementsEnd(); ++i ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    64
		if( i->IsBundle() )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    65
			ProcessBundle( ReceivedBundle(*i), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    66
		else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    67
			ProcessMessage( ReceivedMessage(*i), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    68
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    69
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    70
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    71
void TuioClient::ProcessMessage( const ReceivedMessage& msg, const IpEndpointName& remoteEndpoint) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    72
	try {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    73
		ReceivedMessageArgumentStream args = msg.ArgumentStream();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    74
		ReceivedMessage::const_iterator arg = msg.ArgumentsBegin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    75
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    76
		if( strcmp( msg.AddressPattern(), "/tuio/2Dobj" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    77
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    78
			const char* cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    79
			args >> cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    80
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    81
			if( strcmp( cmd, "set" ) == 0 ){	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    82
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    83
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    84
				int32 s_id, f_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    85
				float xpos, ypos, angle, xspeed, yspeed, rspeed, maccel, raccel;
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
    86
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    87
				args >> s_id >> f_id >> xpos >> ypos >> angle >> xspeed >> yspeed >> rspeed >> maccel >> raccel >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    88
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    89
				std::list<TuioObject*>::iterator tobj;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    90
				for (tobj=objectList.begin(); tobj!= objectList.end(); tobj++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    91
					if((*tobj)->getSessionID()==(long)s_id) break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    92
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    93
				if (tobj == objectList.end()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    94
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    95
					TuioObject *addObject = new TuioObject((long)s_id,(int)f_id,xpos,ypos,angle);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    96
					objectList.push_back(addObject);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    97
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    98
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    99
						(*listener)->addTuioObject(addObject);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   100
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   101
				} else if ( ((*tobj)->getX()!=xpos) || ((*tobj)->getY()!=ypos) || ((*tobj)->getAngle()!=angle) || ((*tobj)->getXSpeed()!=xspeed) || ((*tobj)->getYSpeed()!=yspeed) || ((*tobj)->getRotationSpeed()!=rspeed) || ((*tobj)->getMotionAccel()!=maccel) || ((*tobj)->getRotationAccel()!=raccel) ) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   102
					(*tobj)->update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   103
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   104
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   105
						(*listener)->updateTuioObject((*tobj));
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   106
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   107
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   108
			} else if( strcmp( cmd, "alive" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   109
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   110
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   111
				int32 s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   112
				while(!args.Eos()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   113
					args >> s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   114
					objectBuffer.push_back((long)s_id);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   115
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   116
					std::list<long>::iterator iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   117
					iter = find(aliveObjectList.begin(), aliveObjectList.end(), (long)s_id); 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   118
					if (iter != aliveObjectList.end()) aliveObjectList.erase(iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   119
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   120
				args >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   121
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   122
				std::list<long>::iterator alive_iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   123
				for (alive_iter=aliveObjectList.begin(); alive_iter != aliveObjectList.end(); alive_iter++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   124
					std::list<TuioObject*>::iterator tobj;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   125
					for (tobj=objectList.begin(); tobj!=objectList.end(); tobj++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   126
						TuioObject *deleteObject = (*tobj);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   127
						if(deleteObject->getSessionID()==*alive_iter) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   128
							deleteObject->remove();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   129
							for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   130
								(*listener)->removeTuioObject(deleteObject);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   131
							objectList.erase(tobj);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   132
							delete deleteObject;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   133
							break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   134
						}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   135
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   136
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   137
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   138
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   139
				aliveObjectList = objectBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   140
				objectBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   141
			} else if( strcmp( cmd, "fseq" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   142
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   143
				if(currentFrame>0) lastFrame = currentFrame;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   144
				args >> currentFrame  >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   145
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   146
				if ((currentFrame>=lastFrame) || (currentFrame<0)) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   147
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   148
					long currentTime = lastTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   149
					if (currentFrame>lastFrame) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   150
						currentTime = getCurrentTime()-startTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   151
						lastTime = currentTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   152
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   153
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   154
					for (std::list<TuioObject*>::iterator refreshObject=objectList.begin(); refreshObject!=objectList.end(); refreshObject++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   155
						if ((*refreshObject)->getUpdateTime()==TUIO_UNDEFINED) (*refreshObject)->setUpdateTime(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   156
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   157
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener!=listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   158
						(*listener)->refresh(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   159
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   160
			}
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   161
		} else if( strcmp( msg.AddressPattern(), "/tuio/3Dcur" ) == 0 ) {
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   162
			const char* cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   163
			args >> cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   164
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   165
			if( strcmp( cmd, "set" ) == 0 ){	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   166
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   167
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   168
				int32 s_id;
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   169
				float xpos, ypos, zpos, xspeed, yspeed, maccel;
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   170
				//Modifi� par alexandre.bastien@iri.centrepompidou.fr
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   171
				args >> s_id >> xpos >> ypos >> zpos >> xspeed >> yspeed >> maccel >> EndMessage;
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   172
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   173
				std::list<TuioCursor*>::iterator tcur;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   174
				for (tcur=cursorList.begin(); tcur != cursorList.end(); tcur++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   175
					if((*tcur)->getSessionID()==(long)s_id) break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   176
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   177
				if (tcur == cursorList.end()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   178
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   179
					int f_id = (int)cursorList.size();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   180
					if ((int)(cursorList.size())<=maxFingerID) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   181
						std::list<TuioCursor*>::iterator closestCursor = freeCursorList.begin();
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   182
						//Modifi� par alexandre.bastien@iri.centrepompidou.fr
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   183
						for(std::list<TuioCursor*>::iterator testCursor = freeCursorList.begin();testCursor!= freeCursorList.end(); testCursor++) {
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   184
							if((*testCursor)->getDistance(xpos,ypos,zpos)<(*closestCursor)->getDistance(xpos,ypos,zpos)) closestCursor = testCursor;
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   185
						}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   186
						
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   187
						f_id = (*closestCursor)->getFingerID();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   188
						freeCursorList.erase(closestCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   189
						delete *closestCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   190
					} else maxFingerID = f_id;	
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   191
					//Modifi� par alexandre.bastien@iri.centrepompidou.fr
25
a7b0e40bcab0 Front IDILL :
bastiena
parents: 24
diff changeset
   192
					TuioCursor *addCursor = new TuioCursor((long)s_id,f_id,xpos,ypos,zpos);
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   193
					cursorList.push_back(addCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   194
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   195
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   196
						(*listener)->addTuioCursor(addCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   197
					
24
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   198
				//Modifi� par alexandre.bastien@iri.centrepompidou.fr
2bdf5d51d434 Front IDILL :
bastiena
parents: 21
diff changeset
   199
				} else if ( ((*tcur)->getX()!=xpos) || ((*tcur)->getY()!=ypos) || ((*tcur)->getZ()!=zpos) || ((*tcur)->getXSpeed()!=xspeed) || ((*tcur)->getYSpeed()!=yspeed) || ((*tcur)->getMotionAccel()!=maccel) ) {
25
a7b0e40bcab0 Front IDILL :
bastiena
parents: 24
diff changeset
   200
					(*tcur)->update(xpos,ypos,zpos,xspeed,yspeed,maccel);
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   201
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   202
						(*listener)->updateTuioCursor((*tcur));
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   203
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   204
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   205
			} else if( strcmp( cmd, "alive" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   206
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   207
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   208
				int32 s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   209
				while(!args.Eos()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   210
					args >> s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   211
					cursorBuffer.push_back((long)s_id);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   212
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   213
					std::list<long>::iterator iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   214
					iter = find(aliveCursorList.begin(), aliveCursorList.end(), (long)s_id); 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   215
					if (iter != aliveCursorList.end()) aliveCursorList.erase(iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   216
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   217
				args >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   218
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   219
				std::list<long>::iterator alive_iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   220
				for (alive_iter=aliveCursorList.begin(); alive_iter != aliveCursorList.end(); alive_iter++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   221
					std::list<TuioCursor*>::iterator tcur;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   222
					for (tcur=cursorList.begin(); tcur != cursorList.end(); tcur++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   223
						TuioCursor *deleteCursor = (*tcur);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   224
						if(deleteCursor->getSessionID()==*alive_iter) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   225
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   226
							cursorList.erase(tcur);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   227
							deleteCursor->remove();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   228
							for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   229
								(*listener)->removeTuioCursor(deleteCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   230
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   231
							if (deleteCursor->getFingerID()==maxFingerID) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   232
								maxFingerID = -1;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   233
								delete deleteCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   234
								
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   235
								if (cursorList.size()>0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   236
									std::list<TuioCursor*>::iterator clist;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   237
									for (clist=cursorList.begin(); clist != cursorList.end(); clist++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   238
										int f_id = (*clist)->getFingerID();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   239
										if (f_id>maxFingerID) maxFingerID=f_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   240
									}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   241
									
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   242
									
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   243
									std::list<TuioCursor*>::iterator flist;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   244
									for (flist=freeCursorList.begin(); flist != freeCursorList.end(); flist++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   245
										TuioCursor *freeCursor = (*flist);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   246
										if (freeCursor->getFingerID()>maxFingerID) delete freeCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   247
										else freeCursorBuffer.push_back(freeCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   248
									}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   249
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   250
									freeCursorList = freeCursorBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   251
									freeCursorBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   252
								} 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   253
							} else if (deleteCursor->getFingerID()<maxFingerID) freeCursorList.push_back(deleteCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   254
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   255
							break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   256
						}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   257
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   258
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   259
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   260
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   261
				aliveCursorList = cursorBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   262
				cursorBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   263
			} else if( strcmp( cmd, "fseq" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   264
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   265
				if(currentFrame>0) lastFrame = currentFrame;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   266
				args >> currentFrame  >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   267
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   268
				if ((currentFrame>=lastFrame) || (currentFrame<0)) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   269
					long currentTime = lastTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   270
					if (currentFrame>lastFrame) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   271
						currentTime = getCurrentTime()-startTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   272
						lastTime = currentTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   273
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   274
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   275
					for (std::list<TuioCursor*>::iterator refreshCursor=cursorList.begin(); refreshCursor!=cursorList.end(); refreshCursor++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   276
						if ((*refreshCursor)->getUpdateTime()==TUIO_UNDEFINED) (*refreshCursor)->setUpdateTime(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   277
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   278
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   279
						(*listener)->refresh(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   280
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   281
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   282
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   283
	} catch( Exception& e ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   284
		std::cout << "error while parsing message: "<< msg.AddressPattern() << ": " << e.what() << "\n";
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   285
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   286
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   287
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   288
void TuioClient::ProcessPacket( const char *data, int size, const IpEndpointName& remoteEndpoint ) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   289
	if (listenerList.size()==0) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   290
	ReceivedPacket p( data, size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   291
	if(p.IsBundle()) ProcessBundle( ReceivedBundle(p), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   292
        else ProcessMessage( ReceivedMessage(p), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   293
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   294
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   295
void TuioClient::start(bool lk) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   296
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   297
	if (socket==NULL) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   298
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   299
	locked = lk;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   300
	if (!locked) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   301
		#ifndef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   302
		pthread_create(&thread , NULL, ThreadFunc, this);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   303
		#else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   304
		DWORD threadId;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   305
		thread = CreateThread( 0, 0, ThreadFunc, this, 0, &threadId );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   306
		#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   307
	} else socket->Run();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   308
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   309
	startTime = getCurrentTime();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   310
	lastTime = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   311
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   312
	running = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   313
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   314
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   315
void TuioClient::stop() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   316
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   317
	if (socket==NULL) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   318
	socket->Break();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   319
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   320
	if (!locked) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   321
		#ifdef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   322
		if( thread ) CloseHandle( thread );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   323
		#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   324
		thread = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   325
		locked = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   326
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   327
	running = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   328
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   329
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   330
void TuioClient::addTuioListener(TuioListener *listener) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   331
	listenerList.push_back(listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   332
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   333
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   334
void TuioClient::removeTuioListener(TuioListener *listener) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   335
	std::list<TuioListener*>::iterator result = find(listenerList.begin(),listenerList.end(),listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   336
	if (result!=listenerList.end()) listenerList.remove(listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   337
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   338
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   339
TuioObject* TuioClient::getTuioObject(long s_id) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   340
	for (std::list<TuioObject*>::iterator iter=objectList.begin(); iter != objectList.end(); iter++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   341
		if((*iter)->getSessionID()==s_id) return (*iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   342
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   343
	return NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   344
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   345
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   346
TuioCursor* TuioClient::getTuioCursor(long s_id) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   347
	for (std::list<TuioCursor*>::iterator iter=cursorList.begin(); iter != cursorList.end(); iter++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   348
		if((*iter)->getSessionID()==s_id) return (*iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   349
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   350
	return NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   351
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   352
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   353
std::list<TuioObject*> TuioClient::getTuioObjects() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   354
	return objectList;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   355
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   356
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   357
std::list<TuioCursor*> TuioClient::getTuioCursors() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   358
	return cursorList;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   359
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   360
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   361
long TuioClient::getCurrentTime() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   362
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   363
	#ifdef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   364
		long timestamp = GetTickCount();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   365
	#else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   366
		struct timeval tv;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   367
		struct timezone tz;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   368
		gettimeofday(&tv,&tz);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   369
		long timestamp = (tv.tv_sec*1000)+(tv.tv_usec/1000);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   370
	#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   371
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   372
		return timestamp;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   373
}