Bumping manifests a=b2g-bump
[gecko.git] / dom / gamepad / GamepadService.h
blob4225e3c597b0f4e59df7d65f0579945dd782c469
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/. */
5 #ifndef mozilla_dom_GamepadService_h_
6 #define mozilla_dom_GamepadService_h_
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"
19 class nsIDOMDocument;
21 namespace mozilla {
22 namespace dom {
24 class EventTarget;
26 class GamepadService : public nsIObserver
28 public:
29 NS_DECL_ISUPPORTS
30 NS_DECL_NSIOBSERVER
32 // Get the singleton service
33 static already_AddRefed<GamepadService> GetService();
34 // Return true if the API is preffed on.
35 static bool IsAPIEnabled();
37 void BeginShutdown();
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);
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);
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);
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);
64 // Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex|
65 void SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad);
67 protected:
68 GamepadService();
69 virtual ~GamepadService() {};
70 void StartCleanupTimer();
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);
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);
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);
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);
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;
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);
115 static void TimeoutHandler(nsITimer* aTimer, void* aClosure);
116 static bool sShutdown;
118 // Gamepads connected to the system. Copies of these are handed out
119 // to each window.
120 nsTArray<nsRefPtr<Gamepad> > mGamepads;
121 // Inner windows 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;
129 // Service for testing purposes
130 class GamepadServiceTest : public nsIGamepadServiceTest
132 public:
133 NS_DECL_ISUPPORTS
134 NS_DECL_NSIGAMEPADSERVICETEST
136 GamepadServiceTest();
138 static already_AddRefed<GamepadServiceTest> CreateService();
140 private:
141 static GamepadServiceTest* sSingleton;
142 virtual ~GamepadServiceTest() {};
145 } // namespace dom
146 } // namespace mozilla
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"
153 #endif // mozilla_dom_GamepadService_h_