2016-06-28 01:25:00 +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: */
|
|
|
|
/* 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/dom/GamepadManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Gamepad.h"
|
|
|
|
#include "mozilla/dom/GamepadAxisMoveEvent.h"
|
|
|
|
#include "mozilla/dom/GamepadButtonEvent.h"
|
|
|
|
#include "mozilla/dom/GamepadEvent.h"
|
|
|
|
#include "mozilla/dom/GamepadEventChannelChild.h"
|
|
|
|
#include "mozilla/dom/GamepadMonitoring.h"
|
|
|
|
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2017-06-15 12:59:26 +03:00
|
|
|
#include "nsContentUtils.h"
|
2016-06-28 01:25:00 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2016-10-04 11:30:04 +03:00
|
|
|
#include "VRManagerChild.h"
|
2016-06-28 01:25:00 +03:00
|
|
|
#include "mozilla/Services.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* kGamepadEnabledPref = "dom.gamepad.enabled";
|
|
|
|
const char* kGamepadEventsEnabledPref =
|
|
|
|
"dom.gamepad.non_standard_events.enabled";
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
const nsTArray<RefPtr<nsGlobalWindowInner>>::index_type NoIndex =
|
|
|
|
nsTArray<RefPtr<nsGlobalWindowInner>>::NoIndex;
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
bool sShutdown = false;
|
|
|
|
|
|
|
|
StaticRefPtr<GamepadManager> gGamepadManagerSingleton;
|
2016-10-04 05:57:04 +03:00
|
|
|
const uint32_t VR_GAMEPAD_IDX_OFFSET = 0x01 << 16;
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(GamepadManager, nsIObserver)
|
|
|
|
|
|
|
|
GamepadManager::GamepadManager()
|
|
|
|
: mEnabled(false),
|
|
|
|
mNonstandardEventsEnabled(false),
|
2017-02-02 09:59:44 +03:00
|
|
|
mShuttingDown(false),
|
|
|
|
mPromiseID(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
nsresult GamepadManager::Init() {
|
|
|
|
mEnabled = IsAPIEnabled();
|
|
|
|
mNonstandardEventsEnabled =
|
|
|
|
Preferences::GetBool(kGamepadEventsEnabledPref, false);
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!observerService)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = observerService->AddObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID,
|
|
|
|
false);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
GamepadManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID);
|
|
|
|
}
|
|
|
|
BeginShutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::StopMonitoring() {
|
2016-09-07 13:31:53 +03:00
|
|
|
for (uint32_t i = 0; i < mChannelChildren.Length(); ++i) {
|
|
|
|
mChannelChildren[i]->SendGamepadListenerRemoved();
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
2017-04-24 07:56:41 +03:00
|
|
|
if (gfx::VRManagerChild::IsCreated()) {
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
|
|
|
vm->SendControllerListenerRemoved();
|
|
|
|
}
|
2017-09-28 20:11:24 +03:00
|
|
|
mChannelChildren.Clear();
|
|
|
|
mGamepads.Clear();
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::BeginShutdown() {
|
|
|
|
mShuttingDown = true;
|
|
|
|
StopMonitoring();
|
|
|
|
// 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();
|
|
|
|
sShutdown = true;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
void GamepadManager::AddListener(nsGlobalWindowInner* aWindow) {
|
2016-06-28 01:25:00 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-06-22 11:56:18 +03:00
|
|
|
// IPDL child has not been created
|
|
|
|
if (mChannelChildren.IsEmpty()) {
|
2017-10-24 13:02:40 +03:00
|
|
|
PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actor)) {
|
2017-10-25 09:45:53 +03:00
|
|
|
// We are probably shutting down.
|
|
|
|
return;
|
2017-10-24 13:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GamepadEventChannelChild* child = new GamepadEventChannelChild();
|
|
|
|
PGamepadEventChannelChild* initedChild =
|
|
|
|
actor->SendPGamepadEventChannelConstructor(child);
|
|
|
|
if (NS_WARN_IF(!initedChild)) {
|
2017-10-25 09:45:53 +03:00
|
|
|
// We are probably shutting down.
|
2017-10-24 13:02:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(initedChild == child);
|
|
|
|
child->SendGamepadListenerAdded();
|
|
|
|
mChannelChildren.AppendElement(child);
|
|
|
|
|
|
|
|
if (gfx::VRManagerChild::IsCreated()) {
|
|
|
|
// Construct VRManagerChannel and ask adding the connected
|
|
|
|
// VR controllers to GamepadManager
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
|
|
|
vm->SendControllerListenerAdded();
|
2017-06-22 11:56:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 12:59:26 +03:00
|
|
|
if (!mEnabled || mShuttingDown ||
|
2019-03-05 15:42:51 +03:00
|
|
|
nsContentUtils::ShouldResistFingerprinting(aWindow->GetExtantDoc())) {
|
2016-06-28 01:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListeners.IndexOf(aWindow) != NoIndex) {
|
|
|
|
return; // already exists
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendElement(aWindow);
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
void GamepadManager::RemoveListener(nsGlobalWindowInner* aWindow) {
|
2016-06-28 01:25:00 +03:00
|
|
|
MOZ_ASSERT(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
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:58:22 +03:00
|
|
|
for (auto iter = mGamepads.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
aWindow->RemoveGamepad(iter.Key());
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
mListeners.RemoveElement(aWindow);
|
|
|
|
|
|
|
|
if (mListeners.IsEmpty()) {
|
|
|
|
StopMonitoring();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Gamepad> GamepadManager::GetGamepad(uint32_t aIndex) const {
|
|
|
|
RefPtr<Gamepad> gamepad;
|
|
|
|
if (mGamepads.Get(aIndex, getter_AddRefs(gamepad))) {
|
|
|
|
return gamepad.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:10:55 +03:00
|
|
|
already_AddRefed<Gamepad> GamepadManager::GetGamepad(
|
|
|
|
uint32_t aGamepadId, GamepadServiceType aServiceType) const {
|
|
|
|
return GetGamepad(GetGamepadIndexWithServiceType(aGamepadId, aServiceType));
|
|
|
|
}
|
|
|
|
|
2016-10-04 05:57:04 +03:00
|
|
|
uint32_t GamepadManager::GetGamepadIndexWithServiceType(
|
2018-08-29 22:10:55 +03:00
|
|
|
uint32_t aIndex, GamepadServiceType aServiceType) const {
|
2016-10-04 05:57:04 +03:00
|
|
|
uint32_t newIndex = 0;
|
|
|
|
|
|
|
|
switch (aServiceType) {
|
|
|
|
case GamepadServiceType::Standard:
|
2017-02-02 09:33:18 +03:00
|
|
|
MOZ_ASSERT(aIndex <= VR_GAMEPAD_IDX_OFFSET);
|
|
|
|
newIndex = aIndex;
|
|
|
|
break;
|
2016-10-04 05:57:04 +03:00
|
|
|
case GamepadServiceType::VR:
|
2017-02-02 09:33:18 +03:00
|
|
|
newIndex = aIndex + VR_GAMEPAD_IDX_OFFSET;
|
|
|
|
break;
|
2016-10-04 05:57:04 +03:00
|
|
|
default:
|
2017-02-02 09:33:18 +03:00
|
|
|
MOZ_ASSERT(false);
|
|
|
|
break;
|
2016-10-04 05:57:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return newIndex;
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
void GamepadManager::AddGamepad(uint32_t aIndex, const nsAString& aId,
|
|
|
|
GamepadMappingType aMapping, GamepadHand aHand,
|
2016-10-04 05:57:04 +03:00
|
|
|
GamepadServiceType aServiceType,
|
2016-06-28 01:25:00 +03:00
|
|
|
uint32_t aDisplayID, uint32_t aNumButtons,
|
2017-02-02 09:33:18 +03:00
|
|
|
uint32_t aNumAxes, uint32_t aNumHaptics) {
|
|
|
|
uint32_t newIndex = GetGamepadIndexWithServiceType(aIndex, aServiceType);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
// TODO: bug 852258: get initial button/axis state
|
|
|
|
RefPtr<Gamepad> gamepad = new Gamepad(nullptr, aId,
|
|
|
|
0, // index is set by global window
|
2017-07-14 11:55:52 +03:00
|
|
|
newIndex, aMapping, aHand, aDisplayID,
|
2017-02-02 09:33:18 +03:00
|
|
|
aNumButtons, aNumAxes, aNumHaptics);
|
2016-10-04 05:57:04 +03:00
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
// We store the gamepad related to its index given by the parent process,
|
|
|
|
// and no duplicate index is allowed.
|
2016-10-04 05:57:04 +03:00
|
|
|
MOZ_ASSERT(!mGamepads.Get(newIndex, nullptr));
|
|
|
|
mGamepads.Put(newIndex, gamepad);
|
|
|
|
NewConnectionEvent(newIndex, true);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 05:57:04 +03:00
|
|
|
void GamepadManager::RemoveGamepad(uint32_t aIndex,
|
|
|
|
GamepadServiceType aServiceType) {
|
|
|
|
uint32_t newIndex = GetGamepadIndexWithServiceType(aIndex, aServiceType);
|
|
|
|
|
|
|
|
RefPtr<Gamepad> gamepad = GetGamepad(newIndex);
|
2016-06-28 01:25:00 +03:00
|
|
|
if (!gamepad) {
|
|
|
|
NS_WARNING("Trying to delete gamepad with invalid index");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gamepad->SetConnected(false);
|
2016-10-04 05:57:04 +03:00
|
|
|
NewConnectionEvent(newIndex, false);
|
|
|
|
mGamepads.Remove(newIndex);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::FireButtonEvent(EventTarget* aTarget, Gamepad* aGamepad,
|
|
|
|
uint32_t aButton, double aValue) {
|
|
|
|
nsString name = aValue == 1.0L ? NS_LITERAL_STRING("gamepadbuttondown")
|
|
|
|
: NS_LITERAL_STRING("gamepadbuttonup");
|
|
|
|
GamepadButtonEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
init.mButton = aButton;
|
|
|
|
RefPtr<GamepadButtonEvent> event =
|
|
|
|
GamepadButtonEvent::Constructor(aTarget, name, init);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
aTarget->DispatchEvent(*event);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::FireAxisMoveEvent(EventTarget* aTarget, Gamepad* aGamepad,
|
|
|
|
uint32_t aAxis, double aValue) {
|
|
|
|
GamepadAxisMoveEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
init.mAxis = aAxis;
|
|
|
|
init.mValue = aValue;
|
|
|
|
RefPtr<GamepadAxisMoveEvent> event = GamepadAxisMoveEvent::Constructor(
|
|
|
|
aTarget, NS_LITERAL_STRING("gamepadaxismove"), init);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
aTarget->DispatchEvent(*event);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::NewConnectionEvent(uint32_t aIndex, bool aConnected) {
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Gamepad> gamepad = GetGamepad(aIndex);
|
|
|
|
if (!gamepad) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold on to listeners in a separate array because firing events
|
|
|
|
// can mutate the mListeners array.
|
2017-11-04 01:25:38 +03:00
|
|
|
nsTArray<RefPtr<nsGlobalWindowInner>> listeners(mListeners);
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
if (aConnected) {
|
|
|
|
for (uint32_t i = 0; i < listeners.Length(); i++) {
|
2019-03-05 15:42:51 +03:00
|
|
|
// Do not fire gamepadconnected and gamepaddisconnected events when
|
|
|
|
// privacy.resistFingerprinting is true.
|
|
|
|
if (nsContentUtils::ShouldResistFingerprinting(
|
|
|
|
listeners[i]->GetExtantDoc())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
// Only send events to non-background windows
|
|
|
|
if (!listeners[i]->AsInner()->IsCurrentInnerWindow() ||
|
|
|
|
listeners[i]->GetOuterWindow()->IsBackground()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't fire a connected event here unless the window
|
|
|
|
// has seen input from at least one device.
|
|
|
|
if (!listeners[i]->HasSeenGamepadInput()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetWindowHasSeenGamepad(listeners[i], aIndex);
|
|
|
|
|
|
|
|
RefPtr<Gamepad> listenerGamepad = listeners[i]->GetGamepad(aIndex);
|
|
|
|
if (listenerGamepad) {
|
|
|
|
// Fire event
|
|
|
|
FireConnectionEvent(listeners[i], listenerGamepad, aConnected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For disconnection events, fire one at every window that has received
|
|
|
|
// data from this gamepad.
|
|
|
|
for (uint32_t i = 0; i < listeners.Length(); i++) {
|
|
|
|
// Even background windows get these events, so we don't have to
|
|
|
|
// deal with the hassle of syncing the state of removed gamepads.
|
|
|
|
|
2019-03-05 15:42:51 +03:00
|
|
|
// Do not fire gamepadconnected and gamepaddisconnected events when
|
|
|
|
// privacy.resistFingerprinting is true.
|
|
|
|
if (nsContentUtils::ShouldResistFingerprinting(
|
|
|
|
listeners[i]->GetExtantDoc())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
if (WindowHasSeenGamepad(listeners[i], aIndex)) {
|
|
|
|
RefPtr<Gamepad> listenerGamepad = listeners[i]->GetGamepad(aIndex);
|
|
|
|
if (listenerGamepad) {
|
|
|
|
listenerGamepad->SetConnected(false);
|
|
|
|
// Fire event
|
|
|
|
FireConnectionEvent(listeners[i], listenerGamepad, false);
|
|
|
|
listeners[i]->RemoveGamepad(aIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::FireConnectionEvent(EventTarget* aTarget,
|
|
|
|
Gamepad* aGamepad, bool aConnected) {
|
|
|
|
nsString name = aConnected ? NS_LITERAL_STRING("gamepadconnected")
|
|
|
|
: NS_LITERAL_STRING("gamepaddisconnected");
|
|
|
|
GamepadEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mGamepad = aGamepad;
|
|
|
|
RefPtr<GamepadEvent> event = GamepadEvent::Constructor(aTarget, name, init);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
aTarget->DispatchEvent(*event);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:42:51 +03:00
|
|
|
void GamepadManager::SyncGamepadState(uint32_t aIndex,
|
|
|
|
nsGlobalWindowInner* aWindow,
|
|
|
|
Gamepad* aGamepad) {
|
2017-06-15 12:59:26 +03:00
|
|
|
if (mShuttingDown || !mEnabled ||
|
2019-03-05 15:42:51 +03:00
|
|
|
nsContentUtils::ShouldResistFingerprinting(aWindow->GetExtantDoc())) {
|
2016-06-28 01:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Gamepad> gamepad = GetGamepad(aIndex);
|
|
|
|
if (!gamepad) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aGamepad->SyncState(gamepad);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool GamepadManager::IsServiceRunning() { return !!gGamepadManagerSingleton; }
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<GamepadManager> GamepadManager::GetService() {
|
|
|
|
if (sShutdown) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gGamepadManagerSingleton) {
|
|
|
|
RefPtr<GamepadManager> manager = new GamepadManager();
|
|
|
|
nsresult rv = manager->Init();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
gGamepadManagerSingleton = manager;
|
|
|
|
ClearOnShutdown(&gGamepadManagerSingleton);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GamepadManager> service(gGamepadManagerSingleton);
|
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool GamepadManager::IsAPIEnabled() {
|
|
|
|
return Preferences::GetBool(kGamepadEnabledPref, false);
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
bool GamepadManager::MaybeWindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
|
|
|
|
uint32_t aIndex) {
|
2016-06-28 01:25:00 +03:00
|
|
|
if (!WindowHasSeenGamepad(aWindow, aIndex)) {
|
|
|
|
// This window hasn't seen this gamepad before, so
|
|
|
|
// send a connection event first.
|
|
|
|
SetWindowHasSeenGamepad(aWindow, aIndex);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
bool GamepadManager::WindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
|
|
|
|
uint32_t aIndex) const {
|
2016-06-28 01:25:00 +03:00
|
|
|
RefPtr<Gamepad> gamepad = aWindow->GetGamepad(aIndex);
|
|
|
|
return gamepad != nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
void GamepadManager::SetWindowHasSeenGamepad(nsGlobalWindowInner* aWindow,
|
2016-06-28 01:25:00 +03:00
|
|
|
uint32_t aIndex, bool aHasSeen) {
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
if (mListeners.IndexOf(aWindow) == NoIndex) {
|
|
|
|
// This window isn't even listening for gamepad events.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aHasSeen) {
|
|
|
|
aWindow->SetHasSeenGamepadInput(true);
|
|
|
|
nsCOMPtr<nsISupports> window = ToSupports(aWindow);
|
|
|
|
RefPtr<Gamepad> gamepad = GetGamepad(aIndex);
|
|
|
|
if (!gamepad) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<Gamepad> clonedGamepad = gamepad->Clone(window);
|
|
|
|
aWindow->AddGamepad(aIndex, clonedGamepad);
|
|
|
|
} else {
|
|
|
|
aWindow->RemoveGamepad(aIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::Update(const GamepadChangeEvent& aEvent) {
|
2017-06-22 11:56:18 +03:00
|
|
|
if (!mEnabled || mShuttingDown ||
|
|
|
|
nsContentUtils::ShouldResistFingerprinting()) {
|
2017-04-21 06:34:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
const uint32_t index = aEvent.index();
|
|
|
|
GamepadServiceType serviceType = aEvent.service_type();
|
|
|
|
GamepadChangeEventBody body = aEvent.body();
|
|
|
|
|
|
|
|
if (body.type() == GamepadChangeEventBody::TGamepadAdded) {
|
|
|
|
const GamepadAdded& a = body.get_GamepadAdded();
|
2016-11-04 10:43:55 +03:00
|
|
|
AddGamepad(index, a.id(), static_cast<GamepadMappingType>(a.mapping()),
|
2016-11-14 12:30:31 +03:00
|
|
|
static_cast<GamepadHand>(a.hand()), serviceType, a.display_id(),
|
2017-02-02 09:33:18 +03:00
|
|
|
a.num_buttons(), a.num_axes(), a.num_haptics());
|
2016-06-28 01:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
if (body.type() == GamepadChangeEventBody::TGamepadRemoved) {
|
|
|
|
RemoveGamepad(index, serviceType);
|
2016-06-28 01:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-21 06:34:30 +03:00
|
|
|
|
|
|
|
if (!SetGamepadByEvent(aEvent)) {
|
2016-06-28 01:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-21 06:34:30 +03:00
|
|
|
|
|
|
|
// Hold on to listeners in a separate array because firing events
|
|
|
|
// can mutate the mListeners array.
|
2017-11-04 01:25:38 +03:00
|
|
|
nsTArray<RefPtr<nsGlobalWindowInner>> listeners(mListeners);
|
2017-04-21 06:34:30 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < listeners.Length(); i++) {
|
|
|
|
// Only send events to non-background windows
|
|
|
|
if (!listeners[i]->AsInner()->IsCurrentInnerWindow() ||
|
|
|
|
listeners[i]->GetOuterWindow()->IsBackground()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetGamepadByEvent(aEvent, listeners[i]);
|
|
|
|
MaybeConvertToNonstandardGamepadEvent(aEvent, listeners[i]);
|
2016-10-24 13:06:52 +03:00
|
|
|
}
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GamepadManager::MaybeConvertToNonstandardGamepadEvent(
|
|
|
|
const GamepadChangeEvent& aEvent, nsGlobalWindowInner* aWindow) {
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
if (!mNonstandardEventsEnabled) {
|
2016-10-24 13:06:52 +03:00
|
|
|
return;
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
2017-04-21 06:34:30 +03:00
|
|
|
|
2017-07-27 06:22:29 +03:00
|
|
|
const uint32_t index =
|
|
|
|
GetGamepadIndexWithServiceType(aEvent.index(), aEvent.service_type());
|
|
|
|
RefPtr<Gamepad> gamepad = aWindow->GetGamepad(index);
|
2017-07-17 06:44:39 +03:00
|
|
|
const GamepadChangeEventBody& body = aEvent.body();
|
2017-04-21 06:34:30 +03:00
|
|
|
|
2017-07-17 06:44:39 +03:00
|
|
|
if (gamepad) {
|
|
|
|
switch (body.type()) {
|
|
|
|
case GamepadChangeEventBody::TGamepadButtonInformation: {
|
|
|
|
const GamepadButtonInformation& a = body.get_GamepadButtonInformation();
|
|
|
|
FireButtonEvent(aWindow, gamepad, a.button(), a.value());
|
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
case GamepadChangeEventBody::TGamepadAxisInformation: {
|
|
|
|
const GamepadAxisInformation& a = body.get_GamepadAxisInformation();
|
|
|
|
FireAxisMoveEvent(aWindow, gamepad, a.axis(), a.value());
|
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:25:38 +03:00
|
|
|
bool GamepadManager::SetGamepadByEvent(const GamepadChangeEvent& aEvent,
|
|
|
|
nsGlobalWindowInner* aWindow) {
|
2017-04-21 06:34:30 +03:00
|
|
|
bool ret = false;
|
|
|
|
bool firstTime = false;
|
2017-07-17 06:44:39 +03:00
|
|
|
const uint32_t index =
|
|
|
|
GetGamepadIndexWithServiceType(aEvent.index(), aEvent.service_type());
|
|
|
|
if (aWindow) {
|
|
|
|
firstTime = MaybeWindowHasSeenGamepad(aWindow, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Gamepad> gamepad =
|
|
|
|
aWindow ? aWindow->GetGamepad(index) : GetGamepad(index);
|
|
|
|
const GamepadChangeEventBody& body = aEvent.body();
|
|
|
|
|
|
|
|
if (gamepad) {
|
|
|
|
switch (body.type()) {
|
|
|
|
case GamepadChangeEventBody::TGamepadButtonInformation: {
|
|
|
|
const GamepadButtonInformation& a = body.get_GamepadButtonInformation();
|
2017-04-21 06:34:30 +03:00
|
|
|
gamepad->SetButton(a.button(), a.pressed(), a.touched(), a.value());
|
2017-07-17 06:44:39 +03:00
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
case GamepadChangeEventBody::TGamepadAxisInformation: {
|
|
|
|
const GamepadAxisInformation& a = body.get_GamepadAxisInformation();
|
2017-04-21 06:34:30 +03:00
|
|
|
gamepad->SetAxis(a.axis(), a.value());
|
2017-07-17 06:44:39 +03:00
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
case GamepadChangeEventBody::TGamepadPoseInformation: {
|
|
|
|
const GamepadPoseInformation& a = body.get_GamepadPoseInformation();
|
2017-04-21 06:34:30 +03:00
|
|
|
gamepad->SetPose(a.pose_state());
|
2017-07-17 06:44:39 +03:00
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
case GamepadChangeEventBody::TGamepadHandInformation: {
|
|
|
|
const GamepadHandInformation& a = body.get_GamepadHandInformation();
|
2017-04-21 06:34:30 +03:00
|
|
|
gamepad->SetHand(a.hand());
|
2017-07-17 06:44:39 +03:00
|
|
|
break;
|
2017-04-21 06:34:30 +03:00
|
|
|
}
|
2017-07-17 06:44:39 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = true;
|
2017-04-18 10:55:15 +03:00
|
|
|
}
|
2016-06-28 01:25:00 +03:00
|
|
|
|
2017-04-21 06:34:30 +03:00
|
|
|
if (aWindow && firstTime) {
|
|
|
|
FireConnectionEvent(aWindow, gamepad, true);
|
|
|
|
}
|
2016-06-28 01:25:00 +03:00
|
|
|
|
2017-04-21 06:34:30 +03:00
|
|
|
return ret;
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
2017-02-02 09:33:18 +03:00
|
|
|
already_AddRefed<Promise> GamepadManager::VibrateHaptic(
|
|
|
|
uint32_t aControllerIdx, uint32_t aHapticIndex, double aIntensity,
|
2017-02-02 09:59:44 +03:00
|
|
|
double aDuration, nsIGlobalObject* aGlobal, ErrorResult& aRv) {
|
2017-07-27 12:55:16 +03:00
|
|
|
const char* kGamepadHapticEnabledPref = "dom.gamepad.haptic_feedback.enabled";
|
2017-02-02 09:59:44 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-27 12:55:16 +03:00
|
|
|
if (Preferences::GetBool(kGamepadHapticEnabledPref)) {
|
|
|
|
if (aControllerIdx >= VR_GAMEPAD_IDX_OFFSET) {
|
|
|
|
if (gfx::VRManagerChild::IsCreated()) {
|
|
|
|
const uint32_t index = aControllerIdx - VR_GAMEPAD_IDX_OFFSET;
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
|
|
|
vm->AddPromise(mPromiseID, promise);
|
|
|
|
vm->SendVibrateHaptic(index, aHapticIndex, aIntensity, aDuration,
|
|
|
|
mPromiseID);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const auto& channelChild : mChannelChildren) {
|
|
|
|
channelChild->AddPromise(mPromiseID, promise);
|
|
|
|
channelChild->SendVibrateHaptic(aControllerIdx, aHapticIndex,
|
|
|
|
aIntensity, aDuration, mPromiseID);
|
|
|
|
}
|
2017-02-02 10:00:51 +03:00
|
|
|
}
|
2017-02-02 09:33:18 +03:00
|
|
|
}
|
2017-02-02 09:59:44 +03:00
|
|
|
|
|
|
|
++mPromiseID;
|
|
|
|
return promise.forget();
|
2017-02-02 09:33:18 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 05:17:57 +03:00
|
|
|
void GamepadManager::StopHaptics() {
|
2017-07-27 12:55:16 +03:00
|
|
|
const char* kGamepadHapticEnabledPref = "dom.gamepad.haptic_feedback.enabled";
|
|
|
|
if (!Preferences::GetBool(kGamepadHapticEnabledPref)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-07 05:17:57 +03:00
|
|
|
for (auto iter = mGamepads.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
const uint32_t gamepadIndex = iter.UserData()->HashKey();
|
|
|
|
if (gamepadIndex >= VR_GAMEPAD_IDX_OFFSET) {
|
2017-04-24 07:56:41 +03:00
|
|
|
if (gfx::VRManagerChild::IsCreated()) {
|
|
|
|
const uint32_t index = gamepadIndex - VR_GAMEPAD_IDX_OFFSET;
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
|
|
|
vm->SendStopVibrateHaptic(index);
|
|
|
|
}
|
2017-03-07 05:17:57 +03:00
|
|
|
} else {
|
|
|
|
for (auto& channelChild : mChannelChildren) {
|
|
|
|
channelChild->SendStopVibrateHaptic(gamepadIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|