Cleanup WK2 platform gamepad handling.
[WebKit-https.git] / Source / WebKit2 / UIProcess / Gamepad / UIGamepadProvider.cpp
index 750eaef..2046bf9 100644 (file)
@@ -48,7 +48,6 @@ UIGamepadProvider& UIGamepadProvider::singleton()
 
 UIGamepadProvider::UIGamepadProvider()
     : m_gamepadSyncTimer(RunLoop::main(), this, &UIGamepadProvider::gamepadSyncTimerFired)
-    , m_disableMonitoringTimer(RunLoop::main(), this, &UIGamepadProvider::disableMonitoringTimerFired)
 {
 }
 
@@ -64,17 +63,7 @@ void UIGamepadProvider::gamepadSyncTimerFired()
     if (!webPageProxy || !m_processPoolsUsingGamepads.contains(&webPageProxy->process().processPool()))
         return;
 
-    Vector<GamepadData> gamepadDatas;
-    gamepadDatas.reserveInitialCapacity(m_gamepads.size());
-
-    for (auto& gamepad : m_gamepads) {
-        if (gamepad)
-            gamepadDatas.uncheckedAppend(gamepad->gamepadData());
-        else
-            gamepadDatas.uncheckedAppend({ });
-    }
-
-    webPageProxy->gamepadActivity(gamepadDatas);
+    webPageProxy->gamepadActivity(snapshotGamepads());
 }
 
 void UIGamepadProvider::scheduleGamepadStateSync()
@@ -90,6 +79,24 @@ void UIGamepadProvider::scheduleGamepadStateSync()
     m_gamepadSyncTimer.startOneShot(maximumGamepadUpdateInterval);
 }
 
+void UIGamepadProvider::setInitialConnectedGamepads(const Vector<PlatformGamepad*>& initialGamepads)
+{
+    ASSERT(!m_hasInitialGamepads);
+
+    m_gamepads.resize(initialGamepads.size());
+
+    for (auto* gamepad : initialGamepads) {
+        if (!gamepad)
+            continue;
+        m_gamepads[gamepad->index()] = std::make_unique<UIGamepad>(*gamepad);
+    }
+
+    for (auto& pool : m_processPoolsUsingGamepads)
+        pool->setInitialConnectedGamepads(m_gamepads);
+
+    m_hasInitialGamepads = true;
+}
+
 void UIGamepadProvider::platformGamepadConnected(PlatformGamepad& gamepad)
 {
     if (m_gamepads.size() <= gamepad.index())
@@ -152,7 +159,7 @@ void UIGamepadProvider::processPoolStoppedUsingGamepads(WebProcessPool& pool)
     m_processPoolsUsingGamepads.remove(&pool);
 
     if (m_isMonitoringGamepads && !platformWebPageProxyForGamepadInput())
-        scheduleDisableGamepadMonitoring();
+        platformStopMonitoringInput();
 
     scheduleGamepadStateSync();
 }
@@ -162,26 +169,18 @@ void UIGamepadProvider::viewBecameActive(WebPageProxy& page)
     if (!m_processPoolsUsingGamepads.contains(&page.process().processPool()))
         return;
 
-    m_disableMonitoringTimer.stop();
-    startMonitoringGamepads();
+    if (!m_isMonitoringGamepads)
+        startMonitoringGamepads();
+
+    if (platformWebPageProxyForGamepadInput())
+        platformStartMonitoringInput();
 }
 
 void UIGamepadProvider::viewBecameInactive(WebPageProxy& page)
 {
     auto pageForGamepadInput = platformWebPageProxyForGamepadInput();
     if (pageForGamepadInput == &page)
-        scheduleDisableGamepadMonitoring();
-}
-
-void UIGamepadProvider::scheduleDisableGamepadMonitoring()
-{
-    if (!m_disableMonitoringTimer.isActive())
-        m_disableMonitoringTimer.startOneShot(0);
-}
-
-void UIGamepadProvider::disableMonitoringTimerFired()
-{
-    stopMonitoringGamepads();
+        platformStopMonitoringInput();
 }
 
 void UIGamepadProvider::startMonitoringGamepads()
@@ -200,6 +199,22 @@ void UIGamepadProvider::stopMonitoringGamepads()
 
     m_isMonitoringGamepads = false;
     platformStopMonitoringGamepads();
+    m_gamepads.clear();
+}
+
+Vector<GamepadData> UIGamepadProvider::snapshotGamepads()
+{
+    Vector<GamepadData> gamepadDatas;
+    gamepadDatas.reserveInitialCapacity(m_gamepads.size());
+
+    for (auto& gamepad : m_gamepads) {
+        if (gamepad)
+            gamepadDatas.uncheckedAppend(gamepad->gamepadData());
+        else
+            gamepadDatas.uncheckedAppend({ });
+    }
+
+    return gamepadDatas;
 }
 
 #if !PLATFORM(MAC)
@@ -227,6 +242,14 @@ WebProcessProxy* UIGamepadProvider::platformWebProcessProxyForGamepadInput()
     // FIXME: Implement for other platforms
 }
 
+void UIGamepadProvider::platformStopMonitoringInput()
+{
+}
+
+void UIGamepadProvider::platformStartMonitoringInput()
+{
+}
+
 #endif // !PLATFORM(MAC)
 
 }