Gamepad.h (4523B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_gamepad_Gamepad_h 8 #define mozilla_dom_gamepad_Gamepad_h 9 10 #include <stdint.h> 11 12 #include "mozilla/dom/GamepadBinding.h" 13 #include "mozilla/dom/GamepadButton.h" 14 #include "mozilla/dom/GamepadHandle.h" 15 #include "mozilla/dom/GamepadHapticActuator.h" 16 #include "mozilla/dom/GamepadLightIndicator.h" 17 #include "mozilla/dom/GamepadPose.h" 18 #include "mozilla/dom/GamepadTouch.h" 19 #include "mozilla/dom/Performance.h" 20 #include "nsCOMPtr.h" 21 #include "nsString.h" 22 #include "nsTArray.h" 23 #include "nsTHashMap.h" 24 #include "nsWrapperCache.h" 25 26 namespace mozilla::dom { 27 28 class GamepadHapticActuator; 29 30 // Per spec: 31 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping 32 const int kStandardGamepadButtons = 17; 33 const int kStandardGamepadAxes = 4; 34 35 const int kButtonLeftTrigger = 6; 36 const int kButtonRightTrigger = 7; 37 38 const int kLeftStickXAxis = 0; 39 const int kLeftStickYAxis = 1; 40 const int kRightStickXAxis = 2; 41 const int kRightStickYAxis = 3; 42 43 class Gamepad final : public nsISupports, public nsWrapperCache { 44 public: 45 Gamepad(nsISupports* aParent, const nsAString& aID, int32_t aIndex, 46 GamepadHandle aHandle, GamepadMappingType aMapping, GamepadHand aHand, 47 uint32_t aNumButtons, uint32_t aNumAxes, uint32_t aNumHaptics, 48 uint32_t aNumLightIndicator, uint32_t aNumTouchEvents); 49 50 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 51 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Gamepad) 52 53 void SetConnected(bool aConnected); 54 void SetButton(uint32_t aButton, bool aPressed, bool aTouched, double aValue); 55 void SetAxis(uint32_t aAxis, double aValue); 56 void SetIndex(int32_t aIndex); 57 void SetPose(const GamepadPoseState& aPose); 58 void SetLightIndicatorType(uint32_t aLightIndex, 59 GamepadLightIndicatorType aType); 60 void SetTouchEvent(uint32_t aTouchIndex, const GamepadTouchState& aTouch); 61 void SetHand(GamepadHand aHand); 62 63 // Make the state of this gamepad equivalent to other. 64 void SyncState(Gamepad* aOther); 65 66 // Return a new Gamepad containing the same data as this object, 67 // parented to aParent. 68 already_AddRefed<Gamepad> Clone(nsISupports* aParent); 69 70 nsISupports* GetParentObject() const { return mParent; } 71 72 virtual JSObject* WrapObject(JSContext* aCx, 73 JS::Handle<JSObject*> aGivenProto) override; 74 75 void GetId(nsAString& aID) const { aID = mID; } 76 77 DOMHighResTimeStamp Timestamp() const { return mTimestamp; } 78 79 GamepadMappingType Mapping() { return mMapping; } 80 81 GamepadHand Hand() { return mHand; } 82 83 bool Connected() const { return mConnected; } 84 85 int32_t Index() const { return mIndex; } 86 87 void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const { 88 aButtons = mButtons.Clone(); 89 } 90 91 void GetAxes(nsTArray<double>& aAxes) const { aAxes = mAxes.Clone(); } 92 93 GamepadPose* GetPose() const { return mPose; } 94 95 void GetHapticActuators( 96 nsTArray<RefPtr<GamepadHapticActuator>>& aHapticActuators) const { 97 aHapticActuators = mHapticActuators.Clone(); 98 } 99 100 void GetLightIndicators( 101 nsTArray<RefPtr<GamepadLightIndicator>>& aLightIndicators) const { 102 aLightIndicators = mLightIndicators.Clone(); 103 } 104 105 void GetTouchEvents(nsTArray<RefPtr<GamepadTouch>>& aTouchEvents) const { 106 aTouchEvents = mTouchEvents.Clone(); 107 } 108 109 GamepadHandle GetHandle() const { return mHandle; } 110 111 private: 112 virtual ~Gamepad() = default; 113 void UpdateTimestamp(); 114 115 protected: 116 nsCOMPtr<nsISupports> mParent; 117 nsString mID; 118 int32_t mIndex; 119 // the gamepad hash key in GamepadManager 120 GamepadHandle mHandle; 121 uint32_t mTouchIdHashValue; 122 // The mapping in use. 123 GamepadMappingType mMapping; 124 GamepadHand mHand; 125 126 // true if this gamepad is currently connected. 127 bool mConnected; 128 129 // Current state of buttons, axes. 130 nsTArray<RefPtr<GamepadButton>> mButtons; 131 nsTArray<double> mAxes; 132 DOMHighResTimeStamp mTimestamp; 133 RefPtr<GamepadPose> mPose; 134 nsTArray<RefPtr<GamepadHapticActuator>> mHapticActuators; 135 nsTArray<RefPtr<GamepadLightIndicator>> mLightIndicators; 136 nsTArray<RefPtr<GamepadTouch>> mTouchEvents; 137 nsTHashMap<nsUint32HashKey, uint32_t> mTouchIdHash; 138 }; 139 140 } // namespace mozilla::dom 141 142 #endif // mozilla_dom_gamepad_Gamepad_h