front_idill/extern/fajran-npTuioClient/TuioClient/TuioClient.cpp
author bastiena
Fri, 06 Apr 2012 10:44:54 +0200
changeset 21 e4e5f02787a1
child 24 2bdf5d51d434
permissions -rw-r--r--
Front IDILL : Added Communication extern named fajran-npTuioClient It contains the project generating a dll used as a browser plugin.
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;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    86
					
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
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   102
				} 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
   103
					(*tobj)->update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   104
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   105
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   106
						(*listener)->updateTuioObject((*tobj));
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   107
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   108
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   109
			} else if( strcmp( cmd, "alive" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   110
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   111
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   112
				int32 s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   113
				while(!args.Eos()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   114
					args >> s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   115
					objectBuffer.push_back((long)s_id);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   116
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   117
					std::list<long>::iterator iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   118
					iter = find(aliveObjectList.begin(), aliveObjectList.end(), (long)s_id); 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   119
					if (iter != aliveObjectList.end()) aliveObjectList.erase(iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   120
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   121
				args >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   122
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   123
				std::list<long>::iterator alive_iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   124
				for (alive_iter=aliveObjectList.begin(); alive_iter != aliveObjectList.end(); alive_iter++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   125
					std::list<TuioObject*>::iterator tobj;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   126
					for (tobj=objectList.begin(); tobj!=objectList.end(); tobj++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   127
						TuioObject *deleteObject = (*tobj);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   128
						if(deleteObject->getSessionID()==*alive_iter) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   129
							deleteObject->remove();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   130
							for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   131
								(*listener)->removeTuioObject(deleteObject);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   132
							objectList.erase(tobj);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   133
							delete deleteObject;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   134
							break;
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
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   140
				aliveObjectList = objectBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   141
				objectBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   142
			} else if( strcmp( cmd, "fseq" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   143
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   144
				if(currentFrame>0) lastFrame = currentFrame;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   145
				args >> currentFrame  >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   146
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   147
				if ((currentFrame>=lastFrame) || (currentFrame<0)) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   148
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   149
					long currentTime = lastTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   150
					if (currentFrame>lastFrame) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   151
						currentTime = getCurrentTime()-startTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   152
						lastTime = currentTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   153
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   154
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   155
					for (std::list<TuioObject*>::iterator refreshObject=objectList.begin(); refreshObject!=objectList.end(); refreshObject++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   156
						if ((*refreshObject)->getUpdateTime()==TUIO_UNDEFINED) (*refreshObject)->setUpdateTime(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   157
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   158
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener!=listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   159
						(*listener)->refresh(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   160
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   161
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   162
		} else if( strcmp( msg.AddressPattern(), "/tuio/2Dcur" ) == 0 ) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   163
			const char* cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   164
			args >> cmd;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   165
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   166
			if( strcmp( cmd, "set" ) == 0 ){	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   167
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   168
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   169
				int32 s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   170
				float xpos, ypos, xspeed, yspeed, maccel;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   171
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   172
				args >> s_id >> xpos >> ypos >> xspeed >> yspeed >> maccel >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   173
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   174
				std::list<TuioCursor*>::iterator tcur;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   175
				for (tcur=cursorList.begin(); tcur != cursorList.end(); tcur++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   176
					if((*tcur)->getSessionID()==(long)s_id) break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   177
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   178
				if (tcur == cursorList.end()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   179
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   180
					int f_id = (int)cursorList.size();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   181
					if ((int)(cursorList.size())<=maxFingerID) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   182
						std::list<TuioCursor*>::iterator closestCursor = freeCursorList.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   183
						
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   184
						for(std::list<TuioCursor*>::iterator testCursor = freeCursorList.begin();testCursor!= freeCursorList.end(); testCursor++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   185
							if((*testCursor)->getDistance(xpos,ypos)<(*closestCursor)->getDistance(xpos,ypos)) closestCursor = testCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   186
						}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   187
						
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   188
						f_id = (*closestCursor)->getFingerID();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   189
						freeCursorList.erase(closestCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   190
						delete *closestCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   191
					} else maxFingerID = f_id;	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   192
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   193
					TuioCursor *addCursor = new TuioCursor((long)s_id,f_id,xpos,ypos);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   194
					cursorList.push_back(addCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   195
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   196
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   197
						(*listener)->addTuioCursor(addCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   198
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   199
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   200
				} else if ( ((*tcur)->getX()!=xpos) || ((*tcur)->getY()!=ypos) || ((*tcur)->getXSpeed()!=xspeed) || ((*tcur)->getYSpeed()!=yspeed) || ((*tcur)->getMotionAccel()!=maccel) ) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   201
					(*tcur)->update(xpos,ypos,xspeed,yspeed,maccel);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   202
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   203
						(*listener)->updateTuioCursor((*tcur));
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   204
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   205
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   206
			} else if( strcmp( cmd, "alive" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   207
				if ((currentFrame<lastFrame) && (currentFrame>0)) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   208
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   209
				int32 s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   210
				while(!args.Eos()) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   211
					args >> s_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   212
					cursorBuffer.push_back((long)s_id);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   213
					
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   214
					std::list<long>::iterator iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   215
					iter = find(aliveCursorList.begin(), aliveCursorList.end(), (long)s_id); 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   216
					if (iter != aliveCursorList.end()) aliveCursorList.erase(iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   217
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   218
				args >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   219
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   220
				std::list<long>::iterator alive_iter;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   221
				for (alive_iter=aliveCursorList.begin(); alive_iter != aliveCursorList.end(); alive_iter++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   222
					std::list<TuioCursor*>::iterator tcur;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   223
					for (tcur=cursorList.begin(); tcur != cursorList.end(); tcur++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   224
						TuioCursor *deleteCursor = (*tcur);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   225
						if(deleteCursor->getSessionID()==*alive_iter) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   226
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   227
							cursorList.erase(tcur);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   228
							deleteCursor->remove();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   229
							for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   230
								(*listener)->removeTuioCursor(deleteCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   231
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   232
							if (deleteCursor->getFingerID()==maxFingerID) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   233
								maxFingerID = -1;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   234
								delete deleteCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   235
								
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   236
								if (cursorList.size()>0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   237
									std::list<TuioCursor*>::iterator clist;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   238
									for (clist=cursorList.begin(); clist != cursorList.end(); clist++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   239
										int f_id = (*clist)->getFingerID();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   240
										if (f_id>maxFingerID) maxFingerID=f_id;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   241
									}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   242
									
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   243
									
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   244
									std::list<TuioCursor*>::iterator flist;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   245
									for (flist=freeCursorList.begin(); flist != freeCursorList.end(); flist++) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   246
										TuioCursor *freeCursor = (*flist);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   247
										if (freeCursor->getFingerID()>maxFingerID) delete freeCursor;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   248
										else freeCursorBuffer.push_back(freeCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   249
									}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   250
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   251
									freeCursorList = freeCursorBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   252
									freeCursorBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   253
								} 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   254
							} else if (deleteCursor->getFingerID()<maxFingerID) freeCursorList.push_back(deleteCursor);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   255
							
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   256
							break;
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
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   262
				aliveCursorList = cursorBuffer;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   263
				cursorBuffer.clear();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   264
			} else if( strcmp( cmd, "fseq" ) == 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   265
				
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   266
				if(currentFrame>0) lastFrame = currentFrame;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   267
				args >> currentFrame  >> EndMessage;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   268
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   269
				if ((currentFrame>=lastFrame) || (currentFrame<0)) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   270
					long currentTime = lastTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   271
					if (currentFrame>lastFrame) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   272
						currentTime = getCurrentTime()-startTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   273
						lastTime = currentTime;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   274
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   275
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   276
					for (std::list<TuioCursor*>::iterator refreshCursor=cursorList.begin(); refreshCursor!=cursorList.end(); refreshCursor++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   277
						if ((*refreshCursor)->getUpdateTime()==TUIO_UNDEFINED) (*refreshCursor)->setUpdateTime(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   278
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   279
					for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   280
						(*listener)->refresh(currentTime);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   281
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   282
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   283
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   284
	} catch( Exception& e ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   285
		std::cout << "error while parsing message: "<< msg.AddressPattern() << ": " << e.what() << "\n";
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   286
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   287
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   288
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   289
void TuioClient::ProcessPacket( const char *data, int size, const IpEndpointName& remoteEndpoint ) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   290
	if (listenerList.size()==0) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   291
	ReceivedPacket p( data, size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   292
	if(p.IsBundle()) ProcessBundle( ReceivedBundle(p), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   293
        else ProcessMessage( ReceivedMessage(p), remoteEndpoint);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   294
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   295
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   296
void TuioClient::start(bool lk) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   297
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   298
	if (socket==NULL) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   299
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   300
	locked = lk;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   301
	if (!locked) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   302
		#ifndef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   303
		pthread_create(&thread , NULL, ThreadFunc, this);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   304
		#else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   305
		DWORD threadId;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   306
		thread = CreateThread( 0, 0, ThreadFunc, this, 0, &threadId );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   307
		#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   308
	} else socket->Run();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   309
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   310
	startTime = getCurrentTime();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   311
	lastTime = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   312
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   313
	running = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   314
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   315
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   316
void TuioClient::stop() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   317
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   318
	if (socket==NULL) return;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   319
	socket->Break();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   320
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   321
	if (!locked) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   322
		#ifdef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   323
		if( thread ) CloseHandle( thread );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   324
		#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   325
		thread = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   326
		locked = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   327
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   328
	running = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   329
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   330
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   331
void TuioClient::addTuioListener(TuioListener *listener) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   332
	listenerList.push_back(listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   333
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   334
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   335
void TuioClient::removeTuioListener(TuioListener *listener) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   336
	std::list<TuioListener*>::iterator result = find(listenerList.begin(),listenerList.end(),listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   337
	if (result!=listenerList.end()) listenerList.remove(listener);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   338
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   339
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   340
TuioObject* TuioClient::getTuioObject(long s_id) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   341
	for (std::list<TuioObject*>::iterator iter=objectList.begin(); iter != objectList.end(); iter++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   342
		if((*iter)->getSessionID()==s_id) return (*iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   343
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   344
	return NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   345
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   346
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   347
TuioCursor* TuioClient::getTuioCursor(long s_id) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   348
	for (std::list<TuioCursor*>::iterator iter=cursorList.begin(); iter != cursorList.end(); iter++)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   349
		if((*iter)->getSessionID()==s_id) return (*iter);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   350
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   351
	return NULL;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   352
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   353
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   354
std::list<TuioObject*> TuioClient::getTuioObjects() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   355
	return objectList;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   356
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   357
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   358
std::list<TuioCursor*> TuioClient::getTuioCursors() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   359
	return cursorList;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   360
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   361
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   362
long TuioClient::getCurrentTime() {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   363
	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   364
	#ifdef WIN32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   365
		long timestamp = GetTickCount();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   366
	#else
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   367
		struct timeval tv;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   368
		struct timezone tz;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   369
		gettimeofday(&tv,&tz);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   370
		long timestamp = (tv.tv_sec*1000)+(tv.tv_usec/1000);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   371
	#endif
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   372
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   373
		return timestamp;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   374
}