Line data Source code
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 "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/Performance.h"
16 : #include <stdint.h>
17 : #include "nsCOMPtr.h"
18 : #include "nsString.h"
19 : #include "nsTArray.h"
20 : #include "nsWrapperCache.h"
21 :
22 : namespace mozilla {
23 : namespace dom {
24 :
25 : // Per spec:
26 : // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
27 : const int kStandardGamepadButtons = 17;
28 : const int kStandardGamepadAxes = 4;
29 :
30 : const int kButtonLeftTrigger = 6;
31 : const int kButtonRightTrigger = 7;
32 :
33 : const int kLeftStickXAxis = 0;
34 : const int kLeftStickYAxis = 1;
35 : const int kRightStickXAxis = 2;
36 : const int kRightStickYAxis = 3;
37 :
38 :
39 : class Gamepad final : public nsISupports,
40 : public nsWrapperCache
41 : {
42 : public:
43 : Gamepad(nsISupports* aParent,
44 : const nsAString& aID, uint32_t aIndex,
45 : uint32_t aHashKey,
46 : GamepadMappingType aMapping, GamepadHand aHand,
47 : uint32_t aNumButtons, uint32_t aNumAxes,
48 : uint32_t aNumHaptics);
49 :
50 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
52 :
53 : void SetConnected(bool aConnected);
54 : void SetButton(uint32_t aButton, bool aPressed,
55 : bool aTouched, double aValue);
56 : void SetAxis(uint32_t aAxis, double aValue);
57 : void SetIndex(uint32_t aIndex);
58 : void SetPose(const GamepadPoseState& aPose);
59 : void SetHand(GamepadHand aHand);
60 :
61 : // Make the state of this gamepad equivalent to other.
62 : void SyncState(Gamepad* aOther);
63 :
64 : // Return a new Gamepad containing the same data as this object,
65 : // parented to aParent.
66 : already_AddRefed<Gamepad> Clone(nsISupports* aParent);
67 :
68 0 : nsISupports* GetParentObject() const
69 : {
70 0 : return mParent;
71 : }
72 :
73 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
74 :
75 0 : void GetId(nsAString& aID) const
76 : {
77 0 : aID = mID;
78 0 : }
79 :
80 0 : DOMHighResTimeStamp Timestamp() const
81 : {
82 0 : return mTimestamp;
83 : }
84 :
85 0 : GamepadMappingType Mapping()
86 : {
87 0 : return mMapping;
88 : }
89 :
90 0 : GamepadHand Hand()
91 : {
92 0 : return mHand;
93 : }
94 :
95 0 : bool Connected() const
96 : {
97 0 : return mConnected;
98 : }
99 :
100 0 : uint32_t Index() const
101 : {
102 0 : return mIndex;
103 : }
104 :
105 0 : uint32_t HashKey() const
106 : {
107 0 : return mHashKey;
108 : }
109 :
110 0 : void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const
111 : {
112 0 : aButtons = mButtons;
113 0 : }
114 :
115 0 : void GetAxes(nsTArray<double>& aAxes) const
116 : {
117 0 : aAxes = mAxes;
118 0 : }
119 :
120 0 : GamepadPose* GetPose() const
121 : {
122 0 : return mPose;
123 : }
124 :
125 0 : void GetHapticActuators(nsTArray<RefPtr<GamepadHapticActuator>>& aHapticActuators) const
126 : {
127 0 : aHapticActuators = mHapticActuators;
128 0 : }
129 :
130 : private:
131 0 : virtual ~Gamepad() {}
132 : void UpdateTimestamp();
133 :
134 : protected:
135 : nsCOMPtr<nsISupports> mParent;
136 : nsString mID;
137 : uint32_t mIndex;
138 : // the gamepad hash key in GamepadManager
139 : uint32_t mHashKey;
140 :
141 : // The mapping in use.
142 : GamepadMappingType mMapping;
143 : GamepadHand mHand;
144 :
145 : // true if this gamepad is currently connected.
146 : bool mConnected;
147 :
148 : // Current state of buttons, axes.
149 : nsTArray<RefPtr<GamepadButton>> mButtons;
150 : nsTArray<double> mAxes;
151 : DOMHighResTimeStamp mTimestamp;
152 : RefPtr<GamepadPose> mPose;
153 : nsTArray<RefPtr<GamepadHapticActuator>> mHapticActuators;
154 : };
155 :
156 : } // namespace dom
157 : } // namespace mozilla
158 :
159 : #endif // mozilla_dom_gamepad_Gamepad_h
|