Skip to content

Commit a5c0fea

Browse files
committed
Bug 1299928 - Part 1: Make GamepadManager knows the gamepad is from VRController when adding gamepads; r=kip,lenzak800
MozReview-Commit-ID: IBFqj2JTHxh --HG-- extra : rebase_source : 7716900c17bedfef5b1dff10229f377e17ccb173
1 parent d9d9880 commit a5c0fea

11 files changed

+97
-17
lines changed

dom/gamepad/Gamepad.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ const int kLeftStickYAxis = 1;
3333
const int kRightStickXAxis = 2;
3434
const int kRightStickYAxis = 3;
3535

36+
// Standard channel is used for managing gamepads that
37+
// are from GamepadPlatformService. VR channel
38+
// is for gamepads that are from VRManagerChild.
39+
enum class GamepadServiceType : uint16_t {
40+
Standard,
41+
VR,
42+
NumGamepadServiceType
43+
};
44+
3645
class Gamepad final : public nsISupports,
3746
public nsWrapperCache
3847
{

dom/gamepad/GamepadManager.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const nsTArray<RefPtr<nsGlobalWindow>>::index_type NoIndex =
5050
bool sShutdown = false;
5151

5252
StaticRefPtr<GamepadManager> gGamepadManagerSingleton;
53+
const uint32_t VR_GAMEPAD_IDX_OFFSET = 0x01 << 16;
5354

5455
} // namespace
5556

@@ -188,10 +189,36 @@ GamepadManager::GetGamepad(uint32_t aIndex) const
188189
return nullptr;
189190
}
190191

192+
uint32_t GamepadManager::GetGamepadIndexWithServiceType(uint32_t aIndex,
193+
GamepadServiceType aServiceType)
194+
{
195+
uint32_t newIndex = 0;
196+
197+
switch (aServiceType) {
198+
case GamepadServiceType::Standard:
199+
{
200+
MOZ_ASSERT(aIndex <= VR_GAMEPAD_IDX_OFFSET);
201+
newIndex = aIndex;
202+
break;
203+
}
204+
case GamepadServiceType::VR:
205+
{
206+
newIndex = aIndex + VR_GAMEPAD_IDX_OFFSET;
207+
break;
208+
}
209+
default:
210+
MOZ_ASSERT(false);
211+
break;
212+
}
213+
214+
return newIndex;
215+
}
216+
191217
void
192218
GamepadManager::AddGamepad(uint32_t aIndex,
193219
const nsAString& aId,
194220
GamepadMappingType aMapping,
221+
GamepadServiceType aServiceType,
195222
uint32_t aNumButtons,
196223
uint32_t aNumAxes)
197224
{
@@ -204,24 +231,28 @@ GamepadManager::AddGamepad(uint32_t aIndex,
204231
aNumButtons,
205232
aNumAxes);
206233

234+
uint32_t newIndex = GetGamepadIndexWithServiceType(aIndex, aServiceType);
235+
207236
// We store the gamepad related to its index given by the parent process,
208237
// and no duplicate index is allowed.
209-
MOZ_ASSERT(!mGamepads.Get(aIndex, nullptr));
210-
mGamepads.Put(aIndex, gamepad);
211-
NewConnectionEvent(aIndex, true);
238+
MOZ_ASSERT(!mGamepads.Get(newIndex, nullptr));
239+
mGamepads.Put(newIndex, gamepad);
240+
NewConnectionEvent(newIndex, true);
212241
}
213242

214243
void
215-
GamepadManager::RemoveGamepad(uint32_t aIndex)
244+
GamepadManager::RemoveGamepad(uint32_t aIndex, GamepadServiceType aServiceType)
216245
{
217-
RefPtr<Gamepad> gamepad = GetGamepad(aIndex);
246+
uint32_t newIndex = GetGamepadIndexWithServiceType(aIndex, aServiceType);
247+
248+
RefPtr<Gamepad> gamepad = GetGamepad(newIndex);
218249
if (!gamepad) {
219250
NS_WARNING("Trying to delete gamepad with invalid index");
220251
return;
221252
}
222253
gamepad->SetConnected(false);
223-
NewConnectionEvent(aIndex, false);
224-
mGamepads.Remove(aIndex);
254+
NewConnectionEvent(newIndex, false);
255+
mGamepads.Remove(newIndex);
225256
}
226257

227258
void
@@ -544,13 +575,13 @@ GamepadManager::Update(const GamepadChangeEvent& aEvent)
544575
if (aEvent.type() == GamepadChangeEvent::TGamepadAdded) {
545576
const GamepadAdded& a = aEvent.get_GamepadAdded();
546577
AddGamepad(a.index(), a.id(),
547-
static_cast<GamepadMappingType>(a.mapping()),
578+
a.mapping(), a.service_type(),
548579
a.num_buttons(), a.num_axes());
549580
return;
550581
}
551582
if (aEvent.type() == GamepadChangeEvent::TGamepadRemoved) {
552583
const GamepadRemoved& a = aEvent.get_GamepadRemoved();
553-
RemoveGamepad(a.index());
584+
RemoveGamepad(a.index(), a.service_type());
554585
return;
555586
}
556587
if (aEvent.type() == GamepadChangeEvent::TGamepadButtonInformation) {

dom/gamepad/GamepadManager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ class GamepadManager final : public nsIObserver,
4747

4848
// Add a gamepad to the list of known gamepads.
4949
void AddGamepad(uint32_t aIndex, const nsAString& aID, GamepadMappingType aMapping,
50-
uint32_t aNumButtons, uint32_t aNumAxes);
50+
GamepadServiceType aServiceType, uint32_t aNumButtons, uint32_t aNumAxes);
5151

5252
// Remove the gamepad at |aIndex| from the list of known gamepads.
53-
void RemoveGamepad(uint32_t aIndex);
53+
void RemoveGamepad(uint32_t aIndex, GamepadServiceType aServiceType);
5454

5555
// Update the state of |aButton| for the gamepad at |aIndex| for all
5656
// windows that are listening and visible, and fire one of
@@ -127,6 +127,9 @@ class GamepadManager final : public nsIObserver,
127127
// Indicate that a window has received data from a gamepad.
128128
void SetWindowHasSeenGamepad(nsGlobalWindow* aWindow, uint32_t aIndex,
129129
bool aHasSeen = true);
130+
// Our gamepad index has VR_GAMEPAD_IDX_OFFSET while GamepadChannelType
131+
// is from VRManager.
132+
uint32_t GetGamepadIndexWithServiceType(uint32_t aIndex, GamepadServiceType aServiceType);
130133

131134
// Gamepads connected to the system. Copies of these are handed out
132135
// to each window.

dom/gamepad/GamepadPlatformService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ GamepadPlatformService::AddGamepad(const char* aID,
8787

8888
uint32_t index = ++mGamepadIndex;
8989
GamepadAdded a(NS_ConvertUTF8toUTF16(nsDependentCString(aID)), index,
90-
(uint32_t)aMapping, aNumButtons, aNumAxes);
90+
aMapping, GamepadServiceType::Standard, aNumButtons, aNumAxes);
9191
NotifyGamepadChange<GamepadAdded>(a);
9292
return index;
9393
}
@@ -99,7 +99,7 @@ GamepadPlatformService::RemoveGamepad(uint32_t aIndex)
9999
// platform-dependent backends
100100
MOZ_ASSERT(XRE_IsParentProcess());
101101
MOZ_ASSERT(!NS_IsMainThread());
102-
GamepadRemoved a(aIndex);
102+
GamepadRemoved a(aIndex, GamepadServiceType::Standard);
103103
NotifyGamepadChange<GamepadRemoved>(a);
104104
}
105105

dom/gamepad/GamepadServiceTest.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ GamepadServiceTest::AddGamepad(const nsAString& aID,
122122
return nullptr;
123123
}
124124

125+
// Because GamepadServiceTest::AddGamepad() is opened for Web API,
126+
// we need to convert aMapping from uint32_t to GamepadMappingType here.
125127
GamepadAdded a(nsString(aID), 0,
126-
(uint32_t)aMapping, aNumButtons, aNumAxes);
128+
static_cast<GamepadMappingType>(aMapping),
129+
GamepadServiceType::Standard,
130+
aNumButtons, aNumAxes);
127131
GamepadChangeEvent e(a);
128132
nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(mWindow);
129133

@@ -150,7 +154,7 @@ GamepadServiceTest::RemoveGamepad(uint32_t aIndex)
150154
return;
151155
}
152156

153-
GamepadRemoved a(aIndex);
157+
GamepadRemoved a(aIndex, GamepadServiceType::Standard);
154158
GamepadChangeEvent e(a);
155159

156160
uint32_t id = ++mEventNumber;

dom/gamepad/GamepadServiceTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define mozilla_dom_GamepadServiceTest_h_
99

1010
#include "nsIIPCBackgroundChildCreateCallback.h"
11+
#include "mozilla/dom/GamepadBinding.h"
1112

1213
namespace mozilla {
1314
namespace dom {

dom/gamepad/ipc/GamepadEventTypes.ipdlh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
using mozilla::dom::GamepadMappingType from "mozilla/dom/GamepadMessageUtils.h";
6+
using mozilla::dom::GamepadServiceType from "mozilla/dom/GamepadMessageUtils.h";
7+
8+
59
namespace mozilla {
610
namespace dom {
711

812
struct GamepadAdded {
913
nsString id;
1014
uint32_t index;
11-
uint32_t mapping;
15+
GamepadMappingType mapping;
16+
GamepadServiceType service_type;
1217
uint32_t num_buttons;
1318
uint32_t num_axes;
1419
};
1520

1621
struct GamepadRemoved {
1722
uint32_t index;
23+
GamepadServiceType service_type;
1824
};
1925

2026
struct GamepadAxisInformation {

dom/gamepad/ipc/GamepadMessageUtils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#ifndef mozilla_dom_gamepad_GamepadMessageUtils_h
3+
#define mozilla_dom_gamepad_GamepadMessageUtils_h
4+
5+
#include "ipc/IPCMessageUtils.h"
6+
#include "mozilla/dom/Gamepad.h"
7+
8+
namespace IPC {
9+
10+
template<>
11+
struct ParamTraits<mozilla::dom::GamepadMappingType> :
12+
public ContiguousEnumSerializer<mozilla::dom::GamepadMappingType,
13+
mozilla::dom::GamepadMappingType(mozilla::dom::GamepadMappingType::_empty),
14+
mozilla::dom::GamepadMappingType(mozilla::dom::GamepadMappingType::EndGuard_)> {};
15+
16+
template<>
17+
struct ParamTraits<mozilla::dom::GamepadServiceType> :
18+
public ContiguousEnumSerializer<mozilla::dom::GamepadServiceType,
19+
mozilla::dom::GamepadServiceType(0),
20+
mozilla::dom::GamepadServiceType(
21+
mozilla::dom::GamepadServiceType::NumGamepadServiceType)> {};
22+
} // namespace IPC
23+
24+
#endif // mozilla_dom_gamepad_GamepadMessageUtils_h

dom/gamepad/ipc/GamepadTestChannelParent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GamepadTestChannelParent::RecvGamepadTestEvent(const uint32_t& aID,
2323
nsCString gamepadID;
2424
LossyCopyUTF16toASCII(a.id(), gamepadID);
2525
uint32_t index = service->AddGamepad(gamepadID.get(),
26-
(GamepadMappingType)a.mapping(),
26+
a.mapping(),
2727
a.num_buttons(),
2828
a.num_axes());
2929
if (!mShuttingdown) {

dom/gamepad/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if CONFIG['MOZ_GAMEPAD']:
2020
'GamepadServiceTest.h',
2121
'ipc/GamepadEventChannelChild.h',
2222
'ipc/GamepadEventChannelParent.h',
23+
'ipc/GamepadMessageUtils.h',
2324
'ipc/GamepadTestChannelChild.h',
2425
'ipc/GamepadTestChannelParent.h'
2526
]

gfx/vr/ipc/VRManagerChild.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void ReleaseVRManagerParentSingleton() {
3737
VRManagerChild::VRManagerChild()
3838
: TextureForwarder()
3939
, mDisplaysInitialized(false)
40+
, mGamepadManager(nullptr)
4041
, mInputFrameID(-1)
4142
, mMessageLoop(MessageLoop::current())
4243
, mFrameRequestCallbackCounter(0)

0 commit comments

Comments
 (0)