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

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 279 lines · 194 code · 39 blank · 46 comment · 65 complexity · 7fb9e6801b0e2130e5762e73211e23f3 MD5 · raw file

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