winDInputDevice.h 5.5 KB

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