winDInputDevice.h 5.1 KB

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