winDInputDevice.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 _WINDINPUTDEVICE_H_
  23. #define _WINDINPUTDEVICE_H_
  24. #ifndef _PLATFORMWIN32_H_
  25. #include "platformWin32/platformWin32.h"
  26. #endif
  27. #ifndef _PLATFORMINPUT_H_
  28. #include "platform/platformInput.h"
  29. #endif
  30. #define DIRECTINPUT_VERSION 0x0800
  31. #include <dinput.h>
  32. class DInputDevice : public InputDevice
  33. {
  34. public:
  35. static LPDIRECTINPUT8 smDInputInterface;
  36. protected:
  37. enum Constants
  38. {
  39. QUEUED_BUFFER_SIZE = 128,
  40. SIZEOF_BUTTON = 1, // size of an object's data in bytes
  41. SIZEOF_KEY = 1,
  42. SIZEOF_AXIS = 4,
  43. SIZEOF_POV = 4,
  44. };
  45. static U8 smDeviceCount[ NUM_INPUT_DEVICE_TYPES ];
  46. static bool smInitialized;
  47. /// Are we an XInput device?
  48. bool mIsXInput;
  49. //--------------------------------------
  50. LPDIRECTINPUTDEVICE8 mDevice;
  51. DIDEVICEINSTANCE mDeviceInstance;
  52. DIDEVCAPS mDeviceCaps;
  53. U8 mDeviceType;
  54. U8 mDeviceID;
  55. bool mAcquired;
  56. bool mNeedSync;
  57. LPDIRECTINPUTEFFECT mForceFeedbackEffect; ///< Holds our DirectInput FF Effect
  58. DWORD mNumForceFeedbackAxes; ///< # axes (we only support 0, 1, or 2
  59. DWORD mForceFeedbackAxes[2]; ///< Force Feedback axes offsets into DIOBJECTFORMAT
  60. //--------------------------------------
  61. DIDEVICEOBJECTINSTANCE* mObjInstance;
  62. DIOBJECTDATAFORMAT* mObjFormat;
  63. ObjInfo* mObjInfo;
  64. U8* mObjBuffer1; // polled device input buffers
  65. U8* mObjBuffer2;
  66. U8* mPrevObjBuffer; // points to buffer 1 or 2
  67. // Hack for POV
  68. S32 mPrevPOVPos;
  69. U32 mObjBufferSize; // size of objBuffer*
  70. U32 mObjCount; // number of objects on this device
  71. U32 mObjEnumCount; // used during enumeration ONLY
  72. U32 mObjBufferOfs; // used during enumeration ONLY
  73. static BOOL CALLBACK EnumObjectsProc( const DIDEVICEOBJECTINSTANCE *doi, LPVOID pvRef );
  74. bool enumerateObjects();
  75. bool processAsync();
  76. bool processImmediate();
  77. DWORD findObjInstance( DWORD offset );
  78. bool buildEvent( DWORD offset, S32 newData, S32 oldData );
  79. public:
  80. DInputDevice( const DIDEVICEINSTANCE* deviceInst );
  81. ~DInputDevice();
  82. static void init();
  83. bool create();
  84. void destroy();
  85. bool acquire();
  86. bool unacquire();
  87. bool isAcquired();
  88. bool isPolled();
  89. U8 getDeviceType();
  90. U8 getDeviceID();
  91. const char* getName();
  92. const char* getProductName();
  93. // Constant Effect Force Feedback
  94. void rumble( F32 x, F32 y );
  95. // Console interface functions:
  96. const char* getJoystickAxesString();
  97. static bool joystickDetected();
  98. //
  99. bool process();
  100. };
  101. //------------------------------------------------------------------------------
  102. inline bool DInputDevice::isAcquired()
  103. {
  104. return mAcquired;
  105. }
  106. //------------------------------------------------------------------------------
  107. inline bool DInputDevice::isPolled()
  108. {
  109. //return true;
  110. return ( mDeviceCaps.dwFlags & DIDC_POLLEDDEVICE ) != 0;
  111. }
  112. //------------------------------------------------------------------------------
  113. inline U8 DInputDevice::getDeviceType()
  114. {
  115. return mDeviceType;
  116. }
  117. //------------------------------------------------------------------------------
  118. inline U8 DInputDevice::getDeviceID()
  119. {
  120. return mDeviceID;
  121. }
  122. //------------------------------------------------------------------------------
  123. //------------------------------------------------------------------------------
  124. InputObjectInstances DIK_to_Key( U8 dikCode );
  125. U8 Key_to_DIK( U16 keyCode );
  126. #endif // _H_WINDINPUTDEVICE_