winDInputDevice.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //--------------------------------------
  65. DIDEVICEOBJECTINSTANCE* mObjInstance;
  66. DIOBJECTDATAFORMAT* mObjFormat;
  67. ObjInfo* mObjInfo;
  68. U8* mObjBuffer1; // polled device input buffers
  69. U8* mObjBuffer2;
  70. U8* mPrevObjBuffer; // points to buffer 1 or 2
  71. // Hack for POV
  72. S32 mPrevPOVPos;
  73. U32 mObjBufferSize; // size of objBuffer*
  74. U32 mObjCount; // number of objects on this device
  75. U32 mObjEnumCount; // used during enumeration ONLY
  76. U32 mObjBufferOfs; // used during enumeration ONLY
  77. static BOOL CALLBACK EnumObjectsProc( const DIDEVICEOBJECTINSTANCE *doi, LPVOID pvRef );
  78. bool enumerateObjects();
  79. bool processAsync();
  80. bool processImmediate();
  81. bool processKeyEvent( InputEvent &event );
  82. void syncKeyboardState();
  83. DWORD findObjInstance( DWORD offset );
  84. bool buildEvent( DWORD offset, S32 newData, S32 oldData );
  85. public:
  86. DInputDevice( const DIDEVICEINSTANCE* deviceInst );
  87. ~DInputDevice();
  88. static void init();
  89. static void resetModifierKeys();
  90. bool create();
  91. void destroy();
  92. bool acquire();
  93. bool unacquire();
  94. bool isAcquired();
  95. bool isPolled();
  96. U8 getDeviceType();
  97. U8 getDeviceID();
  98. const char* getName();
  99. const char* getProductName();
  100. // Console interface functions:
  101. const char* getJoystickAxesString();
  102. static bool joystickDetected();
  103. //
  104. bool process();
  105. };
  106. //------------------------------------------------------------------------------
  107. inline bool DInputDevice::isAcquired()
  108. {
  109. return mAcquired;
  110. }
  111. //------------------------------------------------------------------------------
  112. inline bool DInputDevice::isPolled()
  113. {
  114. //return true;
  115. return ( mDeviceCaps.dwFlags & DIDC_POLLEDDEVICE );
  116. }
  117. //------------------------------------------------------------------------------
  118. inline U8 DInputDevice::getDeviceType()
  119. {
  120. return mDeviceType;
  121. }
  122. //------------------------------------------------------------------------------
  123. inline U8 DInputDevice::getDeviceID()
  124. {
  125. return mDeviceID;
  126. }
  127. //------------------------------------------------------------------------------
  128. //------------------------------------------------------------------------------
  129. U16 DIK_to_Key( U8 dikCode );
  130. U8 Key_to_DIK( U16 keyCode );
  131. #endif // _H_WINDINPUTDEVICE_