winDirectInput.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _WINDIRECTINPUT_H_
  23. #define _WINDIRECTINPUT_H_
  24. #ifndef _PLATFORMWIN32_H_
  25. #include "platformWin32/platformWin32.h"
  26. #endif
  27. #ifndef _PLATFORMINPUT_H_
  28. #include "platform/platformInput.h"
  29. #endif
  30. #ifndef _WINDINPUTDEVICE_H_
  31. #include "platformWin32/winDInputDevice.h"
  32. #endif
  33. #define DIRECTINPUT_VERSION 0x0800
  34. #include <dinput.h>
  35. #include <xinput.h>
  36. // XInput related definitions
  37. typedef DWORD (WINAPI* FN_XInputGetState)(DWORD dwUserIndex, XINPUT_STATE* pState);
  38. typedef DWORD (WINAPI* FN_XInputSetState)(DWORD dwUserIndex, XINPUT_VIBRATION* pVibration);
  39. #define XINPUT_MAX_CONTROLLERS 4 // XInput handles up to 4 controllers
  40. #define XINPUT_DEADZONE ( 0.24f * FLOAT(0x7FFF) ) // Default to 24% of the +/- 32767 range. This is a reasonable default value but can be altered if needed.
  41. struct XINPUT_CONTROLLER_STATE
  42. {
  43. XINPUT_STATE state;
  44. bool bConnected;
  45. };
  46. //------------------------------------------------------------------------------
  47. class DInputManager : public InputManager
  48. {
  49. private:
  50. typedef SimGroup Parent;
  51. // XInput state
  52. HMODULE mXInputLib;
  53. FN_XInputGetState mfnXInputGetState;
  54. FN_XInputSetState mfnXInputSetState;
  55. XINPUT_CONTROLLER_STATE mXInputStateOld[XINPUT_MAX_CONTROLLERS];
  56. XINPUT_CONTROLLER_STATE mXInputStateNew[XINPUT_MAX_CONTROLLERS];
  57. U32 mLastDisconnectTime[XINPUT_MAX_CONTROLLERS];
  58. bool mXInputStateReset;
  59. bool mXInputDeadZoneOn;
  60. /// Number of milliseconds to skip checking an xinput device if it was
  61. /// disconnected on last check.
  62. const static U32 csmDisconnectedSkipDelay = 250;
  63. HMODULE mDInputLib;
  64. LPDIRECTINPUT8 mDInputInterface;
  65. static bool smJoystickEnabled;
  66. static bool smXInputEnabled;
  67. bool mJoystickActive;
  68. bool mXInputActive;
  69. void enumerateDevices();
  70. static BOOL CALLBACK EnumDevicesProc( const DIDEVICEINSTANCE *pddi, LPVOID pvRef );
  71. bool acquire( U8 deviceType, U8 deviceID );
  72. void unacquire( U8 deviceType, U8 deviceID );
  73. // XInput worker functions
  74. void buildXInputEvent( U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue );
  75. void fireXInputConnectEvent( S32 controllerID, bool condition, bool connected );
  76. void fireXInputMoveEvent( S32 controllerID, bool condition, InputObjectInstances objInst, F32 fValue );
  77. void fireXInputButtonEvent( S32 controllerID, bool forceFire, S32 button, InputObjectInstances objInst );
  78. void processXInput();
  79. public:
  80. DInputManager();
  81. bool enable();
  82. void disable();
  83. void onDeleteNotify( SimObject* object );
  84. bool onAdd();
  85. void onRemove();
  86. void process();
  87. // DirectInput functions:
  88. static void init();
  89. static bool enableJoystick();
  90. static void disableJoystick();
  91. static bool isJoystickEnabled();
  92. bool activateJoystick();
  93. void deactivateJoystick();
  94. bool isJoystickActive() { return( mJoystickActive ); }
  95. static bool enableXInput();
  96. static void disableXInput();
  97. static bool isXInputEnabled();
  98. bool activateXInput();
  99. void deactivateXInput();
  100. bool isXInputActive() { return( mXInputActive ); }
  101. void resetXInput() { mXInputStateReset = true; }
  102. bool isXInputConnected(S32 controllerID);
  103. S32 getXInputState(S32 controllerID, S32 property, bool current);
  104. // Console interface:
  105. const char* getJoystickAxesString( U32 deviceID );
  106. bool rumble( const char *pDeviceName, F32 x, F32 y );
  107. };
  108. #endif // _H_WINDIRECTINPUT_