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/GamepadPlatformService.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/GamepadEventChannelParent.h"
|
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This is the singleton instance of GamepadPlatformService, can be called
|
|
|
|
// by both background and monitor thread.
|
2016-06-28 01:27:00 +03:00
|
|
|
StaticRefPtr<GamepadPlatformService> gGamepadPlatformServiceSingleton;
|
2016-06-28 01:25:00 +03:00
|
|
|
|
|
|
|
} //namepsace
|
|
|
|
|
|
|
|
GamepadPlatformService::GamepadPlatformService()
|
|
|
|
: mGamepadIndex(0),
|
|
|
|
mMutex("mozilla::dom::GamepadPlatformService")
|
|
|
|
{}
|
|
|
|
|
|
|
|
GamepadPlatformService::~GamepadPlatformService()
|
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-06-28 01:27:00 +03:00
|
|
|
already_AddRefed<GamepadPlatformService>
|
2016-06-28 01:25:00 +03:00
|
|
|
GamepadPlatformService::GetParentService()
|
|
|
|
{
|
|
|
|
//GamepadPlatformService can only be accessed in parent process
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2016-06-28 01:27:00 +03:00
|
|
|
if (!gGamepadPlatformServiceSingleton) {
|
|
|
|
// Only Background Thread can create new GamepadPlatformService instance.
|
|
|
|
if (IsOnBackgroundThread()) {
|
|
|
|
gGamepadPlatformServiceSingleton = new GamepadPlatformService();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
2016-06-28 01:27:00 +03:00
|
|
|
RefPtr<GamepadPlatformService> service(gGamepadPlatformServiceSingleton);
|
|
|
|
return service.forget();
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
GamepadPlatformService::NotifyGamepadChange(const T& aInfo)
|
|
|
|
{
|
|
|
|
// This method is called by monitor populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
GamepadChangeEvent e(aInfo);
|
|
|
|
|
|
|
|
// mChannelParents may be accessed by background thread in the
|
|
|
|
// same time, we use mutex to prevent possible race condtion
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
2016-10-23 20:32:00 +03:00
|
|
|
|
|
|
|
// Buffer all events if we have no Channel to dispatch, which
|
|
|
|
// may happen when performing Mochitest.
|
|
|
|
if (mChannelParents.IsEmpty()) {
|
|
|
|
mPendingEvents.AppendElement(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
for(uint32_t i = 0; i < mChannelParents.Length(); ++i) {
|
|
|
|
mChannelParents[i]->DispatchUpdateEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
GamepadPlatformService::AddGamepad(const char* aID,
|
|
|
|
GamepadMappingType aMapping,
|
2017-02-02 10:00:51 +03:00
|
|
|
GamepadHand aHand,
|
|
|
|
uint32_t aNumButtons, uint32_t aNumAxes,
|
|
|
|
uint32_t aHaptics)
|
2016-06-28 01:25:00 +03:00
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
uint32_t index = ++mGamepadIndex;
|
2017-07-14 11:55:52 +03:00
|
|
|
// Only VR controllers has displayID, we give 0 to the general gamepads.
|
2016-06-28 01:25:00 +03:00
|
|
|
GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), index,
|
2017-02-02 10:00:51 +03:00
|
|
|
aMapping, aHand, GamepadServiceType::Standard,
|
2017-07-14 11:55:52 +03:00
|
|
|
0, aNumButtons, aNumAxes, aHaptics);
|
2017-02-02 09:33:18 +03:00
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
NotifyGamepadChange<GamepadAdded>(a);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::RemoveGamepad(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2016-10-04 05:57:04 +03:00
|
|
|
GamepadRemoved a(aIndex, GamepadServiceType::Standard);
|
2016-06-28 01:25:00 +03:00
|
|
|
NotifyGamepadChange<GamepadRemoved>(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::NewButtonEvent(uint32_t aIndex, uint32_t aButton,
|
2017-04-07 04:35:13 +03:00
|
|
|
bool aPressed, bool aTouched,
|
|
|
|
double aValue)
|
2016-06-28 01:25:00 +03:00
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2016-10-14 12:39:51 +03:00
|
|
|
GamepadButtonInformation a(aIndex, GamepadServiceType::Standard,
|
2017-04-07 04:35:13 +03:00
|
|
|
aButton, aValue, aPressed, aTouched);
|
2016-06-28 01:25:00 +03:00
|
|
|
NotifyGamepadChange<GamepadButtonInformation>(a);
|
|
|
|
}
|
|
|
|
|
2017-04-07 04:35:13 +03:00
|
|
|
void
|
|
|
|
GamepadPlatformService::NewButtonEvent(uint32_t aIndex, uint32_t aButton,
|
|
|
|
bool aPressed, bool aTouched)
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
// When only a digital button is available the value will be synthesized.
|
|
|
|
NewButtonEvent(aIndex, aButton, aPressed, aTouched, aPressed ? 1.0L : 0.0L);
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
void
|
|
|
|
GamepadPlatformService::NewButtonEvent(uint32_t aIndex, uint32_t aButton,
|
|
|
|
bool aPressed)
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
// When only a digital button is available the value will be synthesized.
|
2017-04-07 04:35:13 +03:00
|
|
|
NewButtonEvent(aIndex, aButton, aPressed, aPressed, aPressed ? 1.0L : 0.0L);
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::NewAxisMoveEvent(uint32_t aIndex, uint32_t aAxis,
|
|
|
|
double aValue)
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2016-10-14 12:39:51 +03:00
|
|
|
GamepadAxisInformation a(aIndex, GamepadServiceType::Standard,
|
|
|
|
aAxis, aValue);
|
2016-06-28 01:25:00 +03:00
|
|
|
NotifyGamepadChange<GamepadAxisInformation>(a);
|
|
|
|
}
|
|
|
|
|
2017-02-02 10:00:51 +03:00
|
|
|
void
|
|
|
|
GamepadPlatformService::NewPoseEvent(uint32_t aIndex,
|
|
|
|
const GamepadPoseState& aPose)
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
GamepadPoseInformation a(aIndex, GamepadServiceType::Standard,
|
|
|
|
aPose);
|
|
|
|
NotifyGamepadChange<GamepadPoseInformation>(a);
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
void
|
|
|
|
GamepadPlatformService::ResetGamepadIndexes()
|
|
|
|
{
|
|
|
|
// This method is called by monitor thread populated in
|
|
|
|
// platform-dependent backends
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
mGamepadIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::AddChannelParent(GamepadEventChannelParent* aParent)
|
|
|
|
{
|
|
|
|
// mChannelParents can only be modified once GamepadEventChannelParent
|
|
|
|
// is created or removed in Background thread
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MOZ_ASSERT(!mChannelParents.Contains(aParent));
|
|
|
|
|
|
|
|
// We use mutex here to prevent race condition with monitor thread
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
|
|
|
mChannelParents.AppendElement(aParent);
|
2016-10-23 20:32:00 +03:00
|
|
|
FlushPendingEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::FlushPendingEvents()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(!mChannelParents.IsEmpty());
|
|
|
|
|
|
|
|
if (mPendingEvents.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This method must be called with mMutex held because it accesses
|
|
|
|
// mChannelParents.
|
|
|
|
for (uint32_t i=0; i<mChannelParents.Length(); ++i) {
|
|
|
|
for (uint32_t j=0; j<mPendingEvents.Length();++j) {
|
|
|
|
mChannelParents[i]->DispatchUpdateEvent(mPendingEvents[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mPendingEvents.Clear();
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::RemoveChannelParent(GamepadEventChannelParent* aParent)
|
|
|
|
{
|
|
|
|
// mChannelParents can only be modified once GamepadEventChannelParent
|
|
|
|
// is created or removed in Background thread
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MOZ_ASSERT(mChannelParents.Contains(aParent));
|
|
|
|
|
|
|
|
// We use mutex here to prevent race condition with monitor thread
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
|
|
|
mChannelParents.RemoveElement(aParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GamepadPlatformService::HasGamepadListeners()
|
|
|
|
{
|
|
|
|
// mChannelParents may be accessed by background thread in the
|
|
|
|
// same time, we use mutex to prevent possible race condtion
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// We use mutex here to prevent race condition with monitor thread
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
|
|
|
for (uint32_t i = 0; i < mChannelParents.Length(); i++) {
|
|
|
|
if(mChannelParents[i]->HasGamepadListener()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::MaybeShutdown()
|
|
|
|
{
|
|
|
|
// This method is invoked in MaybeStopGamepadMonitoring when
|
|
|
|
// an IPDL channel is going to be destroyed
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2016-06-28 01:27:00 +03:00
|
|
|
// We have to release gGamepadPlatformServiceSingleton ouside
|
|
|
|
// the mutex as well as making upcoming GetParentService() call
|
|
|
|
// recreate new singleton, so we use this RefPtr to temporarily
|
|
|
|
// hold the reference, postponing the release process until this
|
|
|
|
// method ends.
|
|
|
|
RefPtr<GamepadPlatformService> kungFuDeathGrip;
|
|
|
|
|
2016-06-28 01:25:00 +03:00
|
|
|
bool isChannelParentEmpty;
|
|
|
|
{
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
|
|
|
isChannelParentEmpty = mChannelParents.IsEmpty();
|
2016-06-28 01:27:00 +03:00
|
|
|
if(isChannelParentEmpty) {
|
|
|
|
kungFuDeathGrip = gGamepadPlatformServiceSingleton;
|
|
|
|
gGamepadPlatformServiceSingleton = nullptr;
|
|
|
|
}
|
2016-06-28 01:25:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GamepadPlatformService::Cleanup()
|
|
|
|
{
|
|
|
|
// This method is called when GamepadPlatformService is
|
|
|
|
// successfully distructed in background thread
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
MutexAutoLock autoLock(mMutex);
|
|
|
|
mChannelParents.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|