Bug 1787269 [wpt PR 35624] - Further refine STP download regexs, a=testonly
[gecko.git] / dom / gamepad / GamepadManager.h
blob794c0d1ca472a00f5033e2a9a31c8a5d9d50ef03
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_GamepadManager_h_
8 #define mozilla_dom_GamepadManager_h_
10 #include "nsIObserver.h"
11 #include "nsRefPtrHashtable.h"
12 // Needed for GamepadMappingType
13 #include "mozilla/dom/GamepadBinding.h"
14 #include "mozilla/dom/GamepadHandle.h"
15 #include <utility>
17 class nsGlobalWindowInner;
18 class nsIGlobalObject;
20 namespace mozilla {
21 namespace gfx {
22 class VRManagerChild;
23 } // namespace gfx
24 namespace dom {
26 class EventTarget;
27 class Gamepad;
28 class GamepadChangeEvent;
29 class GamepadEventChannelChild;
31 class GamepadManager final : public nsIObserver {
32 public:
33 NS_DECL_ISUPPORTS
34 NS_DECL_NSIOBSERVER
36 // Returns true if we actually have a service up and running
37 static bool IsServiceRunning();
38 // Get the singleton service
39 static already_AddRefed<GamepadManager> GetService();
41 void BeginShutdown();
42 void StopMonitoring();
44 // Indicate that |aWindow| wants to receive gamepad events.
45 void AddListener(nsGlobalWindowInner* aWindow);
46 // Indicate that |aWindow| should no longer receive gamepad events.
47 void RemoveListener(nsGlobalWindowInner* aWindow);
49 // Add a gamepad to the list of known gamepads.
50 void AddGamepad(GamepadHandle aHandle, const nsAString& aID,
51 GamepadMappingType aMapping, GamepadHand aHand,
52 uint32_t aDisplayID, uint32_t aNumButtons, uint32_t aNumAxes,
53 uint32_t aNumHaptics, uint32_t aNumLightIndicator,
54 uint32_t aNumTouchEvents);
56 // Remove the gamepad at |aIndex| from the list of known gamepads.
57 void RemoveGamepad(GamepadHandle aHandle);
59 // Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex|
60 void SyncGamepadState(GamepadHandle aHandle, nsGlobalWindowInner* aWindow,
61 Gamepad* aGamepad);
63 // Returns gamepad object if index exists, null otherwise
64 already_AddRefed<Gamepad> GetGamepad(GamepadHandle aHandle) const;
66 // Receive GamepadChangeEvent messages from parent process to fire DOM events
67 void Update(const GamepadChangeEvent& aGamepadEvent);
69 // Trigger vibrate haptic event to gamepad channels.
70 already_AddRefed<Promise> VibrateHaptic(GamepadHandle aHandle,
71 uint32_t aHapticIndex,
72 double aIntensity, double aDuration,
73 nsIGlobalObject* aGlobal,
74 ErrorResult& aRv);
75 // Send stop haptic events to gamepad channels.
76 void StopHaptics();
78 // Set light indicator color event to gamepad channels.
79 already_AddRefed<Promise> SetLightIndicatorColor(GamepadHandle aHandle,
80 uint32_t aLightColorIndex,
81 uint8_t aRed, uint8_t aGreen,
82 uint8_t aBlue,
83 nsIGlobalObject* aGlobal,
84 ErrorResult& aRv);
86 protected:
87 GamepadManager();
88 ~GamepadManager() = default;
90 // Fire a gamepadconnected or gamepaddisconnected event for the gamepad
91 // at |aIndex| to all windows that are listening and have received
92 // gamepad input.
93 void NewConnectionEvent(GamepadHandle aHandle, bool aConnected);
95 // Fire a gamepadaxismove event to the window at |aTarget| for |aGamepad|.
96 void FireAxisMoveEvent(EventTarget* aTarget, Gamepad* aGamepad, uint32_t axis,
97 double value);
99 // Fire one of gamepadbutton{up,down} event at the window at |aTarget| for
100 // |aGamepad|.
101 void FireButtonEvent(EventTarget* aTarget, Gamepad* aGamepad,
102 uint32_t aButton, double aValue);
104 // Fire one of gamepad{connected,disconnected} event at the window at
105 // |aTarget| for |aGamepad|.
106 void FireConnectionEvent(EventTarget* aTarget, Gamepad* aGamepad,
107 bool aConnected);
109 // true if this feature is enabled in preferences
110 bool mEnabled;
111 // true if non-standard events are enabled in preferences
112 bool mNonstandardEventsEnabled;
113 // true when shutdown has begun
114 bool mShuttingDown;
116 RefPtr<GamepadEventChannelChild> mChannelChild;
118 private:
119 nsresult Init();
121 void MaybeConvertToNonstandardGamepadEvent(const GamepadChangeEvent& aEvent,
122 nsGlobalWindowInner* aWindow);
124 bool SetGamepadByEvent(const GamepadChangeEvent& aEvent,
125 nsGlobalWindowInner* aWindow = nullptr);
126 // To avoid unintentionally causing the gamepad be activated.
127 // Returns false if this gamepad hasn't been seen by this window
128 // and the axis move data is less than AXIS_FIRST_INTENT_THRESHOLD_VALUE.
129 bool AxisMoveIsFirstIntent(nsGlobalWindowInner* aWindow,
130 GamepadHandle aHandle,
131 const GamepadChangeEvent& aEvent);
132 bool MaybeWindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
133 GamepadHandle aHandle);
134 // Returns true if we have already sent data from this gamepad
135 // to this window. This should only return true if the user
136 // explicitly interacted with a gamepad while this window
137 // was focused, by pressing buttons or similar actions.
138 bool WindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
139 GamepadHandle aHandle) const;
140 // Indicate that a window has received data from a gamepad.
141 void SetWindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
142 GamepadHandle aHandle, bool aHasSeen = true);
144 // Gamepads connected to the system. Copies of these are handed out
145 // to each window.
146 nsRefPtrHashtable<nsGenericHashKey<GamepadHandle>, Gamepad> mGamepads;
147 // Inner windows that are listening for gamepad events.
148 // has been sent to that window.
149 nsTArray<RefPtr<nsGlobalWindowInner>> mListeners;
150 uint32_t mPromiseID;
153 } // namespace dom
154 } // namespace mozilla
156 #endif // mozilla_dom_GamepadManager_h_