2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2011-08-03 22:12:08 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_GamepadService_h_
|
|
|
|
#define mozilla_dom_GamepadService_h_
|
|
|
|
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2011-08-03 22:12:08 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsIGamepadServiceTest.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIFocusManager.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsTArray.h"
|
2015-04-24 01:05:29 +03:00
|
|
|
// Needed for GamepadMappingType
|
|
|
|
#include "mozilla/dom/GamepadBinding.h"
|
2011-08-03 22:12:08 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class EventTarget;
|
2015-04-24 01:05:29 +03:00
|
|
|
class GamepadChangeEvent;
|
|
|
|
class Gamepad;
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
class GamepadService : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2015-04-24 01:05:29 +03:00
|
|
|
// Returns true if we actually have a service up and running
|
|
|
|
static bool IsServiceRunning();
|
2011-08-03 22:12:08 +04:00
|
|
|
// Get the singleton service
|
|
|
|
static already_AddRefed<GamepadService> GetService();
|
2012-02-16 04:47:13 +04:00
|
|
|
// Return true if the API is preffed on.
|
|
|
|
static bool IsAPIEnabled();
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
void BeginShutdown();
|
|
|
|
|
|
|
|
// Indicate that |aWindow| wants to receive gamepad events.
|
|
|
|
void AddListener(nsGlobalWindow* aWindow);
|
|
|
|
// Indicate that |aWindow| should no longer receive gamepad events.
|
|
|
|
void RemoveListener(nsGlobalWindow* aWindow);
|
|
|
|
|
2015-04-24 01:05:29 +03:00
|
|
|
// Add a gamepad to the list of known gamepads.
|
|
|
|
void AddGamepad(uint32_t aIndex, const nsAString& aID, GamepadMappingType aMapping,
|
2013-04-09 16:43:25 +04:00
|
|
|
uint32_t aNumButtons, uint32_t aNumAxes);
|
2015-04-24 01:05:29 +03:00
|
|
|
|
2011-08-03 22:12:08 +04:00
|
|
|
// Remove the gamepad at |aIndex| from the list of known gamepads.
|
|
|
|
void RemoveGamepad(uint32_t aIndex);
|
|
|
|
|
2013-08-01 21:52:27 +04:00
|
|
|
// Update the state of |aButton| for the gamepad at |aIndex| for all
|
|
|
|
// windows that are listening and visible, and fire one of
|
|
|
|
// a gamepadbutton{up,down} event at them as well.
|
2013-04-09 16:43:25 +04:00
|
|
|
// aPressed is used for digital buttons, aValue is for analog buttons.
|
|
|
|
void NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
|
|
|
|
double aValue);
|
2013-08-01 21:52:27 +04:00
|
|
|
|
|
|
|
// Update the state of |aAxis| for the gamepad at |aIndex| for all
|
|
|
|
// windows that are listening and visible, and fire a gamepadaxismove
|
|
|
|
// event at them as well.
|
2011-08-03 22:12:08 +04:00
|
|
|
void NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue);
|
|
|
|
|
|
|
|
// Synchronize the state of |aGamepad| to match the gamepad stored at |aIndex|
|
2012-02-16 04:47:13 +04:00
|
|
|
void SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad);
|
2011-08-03 22:12:08 +04:00
|
|
|
|
2015-04-24 01:05:29 +03:00
|
|
|
// Returns gamepad object if index exists, null otherwise
|
|
|
|
already_AddRefed<Gamepad> GetGamepad(uint32_t aIndex);
|
|
|
|
|
|
|
|
// Receive GamepadChangeEvent messages from parent process to fire DOM events
|
|
|
|
void Update(const GamepadChangeEvent& aGamepadEvent);
|
2011-08-03 22:12:08 +04:00
|
|
|
protected:
|
|
|
|
GamepadService();
|
|
|
|
virtual ~GamepadService() {};
|
|
|
|
void StartCleanupTimer();
|
|
|
|
|
2013-08-01 21:52:27 +04:00
|
|
|
// Fire a gamepadconnected or gamepaddisconnected event for the gamepad
|
|
|
|
// at |aIndex| to all windows that are listening and have received
|
|
|
|
// gamepad input.
|
2011-08-03 22:12:08 +04:00
|
|
|
void NewConnectionEvent(uint32_t aIndex, bool aConnected);
|
2013-08-01 21:52:27 +04:00
|
|
|
|
|
|
|
// Fire a gamepadaxismove event to the window at |aTarget| for |aGamepad|.
|
2011-08-03 22:12:08 +04:00
|
|
|
void FireAxisMoveEvent(EventTarget* aTarget,
|
2012-02-16 04:47:13 +04:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 22:12:08 +04:00
|
|
|
uint32_t axis,
|
|
|
|
double value);
|
2013-08-01 21:52:27 +04:00
|
|
|
|
|
|
|
// Fire one of gamepadbutton{up,down} event at the window at |aTarget| for
|
|
|
|
// |aGamepad|.
|
2011-08-03 22:12:08 +04:00
|
|
|
void FireButtonEvent(EventTarget* aTarget,
|
2012-02-16 04:47:13 +04:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 22:12:08 +04:00
|
|
|
uint32_t aButton,
|
|
|
|
double aValue);
|
2013-08-01 21:52:27 +04:00
|
|
|
|
|
|
|
// Fire one of gamepad{connected,disconnected} event at the window at
|
|
|
|
// |aTarget| for |aGamepad|.
|
2011-08-03 22:12:08 +04:00
|
|
|
void FireConnectionEvent(EventTarget* aTarget,
|
2012-02-16 04:47:13 +04:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 22:12:08 +04:00
|
|
|
bool aConnected);
|
|
|
|
|
2013-04-09 16:43:24 +04:00
|
|
|
// true if this feature is enabled in preferences
|
|
|
|
bool mEnabled;
|
2013-06-19 23:05:47 +04:00
|
|
|
// true if non-standard events are enabled in preferences
|
|
|
|
bool mNonstandardEventsEnabled;
|
2011-08-03 22:12:08 +04:00
|
|
|
// true if the platform-specific backend has started work
|
|
|
|
bool mStarted;
|
|
|
|
// true when shutdown has begun
|
|
|
|
bool mShuttingDown;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns true if we have already sent data from this gamepad
|
|
|
|
// to this window. This should only return true if the user
|
|
|
|
// explicitly interacted with a gamepad while this window
|
|
|
|
// was focused, by pressing buttons or similar actions.
|
|
|
|
bool WindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex);
|
|
|
|
// Indicate that a window has recieved data from a gamepad.
|
|
|
|
void SetWindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex,
|
|
|
|
bool aHasSeen = true);
|
|
|
|
|
|
|
|
static void TimeoutHandler(nsITimer* aTimer, void* aClosure);
|
|
|
|
static bool sShutdown;
|
|
|
|
|
|
|
|
// Gamepads connected to the system. Copies of these are handed out
|
|
|
|
// to each window.
|
2015-04-24 01:05:29 +03:00
|
|
|
nsRefPtrHashtable<nsUint32HashKey, Gamepad> mGamepads;
|
2014-06-29 19:36:02 +04:00
|
|
|
// Inner windows that are listening for gamepad events.
|
2011-08-03 22:12:08 +04:00
|
|
|
// has been sent to that window.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsGlobalWindow> > mListeners;
|
2011-08-03 22:12:08 +04:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2011-08-03 22:12:08 +04:00
|
|
|
#endif // mozilla_dom_GamepadService_h_
|