|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozilla_dom_GamepadService_h_ |
|
6 #define mozilla_dom_GamepadService_h_ |
|
7 |
|
8 #include <stdint.h> |
|
9 #include "Gamepad.h" |
|
10 #include "nsAutoPtr.h" |
|
11 #include "nsCOMArray.h" |
|
12 #include "nsIGamepadServiceTest.h" |
|
13 #include "nsGlobalWindow.h" |
|
14 #include "nsIFocusManager.h" |
|
15 #include "nsIObserver.h" |
|
16 #include "nsITimer.h" |
|
17 #include "nsTArray.h" |
|
18 |
|
19 class nsIDOMDocument; |
|
20 |
|
21 namespace mozilla { |
|
22 namespace dom { |
|
23 |
|
24 class EventTarget; |
|
25 |
|
26 class GamepadService : public nsIObserver |
|
27 { |
|
28 public: |
|
29 NS_DECL_ISUPPORTS |
|
30 NS_DECL_NSIOBSERVER |
|
31 |
|
32 // Get the singleton service |
|
33 static already_AddRefed<GamepadService> GetService(); |
|
34 // Return true if the API is preffed on. |
|
35 static bool IsAPIEnabled(); |
|
36 |
|
37 void BeginShutdown(); |
|
38 |
|
39 // Indicate that |aWindow| wants to receive gamepad events. |
|
40 void AddListener(nsGlobalWindow* aWindow); |
|
41 // Indicate that |aWindow| should no longer receive gamepad events. |
|
42 void RemoveListener(nsGlobalWindow* aWindow); |
|
43 |
|
44 // Add a gamepad to the list of known gamepads, and return its index. |
|
45 uint32_t AddGamepad(const char* aID, GamepadMappingType aMapping, |
|
46 uint32_t aNumButtons, uint32_t aNumAxes); |
|
47 // Remove the gamepad at |aIndex| from the list of known gamepads. |
|
48 void RemoveGamepad(uint32_t aIndex); |
|
49 |
|
50 // Update the state of |aButton| for the gamepad at |aIndex| for all |
|
51 // windows that are listening and visible, and fire one of |
|
52 // a gamepadbutton{up,down} event at them as well. |
|
53 // aPressed is used for digital buttons, aValue is for analog buttons. |
|
54 void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed, |
|
55 double aValue); |
|
56 // When only a digital button is available the value will be synthesized. |
|
57 void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed); |
|
58 |
|
59 // Update the state of |aAxis| for the gamepad at |aIndex| for all |
|
60 // windows that are listening and visible, and fire a gamepadaxismove |
|
61 // event at them as well. |
|
62 void NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue); |
|
63 |
|
64 // Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex| |
|
65 void SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad); |
|
66 |
|
67 protected: |
|
68 GamepadService(); |
|
69 virtual ~GamepadService() {}; |
|
70 void StartCleanupTimer(); |
|
71 |
|
72 // Fire a gamepadconnected or gamepaddisconnected event for the gamepad |
|
73 // at |aIndex| to all windows that are listening and have received |
|
74 // gamepad input. |
|
75 void NewConnectionEvent(uint32_t aIndex, bool aConnected); |
|
76 |
|
77 // Fire a gamepadaxismove event to the window at |aTarget| for |aGamepad|. |
|
78 void FireAxisMoveEvent(EventTarget* aTarget, |
|
79 Gamepad* aGamepad, |
|
80 uint32_t axis, |
|
81 double value); |
|
82 |
|
83 // Fire one of gamepadbutton{up,down} event at the window at |aTarget| for |
|
84 // |aGamepad|. |
|
85 void FireButtonEvent(EventTarget* aTarget, |
|
86 Gamepad* aGamepad, |
|
87 uint32_t aButton, |
|
88 double aValue); |
|
89 |
|
90 // Fire one of gamepad{connected,disconnected} event at the window at |
|
91 // |aTarget| for |aGamepad|. |
|
92 void FireConnectionEvent(EventTarget* aTarget, |
|
93 Gamepad* aGamepad, |
|
94 bool aConnected); |
|
95 |
|
96 // true if this feature is enabled in preferences |
|
97 bool mEnabled; |
|
98 // true if non-standard events are enabled in preferences |
|
99 bool mNonstandardEventsEnabled; |
|
100 // true if the platform-specific backend has started work |
|
101 bool mStarted; |
|
102 // true when shutdown has begun |
|
103 bool mShuttingDown; |
|
104 |
|
105 private: |
|
106 // Returns true if we have already sent data from this gamepad |
|
107 // to this window. This should only return true if the user |
|
108 // explicitly interacted with a gamepad while this window |
|
109 // was focused, by pressing buttons or similar actions. |
|
110 bool WindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex); |
|
111 // Indicate that a window has recieved data from a gamepad. |
|
112 void SetWindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex, |
|
113 bool aHasSeen = true); |
|
114 |
|
115 static void TimeoutHandler(nsITimer* aTimer, void* aClosure); |
|
116 static bool sShutdown; |
|
117 |
|
118 // Gamepads connected to the system. Copies of these are handed out |
|
119 // to each window. |
|
120 nsTArray<nsRefPtr<Gamepad> > mGamepads; |
|
121 // nsGlobalWindows that are listening for gamepad events. |
|
122 // has been sent to that window. |
|
123 nsTArray<nsRefPtr<nsGlobalWindow> > mListeners; |
|
124 nsCOMPtr<nsITimer> mTimer; |
|
125 nsCOMPtr<nsIFocusManager> mFocusManager; |
|
126 nsCOMPtr<nsIObserver> mObserver; |
|
127 }; |
|
128 |
|
129 // Service for testing purposes |
|
130 class GamepadServiceTest : public nsIGamepadServiceTest |
|
131 { |
|
132 public: |
|
133 NS_DECL_ISUPPORTS |
|
134 NS_DECL_NSIGAMEPADSERVICETEST |
|
135 |
|
136 GamepadServiceTest(); |
|
137 |
|
138 static already_AddRefed<GamepadServiceTest> CreateService(); |
|
139 |
|
140 private: |
|
141 static GamepadServiceTest* sSingleton; |
|
142 virtual ~GamepadServiceTest() {}; |
|
143 }; |
|
144 |
|
145 } // namespace dom |
|
146 } // namespace mozilla |
|
147 |
|
148 #define NS_GAMEPAD_TEST_CID \ |
|
149 { 0xfb1fcb57, 0xebab, 0x4cf4, \ |
|
150 { 0x96, 0x3b, 0x1e, 0x4d, 0xb8, 0x52, 0x16, 0x96 } } |
|
151 #define NS_GAMEPAD_TEST_CONTRACTID "@mozilla.org/gamepad-test;1" |
|
152 |
|
153 #endif // mozilla_dom_GamepadService_h_ |