Gamepad.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_gamepad_Gamepad_h
  6. #define mozilla_dom_gamepad_Gamepad_h
  7. #include "mozilla/ErrorResult.h"
  8. #include "mozilla/dom/GamepadBinding.h"
  9. #include "mozilla/dom/GamepadButton.h"
  10. #include "mozilla/dom/GamepadPose.h"
  11. #include "mozilla/dom/Performance.h"
  12. #include <stdint.h>
  13. #include "nsCOMPtr.h"
  14. #include "nsString.h"
  15. #include "nsTArray.h"
  16. #include "nsWrapperCache.h"
  17. namespace mozilla {
  18. namespace dom {
  19. // Per spec:
  20. // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
  21. const int kStandardGamepadButtons = 17;
  22. const int kStandardGamepadAxes = 4;
  23. const int kButtonLeftTrigger = 6;
  24. const int kButtonRightTrigger = 7;
  25. const int kLeftStickXAxis = 0;
  26. const int kLeftStickYAxis = 1;
  27. const int kRightStickXAxis = 2;
  28. const int kRightStickYAxis = 3;
  29. class Gamepad final : public nsISupports,
  30. public nsWrapperCache
  31. {
  32. public:
  33. Gamepad(nsISupports* aParent,
  34. const nsAString& aID, uint32_t aIndex,
  35. GamepadMappingType aMapping,
  36. uint32_t aNumButtons, uint32_t aNumAxes);
  37. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  38. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
  39. void SetConnected(bool aConnected);
  40. void SetButton(uint32_t aButton, bool aPressed, double aValue);
  41. void SetAxis(uint32_t aAxis, double aValue);
  42. void SetIndex(uint32_t aIndex);
  43. void SetPose(const GamepadPoseState& aPose);
  44. // Make the state of this gamepad equivalent to other.
  45. void SyncState(Gamepad* aOther);
  46. // Return a new Gamepad containing the same data as this object,
  47. // parented to aParent.
  48. already_AddRefed<Gamepad> Clone(nsISupports* aParent);
  49. nsISupports* GetParentObject() const
  50. {
  51. return mParent;
  52. }
  53. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  54. void GetId(nsAString& aID) const
  55. {
  56. aID = mID;
  57. }
  58. DOMHighResTimeStamp Timestamp() const
  59. {
  60. return mTimestamp;
  61. }
  62. GamepadMappingType Mapping()
  63. {
  64. return mMapping;
  65. }
  66. bool Connected() const
  67. {
  68. return mConnected;
  69. }
  70. uint32_t Index() const
  71. {
  72. return mIndex;
  73. }
  74. void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const
  75. {
  76. aButtons = mButtons;
  77. }
  78. void GetAxes(nsTArray<double>& aAxes) const
  79. {
  80. aAxes = mAxes;
  81. }
  82. GamepadPose* GetPose() const
  83. {
  84. return mPose;
  85. }
  86. private:
  87. virtual ~Gamepad() {}
  88. void UpdateTimestamp();
  89. protected:
  90. nsCOMPtr<nsISupports> mParent;
  91. nsString mID;
  92. uint32_t mIndex;
  93. // The mapping in use.
  94. GamepadMappingType mMapping;
  95. // true if this gamepad is currently connected.
  96. bool mConnected;
  97. // Current state of buttons, axes.
  98. nsTArray<RefPtr<GamepadButton>> mButtons;
  99. nsTArray<double> mAxes;
  100. DOMHighResTimeStamp mTimestamp;
  101. RefPtr<GamepadPose> mPose;
  102. };
  103. } // namespace dom
  104. } // namespace mozilla
  105. #endif // mozilla_dom_gamepad_Gamepad_h