1 /*
2  * Copyright (C) 2011, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_
27 #define THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_
28 
29 #include "device/gamepad/public/cpp/gamepad.h"
30 #include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
31 #include "third_party/blink/renderer/modules/gamepad/gamepad_button.h"
32 #include "third_party/blink/renderer/modules/gamepad/gamepad_haptic_actuator.h"
33 #include "third_party/blink/renderer/modules/modules_export.h"
34 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
35 #include "third_party/blink/renderer/platform/heap/handle.h"
36 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
37 #include "third_party/blink/renderer/platform/wtf/vector.h"
38 
39 namespace blink {
40 
41 class MODULES_EXPORT Gamepad final : public ScriptWrappable {
42   DEFINE_WRAPPERTYPEINFO();
43 
44  public:
45   // Objects implementing this interface are garbage-collected.
46   class Client : public GarbageCollectedMixin {
47    public:
48     virtual GamepadHapticActuator* GetVibrationActuatorForGamepad(
49         const Gamepad&) = 0;
50     virtual ~Client() = default;
51   };
52 
53   Gamepad(Client* client,
54           int index,
55           base::TimeTicks time_origin,
56           base::TimeTicks time_floor);
57   ~Gamepad() override;
58 
59   void UpdateFromDeviceState(const device::Gamepad&);
60 
61   typedef Vector<double> DoubleVector;
62 
id()63   const String& id() const { return id_; }
SetId(const String & id)64   void SetId(const String& id) { id_ = id; }
65 
index()66   int index() const { return index_; }
67 
connected()68   bool connected() const { return connected_; }
SetConnected(bool val)69   void SetConnected(bool val) { connected_ = val; }
70 
timestamp()71   DOMHighResTimeStamp timestamp() const { return timestamp_; }
72 
mapping()73   const String& mapping() const { return mapping_; }
74   void SetMapping(device::GamepadMapping mapping);
75 
76   const DoubleVector& axes();
77   void SetAxes(unsigned count, const double* data);
isAxisDataDirty()78   bool isAxisDataDirty() const { return is_axis_data_dirty_; }
79 
80   const GamepadButtonVector& buttons();
81   void SetButtons(unsigned count, const device::GamepadButton* data);
isButtonDataDirty()82   bool isButtonDataDirty() const { return is_button_data_dirty_; }
83 
84   GamepadHapticActuator* vibrationActuator() const;
85   void SetVibrationActuatorInfo(const device::GamepadHapticActuator&);
HasVibrationActuator()86   bool HasVibrationActuator() const { return has_vibration_actuator_; }
GetVibrationActuatorType()87   device::GamepadHapticActuatorType GetVibrationActuatorType() const {
88     return vibration_actuator_type_;
89   }
90 
91   void Trace(Visitor*) const override;
92 
93  private:
94   void SetTimestamp(const device::Gamepad& device_gamepad);
95 
96   Member<Client> client_;
97 
98   // A string identifying the gamepad model.
99   String id_;
100 
101   // The index of this gamepad within the GamepadList.
102   const int index_;
103 
104   // True if this gamepad was still connected when gamepad state was captured.
105   bool connected_;
106 
107   // The current time when the gamepad state was captured.
108   DOMHighResTimeStamp timestamp_;
109 
110   // A string indicating whether the standard mapping is in use.
111   String mapping_;
112 
113   // Snapshot of the axis state.
114   DoubleVector axes_;
115 
116   // Snapshot of the button state.
117   GamepadButtonVector buttons_;
118 
119   // True if the gamepad can produce haptic vibration effects.
120   bool has_vibration_actuator_;
121 
122   // The type of haptic actuator used for vibration effects.
123   device::GamepadHapticActuatorType vibration_actuator_type_;
124 
125   // True if the data in |axes_| has changed since the last time it was
126   // accessed.
127   bool is_axis_data_dirty_;
128 
129   // True if the data in |buttons_| has changed since the last time it was
130   // accessed.
131   bool is_button_data_dirty_;
132 
133   // Base time on which all relative timestamps are based.
134   const base::TimeTicks time_origin_;
135 
136   // Minimum value to use for timestamps from the device.
137   const base::TimeTicks time_floor_;
138 };
139 
140 }  // namespace blink
141 
142 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_GAMEPAD_GAMEPAD_H_
143