750eaefa610788755f01fec4739c6b70687da9e6
[WebKit-https.git] / Source / WebKit2 / UIProcess / Gamepad / UIGamepadProvider.cpp
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "UIGamepadProvider.h"
28
29 #if ENABLE(GAMEPAD)
30
31 #include "GamepadData.h"
32 #include "UIGamepad.h"
33 #include "WebProcessPool.h"
34 #include <WebCore/HIDGamepadProvider.h>
35 #include <wtf/NeverDestroyed.h>
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 static const double maximumGamepadUpdateInterval = 1 / 120.0;
42
43 UIGamepadProvider& UIGamepadProvider::singleton()
44 {
45     static NeverDestroyed<UIGamepadProvider> sharedProvider;
46     return sharedProvider;
47 }
48
49 UIGamepadProvider::UIGamepadProvider()
50     : m_gamepadSyncTimer(RunLoop::main(), this, &UIGamepadProvider::gamepadSyncTimerFired)
51     , m_disableMonitoringTimer(RunLoop::main(), this, &UIGamepadProvider::disableMonitoringTimerFired)
52 {
53 }
54
55 UIGamepadProvider::~UIGamepadProvider()
56 {
57     if (!m_processPoolsUsingGamepads.isEmpty())
58         platformStopMonitoringGamepads();
59 }
60
61 void UIGamepadProvider::gamepadSyncTimerFired()
62 {
63     auto webPageProxy = platformWebPageProxyForGamepadInput();
64     if (!webPageProxy || !m_processPoolsUsingGamepads.contains(&webPageProxy->process().processPool()))
65         return;
66
67     Vector<GamepadData> gamepadDatas;
68     gamepadDatas.reserveInitialCapacity(m_gamepads.size());
69
70     for (auto& gamepad : m_gamepads) {
71         if (gamepad)
72             gamepadDatas.uncheckedAppend(gamepad->gamepadData());
73         else
74             gamepadDatas.uncheckedAppend({ });
75     }
76
77     webPageProxy->gamepadActivity(gamepadDatas);
78 }
79
80 void UIGamepadProvider::scheduleGamepadStateSync()
81 {
82     if (!m_isMonitoringGamepads || m_gamepadSyncTimer.isActive())
83         return;
84
85     if (m_gamepads.isEmpty() || m_processPoolsUsingGamepads.isEmpty()) {
86         m_gamepadSyncTimer.stop();
87         return;
88     }
89
90     m_gamepadSyncTimer.startOneShot(maximumGamepadUpdateInterval);
91 }
92
93 void UIGamepadProvider::platformGamepadConnected(PlatformGamepad& gamepad)
94 {
95     if (m_gamepads.size() <= gamepad.index())
96         m_gamepads.resize(gamepad.index() + 1);
97
98     ASSERT(!m_gamepads[gamepad.index()]);
99     m_gamepads[gamepad.index()] = std::make_unique<UIGamepad>(gamepad);
100
101     scheduleGamepadStateSync();
102
103     for (auto& pool : m_processPoolsUsingGamepads)
104         pool->gamepadConnected(*m_gamepads[gamepad.index()]);
105 }
106
107 void UIGamepadProvider::platformGamepadDisconnected(PlatformGamepad& gamepad)
108 {
109     ASSERT(gamepad.index() < m_gamepads.size());
110     ASSERT(m_gamepads[gamepad.index()]);
111
112     std::unique_ptr<UIGamepad> disconnectedGamepad = WTFMove(m_gamepads[gamepad.index()]);
113
114     scheduleGamepadStateSync();
115
116     for (auto& pool : m_processPoolsUsingGamepads)
117         pool->gamepadDisconnected(*disconnectedGamepad);
118 }
119
120 void UIGamepadProvider::platformGamepadInputActivity()
121 {
122     auto platformGamepads = this->platformGamepads();
123     ASSERT(platformGamepads.size() == m_gamepads.size());
124
125     for (size_t i = 0; i < platformGamepads.size(); ++i) {
126         if (!platformGamepads[i]) {
127             ASSERT(!m_gamepads[i]);
128             continue;
129         }
130
131         ASSERT(m_gamepads[i]);
132         m_gamepads[i]->updateFromPlatformGamepad(*platformGamepads[i]);
133     }
134
135     scheduleGamepadStateSync();
136 }
137
138 void UIGamepadProvider::processPoolStartedUsingGamepads(WebProcessPool& pool)
139 {
140     ASSERT(!m_processPoolsUsingGamepads.contains(&pool));
141     m_processPoolsUsingGamepads.add(&pool);
142
143     if (!m_isMonitoringGamepads && platformWebPageProxyForGamepadInput())
144         startMonitoringGamepads();
145
146     scheduleGamepadStateSync();
147 }
148
149 void UIGamepadProvider::processPoolStoppedUsingGamepads(WebProcessPool& pool)
150 {
151     ASSERT(m_processPoolsUsingGamepads.contains(&pool));
152     m_processPoolsUsingGamepads.remove(&pool);
153
154     if (m_isMonitoringGamepads && !platformWebPageProxyForGamepadInput())
155         scheduleDisableGamepadMonitoring();
156
157     scheduleGamepadStateSync();
158 }
159
160 void UIGamepadProvider::viewBecameActive(WebPageProxy& page)
161 {
162     if (!m_processPoolsUsingGamepads.contains(&page.process().processPool()))
163         return;
164
165     m_disableMonitoringTimer.stop();
166     startMonitoringGamepads();
167 }
168
169 void UIGamepadProvider::viewBecameInactive(WebPageProxy& page)
170 {
171     auto pageForGamepadInput = platformWebPageProxyForGamepadInput();
172     if (pageForGamepadInput == &page)
173         scheduleDisableGamepadMonitoring();
174 }
175
176 void UIGamepadProvider::scheduleDisableGamepadMonitoring()
177 {
178     if (!m_disableMonitoringTimer.isActive())
179         m_disableMonitoringTimer.startOneShot(0);
180 }
181
182 void UIGamepadProvider::disableMonitoringTimerFired()
183 {
184     stopMonitoringGamepads();
185 }
186
187 void UIGamepadProvider::startMonitoringGamepads()
188 {
189     if (m_isMonitoringGamepads)
190         return;
191
192     m_isMonitoringGamepads = true;
193     platformStartMonitoringGamepads();
194 }
195
196 void UIGamepadProvider::stopMonitoringGamepads()
197 {
198     if (!m_isMonitoringGamepads)
199         return;
200
201     m_isMonitoringGamepads = false;
202     platformStopMonitoringGamepads();
203 }
204
205 #if !PLATFORM(MAC)
206
207 void UIGamepadProvider::platformStartMonitoringGamepads()
208 {
209     // FIXME: Implement for other platforms
210 }
211
212 void UIGamepadProvider::platformStopMonitoringGamepads()
213 {
214     // FIXME: Implement for other platforms
215 }
216
217 const Vector<PlatformGamepad*>& UIGamepadProvider::platformGamepads()
218 {
219     static NeverDestroyed<Vector<PlatformGamepad*>> emptyGamepads;
220     return emptyGamepads;
221
222     // FIXME: Implement for other platforms
223 }
224
225 WebProcessProxy* UIGamepadProvider::platformWebProcessProxyForGamepadInput()
226 {
227     // FIXME: Implement for other platforms
228 }
229
230 #endif // !PLATFORM(MAC)
231
232 }
233
234 #endif // ENABLE(GAMEPAD)