@@ -50,6 +50,7 @@ const nsTArray<RefPtr<nsGlobalWindow>>::index_type NoIndex =
5050bool sShutdown = false ;
5151
5252StaticRefPtr<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+
191217void
192218GamepadManager::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
214243void
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
227258void
@@ -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) {
0 commit comments