"LLaDA2.0/run_Transformers_t2i_thin.py" did not exist on "57789237194bcd01867f7bc9223517c826e1628c"
OISWiiMoteFactoryCreator.cpp 6.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "OISConfig.h"
#ifdef OIS_WIN32_WIIMOTE_SUPPORT
/*
The zlib/libpng License

Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)

This software is provided 'as-is', without any express or implied warranty. In no event will
the authors be held liable for any damages arising from the use of this software.

Ben Hymers's avatar
Ben Hymers committed
11
Permission is granted to anyone to use this software for any purpose, including commercial
12
13
14
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

Ben Hymers's avatar
Ben Hymers committed
15
16
17
    1. The origin of this software must not be misrepresented; you must not claim that
		you wrote the original software. If you use this software in a product,
		an acknowledgment in the product documentation would be appreciated but is
18
19
		not required.

Ben Hymers's avatar
Ben Hymers committed
20
    2. Altered source versions must be plainly marked as such, and must not be
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/
#include "OISWiiMoteFactoryCreator.h"
#include "OISException.h"
#include "OISWiiMote.h"
#include <assert.h>
#include <boost/thread.hpp>   //include here, keep compilation times down
#include <boost/function.hpp>
#include <boost/bind.hpp>


using namespace OIS;

//---------------------------------------------------------------------------------//
WiiMoteFactoryCreator::WiiMoteFactoryCreator() :
	mVendorName("cWiiMote"),
	mCount(0),
	mtThreadHandler(0),
	mtWiiMoteListMutex(0),
	mtThreadRunning(0)
{
	//Discover how many Wii's there are
	for( ; mCount < OIS_cWiiMote_MAX_WIIS; ++mCount )
	{
		cWiiMote wii;
		if( wii.ConnectToDevice(mCount) == false )
			break;
	}

	//Store how many WiiMotes there were in the form of integer handles
	for(int i = 0; i < mCount; ++i)
		mFreeWiis.push_back(i);

	//The mutex lasts the whole life of this class. The thread does not.
	mtWiiMoteListMutex = new boost::mutex();
}

//---------------------------------------------------------------------------------//
WiiMoteFactoryCreator::~WiiMoteFactoryCreator()
{
	//Thread (once all objects destroyed) should be killed off already
Ben Hymers's avatar
Ben Hymers committed
64
	assert( (mtThreadRunning == false && mtThreadHandler == 0) &&
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
		"~WiiMoteFactoryCreator(): invalid state.. Some objects left dangling!");

	delete mtWiiMoteListMutex;
}

//---------------------------------------------------------------------------------//
DeviceList WiiMoteFactoryCreator::freeDeviceList()
{
	DeviceList list;
	for( std::deque<int>::iterator i = mFreeWiis.begin(); i != mFreeWiis.end(); ++i )
	{
		list.insert(std::make_pair(OISJoyStick, mVendorName));
	}
	return list;
}

//---------------------------------------------------------------------------------//
int WiiMoteFactoryCreator::totalDevices(Type iType)
{
	if( iType == OISJoyStick )
		return mCount;
	else
		return 0;
}

//---------------------------------------------------------------------------------//
int WiiMoteFactoryCreator::freeDevices(Type iType)
{
	if( iType == OISJoyStick )
		return (int)mFreeWiis.size();
	else
		return 0;
}

//---------------------------------------------------------------------------------//
bool WiiMoteFactoryCreator::vendorExist(Type iType, const std::string & vendor)
{
	if( iType == OISJoyStick && mVendorName == vendor )
		return true;
	else
		return false;
}

//---------------------------------------------------------------------------------//
Object* WiiMoteFactoryCreator::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
{
	if( mFreeWiis.size() > 0 && (vendor == "" || vendor == mVendorName ) )
	{
		int id = mFreeWiis.front();
		mFreeWiis.pop_front();
		WiiMote *wii = new WiiMote(creator, id, bufferMode, this);

		if( mtThreadRunning == false )
		{	//Create common thread manager (this is the first wiimote created)
			mtThreadRunning = true;
			mtThreadHandler = new boost::thread(boost::bind(&WiiMoteFactoryCreator::_updateWiiMotesThread, this));
		}
Ben Hymers's avatar
Ben Hymers committed
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
		//Now, add new WiiMote to thread manager for polling
		{	//Get an auto lock on the list of active wiimotes
			boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);
			mtInUseWiiMotes.push_back(wii);
		}

		return wii;
	}
	else
		OIS_EXCEPT(E_InputDeviceNonExistant, "No Device found which matches description!");
}

//---------------------------------------------------------------------------------//
void WiiMoteFactoryCreator::destroyObject(Object* obj)
{
	if( obj == 0 )
		return;

	int wiis_alive = 0;

	{	//Get an auto lock on the list of active wiimotes
		boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);

		//Find object
		std::vector<WiiMote*>::iterator i = std::find(mtInUseWiiMotes.begin(), mtInUseWiiMotes.end(), obj);
		if( i == mtInUseWiiMotes.end() )
			OIS_EXCEPT(E_General, "Device not found in wimote collection!");

		//Erase opject
		mtInUseWiiMotes.erase(i);

		//Delete object
		delete obj;

		wiis_alive = (int)mtInUseWiiMotes.size();
	}

	//Destroy thread if no longer in use (we do this after unlocking mutex!)
	if( wiis_alive == 0 && mtThreadRunning )
	{
		mtThreadRunning = false;
		mtThreadHandler->join();
		delete mtThreadHandler;
		mtThreadHandler = 0;
	}

}

//---------------------------------------------------------------------------------//
void WiiMoteFactoryCreator::_returnWiiMote(int id)
{	//Restore ID to controller pool
	mFreeWiis.push_front(id);
}

//---------------------------------------------------------------------------------//
bool WiiMoteFactoryCreator::_updateWiiMotesThread()
{
	boost::xtime timer;

	while(mtThreadRunning)
	{
		int numMotes = 0;
		{	//Get an auto lock on the list of active wiimotes
			boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);
			numMotes = (int)mtInUseWiiMotes.size();
			for( std::vector<WiiMote*>::iterator i = mtInUseWiiMotes.begin(), e = mtInUseWiiMotes.end(); i != e; ++i )
			{	//Update it
				(*i)->_threadUpdate();
			}
		}

		//ok, we have updated all wiimotes, let us rest a bit
Ben Hymers's avatar
Ben Hymers committed
195
		//sleep time = 30 / 1000
196
197
198
199
200
201
202
203
204
205
206
		//boost::thread::sleep(xtime) todo xxx wip use sleep instead??
		//boost::thread::yield();
		boost::xtime_get(&timer, boost::TIME_UTC);
		timer.nsec += 20000000; //20 000 000 ~= 1/50 sec
		boost::thread::sleep(timer);
	}

	return true;
}

#endif