diff options
Diffstat (limited to 'Source/WebKit2/WebProcess/Gamepad')
-rw-r--r-- | Source/WebKit2/WebProcess/Gamepad/WebGamepad.cpp | 75 | ||||
-rw-r--r-- | Source/WebKit2/WebProcess/Gamepad/WebGamepad.h | 54 | ||||
-rw-r--r-- | Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.cpp | 140 | ||||
-rw-r--r-- | Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.h | 68 |
4 files changed, 337 insertions, 0 deletions
diff --git a/Source/WebKit2/WebProcess/Gamepad/WebGamepad.cpp b/Source/WebKit2/WebProcess/Gamepad/WebGamepad.cpp new file mode 100644 index 000000000..4ab1ec744 --- /dev/null +++ b/Source/WebKit2/WebProcess/Gamepad/WebGamepad.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebGamepad.h" + +#if ENABLE(GAMEPAD) + +#include "GamepadData.h" +#include "Logging.h" +#include <wtf/CurrentTime.h> + + +namespace WebKit { + +WebGamepad::WebGamepad(const GamepadData& gamepadData) + : PlatformGamepad(gamepadData.index()) +{ + LOG(Gamepad, "Connecting WebGamepad %u", gamepadData.index()); + + m_id = gamepadData.id(); + m_axisValues.resize(gamepadData.axisValues().size()); + m_buttonValues.resize(gamepadData.buttonValues().size()); + + updateValues(gamepadData); +} + +const Vector<double>& WebGamepad::axisValues() const +{ + return m_axisValues; +} + +const Vector<double>& WebGamepad::buttonValues() const +{ + return m_buttonValues; +} + +void WebGamepad::updateValues(const GamepadData& gamepadData) +{ + ASSERT(!gamepadData.isNull()); + ASSERT(gamepadData.index() == index()); + ASSERT(m_axisValues.size() == gamepadData.axisValues().size()); + ASSERT(m_buttonValues.size() == gamepadData.buttonValues().size()); + + m_axisValues = gamepadData.axisValues(); + m_buttonValues = gamepadData.buttonValues(); + + m_lastUpdateTime = gamepadData.lastUpdateTime(); +} + +} + +#endif // ENABLE(GAMEPAD) diff --git a/Source/WebKit2/WebProcess/Gamepad/WebGamepad.h b/Source/WebKit2/WebProcess/Gamepad/WebGamepad.h new file mode 100644 index 000000000..7a04eefa2 --- /dev/null +++ b/Source/WebKit2/WebProcess/Gamepad/WebGamepad.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if ENABLE(GAMEPAD) + +#include <WebCore/PlatformGamepad.h> + +namespace WebKit { + +class SharedMemory; + +class GamepadData; + +class WebGamepad : public WebCore::PlatformGamepad { +public: + WebGamepad(const GamepadData&); + + const Vector<double>& axisValues() const override; + const Vector<double>& buttonValues() const override; + + void updateValues(const GamepadData&); + +private: + Vector<double> m_axisValues; + Vector<double> m_buttonValues; +}; + +} + +#endif // ENABLE(GAMEPAD) diff --git a/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.cpp b/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.cpp new file mode 100644 index 000000000..73262864f --- /dev/null +++ b/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebGamepadProvider.h" + +#if ENABLE(GAMEPAD) + +#include "GamepadData.h" +#include "WebGamepad.h" +#include "WebProcess.h" +#include "WebProcessPoolMessages.h" +#include <WebCore/GamepadProviderClient.h> +#include <wtf/NeverDestroyed.h> + +using namespace WebCore; + +namespace WebKit { + +WebGamepadProvider& WebGamepadProvider::singleton() +{ + static NeverDestroyed<WebGamepadProvider> provider; + return provider; +} + +WebGamepadProvider::WebGamepadProvider() +{ +} + +WebGamepadProvider::~WebGamepadProvider() +{ +} + +void WebGamepadProvider::setInitialGamepads(const Vector<GamepadData>& gamepadDatas) +{ + ASSERT(m_gamepads.isEmpty()); + + m_gamepads.resize(gamepadDatas.size()); + m_rawGamepads.resize(gamepadDatas.size()); + for (size_t i = 0; i < gamepadDatas.size(); ++i) { + if (gamepadDatas[i].isNull()) + continue; + + m_gamepads[i] = std::make_unique<WebGamepad>(gamepadDatas[i]); + m_rawGamepads[i] = m_gamepads[i].get(); + } +} + +void WebGamepadProvider::gamepadConnected(const GamepadData& gamepadData) +{ + if (m_gamepads.size() <= gamepadData.index()) { + m_gamepads.resize(gamepadData.index() + 1); + m_rawGamepads.resize(gamepadData.index() + 1); + } + + ASSERT(!m_gamepads[gamepadData.index()]); + + m_gamepads[gamepadData.index()] = std::make_unique<WebGamepad>(gamepadData); + m_rawGamepads[gamepadData.index()] = m_gamepads[gamepadData.index()].get(); + + for (auto* client : m_clients) + client->platformGamepadConnected(*m_gamepads[gamepadData.index()]); +} + +void WebGamepadProvider::gamepadDisconnected(unsigned index) +{ + ASSERT(m_gamepads.size() > index); + + std::unique_ptr<WebGamepad> disconnectedGamepad = WTFMove(m_gamepads[index]); + m_rawGamepads[index] = nullptr; + + for (auto* client : m_clients) + client->platformGamepadDisconnected(*disconnectedGamepad); +} + +void WebGamepadProvider::gamepadActivity(const Vector<GamepadData>& gamepadDatas, bool shouldMakeGamepadsVisible) +{ + ASSERT(m_gamepads.size() == gamepadDatas.size()); + + for (size_t i = 0; i < m_gamepads.size(); ++i) { + if (m_gamepads[i]) + m_gamepads[i]->updateValues(gamepadDatas[i]); + } + + for (auto* client : m_clients) + client->platformGamepadInputActivity(shouldMakeGamepadsVisible); +} + +void WebGamepadProvider::startMonitoringGamepads(GamepadProviderClient& client) +{ + bool processHadGamepadClients = !m_clients.isEmpty(); + + ASSERT(!m_clients.contains(&client)); + m_clients.add(&client); + + if (!processHadGamepadClients) + WebProcess::singleton().send(Messages::WebProcessPool::StartedUsingGamepads(), 0); +} + +void WebGamepadProvider::stopMonitoringGamepads(GamepadProviderClient& client) +{ + bool processHadGamepadClients = !m_clients.isEmpty(); + + ASSERT(m_clients.contains(&client)); + m_clients.remove(&client); + + if (processHadGamepadClients && m_clients.isEmpty()) + WebProcess::singleton().send(Messages::WebProcessPool::StoppedUsingGamepads(), 0); +} + +const Vector<PlatformGamepad*>& WebGamepadProvider::platformGamepads() +{ + return m_rawGamepads; +} + +} + +#endif // ENABLE(GAMEPAD) diff --git a/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.h b/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.h new file mode 100644 index 000000000..dc9750a7c --- /dev/null +++ b/Source/WebKit2/WebProcess/Gamepad/WebGamepadProvider.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2016 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if ENABLE(GAMEPAD) + +#include <WebCore/GamepadProvider.h> +#include <wtf/HashSet.h> +#include <wtf/NeverDestroyed.h> + +namespace WebKit { + +class SharedMemory; +class WebGamepad; + +class GamepadData; + +class WebGamepadProvider : public WebCore::GamepadProvider { +public: + static WebGamepadProvider& singleton(); + + void gamepadConnected(const GamepadData&); + void gamepadDisconnected(unsigned index); + void gamepadActivity(const Vector<GamepadData>&, bool shouldMakeGamepadsVisible); + + void setInitialGamepads(const Vector<GamepadData>&); + +private: + friend NeverDestroyed<WebGamepadProvider>; + WebGamepadProvider(); + ~WebGamepadProvider() final; + + void startMonitoringGamepads(WebCore::GamepadProviderClient&) final; + void stopMonitoringGamepads(WebCore::GamepadProviderClient&) final; + const Vector<WebCore::PlatformGamepad*>& platformGamepads() final; + + HashSet<WebCore::GamepadProviderClient*> m_clients; + + Vector<std::unique_ptr<WebGamepad>> m_gamepads; + Vector<WebCore::PlatformGamepad*> m_rawGamepads; +}; + +} // namespace WebKit + +#endif // ENABLE(GAMEPAD) |