razerHydraDevice.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 _RAZERHYDRADEVICE_H_
  23. #define _RAZERHYDRADEVICE_H_
  24. #include "platform/input/razerHydra/razerHydraConstants.h"
  25. #include "platform/platformDlibrary.h"
  26. #include "platform/input/IInputDevice.h"
  27. #include "platform/input/event.h"
  28. #include "core/util/tSingleton.h"
  29. #include "math/mQuat.h"
  30. #include "sixense.h"
  31. #define HYDRA_ACTIVE_CHECK_FREQ 1000
  32. #define FN_HYDRA __cdecl
  33. // Library function typedefs
  34. typedef int (FN_HYDRA* FN_SixenseInit)();
  35. typedef int (FN_HYDRA* FN_SixenseExit)();
  36. typedef int (FN_HYDRA* FN_SixenseGetMaxBases)();
  37. typedef int (FN_HYDRA* FN_SixenseSetActiveBase)(int base_num);
  38. typedef int (FN_HYDRA* FN_SixenseIsBaseConnected)(int base_num);
  39. typedef int (FN_HYDRA* FN_SixenseGetMaxControllers)();
  40. typedef int (FN_HYDRA* FN_SixenseIsControllerEnabled)(int which);
  41. typedef int (FN_HYDRA* FN_SixenseGetNumActiveControllers)();
  42. typedef int (FN_HYDRA* FN_SixenseGetHistorySize)();
  43. typedef int (FN_HYDRA* FN_SixenseGetData)(int which, int index_back, sixenseControllerData *);
  44. typedef int (FN_HYDRA* FN_SixenseGetAllData)(int index_back, sixenseAllControllerData *);
  45. typedef int (FN_HYDRA* FN_SixenseGetNewestData)(int which, sixenseControllerData *);
  46. typedef int (FN_HYDRA* FN_SixenseGetAllNewestData)(sixenseAllControllerData *);
  47. typedef int (FN_HYDRA* FN_SixenseSetHemisphereTrackingMode)(int which_controller, int state);
  48. typedef int (FN_HYDRA* FN_SixenseGetHemisphereTrackingMode)(int which_controller, int *state);
  49. typedef int (FN_HYDRA* FN_SixenseAutoEnableHemisphereTracking)(int which_controller);
  50. typedef int (FN_HYDRA* FN_SixenseSetHighPriorityBindingEnabled)(int on_or_off);
  51. typedef int (FN_HYDRA* FN_SixenseGetHighPriorityBindingEnabled)(int *on_or_off);
  52. typedef int (FN_HYDRA* FN_SixenseTriggerVibration)(int controller_id, int duration_100ms, int pattern_id);
  53. typedef int (FN_HYDRA* FN_SixenseSetFilterEnabled)(int on_or_off);
  54. typedef int (FN_HYDRA* FN_SixenseGetFilterEnabled)(int *on_or_off);
  55. typedef int (FN_HYDRA* FN_SixenseSetFilterParams)(float near_range, float near_val, float far_range, float far_val);
  56. typedef int (FN_HYDRA* FN_SixenseGetFilterParams)(float *near_range, float *near_val, float *far_range, float *far_val);
  57. typedef int (FN_HYDRA* FN_SixenseSetBaseColor)(unsigned char red, unsigned char green, unsigned char blue);
  58. typedef int (FN_HYDRA* FN_SixenseGetBaseColor)(unsigned char *red, unsigned char *green, unsigned char *blue);
  59. struct RazerHyrdaControllerData;
  60. class RazerHydraDevice : public IInputDevice
  61. {
  62. public:
  63. static bool smEnableDevice;
  64. // Should events be sent when a controller is docked
  65. static bool smProcessWhenDocked;
  66. // The type of position events to broadcast
  67. static bool smSeparatePositionEvents;
  68. static bool smCombinedPositionEvents;
  69. // Broadcast controller rotation as axis
  70. static bool smRotationAsAxisEvents;
  71. // The maximum controller angle when used as an axis event
  72. // as measured from a vector pointing straight up (in degrees)
  73. static F32 smMaximumAxisAngle;
  74. // Indicates that a whole frame event should be generated and frames
  75. // should be buffered.
  76. static bool smGenerateWholeFrameEvents;
  77. // Controller Action Codes
  78. static U32 RH_DOCKED[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  79. static U32 RH_POSX[RazerHydraConstants::MaxControllers]; // SI_FLOAT
  80. static U32 RH_POSY[RazerHydraConstants::MaxControllers];
  81. static U32 RH_POSZ[RazerHydraConstants::MaxControllers];
  82. static U32 RH_POS[RazerHydraConstants::MaxControllers]; // SI_POS
  83. static U32 RH_ROT[RazerHydraConstants::MaxControllers]; // SI_ROT
  84. static U32 RH_ROTAXISX[RazerHydraConstants::MaxControllers]; // SI_AXIS
  85. static U32 RH_ROTAXISY[RazerHydraConstants::MaxControllers];
  86. static U32 RH_THUMBX[RazerHydraConstants::MaxControllers]; // SI_AXIS
  87. static U32 RH_THUMBY[RazerHydraConstants::MaxControllers];
  88. static U32 RH_TRIGGER[RazerHydraConstants::MaxControllers]; // SI_AXIS
  89. static U32 RH_SHOULDER[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  90. static U32 RH_THUMB[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  91. static U32 RH_START[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  92. static U32 RH_1[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  93. static U32 RH_2[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  94. static U32 RH_3[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  95. static U32 RH_4[RazerHydraConstants::MaxControllers]; // SI_BUTTON
  96. // Whole frame
  97. static U32 RH_FRAME; // SI_INT
  98. public:
  99. RazerHydraDevice();
  100. ~RazerHydraDevice();
  101. static void staticInit();
  102. bool enable();
  103. void disable();
  104. bool process();
  105. bool isActive() { return mActive; }
  106. bool isControllerDocked(U32 controller);
  107. const Point3F& getControllerPosition(U32 controller);
  108. const QuatF& getControllerRotation(U32 controller);
  109. protected:
  110. DLibraryRef mRazerHydraLib;
  111. FN_SixenseInit mfnSixenseInit;
  112. FN_SixenseExit mfnSixenseExit;
  113. FN_SixenseGetMaxBases mfnSixenseGetMaxBases;
  114. FN_SixenseSetActiveBase mfnSixenseSetActiveBase;
  115. FN_SixenseIsBaseConnected mfnSixenseIsBaseConnected;
  116. FN_SixenseGetMaxControllers mfnSixenseGetMaxControllers;
  117. FN_SixenseIsControllerEnabled mfnSixenseIsControllerEnabled;
  118. FN_SixenseGetNumActiveControllers mfnSixenseGetNumActiveControllers;
  119. FN_SixenseGetHistorySize mfnSixenseGetHistorySize;
  120. FN_SixenseGetData mfnSixenseGetData;
  121. FN_SixenseGetAllData mfnSixenseGetAllData;
  122. FN_SixenseGetNewestData mfnSixenseGetNewestData;
  123. FN_SixenseGetAllNewestData mfnSixenseGetAllNewestData;
  124. FN_SixenseSetHemisphereTrackingMode mfnSixenseSetHemisphereTrackingMode;
  125. FN_SixenseGetHemisphereTrackingMode mfnSixenseGetHemisphereTrackingMode;
  126. FN_SixenseAutoEnableHemisphereTracking mfnSixenseAutoEnableHemisphereTracking;
  127. FN_SixenseSetHighPriorityBindingEnabled mfnSixenseSetHighPriorityBindingEnabled;
  128. FN_SixenseGetHighPriorityBindingEnabled mfnSixenseGetHighPriorityBindingEnabled;
  129. FN_SixenseTriggerVibration mfnSixenseTriggerVibration;
  130. FN_SixenseSetFilterEnabled mfnSixenseSetFilterEnabled;
  131. FN_SixenseGetFilterEnabled mfnSixenseGetFilterEnabled;
  132. FN_SixenseSetFilterParams mfnSixenseSetFilterParams;
  133. FN_SixenseGetFilterParams mfnSixenseGetFilterParams;
  134. FN_SixenseSetBaseColor mfnSixenseSetBaseColor;
  135. FN_SixenseGetBaseColor mfnSixenseGetBaseColor;
  136. S32 mMaximumBases;
  137. S32 mMaximumControllers;
  138. S32 mNumberActiveControllers;
  139. /// Is the Razer Hydra active (enabled means the library is loaded)
  140. bool mActive;
  141. /// When was the last check for an active Hydra
  142. S32 mLastActiveCheck;
  143. /// Buffers to store data for the controllers
  144. RazerHyrdaControllerData* mDataBuffer[RazerHydraConstants::MaxControllers][2];
  145. /// Points to the buffers that holds the previously collected data
  146. /// for each controller
  147. RazerHyrdaControllerData* mPrevData[RazerHydraConstants::MaxControllers];
  148. protected:
  149. /// Build out the codes used for controller actions with the
  150. /// Input Event Manager
  151. void buildCodeTable();
  152. /// Checks if there are active controllers
  153. bool checkControllers();
  154. public:
  155. // For ManagedSingleton.
  156. static const char* getSingletonName() { return "RazerHydraDevice"; }
  157. };
  158. /// Returns the RazerHydraDevice singleton.
  159. #define RAZERHYDRADEV ManagedSingleton<RazerHydraDevice>::instance()
  160. #endif // _RAZERHYDRADEVICE_H_