Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / gamepad / Gamepad.h
blob689ea7f6d2a452df4b768928b911cb5a566f257a
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/. */
7 #ifndef mozilla_dom_gamepad_Gamepad_h
8 #define mozilla_dom_gamepad_Gamepad_h
10 #include "mozilla/dom/GamepadBinding.h"
11 #include "mozilla/dom/GamepadButton.h"
12 #include "mozilla/dom/GamepadHandle.h"
13 #include "mozilla/dom/GamepadPose.h"
14 #include "mozilla/dom/GamepadHapticActuator.h"
15 #include "mozilla/dom/GamepadLightIndicator.h"
16 #include "mozilla/dom/GamepadTouch.h"
17 #include "mozilla/dom/Performance.h"
18 #include <stdint.h>
19 #include "nsCOMPtr.h"
20 #include "nsTHashMap.h"
21 #include "nsString.h"
22 #include "nsTArray.h"
23 #include "nsWrapperCache.h"
25 namespace mozilla::dom {
27 class GamepadHapticActuator;
29 // Per spec:
30 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
31 const int kStandardGamepadButtons = 17;
32 const int kStandardGamepadAxes = 4;
34 const int kButtonLeftTrigger = 6;
35 const int kButtonRightTrigger = 7;
37 const int kLeftStickXAxis = 0;
38 const int kLeftStickYAxis = 1;
39 const int kRightStickXAxis = 2;
40 const int kRightStickYAxis = 3;
42 class Gamepad final : public nsISupports, public nsWrapperCache {
43 public:
44 Gamepad(nsISupports* aParent, const nsAString& aID, int32_t aIndex,
45 GamepadHandle aHandle, GamepadMappingType aMapping, GamepadHand aHand,
46 uint32_t aDisplayID, uint32_t aNumButtons, uint32_t aNumAxes,
47 uint32_t aNumHaptics, uint32_t aNumLightIndicator,
48 uint32_t aNumTouchEvents);
50 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Gamepad)
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);
63 // Make the state of this gamepad equivalent to other.
64 void SyncState(Gamepad* aOther);
66 // Return a new Gamepad containing the same data as this object,
67 // parented to aParent.
68 already_AddRefed<Gamepad> Clone(nsISupports* aParent);
70 nsISupports* GetParentObject() const { return mParent; }
72 virtual JSObject* WrapObject(JSContext* aCx,
73 JS::Handle<JSObject*> aGivenProto) override;
75 void GetId(nsAString& aID) const { aID = mID; }
77 DOMHighResTimeStamp Timestamp() const { return mTimestamp; }
79 GamepadMappingType Mapping() { return mMapping; }
81 uint32_t DisplayId() const { return mDisplayId; }
83 GamepadHand Hand() { return mHand; }
85 bool Connected() const { return mConnected; }
87 int32_t Index() const { return mIndex; }
89 void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const {
90 aButtons = mButtons.Clone();
93 void GetAxes(nsTArray<double>& aAxes) const { aAxes = mAxes.Clone(); }
95 GamepadPose* GetPose() const { return mPose; }
97 void GetHapticActuators(
98 nsTArray<RefPtr<GamepadHapticActuator>>& aHapticActuators) const {
99 aHapticActuators = mHapticActuators.Clone();
102 void GetLightIndicators(
103 nsTArray<RefPtr<GamepadLightIndicator>>& aLightIndicators) const {
104 aLightIndicators = mLightIndicators.Clone();
107 void GetTouchEvents(nsTArray<RefPtr<GamepadTouch>>& aTouchEvents) const {
108 aTouchEvents = mTouchEvents.Clone();
111 GamepadHandle GetHandle() const { return mHandle; }
113 private:
114 virtual ~Gamepad() = default;
115 void UpdateTimestamp();
117 protected:
118 nsCOMPtr<nsISupports> mParent;
119 nsString mID;
120 int32_t mIndex;
121 // the gamepad hash key in GamepadManager
122 GamepadHandle mHandle;
123 uint32_t mDisplayId;
124 uint32_t mTouchIdHashValue;
125 // The mapping in use.
126 GamepadMappingType mMapping;
127 GamepadHand mHand;
129 // true if this gamepad is currently connected.
130 bool mConnected;
132 // Current state of buttons, axes.
133 nsTArray<RefPtr<GamepadButton>> mButtons;
134 nsTArray<double> mAxes;
135 DOMHighResTimeStamp mTimestamp;
136 RefPtr<GamepadPose> mPose;
137 nsTArray<RefPtr<GamepadHapticActuator>> mHapticActuators;
138 nsTArray<RefPtr<GamepadLightIndicator>> mLightIndicators;
139 nsTArray<RefPtr<GamepadTouch>> mTouchEvents;
140 nsTHashMap<nsUint32HashKey, uint32_t> mTouchIdHash;
143 } // namespace mozilla::dom
145 #endif // mozilla_dom_gamepad_Gamepad_h