Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/Gamepad.h
Line
Count
Source (jump to first uncovered line)
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 aDisplayID, uint32_t aNumButtons,
48
          uint32_t aNumAxes, uint32_t aNumHaptics);
49
50
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51
  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
  nsISupports* GetParentObject() const
69
0
  {
70
0
    return mParent;
71
0
  }
72
73
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
74
75
  void GetId(nsAString& aID) const
76
0
  {
77
0
    aID = mID;
78
0
  }
79
80
  DOMHighResTimeStamp Timestamp() const
81
0
  {
82
0
     return mTimestamp;
83
0
  }
84
85
  GamepadMappingType Mapping()
86
0
  {
87
0
    return mMapping;
88
0
  }
89
90
  uint32_t DisplayId() const
91
0
  {
92
0
    return mDisplayId;
93
0
  }
94
95
  GamepadHand Hand()
96
0
  {
97
0
    return mHand;
98
0
  }
99
100
  bool Connected() const
101
0
  {
102
0
    return mConnected;
103
0
  }
104
105
  uint32_t Index() const
106
0
  {
107
0
    return mIndex;
108
0
  }
109
110
  uint32_t HashKey() const
111
0
  {
112
0
    return mHashKey;
113
0
  }
114
115
  void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const
116
0
  {
117
0
    aButtons = mButtons;
118
0
  }
119
120
  void GetAxes(nsTArray<double>& aAxes) const
121
0
  {
122
0
    aAxes = mAxes;
123
0
  }
124
125
  GamepadPose* GetPose() const
126
0
  {
127
0
    return mPose;
128
0
  }
129
130
  void GetHapticActuators(nsTArray<RefPtr<GamepadHapticActuator>>& aHapticActuators) const
131
0
  {
132
0
    aHapticActuators = mHapticActuators;
133
0
  }
134
135
private:
136
0
  virtual ~Gamepad() {}
137
  void UpdateTimestamp();
138
139
protected:
140
  nsCOMPtr<nsISupports> mParent;
141
  nsString mID;
142
  uint32_t mIndex;
143
  // the gamepad hash key in GamepadManager
144
  uint32_t mHashKey;
145
  uint32_t mDisplayId;
146
  // The mapping in use.
147
  GamepadMappingType mMapping;
148
  GamepadHand mHand;
149
150
  // true if this gamepad is currently connected.
151
  bool mConnected;
152
153
  // Current state of buttons, axes.
154
  nsTArray<RefPtr<GamepadButton>> mButtons;
155
  nsTArray<double> mAxes;
156
  DOMHighResTimeStamp mTimestamp;
157
  RefPtr<GamepadPose> mPose;
158
  nsTArray<RefPtr<GamepadHapticActuator>> mHapticActuators;
159
};
160
161
} // namespace dom
162
} // namespace mozilla
163
164
#endif // mozilla_dom_gamepad_Gamepad_h