1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/gamepad/GamepadService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef mozilla_dom_GamepadService_h_ 1.9 +#define mozilla_dom_GamepadService_h_ 1.10 + 1.11 +#include <stdint.h> 1.12 +#include "Gamepad.h" 1.13 +#include "nsAutoPtr.h" 1.14 +#include "nsCOMArray.h" 1.15 +#include "nsIGamepadServiceTest.h" 1.16 +#include "nsGlobalWindow.h" 1.17 +#include "nsIFocusManager.h" 1.18 +#include "nsIObserver.h" 1.19 +#include "nsITimer.h" 1.20 +#include "nsTArray.h" 1.21 + 1.22 +class nsIDOMDocument; 1.23 + 1.24 +namespace mozilla { 1.25 +namespace dom { 1.26 + 1.27 +class EventTarget; 1.28 + 1.29 +class GamepadService : public nsIObserver 1.30 +{ 1.31 + public: 1.32 + NS_DECL_ISUPPORTS 1.33 + NS_DECL_NSIOBSERVER 1.34 + 1.35 + // Get the singleton service 1.36 + static already_AddRefed<GamepadService> GetService(); 1.37 + // Return true if the API is preffed on. 1.38 + static bool IsAPIEnabled(); 1.39 + 1.40 + void BeginShutdown(); 1.41 + 1.42 + // Indicate that |aWindow| wants to receive gamepad events. 1.43 + void AddListener(nsGlobalWindow* aWindow); 1.44 + // Indicate that |aWindow| should no longer receive gamepad events. 1.45 + void RemoveListener(nsGlobalWindow* aWindow); 1.46 + 1.47 + // Add a gamepad to the list of known gamepads, and return its index. 1.48 + uint32_t AddGamepad(const char* aID, GamepadMappingType aMapping, 1.49 + uint32_t aNumButtons, uint32_t aNumAxes); 1.50 + // Remove the gamepad at |aIndex| from the list of known gamepads. 1.51 + void RemoveGamepad(uint32_t aIndex); 1.52 + 1.53 + // Update the state of |aButton| for the gamepad at |aIndex| for all 1.54 + // windows that are listening and visible, and fire one of 1.55 + // a gamepadbutton{up,down} event at them as well. 1.56 + // aPressed is used for digital buttons, aValue is for analog buttons. 1.57 + void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed, 1.58 + double aValue); 1.59 + // When only a digital button is available the value will be synthesized. 1.60 + void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed); 1.61 + 1.62 + // Update the state of |aAxis| for the gamepad at |aIndex| for all 1.63 + // windows that are listening and visible, and fire a gamepadaxismove 1.64 + // event at them as well. 1.65 + void NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue); 1.66 + 1.67 + // Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex| 1.68 + void SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad); 1.69 + 1.70 + protected: 1.71 + GamepadService(); 1.72 + virtual ~GamepadService() {}; 1.73 + void StartCleanupTimer(); 1.74 + 1.75 + // Fire a gamepadconnected or gamepaddisconnected event for the gamepad 1.76 + // at |aIndex| to all windows that are listening and have received 1.77 + // gamepad input. 1.78 + void NewConnectionEvent(uint32_t aIndex, bool aConnected); 1.79 + 1.80 + // Fire a gamepadaxismove event to the window at |aTarget| for |aGamepad|. 1.81 + void FireAxisMoveEvent(EventTarget* aTarget, 1.82 + Gamepad* aGamepad, 1.83 + uint32_t axis, 1.84 + double value); 1.85 + 1.86 + // Fire one of gamepadbutton{up,down} event at the window at |aTarget| for 1.87 + // |aGamepad|. 1.88 + void FireButtonEvent(EventTarget* aTarget, 1.89 + Gamepad* aGamepad, 1.90 + uint32_t aButton, 1.91 + double aValue); 1.92 + 1.93 + // Fire one of gamepad{connected,disconnected} event at the window at 1.94 + // |aTarget| for |aGamepad|. 1.95 + void FireConnectionEvent(EventTarget* aTarget, 1.96 + Gamepad* aGamepad, 1.97 + bool aConnected); 1.98 + 1.99 + // true if this feature is enabled in preferences 1.100 + bool mEnabled; 1.101 + // true if non-standard events are enabled in preferences 1.102 + bool mNonstandardEventsEnabled; 1.103 + // true if the platform-specific backend has started work 1.104 + bool mStarted; 1.105 + // true when shutdown has begun 1.106 + bool mShuttingDown; 1.107 + 1.108 + private: 1.109 + // Returns true if we have already sent data from this gamepad 1.110 + // to this window. This should only return true if the user 1.111 + // explicitly interacted with a gamepad while this window 1.112 + // was focused, by pressing buttons or similar actions. 1.113 + bool WindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex); 1.114 + // Indicate that a window has recieved data from a gamepad. 1.115 + void SetWindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex, 1.116 + bool aHasSeen = true); 1.117 + 1.118 + static void TimeoutHandler(nsITimer* aTimer, void* aClosure); 1.119 + static bool sShutdown; 1.120 + 1.121 + // Gamepads connected to the system. Copies of these are handed out 1.122 + // to each window. 1.123 + nsTArray<nsRefPtr<Gamepad> > mGamepads; 1.124 + // nsGlobalWindows that are listening for gamepad events. 1.125 + // has been sent to that window. 1.126 + nsTArray<nsRefPtr<nsGlobalWindow> > mListeners; 1.127 + nsCOMPtr<nsITimer> mTimer; 1.128 + nsCOMPtr<nsIFocusManager> mFocusManager; 1.129 + nsCOMPtr<nsIObserver> mObserver; 1.130 +}; 1.131 + 1.132 +// Service for testing purposes 1.133 +class GamepadServiceTest : public nsIGamepadServiceTest 1.134 +{ 1.135 +public: 1.136 + NS_DECL_ISUPPORTS 1.137 + NS_DECL_NSIGAMEPADSERVICETEST 1.138 + 1.139 + GamepadServiceTest(); 1.140 + 1.141 + static already_AddRefed<GamepadServiceTest> CreateService(); 1.142 + 1.143 +private: 1.144 + static GamepadServiceTest* sSingleton; 1.145 + virtual ~GamepadServiceTest() {}; 1.146 +}; 1.147 + 1.148 +} // namespace dom 1.149 +} // namespace mozilla 1.150 + 1.151 +#define NS_GAMEPAD_TEST_CID \ 1.152 +{ 0xfb1fcb57, 0xebab, 0x4cf4, \ 1.153 +{ 0x96, 0x3b, 0x1e, 0x4d, 0xb8, 0x52, 0x16, 0x96 } } 1.154 +#define NS_GAMEPAD_TEST_CONTRACTID "@mozilla.org/gamepad-test;1" 1.155 + 1.156 +#endif // mozilla_dom_GamepadService_h_