winDirectInput.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. struct InputEvent;
  48. //------------------------------------------------------------------------------
  49. class DInputManager : public InputManager
  50. {
  51. private:
  52. typedef SimGroup Parent;
  53. // XInput state
  54. HMODULE mXInputLib;
  55. FN_XInputGetState mfnXInputGetState;
  56. FN_XInputSetState mfnXInputSetState;
  57. XINPUT_CONTROLLER_STATE mXInputStateOld[XINPUT_MAX_CONTROLLERS];
  58. XINPUT_CONTROLLER_STATE mXInputStateNew[XINPUT_MAX_CONTROLLERS];
  59. U32 mLastDisconnectTime[XINPUT_MAX_CONTROLLERS];
  60. bool mXInputStateReset;
  61. bool mXInputDeadZoneOn;
  62. /// Number of milliseconds to skip checking an xinput device if it was
  63. /// disconnected on last check.
  64. const static U32 csmDisconnectedSkipDelay = 250;
  65. HMODULE mDInputLib;
  66. LPDIRECTINPUT8 mDInputInterface;
  67. static bool smKeyboardEnabled;
  68. static bool smMouseEnabled;
  69. static bool smJoystickEnabled;
  70. static bool smXInputEnabled;
  71. bool mKeyboardActive;
  72. bool mMouseActive;
  73. bool mJoystickActive;
  74. bool mXInputActive;
  75. void enumerateDevices();
  76. static BOOL CALLBACK EnumDevicesProc( const DIDEVICEINSTANCE *pddi, LPVOID pvRef );
  77. bool acquire( U8 deviceType, U8 deviceID );
  78. void unacquire( U8 deviceType, U8 deviceID );
  79. // XInput worker functions
  80. void buildXInputEvent( U32 deviceInst, XInputEventType objType, InputObjectInstances objInst, InputActionType action, float fValue );
  81. void fireXInputConnectEvent( int controllerID, bool condition, bool connected );
  82. void fireXInputMoveEvent( int controllerID, bool condition, InputObjectInstances objInst, float fValue );
  83. void fireXInputButtonEvent( int controllerID, bool forceFire, int button, InputObjectInstances objInst );
  84. void processXInput();
  85. public:
  86. DInputManager();
  87. bool enable();
  88. void disable();
  89. void onDeleteNotify( SimObject* object );
  90. bool onAdd();
  91. void onRemove();
  92. void process();
  93. // DirectInput functions:
  94. static void init();
  95. static bool enableKeyboard();
  96. static void disableKeyboard();
  97. static bool isKeyboardEnabled();
  98. bool activateKeyboard();
  99. void deactivateKeyboard();
  100. bool isKeyboardActive() { return( mKeyboardActive ); }
  101. static bool enableMouse();
  102. static void disableMouse();
  103. static bool isMouseEnabled();
  104. bool activateMouse();
  105. void deactivateMouse();
  106. bool isMouseActive() { return( mMouseActive ); }
  107. static bool enableJoystick();
  108. static void disableJoystick();
  109. static bool isJoystickEnabled();
  110. bool activateJoystick();
  111. void deactivateJoystick();
  112. bool isJoystickActive() { return( mJoystickActive ); }
  113. static bool enableXInput();
  114. static void disableXInput();
  115. static bool isXInputEnabled();
  116. bool activateXInput();
  117. void deactivateXInput();
  118. bool isXInputActive() { return( mXInputActive ); }
  119. void resetXInput() { mXInputStateReset = true; }
  120. bool isXInputConnected(int controllerID);
  121. int getXInputState(int controllerID, int property, bool current);
  122. // Console interface:
  123. const char* getJoystickAxesString( U32 deviceID );
  124. bool rumble( const char *pDeviceName, float x, float y );
  125. };
  126. #endif // _H_WINDIRECTINPUT_