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_
10 #include "nsAutoPtr.h"
11 #include "nsCOMArray.h"
12 #include "nsIGamepadServiceTest.h"
13 #include "nsGlobalWindow.h"
14 #include "nsIFocusManager.h"
15 #include "nsIObserver.h"
26 class GamepadService
: public 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();
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
,
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
);
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
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
,
83 // Fire one of gamepadbutton{up,down} event at the window at |aTarget| for
85 void FireButtonEvent(EventTarget
* aTarget
,
90 // Fire one of gamepad{connected,disconnected} event at the window at
91 // |aTarget| for |aGamepad|.
92 void FireConnectionEvent(EventTarget
* aTarget
,
96 // true if this feature is enabled in preferences
98 // true if non-standard events are enabled in preferences
99 bool mNonstandardEventsEnabled
;
100 // true if the platform-specific backend has started work
102 // true when shutdown has begun
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
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
134 NS_DECL_NSIGAMEPADSERVICETEST
136 GamepadServiceTest();
138 static already_AddRefed
<GamepadServiceTest
> CreateService();
141 static GamepadServiceTest
* sSingleton
;
142 virtual ~GamepadServiceTest() {};
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_