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/ErrorResult.h"
11 #include "mozilla/dom/GamepadBinding.h"
12 #include "mozilla/dom/GamepadButton.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"
22 #include "nsWrapperCache.h"
28 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
29 const int kStandardGamepadButtons
= 17;
30 const int kStandardGamepadAxes
= 4;
32 const int kButtonLeftTrigger
= 6;
33 const int kButtonRightTrigger
= 7;
35 const int kLeftStickXAxis
= 0;
36 const int kLeftStickYAxis
= 1;
37 const int kRightStickXAxis
= 2;
38 const int kRightStickYAxis
= 3;
40 class Gamepad final
: public nsISupports
, public nsWrapperCache
{
42 Gamepad(nsISupports
* aParent
, const nsAString
& aID
, int32_t aIndex
,
43 uint32_t aHashKey
, GamepadMappingType aMapping
, GamepadHand aHand
,
44 uint32_t aDisplayID
, uint32_t aNumButtons
, uint32_t aNumAxes
,
45 uint32_t aNumHaptics
, uint32_t aNumLightIndicator
,
46 uint32_t aNumTouchEvents
);
48 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
49 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad
)
51 void SetConnected(bool aConnected
);
52 void SetButton(uint32_t aButton
, bool aPressed
, bool aTouched
, double aValue
);
53 void SetAxis(uint32_t aAxis
, double aValue
);
54 void SetIndex(int32_t aIndex
);
55 void SetPose(const GamepadPoseState
& aPose
);
56 void SetLightIndicatorType(uint32_t aLightIndex
,
57 GamepadLightIndicatorType aType
);
58 void SetTouchEvent(uint32_t aTouchIndex
, const GamepadTouchState
& aTouch
);
59 void SetHand(GamepadHand aHand
);
61 // Make the state of this gamepad equivalent to other.
62 void SyncState(Gamepad
* aOther
);
64 // Return a new Gamepad containing the same data as this object,
65 // parented to aParent.
66 already_AddRefed
<Gamepad
> Clone(nsISupports
* aParent
);
68 nsISupports
* GetParentObject() const { return mParent
; }
70 virtual JSObject
* WrapObject(JSContext
* aCx
,
71 JS::Handle
<JSObject
*> aGivenProto
) override
;
73 void GetId(nsAString
& aID
) const { aID
= mID
; }
75 DOMHighResTimeStamp
Timestamp() const { return mTimestamp
; }
77 GamepadMappingType
Mapping() { return mMapping
; }
79 uint32_t DisplayId() const { return mDisplayId
; }
81 GamepadHand
Hand() { return mHand
; }
83 bool Connected() const { return mConnected
; }
85 int32_t Index() const { return mIndex
; }
87 uint32_t HashKey() const { return mHashKey
; }
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();
112 virtual ~Gamepad() = default;
113 void UpdateTimestamp();
116 nsCOMPtr
<nsISupports
> mParent
;
119 // the gamepad hash key in GamepadManager
122 uint32_t mTouchIdHashValue
;
123 // The mapping in use.
124 GamepadMappingType mMapping
;
127 // true if this gamepad is currently connected.
130 // Current state of buttons, axes.
131 nsTArray
<RefPtr
<GamepadButton
>> mButtons
;
132 nsTArray
<double> mAxes
;
133 DOMHighResTimeStamp mTimestamp
;
134 RefPtr
<GamepadPose
> mPose
;
135 nsTArray
<RefPtr
<GamepadHapticActuator
>> mHapticActuators
;
136 nsTArray
<RefPtr
<GamepadLightIndicator
>> mLightIndicators
;
137 nsTArray
<RefPtr
<GamepadTouch
>> mTouchEvents
;
138 nsDataHashtable
<nsUint32HashKey
, uint32_t> mTouchIdHash
;
142 } // namespace mozilla
144 #endif // mozilla_dom_gamepad_Gamepad_h