OISInputManager.cpp 7.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
64
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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
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.

Permission is granted to anyone to use this software for any purpose, including commercial 
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

    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 
		not required.

    2. Altered source versions must be plainly marked as such, and must not be 
		misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/
#include "OISInputManager.h"
#include "OISException.h"
#include "OISFactoryCreator.h"
#include "OISObject.h"
#include <sstream>
#include <algorithm>

//Bring in correct Header / InputManager for current build platform
#if defined OIS_SDL_PLATFORM
#  include "SDL/SDLInputManager.h"
#elif defined OIS_WIN32_PLATFORM
#  include "win32/Win32InputManager.h"
#elif defined OIS_LINUX_PLATFORM
#  include "linux/LinuxInputManager.h"
#elif defined OIS_APPLE_PLATFORM
#  include "mac/MacInputManager.h"
#elif defined OIS_IPHONE_PLATFORM
#  include "iphone/iPhoneInputManager.h"
#elif defined OIS_XBOX_PLATFORM
#  include "xbox/XBoxInputManager.h"
#endif

//Bring in extra controls
#if defined OIS_LIRC_SUPPORT
#  include "extras/LIRC/OISLIRCFactoryCreator.h"
#endif
#if defined OIS_WIN32_WIIMOTE_SUPPORT
#  include "win32/extras/WiiMote/OISWiiMoteFactoryCreator.h"
#endif


using namespace OIS;

//----------------------------------------------------------------------------//
InputManager::InputManager(const std::string& name) :
	m_VersionName(OIS_VERSION_NAME),
	mInputSystemName(name),
	m_lircSupport(0),
	m_wiiMoteSupport(0)
{
    mFactories.clear();
    mFactoryObjects.clear();
}

//----------------------------------------------------------------------------//
InputManager::~InputManager()
{
#if defined OIS_LIRC_SUPPORT
	delete m_lircSupport;
#endif

#if defined OIS_WIN32_WIIMOTE_SUPPORT
	delete m_wiiMoteSupport;
#endif
}

//----------------------------------------------------------------------------//
unsigned int InputManager::getVersionNumber()
{
	return OIS_VERSION;
}

//----------------------------------------------------------------------------//
const std::string &InputManager::getVersionName()
{
	return m_VersionName;
}

//----------------------------------------------------------------------------//
InputManager* InputManager::createInputSystem( std::size_t windowhandle )
{
	ParamList pl;
	std::ostringstream wnd;
	wnd << windowhandle;
	pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));

	return createInputSystem( pl );
}

//----------------------------------------------------------------------------//
InputManager* InputManager::createInputSystem( ParamList &paramList )
{
	InputManager* im = 0;

#if defined OIS_SDL_PLATFORM
	im = new SDLInputManager();
#elif defined OIS_WIN32_PLATFORM
	im = new Win32InputManager();
#elif defined OIS_XBOX_PLATFORM
	im = new XBoxInputManager();
#elif defined OIS_LINUX_PLATFORM
	im = new LinuxInputManager();
#elif defined OIS_APPLE_PLATFORM
	im = new MacInputManager();
#elif defined OIS_IPHONE_PLATFORM
	im = new iPhoneInputManager();
#else
	OIS_EXCEPT(E_General, "No platform library.. check build platform defines!");
#endif 

	try
	{
		im->_initialize(paramList);
	}
	catch(...)
	{
		delete im;
		throw; //rethrow
	}

	return im;
}

//----------------------------------------------------------------------------//
void InputManager::destroyInputSystem(InputManager* manager)
{
	if( manager == 0 )
		return;

	//Cleanup before deleting...
	for( FactoryCreatedObject::iterator i = manager->mFactoryObjects.begin(); 
		i != manager->mFactoryObjects.end(); ++i )
	{
		i->second->destroyObject( i->first );
	}

	manager->mFactoryObjects.clear();
	delete manager;
}

//--------------------------------------------------------------------------------//
const std::string& InputManager::inputSystemName()
{
	return mInputSystemName;
}

//--------------------------------------------------------------------------------//
int InputManager::getNumberOfDevices( Type iType )
{
	//Count up all the factories devices
	int factoyObjects = 0;
	FactoryList::iterator i = mFactories.begin(), e = mFactories.end();
	for( ; i != e; ++i )
		factoyObjects += (*i)->totalDevices(iType);

	return factoyObjects;
}

//----------------------------------------------------------------------------//
DeviceList InputManager::listFreeDevices()
{
	DeviceList list;
	FactoryList::iterator i = mFactories.begin(), e = mFactories.end();
	for( ; i != e; ++i )
	{
		DeviceList temp = (*i)->freeDeviceList();
		list.insert(temp.begin(), temp.end());
	}

	return list;
}

//----------------------------------------------------------------------------//
Object* InputManager::createInputObject( Type iType, bool bufferMode, const std::string &vendor )
{
	Object* obj = 0;
	FactoryList::iterator i = mFactories.begin(), e = mFactories.end();
	for( ; i != e; ++i)
	{
		if( (*i)->freeDevices(iType) > 0 )
		{
			if( vendor == "" || (*i)->vendorExist(iType, vendor) )
			{
				obj = (*i)->createObject(this, iType, bufferMode, vendor);
				mFactoryObjects[obj] = (*i);
				break;
			}
		}
	}

	if(!obj)
		OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");

	try
	{	//Intialize device
		obj->_initialize();
	}
	catch(...)
	{	//Somekind of error, cleanup and rethrow
		destroyInputObject(obj);
		throw;
	}

	return obj;
}

//----------------------------------------------------------------------------//
void InputManager::destroyInputObject( Object* obj )
{
	if( obj == 0 )
		return;

	FactoryCreatedObject::iterator i = mFactoryObjects.find(obj);
	if( i != mFactoryObjects.end() )
	{
		i->second->destroyObject(obj);
		mFactoryObjects.erase(i);
	}
	else
	{
		OIS_EXCEPT(E_General, "Object creator not known.");
	}
}

//----------------------------------------------------------------------------//
void InputManager::addFactoryCreator( FactoryCreator* factory )
{
	if(factory != 0)
		mFactories.push_back(factory);
}

//----------------------------------------------------------------------------//
void InputManager::removeFactoryCreator( FactoryCreator* factory )
{
	if(factory != 0)
	{
		//First, destroy all devices created with the factory
		for( FactoryCreatedObject::iterator i = mFactoryObjects.begin(); i != mFactoryObjects.end(); ++i )
		{
			if( i->second == factory )
			{
				i->second->destroyObject(i->first);
				mFactoryObjects.erase(i++);
			}
		}

		//Now, remove the factory itself
		FactoryList::iterator fact = std::find(mFactories.begin(), mFactories.end(), factory);
		if( fact != mFactories.end() )
			mFactories.erase(fact);
	}
}

//----------------------------------------------------------------------------//
void InputManager::enableAddOnFactory(AddOnFactories factory)
{
#if defined OIS_LIRC_SUPPORT
	if( factory == AddOn_LIRC || factory == AddOn_All )
	{
		if( m_lircSupport == 0 )
		{
			m_lircSupport = new LIRCFactoryCreator();
			addFactoryCreator(m_lircSupport);
		}
	}
#endif

#if defined OIS_WIN32_WIIMOTE_SUPPORT
	if( factory == AddOn_WiiMote || factory == AddOn_All )
	{
		if( m_wiiMoteSupport == 0 )
		{
			m_wiiMoteSupport = new WiiMoteFactoryCreator();
			addFactoryCreator(m_wiiMoteSupport);
		}
	}
#endif
}