Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
10 KiB

  1. // ===== Copyright 1996-2012, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Provides Cross-Platform Input Device information
  4. //
  5. // ==========================================================================
  6. #include "platforminputdevice.h"
  7. #include "dbg.h"
  8. // NOTE: This has to be the last file included!
  9. #include "tier0/memdbgon.h"
  10. class CAutoInit
  11. {
  12. public:
  13. CAutoInit( void )
  14. {
  15. PlatformInputDevice::InitPlatfromInputDeviceInfo();
  16. }
  17. };
  18. CAutoInit forceInputInit;
  19. int countBits( uint32 iValue )
  20. {
  21. int count = 0;
  22. while ( iValue != 0 )
  23. {
  24. if ( iValue & 0x01 )
  25. {
  26. count++;
  27. }
  28. iValue >>= 1;
  29. }
  30. return count;
  31. }
  32. uint32 getValueofNthSetBit( uint32 iValue, int bitNo )
  33. {
  34. if ( bitNo < 1 || bitNo > 32 )
  35. return 0;
  36. uint32 curValue = 1;
  37. int bitCount = 0;
  38. while ( curValue > 0 )
  39. {
  40. if ( ( iValue & curValue ) != 0 )
  41. {
  42. bitCount++;
  43. if ( bitCount == bitNo)
  44. {
  45. return curValue;
  46. }
  47. }
  48. curValue <<= 1;
  49. }
  50. return 0;
  51. }
  52. int getOrdinalOfSetBit( uint32 iValue, uint32 theBit )
  53. {
  54. if ( ( iValue & theBit ) == 0 )
  55. return 0;
  56. Assert( countBits( theBit ) == 1 );
  57. uint32 curValue = 1;
  58. int bitCount = 0;
  59. while ( curValue > 0 )
  60. {
  61. if ( ( iValue & curValue ) != 0 )
  62. {
  63. bitCount++;
  64. if ( curValue == theBit )
  65. {
  66. return bitCount;
  67. }
  68. }
  69. curValue <<= 1;
  70. }
  71. return 0;
  72. }
  73. bool PlatformInputDevice::s_Initialized = false;
  74. InputDevice_t PlatformInputDevice::s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_COUNT];
  75. int PlatformInputDevice::s_numberPlatformInputDevices[INPUT_DEVICE_PLATFORM_COUNT];
  76. InputDevice_t PlatformInputDevice::s_AllInputDevices = INPUT_DEVICE_NONE;
  77. const InputDevicePlatform_t PlatformInputDevice::s_LocalInputPlatform =
  78. #if defined( PLATFORM_WINDOWS_PC )
  79. INPUT_DEVICE_PLATFORM_WINDOWS;
  80. #elif defined( PLATFORM_OSX )
  81. INPUT_DEVICE_PLATFORM_OSX;
  82. #elif defined( PLATFORM_X360 )
  83. INPUT_DEVICE_PLATFORM_XBOX360;
  84. #elif defined( PLATFORM_PS3 )
  85. INPUT_DEVICE_PLATFORM_PS3;
  86. #elif defined( PLATFORM_LINUX )
  87. INPUT_DEVICE_PLATFORM_LINUX;
  88. #else
  89. INPUT_DEVICE_PLATFORM_NONE;
  90. #endif
  91. void PlatformInputDevice::InitPlatfromInputDeviceInfo( void )
  92. {
  93. // clear all
  94. s_AllInputDevices = INPUT_DEVICE_NONE;
  95. for ( int n = 0; n < INPUT_DEVICE_PLATFORM_COUNT; n++ )
  96. {
  97. s_validPlatformInputDevices[n] = INPUT_DEVICE_NONE;
  98. }
  99. // Windows PC
  100. s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_WINDOWS] =
  101. INPUT_DEVICE_KEYBOARD_MOUSE |
  102. INPUT_DEVICE_GAMEPAD |
  103. INPUT_DEVICE_HYDRA |
  104. INPUT_DEVICE_STEAM_CONTROLLER;
  105. // Mac OSX
  106. s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_OSX] =
  107. INPUT_DEVICE_KEYBOARD_MOUSE |
  108. INPUT_DEVICE_STEAM_CONTROLLER;
  109. // Xbox 360
  110. s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_XBOX360] =
  111. INPUT_DEVICE_GAMEPAD;
  112. // Playstation 3
  113. s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_PS3] =
  114. INPUT_DEVICE_KEYBOARD_MOUSE |
  115. INPUT_DEVICE_GAMEPAD |
  116. INPUT_DEVICE_PLAYSTATION_MOVE |
  117. INPUT_DEVICE_SHARPSHOOTER;
  118. // Linux PC
  119. s_validPlatformInputDevices[INPUT_DEVICE_PLATFORM_LINUX] =
  120. INPUT_DEVICE_KEYBOARD_MOUSE |
  121. INPUT_DEVICE_STEAM_CONTROLLER;
  122. for (int n = 0; n < INPUT_DEVICE_PLATFORM_COUNT; n++ )
  123. {
  124. s_numberPlatformInputDevices[n] = countBits( s_validPlatformInputDevices[n] );
  125. s_AllInputDevices = s_AllInputDevices | s_validPlatformInputDevices[n];
  126. }
  127. #if defined ( _PS3 )
  128. // On PS3 we need to make sure steamworks gets updated with all the required ELO data slots.
  129. // If we change how many devices we have, we need to update steamworks with the proper number
  130. // of ELO variables.
  131. AssertMsg( s_numberPlatformInputDevices[INPUT_DEVICE_PLATFORM_PS3] == 4, "You must update Steamworks with the correct number of input devices for the ELO values." );
  132. #endif
  133. s_Initialized = true;
  134. }
  135. int PlatformInputDevice::GetInputDeviceCountforPlatform( InputDevicePlatform_t platform )
  136. {
  137. Assert( s_Initialized );
  138. if ( platform < INPUT_DEVICE_PLATFORM_LOCAL || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  139. {
  140. AssertMsg( false, "invalid platform" );
  141. return 0;
  142. }
  143. return s_numberPlatformInputDevices[ ( ( platform == INPUT_DEVICE_PLATFORM_LOCAL ) ? s_LocalInputPlatform : platform ) ];
  144. }
  145. InputDevice_t PlatformInputDevice::GetValidInputDevicesForPlatform( InputDevicePlatform_t platform )
  146. {
  147. Assert( s_Initialized );
  148. if ( platform < INPUT_DEVICE_PLATFORM_LOCAL || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  149. {
  150. AssertMsg( false, "invalid platform" );
  151. return INPUT_DEVICE_NONE;
  152. }
  153. return s_validPlatformInputDevices[ ( ( platform == INPUT_DEVICE_PLATFORM_LOCAL ) ? s_LocalInputPlatform : platform ) ];
  154. }
  155. bool PlatformInputDevice::IsInputDeviceValid( InputDevice_t device, InputDevicePlatform_t platform )
  156. {
  157. Assert( s_Initialized );
  158. // make sure inputs make sense
  159. if ( platform < INPUT_DEVICE_PLATFORM_ANY || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  160. {
  161. AssertMsg( false, "invalid platform" );
  162. return false;
  163. }
  164. if ( countBits( device ) != 1 || ( device & s_AllInputDevices ) != device )
  165. {
  166. return false;
  167. }
  168. // any platform? already checked it above
  169. if ( platform == INPUT_DEVICE_PLATFORM_ANY )
  170. {
  171. return true;
  172. }
  173. // translate to current hardware platform if necessary
  174. if ( platform == INPUT_DEVICE_PLATFORM_LOCAL )
  175. {
  176. platform = s_LocalInputPlatform;
  177. }
  178. return ( ( s_validPlatformInputDevices[platform] & device ) == device );
  179. }
  180. const char *PlatformInputDevice::GetInputDeviceNameUI( InputDevice_t device )
  181. {
  182. switch ( device )
  183. {
  184. case INPUT_DEVICE_KEYBOARD_MOUSE: return "#INPUT_DEVICE_KBMOUSE";
  185. #if defined( _PS3 )
  186. case INPUT_DEVICE_GAMEPAD: return "#INPUT_DEVICE_GAMEPAD_PS3";
  187. #else
  188. case INPUT_DEVICE_GAMEPAD: return "#INPUT_DEVICE_GAMEPAD_XBOX";
  189. #endif
  190. case INPUT_DEVICE_PLAYSTATION_MOVE: return "#INPUT_DEVICE_PSMOVE";
  191. case INPUT_DEVICE_HYDRA: return "#INPUT_DEVICE_HYDRA";
  192. case INPUT_DEVICE_SHARPSHOOTER: return "#INPUT_DEVICE_SHARPSHOOTER";
  193. case INPUT_DEVICE_MOVE_NAV_CONTROLLER: return "#INPUT_DEVICE_MOVE_NAV_CONTROLLER";
  194. default:
  195. {
  196. AssertMsg( false, "Invalid Input Device" );
  197. return "<invalid>";
  198. }
  199. }
  200. }
  201. const char *PlatformInputDevice::GetInputDeviceNameInternal( InputDevice_t device )
  202. {
  203. switch ( device )
  204. {
  205. case INPUT_DEVICE_KEYBOARD_MOUSE: return "KBMOUSE";
  206. case INPUT_DEVICE_GAMEPAD: return "GAMEPAD";
  207. case INPUT_DEVICE_PLAYSTATION_MOVE: return "PSMOVE";
  208. case INPUT_DEVICE_HYDRA: return "HYDRA";
  209. case INPUT_DEVICE_SHARPSHOOTER: return "SHARPSHOOTER";
  210. case INPUT_DEVICE_MOVE_NAV_CONTROLLER: return "NAV_CONTROLLER";
  211. default:
  212. {
  213. AssertMsg( false, "Invalid Input Device" );
  214. return "<INVALID>";
  215. }
  216. }
  217. }
  218. InputDevicePlatform_t PlatformInputDevice::GetLocalInputDevicePlatform( void )
  219. {
  220. return s_LocalInputPlatform;
  221. }
  222. bool PlatformInputDevice::IsInputDevicePlatformValid( InputDevicePlatform_t platform )
  223. {
  224. return ( platform > INPUT_DEVICE_PLATFORM_NONE && platform < INPUT_DEVICE_PLATFORM_COUNT );
  225. }
  226. const char *PlatformInputDevice::GetInputDevicePlatformName( InputDevicePlatform_t platform )
  227. {
  228. switch ( platform )
  229. {
  230. case INPUT_DEVICE_PLATFORM_NONE: return "NONE (Not Set)";
  231. case INPUT_DEVICE_PLATFORM_WINDOWS: return "Windows PC";
  232. case INPUT_DEVICE_PLATFORM_OSX: return "Mac OS X";
  233. case INPUT_DEVICE_PLATFORM_XBOX360: return "Xbox 360";
  234. case INPUT_DEVICE_PLATFORM_PS3: return "Playstation 3";
  235. case INPUT_DEVICE_PLATFORM_LINUX: return "Linux PC";
  236. default:
  237. {
  238. AssertMsg( false, "Invalid Input Platform" );
  239. return "<invalid>";
  240. }
  241. }
  242. }
  243. InputDevice_t PlatformInputDevice::GetDefaultInputDeviceForPlatform( InputDevicePlatform_t platform )
  244. {
  245. // make sure inputs make sense
  246. if ( platform < INPUT_DEVICE_PLATFORM_LOCAL || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  247. {
  248. AssertMsg( false, "invalid platform" );
  249. return INPUT_DEVICE_NONE;
  250. }
  251. // translate to current hardware platform if necessary
  252. if ( platform == INPUT_DEVICE_PLATFORM_LOCAL )
  253. {
  254. platform = s_LocalInputPlatform;
  255. }
  256. switch ( platform )
  257. {
  258. case INPUT_DEVICE_PLATFORM_NONE: return INPUT_DEVICE_NONE;
  259. case INPUT_DEVICE_PLATFORM_WINDOWS: return INPUT_DEVICE_KEYBOARD_MOUSE;
  260. case INPUT_DEVICE_PLATFORM_OSX: return INPUT_DEVICE_KEYBOARD_MOUSE;
  261. case INPUT_DEVICE_PLATFORM_XBOX360: return INPUT_DEVICE_GAMEPAD;
  262. case INPUT_DEVICE_PLATFORM_PS3: return INPUT_DEVICE_GAMEPAD;
  263. case INPUT_DEVICE_PLATFORM_LINUX: return INPUT_DEVICE_KEYBOARD_MOUSE;
  264. default:
  265. {
  266. AssertMsg( false, "Default device missing" );
  267. return INPUT_DEVICE_NONE;
  268. }
  269. }
  270. }
  271. int PlatformInputDevice::GetInputDeviceOrdinalForPlatform( InputDevice_t device, InputDevicePlatform_t platform )
  272. {
  273. Assert( s_Initialized );
  274. // make sure inputs make sense
  275. if ( platform < INPUT_DEVICE_PLATFORM_LOCAL || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  276. {
  277. AssertMsg( false, "invalid platform" );
  278. return 0;
  279. }
  280. if ( countBits( device ) != 1 || ( device & s_AllInputDevices ) != device )
  281. {
  282. AssertMsg( false, "invalid device" );
  283. return 0;
  284. }
  285. // translate to current hardware platform if necessary
  286. if ( platform == INPUT_DEVICE_PLATFORM_LOCAL )
  287. {
  288. platform = s_LocalInputPlatform;
  289. }
  290. if ( ( s_validPlatformInputDevices[platform] & device ) == 0 )
  291. {
  292. return 0;
  293. }
  294. return getOrdinalOfSetBit( (uint32) s_validPlatformInputDevices[platform], (uint32) device );
  295. }
  296. InputDevice_t PlatformInputDevice::GetInputDeviceTypefromPlatformOrdinal( int deviceNo, InputDevicePlatform_t platform )
  297. {
  298. Assert( s_Initialized );
  299. // make sure inputs make sense
  300. if ( platform < INPUT_DEVICE_PLATFORM_LOCAL || platform >= INPUT_DEVICE_PLATFORM_COUNT )
  301. {
  302. AssertMsg( false, "invalid platform" );
  303. return INPUT_DEVICE_NONE;
  304. }
  305. // translate to current hardware platform if necessary
  306. if ( platform == INPUT_DEVICE_PLATFORM_LOCAL )
  307. {
  308. platform = s_LocalInputPlatform;
  309. }
  310. if ( deviceNo < 1 || deviceNo > s_numberPlatformInputDevices[platform] )
  311. {
  312. AssertMsg( false, "bad platform device ordnial" );
  313. return INPUT_DEVICE_NONE;
  314. }
  315. return (InputDevice_t) getValueofNthSetBit( (uint32) s_validPlatformInputDevices[platform], deviceNo );
  316. }
  317. bool PlatformInputDevice::IsInputDeviceAPointer( InputDevice_t device )
  318. {
  319. switch ( device )
  320. {
  321. case INPUT_DEVICE_NONE:
  322. case INPUT_DEVICE_KEYBOARD_MOUSE:
  323. case INPUT_DEVICE_GAMEPAD:
  324. case INPUT_DEVICE_MOVE_NAV_CONTROLLER:
  325. return false;
  326. case INPUT_DEVICE_PLAYSTATION_MOVE:
  327. case INPUT_DEVICE_HYDRA:
  328. case INPUT_DEVICE_SHARPSHOOTER:
  329. return true;
  330. default:
  331. AssertMsg(false, "Device not handled.");
  332. break;
  333. }
  334. return false;
  335. }