2011-08-03 11:12:08 -07: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-04-09 05:43:24 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
|
|
|
|
#include "GamepadService.h"
|
2012-02-15 16:47:13 -08:00
|
|
|
#include "Gamepad.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "GeneratedEvents.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
|
2013-09-11 15:35:51 -07:00
|
|
|
#include "mozilla/dom/GamepadAxisMoveEvent.h"
|
|
|
|
#include "mozilla/dom/GamepadButtonEvent.h"
|
|
|
|
#include "mozilla/dom/GamepadEvent.h"
|
|
|
|
|
2011-08-03 11:12:08 -07:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
2013-04-09 05:43:24 -07:00
|
|
|
const char* kGamepadEnabledPref = "dom.gamepad.enabled";
|
2013-06-19 12:05:47 -07:00
|
|
|
const char* kGamepadEventsEnabledPref =
|
|
|
|
"dom.gamepad.non_standard_events.enabled";
|
2011-08-03 11:12:08 -07:00
|
|
|
// Amount of time to wait before cleaning up gamepad resources
|
|
|
|
// when no pages are listening for events.
|
|
|
|
const int kCleanupDelayMS = 2000;
|
|
|
|
const nsTArray<nsRefPtr<nsGlobalWindow> >::index_type NoIndex =
|
|
|
|
nsTArray<nsRefPtr<nsGlobalWindow> >::NoIndex;
|
|
|
|
|
|
|
|
StaticRefPtr<GamepadService> gGamepadServiceSingleton;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool GamepadService::sShutdown = false;
|
|
|
|
|
2013-04-09 05:43:24 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(GamepadService, nsIObserver)
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
GamepadService::GamepadService()
|
|
|
|
: mStarted(false),
|
|
|
|
mShuttingDown(false)
|
|
|
|
{
|
2012-02-15 16:47:13 -08:00
|
|
|
mEnabled = IsAPIEnabled();
|
2013-06-19 12:05:47 -07:00
|
|
|
mNonstandardEventsEnabled =
|
|
|
|
Preferences::GetBool(kGamepadEventsEnabledPref, false);
|
2011-08-03 11:12:08 -07:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
observerService->AddObserver(this,
|
|
|
|
NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
GamepadService::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
2013-04-09 05:43:24 -07:00
|
|
|
observerService->RemoveObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
BeginShutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::BeginShutdown()
|
|
|
|
{
|
|
|
|
mShuttingDown = true;
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2013-04-09 05:43:24 -07:00
|
|
|
if (mStarted) {
|
|
|
|
mozilla::hal::StopMonitoringGamepadStatus();
|
|
|
|
mStarted = false;
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
// Don't let windows call back to unregister during shutdown
|
|
|
|
for (uint32_t i = 0; i < mListeners.Length(); i++) {
|
|
|
|
mListeners[i]->SetHasGamepadEventListener(false);
|
|
|
|
}
|
|
|
|
mListeners.Clear();
|
|
|
|
mGamepads.Clear();
|
|
|
|
sShutdown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::AddListener(nsGlobalWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListeners.IndexOf(aWindow) != NoIndex) {
|
|
|
|
return; // already exists
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:24 -07:00
|
|
|
if (!mStarted && mEnabled) {
|
2011-08-03 11:12:08 -07:00
|
|
|
mozilla::hal::StartMonitoringGamepadStatus();
|
|
|
|
mStarted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendElement(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::RemoveListener(nsGlobalWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
// Doesn't matter at this point. It's possible we're being called
|
|
|
|
// as a result of our own destructor here, so just bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListeners.IndexOf(aWindow) == NoIndex) {
|
|
|
|
return; // doesn't exist
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.RemoveElement(aWindow);
|
|
|
|
|
2013-04-09 05:43:24 -07:00
|
|
|
if (mListeners.Length() == 0 && !mShuttingDown && mStarted) {
|
2011-08-03 11:12:08 -07:00
|
|
|
StartCleanupTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
GamepadService::AddGamepad(const char* aId,
|
2013-04-09 05:43:25 -07:00
|
|
|
GamepadMappingType aMapping,
|
2011-08-03 11:12:08 -07:00
|
|
|
uint32_t aNumButtons,
|
|
|
|
uint32_t aNumAxes)
|
|
|
|
{
|
|
|
|
//TODO: bug 852258: get initial button/axis state
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad =
|
|
|
|
new Gamepad(nullptr,
|
|
|
|
NS_ConvertUTF8toUTF16(nsDependentCString(aId)),
|
|
|
|
0,
|
2013-04-09 05:43:25 -07:00
|
|
|
aMapping,
|
2012-02-15 16:47:13 -08:00
|
|
|
aNumButtons,
|
|
|
|
aNumAxes);
|
2011-08-03 11:12:08 -07:00
|
|
|
int index = -1;
|
|
|
|
for (uint32_t i = 0; i < mGamepads.Length(); i++) {
|
|
|
|
if (!mGamepads[i]) {
|
|
|
|
mGamepads[i] = gamepad;
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index == -1) {
|
|
|
|
mGamepads.AppendElement(gamepad);
|
|
|
|
index = mGamepads.Length() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gamepad->SetIndex(index);
|
|
|
|
NewConnectionEvent(index, true);
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::RemoveGamepad(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
if (aIndex < mGamepads.Length()) {
|
|
|
|
mGamepads[aIndex]->SetConnected(false);
|
|
|
|
NewConnectionEvent(aIndex, false);
|
|
|
|
// If this is the last entry in the list, just remove it.
|
|
|
|
if (aIndex == mGamepads.Length() - 1) {
|
|
|
|
mGamepads.RemoveElementAt(aIndex);
|
|
|
|
} else {
|
|
|
|
// Otherwise just null it out and leave it, so the
|
|
|
|
// indices of the following entries remain valid.
|
|
|
|
mGamepads[aIndex] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed)
|
2013-04-09 05:43:25 -07:00
|
|
|
{
|
|
|
|
// Synthesize a value: 1.0 for pressed, 0.0 for unpressed.
|
|
|
|
NewButtonEvent(aIndex, aButton, aPressed, aPressed ? 1.0L : 0.0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::NewButtonEvent(uint32_t aIndex, uint32_t aButton, bool aPressed,
|
|
|
|
double aValue)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
if (mShuttingDown || aIndex >= mGamepads.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
mGamepads[aIndex]->SetButton(aButton, aPressed, aValue);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
// Hold on to listeners in a separate array because firing events
|
|
|
|
// can mutate the mListeners array.
|
|
|
|
nsTArray<nsRefPtr<nsGlobalWindow> > listeners(mListeners);
|
|
|
|
|
|
|
|
for (uint32_t i = listeners.Length(); i > 0 ; ) {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
// Only send events to non-background windows
|
2013-10-16 09:11:24 -07:00
|
|
|
if (!listeners[i]->IsCurrentInnerWindow() ||
|
2011-08-03 11:12:08 -07:00
|
|
|
listeners[i]->GetOuterWindow()->IsBackground()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-01 10:52:27 -07:00
|
|
|
bool first_time = false;
|
2011-08-03 11:12:08 -07:00
|
|
|
if (!WindowHasSeenGamepad(listeners[i], aIndex)) {
|
|
|
|
// This window hasn't seen this gamepad before, so
|
|
|
|
// send a connection event first.
|
2013-08-01 10:52:27 -07:00
|
|
|
SetWindowHasSeenGamepad(listeners[i], aIndex);
|
|
|
|
first_time = true;
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = listeners[i]->GetGamepad(aIndex);
|
2011-08-03 11:12:08 -07:00
|
|
|
if (gamepad) {
|
2013-04-09 05:43:25 -07:00
|
|
|
gamepad->SetButton(aButton, aPressed, aValue);
|
2013-08-01 10:52:27 -07:00
|
|
|
if (first_time) {
|
|
|
|
FireConnectionEvent(listeners[i], gamepad, true);
|
|
|
|
}
|
2013-06-19 12:05:47 -07:00
|
|
|
if (mNonstandardEventsEnabled) {
|
|
|
|
// Fire event
|
|
|
|
FireButtonEvent(listeners[i], gamepad, aButton, aValue);
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::FireButtonEvent(EventTarget* aTarget,
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 11:12:08 -07:00
|
|
|
uint32_t aButton,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
nsString name = aValue == 1.0L ? NS_LITERAL_STRING("gamepadbuttondown") :
|
|
|
|
NS_LITERAL_STRING("gamepadbuttonup");
|
2013-10-09 09:05:22 -07:00
|
|
|
GamepadButtonEventInit init;
|
2013-09-11 15:35:51 -07:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
init.mButton = aButton;
|
|
|
|
nsRefPtr<GamepadButtonEvent> event =
|
|
|
|
GamepadButtonEvent::Constructor(aTarget, name, init);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2013-09-11 15:35:51 -07:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
bool defaultActionEnabled = true;
|
2011-08-03 11:12:08 -07:00
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis, double aValue)
|
|
|
|
{
|
|
|
|
if (mShuttingDown || aIndex >= mGamepads.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mGamepads[aIndex]->SetAxis(aAxis, aValue);
|
|
|
|
|
|
|
|
// Hold on to listeners in a separate array because firing events
|
|
|
|
// can mutate the mListeners array.
|
|
|
|
nsTArray<nsRefPtr<nsGlobalWindow> > listeners(mListeners);
|
|
|
|
|
|
|
|
for (uint32_t i = listeners.Length(); i > 0 ; ) {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
// Only send events to non-background windows
|
2013-10-16 09:11:24 -07:00
|
|
|
if (!listeners[i]->IsCurrentInnerWindow() ||
|
2011-08-03 11:12:08 -07:00
|
|
|
listeners[i]->GetOuterWindow()->IsBackground()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-01 10:52:27 -07:00
|
|
|
bool first_time = false;
|
2011-08-03 11:12:08 -07:00
|
|
|
if (!WindowHasSeenGamepad(listeners[i], aIndex)) {
|
|
|
|
// This window hasn't seen this gamepad before, so
|
|
|
|
// send a connection event first.
|
2013-08-01 10:52:27 -07:00
|
|
|
SetWindowHasSeenGamepad(listeners[i], aIndex);
|
|
|
|
first_time = true;
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = listeners[i]->GetGamepad(aIndex);
|
2011-08-03 11:12:08 -07:00
|
|
|
if (gamepad) {
|
|
|
|
gamepad->SetAxis(aAxis, aValue);
|
2013-08-01 10:52:27 -07:00
|
|
|
if (first_time) {
|
|
|
|
FireConnectionEvent(listeners[i], gamepad, true);
|
|
|
|
}
|
2013-06-19 12:05:47 -07:00
|
|
|
if (mNonstandardEventsEnabled) {
|
|
|
|
// Fire event
|
|
|
|
FireAxisMoveEvent(listeners[i], gamepad, aAxis, aValue);
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::FireAxisMoveEvent(EventTarget* aTarget,
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 11:12:08 -07:00
|
|
|
uint32_t aAxis,
|
|
|
|
double aValue)
|
|
|
|
{
|
2013-10-09 09:05:22 -07:00
|
|
|
GamepadAxisMoveEventInit init;
|
2013-09-11 15:35:51 -07:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
init.mAxis = aAxis;
|
|
|
|
init.mValue = aValue;
|
|
|
|
nsRefPtr<GamepadAxisMoveEvent> event =
|
|
|
|
GamepadAxisMoveEvent::Constructor(aTarget,
|
|
|
|
NS_LITERAL_STRING("gamepadaxismove"),
|
|
|
|
init);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2013-09-11 15:35:51 -07:00
|
|
|
bool defaultActionEnabled = true;
|
2011-08-03 11:12:08 -07:00
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::NewConnectionEvent(uint32_t aIndex, bool aConnected)
|
|
|
|
{
|
|
|
|
if (mShuttingDown || aIndex >= mGamepads.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold on to listeners in a separate array because firing events
|
|
|
|
// can mutate the mListeners array.
|
|
|
|
nsTArray<nsRefPtr<nsGlobalWindow> > listeners(mListeners);
|
|
|
|
|
|
|
|
if (aConnected) {
|
|
|
|
for (uint32_t i = listeners.Length(); i > 0 ; ) {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
// Only send events to non-background windows
|
2013-10-16 09:11:24 -07:00
|
|
|
if (!listeners[i]->IsCurrentInnerWindow() ||
|
2013-08-01 10:52:27 -07:00
|
|
|
listeners[i]->GetOuterWindow()->IsBackground()) {
|
2011-08-03 11:12:08 -07:00
|
|
|
continue;
|
2013-08-01 10:52:27 -07:00
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
// We don't fire a connected event here unless the window
|
|
|
|
// has seen input from at least one device.
|
2013-08-01 10:52:27 -07:00
|
|
|
if (!listeners[i]->HasSeenGamepadInput()) {
|
|
|
|
continue;
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SetWindowHasSeenGamepad(listeners[i], aIndex);
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = listeners[i]->GetGamepad(aIndex);
|
2011-08-03 11:12:08 -07:00
|
|
|
if (gamepad) {
|
|
|
|
// Fire event
|
|
|
|
FireConnectionEvent(listeners[i], gamepad, aConnected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For disconnection events, fire one at every window that has received
|
|
|
|
// data from this gamepad.
|
|
|
|
for (uint32_t i = listeners.Length(); i > 0 ; ) {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
// Even background windows get these events, so we don't have to
|
|
|
|
// deal with the hassle of syncing the state of removed gamepads.
|
|
|
|
|
|
|
|
if (WindowHasSeenGamepad(listeners[i], aIndex)) {
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = listeners[i]->GetGamepad(aIndex);
|
2011-08-03 11:12:08 -07:00
|
|
|
if (gamepad) {
|
|
|
|
gamepad->SetConnected(false);
|
|
|
|
// Fire event
|
|
|
|
FireConnectionEvent(listeners[i], gamepad, false);
|
|
|
|
listeners[i]->RemoveGamepad(aIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::FireConnectionEvent(EventTarget* aTarget,
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad* aGamepad,
|
2011-08-03 11:12:08 -07:00
|
|
|
bool aConnected)
|
|
|
|
{
|
|
|
|
nsString name = aConnected ? NS_LITERAL_STRING("gamepadconnected") :
|
|
|
|
NS_LITERAL_STRING("gamepaddisconnected");
|
2013-10-09 09:05:22 -07:00
|
|
|
GamepadEventInit init;
|
2013-09-11 15:35:51 -07:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
nsRefPtr<GamepadEvent> event =
|
|
|
|
GamepadEvent::Constructor(aTarget, name, init);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2013-09-11 15:35:51 -07:00
|
|
|
bool defaultActionEnabled = true;
|
2011-08-03 11:12:08 -07:00
|
|
|
aTarget->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 16:47:13 -08:00
|
|
|
GamepadService::SyncGamepadState(uint32_t aIndex, Gamepad* aGamepad)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
2013-04-09 05:43:24 -07:00
|
|
|
if (mShuttingDown || !mEnabled || aIndex > mGamepads.Length()) {
|
2011-08-03 11:12:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aGamepad->SyncState(mGamepads[aIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<GamepadService>
|
|
|
|
GamepadService::GetService()
|
|
|
|
{
|
|
|
|
if (sShutdown) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gGamepadServiceSingleton) {
|
|
|
|
gGamepadServiceSingleton = new GamepadService();
|
|
|
|
ClearOnShutdown(&gGamepadServiceSingleton);
|
|
|
|
}
|
|
|
|
nsRefPtr<GamepadService> service(gGamepadServiceSingleton);
|
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
GamepadService::IsAPIEnabled() {
|
|
|
|
return Preferences::GetBool(kGamepadEnabledPref, false);
|
|
|
|
}
|
|
|
|
|
2011-08-03 11:12:08 -07:00
|
|
|
bool
|
|
|
|
GamepadService::WindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex)
|
|
|
|
{
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = aWindow->GetGamepad(aIndex);
|
2011-08-03 11:12:08 -07:00
|
|
|
return gamepad != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::SetWindowHasSeenGamepad(nsGlobalWindow* aWindow,
|
|
|
|
uint32_t aIndex,
|
|
|
|
bool aHasSeen)
|
|
|
|
{
|
|
|
|
if (mListeners.IndexOf(aWindow) == NoIndex) {
|
|
|
|
// This window isn't even listening for gamepad events.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHasSeen) {
|
|
|
|
aWindow->SetHasSeenGamepadInput(true);
|
2013-10-08 08:51:42 -07:00
|
|
|
nsCOMPtr<nsISupports> window = ToSupports(aWindow);
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> gamepad = mGamepads[aIndex]->Clone(window);
|
2011-08-03 11:12:08 -07:00
|
|
|
aWindow->AddGamepad(aIndex, gamepad);
|
|
|
|
} else {
|
|
|
|
aWindow->RemoveGamepad(aIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
GamepadService::TimeoutHandler(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
// the reason that we use self, instead of just using nsITimerCallback or nsIObserver
|
|
|
|
// is so that subclasses are free to use timers without worry about the base classes's
|
|
|
|
// usage.
|
|
|
|
GamepadService* self = reinterpret_cast<GamepadService*>(aClosure);
|
|
|
|
if (!self) {
|
|
|
|
NS_ERROR("no self");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mListeners.Length() == 0) {
|
|
|
|
mozilla::hal::StopMonitoringGamepadStatus();
|
|
|
|
self->mStarted = false;
|
|
|
|
if (!self->mGamepads.IsEmpty()) {
|
|
|
|
self->mGamepads.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadService::StartCleanupTimer()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->InitWithFuncCallback(TimeoutHandler,
|
|
|
|
this,
|
|
|
|
kCleanupDelayMS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the test service. This is just to provide a simple binding
|
|
|
|
* of the GamepadService to JavaScript via XPCOM so that we can write Mochitests
|
|
|
|
* that add and remove fake gamepads, avoiding the platform-specific backends.
|
|
|
|
*/
|
|
|
|
NS_IMPL_ISUPPORTS1(GamepadServiceTest, nsIGamepadServiceTest)
|
|
|
|
|
|
|
|
GamepadServiceTest* GamepadServiceTest::sSingleton = nullptr;
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<GamepadServiceTest>
|
|
|
|
GamepadServiceTest::CreateService()
|
|
|
|
{
|
|
|
|
if (sSingleton == nullptr) {
|
|
|
|
sSingleton = new GamepadServiceTest();
|
|
|
|
}
|
|
|
|
nsRefPtr<GamepadServiceTest> service = sSingleton;
|
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
GamepadServiceTest::GamepadServiceTest()
|
|
|
|
{
|
|
|
|
/* member initializers and constructor code */
|
2013-10-16 09:11:24 -07:00
|
|
|
nsRefPtr<GamepadService> service = GamepadService::GetService();
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
/* uint32_t addGamepad (in string id, in unsigned long mapping, in unsigned long numButtons, in unsigned long numAxes); */
|
2011-08-03 11:12:08 -07:00
|
|
|
NS_IMETHODIMP GamepadServiceTest::AddGamepad(const char* aID,
|
2013-04-09 05:43:25 -07:00
|
|
|
uint32_t aMapping,
|
2011-08-03 11:12:08 -07:00
|
|
|
uint32_t aNumButtons,
|
|
|
|
uint32_t aNumAxes,
|
|
|
|
uint32_t* aRetval)
|
|
|
|
{
|
|
|
|
*aRetval = gGamepadServiceSingleton->AddGamepad(aID,
|
2013-04-09 05:43:25 -07:00
|
|
|
static_cast<GamepadMappingType>(aMapping),
|
2011-08-03 11:12:08 -07:00
|
|
|
aNumButtons,
|
|
|
|
aNumAxes);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void removeGamepad (in uint32_t index); */
|
|
|
|
NS_IMETHODIMP GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
gGamepadServiceSingleton->RemoveGamepad(aIndex);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void newButtonEvent (in uint32_t index, in uint32_t button,
|
|
|
|
in boolean pressed); */
|
|
|
|
NS_IMETHODIMP GamepadServiceTest::NewButtonEvent(uint32_t aIndex,
|
|
|
|
uint32_t aButton,
|
|
|
|
bool aPressed)
|
|
|
|
{
|
|
|
|
gGamepadServiceSingleton->NewButtonEvent(aIndex, aButton, aPressed);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void newAxisMoveEvent (in uint32_t index, in uint32_t axis,
|
|
|
|
in double value); */
|
|
|
|
NS_IMETHODIMP GamepadServiceTest::NewAxisMoveEvent(uint32_t aIndex,
|
|
|
|
uint32_t aAxis,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
gGamepadServiceSingleton->NewAxisMoveEvent(aIndex, aAxis, aValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|