PageRenderTime 61ms CodeModel.GetById 10ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ois/src/win32/Win32InputManager.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 279 lines | 194 code | 39 blank | 46 comment | 62 complexity | 7fb9e6801b0e2130e5762e73211e23f3 MD5 | raw file
  1/*
  2The zlib/libpng License
  3
  4Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  5
  6This software is provided 'as-is', without any express or implied warranty. In no event will
  7the authors be held liable for any damages arising from the use of this software.
  8
  9Permission is granted to anyone to use this software for any purpose, including commercial 
 10applications, and to alter it and redistribute it freely, subject to the following
 11restrictions:
 12
 13    1. The origin of this software must not be misrepresented; you must not claim that 
 14		you wrote the original software. If you use this software in a product, 
 15		an acknowledgment in the product documentation would be appreciated but is 
 16		not required.
 17
 18    2. Altered source versions must be plainly marked as such, and must not be 
 19		misrepresented as being the original software.
 20
 21    3. This notice may not be removed or altered from any source distribution.
 22*/
 23#include "win32/Win32InputManager.h"
 24#include "win32/Win32KeyBoard.h"
 25#include "win32/Win32Mouse.h"
 26#include "win32/Win32JoyStick.h"
 27#include "OISException.h"
 28
 29using namespace OIS;
 30
 31//--------------------------------------------------------------------------------//
 32Win32InputManager::Win32InputManager() : InputManager("Win32InputManager")
 33{
 34	hWnd = 0;
 35	mDirectInput = 0;
 36
 37	kbSettings    = 0;
 38	mouseSettings = 0;
 39	joySettings   = 0;
 40
 41	joySticks = 0;
 42	keyboardUsed = mouseUsed = false;
 43
 44	//Setup our internal factories
 45	mFactories.push_back(this);
 46}
 47
 48//--------------------------------------------------------------------------------//
 49Win32InputManager::~Win32InputManager()
 50{
 51	if( mDirectInput )
 52	{
 53		mDirectInput->Release();
 54		mDirectInput = 0;
 55	}
 56}
 57
 58//--------------------------------------------------------------------------------//
 59void Win32InputManager::_initialize( ParamList &paramList )
 60{
 61	HINSTANCE hInst = 0;
 62	HRESULT hr;
 63
 64
 65	//First of all, get the Windows Handle and Instance
 66	ParamList::iterator i = paramList.find("WINDOW");
 67	if( i == paramList.end() ) 
 68		OIS_EXCEPT( E_InvalidParam, "Win32InputManager::Win32InputManager >> No HWND found!" );
 69
 70	// Get number as 64 bit and then convert. Handles the case of 32 or 64 bit HWND
 71#ifdef _MSC_VER
 72	unsigned __int64 handle = _strtoui64(i->second.c_str(), 0, 10);
 73#else
 74    unsigned __int64 handle = strtoull(i->second.c_str(), 0, 10);
 75#endif
 76	hWnd  = (HWND)handle;
 77
 78	if( IsWindow(hWnd) == 0 )
 79		OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> The sent HWND is not valid!");
 80
 81	hInst = GetModuleHandle(0);
 82
 83	//Create the device
 84	hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&mDirectInput, NULL );
 85    if (FAILED(hr))	
 86		OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> Not able to init DirectX8 Input!");
 87
 88	//Ok, now we have DirectInput, parse whatever extra settings were sent to us
 89	_parseConfigSettings( paramList );
 90
 91	// Enumerate devices ...
 92	_enumerateDevices();
 93}
 94
 95//--------------------------------------------------------------------------------//
 96void Win32InputManager::_parseConfigSettings( ParamList &paramList )
 97{
 98	//Here we pick up settings such as a device's cooperation mode
 99	std::map<std::string, DWORD> temp;
100	temp["DISCL_BACKGROUND"]	= DISCL_BACKGROUND;
101	temp["DISCL_EXCLUSIVE"]		= DISCL_EXCLUSIVE;
102	temp["DISCL_FOREGROUND"]	= DISCL_FOREGROUND;
103	temp["DISCL_NONEXCLUSIVE"]	= DISCL_NONEXCLUSIVE;
104	temp["DISCL_NOWINKEY"]		= DISCL_NOWINKEY;
105
106	//Check for pairs: ie. ("w32_keyboard","DISCL_NOWINKEY")("w32_keyboard","DISCL_FOREGROUND")
107	ParamList::iterator i = paramList.begin(), e = paramList.end();
108	for( ; i != e; ++i ) 
109	{
110		if( i->first == "w32_keyboard" )
111				kbSettings |= temp[i->second];
112		else if( i->first == "w32_mouse" )
113				mouseSettings |= temp[i->second];
114		else if( i->first == "w32_joystick" )
115				joySettings |= temp[i->second];
116	}
117	if( kbSettings == 0 ) kbSettings = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY;
118	if( mouseSettings == 0 ) mouseSettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
119	if( joySettings == 0 ) joySettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
120}
121
122//--------------------------------------------------------------------------------//
123void Win32InputManager::_enumerateDevices()
124{
125	//Enumerate all attached devices
126	mDirectInput->EnumDevices(NULL, _DIEnumDevCallback, this, DIEDFL_ATTACHEDONLY);
127
128#ifdef OIS_WIN32_XINPUT_SUPPORT
129	//let's check how many possible XInput devices we may have (max 4)... 
130	for(int i = 0; i < 3; ++i)
131	{
132		XINPUT_STATE state;
133		if(XInputGetState(i, &state) != ERROR_DEVICE_NOT_CONNECTED)
134		{	//Once we found 1, just check our whole list against devices
135			Win32JoyStick::CheckXInputDevices(unusedJoyStickList);
136			break;
137		}
138	}
139#endif
140}
141
142//--------------------------------------------------------------------------------//
143BOOL CALLBACK Win32InputManager::_DIEnumDevCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
144{
145	Win32InputManager *_this_ = static_cast<Win32InputManager*>(pvRef);
146
147	// Register only game devices (keyboard and mouse are managed differently).
148	if( GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK ||
149		GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD ||
150		GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_1STPERSON ||
151		GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DRIVING ||
152		GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_FLIGHT)
153	{
154		JoyStickInfo jsInfo;
155		jsInfo.isXInput = false;
156		jsInfo.productGuid = lpddi->guidProduct;
157		jsInfo.deviceID = lpddi->guidInstance;
158		jsInfo.vendor = lpddi->tszInstanceName;
159		jsInfo.devId = _this_->joySticks;
160
161		_this_->joySticks++;
162		
163		_this_->unusedJoyStickList.push_back( jsInfo );
164	}
165
166	return DIENUM_CONTINUE;
167}
168
169//----------------------------------------------------------------------------//
170void Win32InputManager::_returnJoyStick(const JoyStickInfo& joystick)
171{
172	unusedJoyStickList.push_back(joystick);
173}
174
175//----------------------------------------------------------------------------//
176DeviceList Win32InputManager::freeDeviceList()
177{
178	DeviceList ret;
179
180	if( keyboardUsed == false )
181		ret.insert(std::make_pair(OISKeyboard, mInputSystemName));
182
183	if( mouseUsed == false )
184		ret.insert(std::make_pair(OISMouse, mInputSystemName));
185
186	for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
187		ret.insert(std::make_pair(OISJoyStick, i->vendor));
188
189	return ret;
190}
191
192//----------------------------------------------------------------------------//
193int Win32InputManager::totalDevices(Type iType)
194{
195	switch(iType)
196	{
197	case OISKeyboard: return 1;
198	case OISMouse: return 1;
199	case OISJoyStick: return joySticks;
200	default: return 0;
201	}
202}
203
204//----------------------------------------------------------------------------//
205int Win32InputManager::freeDevices(Type iType)
206{
207	switch(iType)
208	{
209	case OISKeyboard: return keyboardUsed ? 0 : 1;
210	case OISMouse: return mouseUsed ? 0 : 1;
211	case OISJoyStick: return (int)unusedJoyStickList.size();
212	default: return 0;
213	}
214}
215
216//----------------------------------------------------------------------------//
217bool Win32InputManager::vendorExist(Type iType, const std::string & vendor)
218{
219	if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName )
220	{
221		return true;
222	}
223	else if( iType == OISJoyStick )
224	{
225		for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
226			if(i->vendor == vendor)
227				return true;
228	}
229
230	return false;
231}
232
233//----------------------------------------------------------------------------//
234Object* Win32InputManager::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
235{
236	Object *obj = 0;
237
238	switch(iType)
239	{
240	case OISKeyboard: 
241	{
242		if( keyboardUsed == false )
243			obj = new Win32Keyboard(this, mDirectInput, bufferMode, kbSettings);
244		break;
245	}
246	case OISMouse:
247	{
248		if( mouseUsed == false )
249			obj = new Win32Mouse(this, mDirectInput, bufferMode, mouseSettings);
250		break;
251	}
252	case OISJoyStick:
253	{
254		for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
255		{
256			if(vendor == "" || i->vendor == vendor)
257			{
258				obj = new Win32JoyStick(this, mDirectInput, bufferMode, joySettings, *i);
259				unusedJoyStickList.erase(i);
260				break;
261			}
262		}
263		break;
264	}
265	default:
266		break;
267	}
268
269	if( obj == 0 )
270		OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
271
272	return obj;
273}
274
275//----------------------------------------------------------------------------//
276void Win32InputManager::destroyObject(Object* obj)
277{
278	delete obj;
279}