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 "MockGamepadProvider.h" |
28 | |
29 | #if ENABLE(GAMEPAD) |
30 | |
31 | #include "GamepadProviderClient.h" |
32 | #include "MockGamepad.h" |
33 | #include <wtf/MainThread.h> |
34 | #include <wtf/NeverDestroyed.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | MockGamepadProvider& MockGamepadProvider::singleton() |
39 | { |
40 | static NeverDestroyed<MockGamepadProvider> sharedProvider; |
41 | return sharedProvider; |
42 | } |
43 | |
44 | MockGamepadProvider::MockGamepadProvider() = default; |
45 | |
46 | void MockGamepadProvider::startMonitoringGamepads(GamepadProviderClient& client) |
47 | { |
48 | ASSERT(!m_clients.contains(&client)); |
49 | m_clients.add(&client); |
50 | } |
51 | |
52 | void MockGamepadProvider::stopMonitoringGamepads(GamepadProviderClient& client) |
53 | { |
54 | ASSERT(m_clients.contains(&client)); |
55 | m_clients.remove(&client); |
56 | } |
57 | |
58 | void MockGamepadProvider::setMockGamepadDetails(unsigned index, const String& gamepadID, unsigned axisCount, unsigned buttonCount) |
59 | { |
60 | if (index >= m_mockGamepadVector.size()) |
61 | m_mockGamepadVector.resize(index + 1); |
62 | |
63 | if (m_mockGamepadVector[index]) |
64 | m_mockGamepadVector[index]->updateDetails(gamepadID, axisCount, buttonCount); |
65 | else |
66 | m_mockGamepadVector[index] = std::make_unique<MockGamepad>(index, gamepadID, axisCount, buttonCount); |
67 | } |
68 | |
69 | bool MockGamepadProvider::connectMockGamepad(unsigned index) |
70 | { |
71 | if (index < m_connectedGamepadVector.size() && m_connectedGamepadVector[index]) { |
72 | LOG_ERROR("MockGamepadProvider: Attempt to connect a fake gamepad that is already connected (%u)" , index); |
73 | return false; |
74 | } |
75 | |
76 | if (index >= m_mockGamepadVector.size() || !m_mockGamepadVector[index]) { |
77 | LOG_ERROR("MockGamepadProvider: Attempt to connect a fake gamepad that doesn't have details set(%u)" , index); |
78 | return false; |
79 | } |
80 | |
81 | if (m_connectedGamepadVector.size() <= index) |
82 | m_connectedGamepadVector.reserveCapacity(index + 1); |
83 | |
84 | while (m_connectedGamepadVector.size() <= index) |
85 | m_connectedGamepadVector.uncheckedAppend(nullptr); |
86 | |
87 | m_connectedGamepadVector[index] = m_mockGamepadVector[index].get(); |
88 | |
89 | for (auto& client : m_clients) |
90 | client->platformGamepadConnected(*m_connectedGamepadVector[index]); |
91 | |
92 | return true; |
93 | } |
94 | |
95 | bool MockGamepadProvider::disconnectMockGamepad(unsigned index) |
96 | { |
97 | if (index >= m_connectedGamepadVector.size() || !m_connectedGamepadVector[index]) { |
98 | LOG_ERROR("MockGamepadProvider: Attempt to disconnect a fake gamepad that is not connected (%u)" , index); |
99 | return false; |
100 | } |
101 | if (m_connectedGamepadVector[index] != m_mockGamepadVector[index].get()) { |
102 | LOG_ERROR("MockGamepadProvider: Vectors of fake gamepads and connected gamepads have gotten out of sync" ); |
103 | return false; |
104 | } |
105 | |
106 | m_connectedGamepadVector[index] = nullptr; |
107 | |
108 | for (auto& client : m_clients) |
109 | client->platformGamepadDisconnected(*m_mockGamepadVector[index]); |
110 | |
111 | return true; |
112 | } |
113 | |
114 | bool MockGamepadProvider::setMockGamepadAxisValue(unsigned index, unsigned axisIndex, double value) |
115 | { |
116 | if (index >= m_mockGamepadVector.size() || !m_mockGamepadVector[index]) { |
117 | LOG_ERROR("MockGamepadProvider: Attempt to set axis value on a fake gamepad that doesn't exist (%u)" , index); |
118 | return false; |
119 | } |
120 | |
121 | m_mockGamepadVector[index]->setAxisValue(axisIndex, value); |
122 | gamepadInputActivity(); |
123 | return true; |
124 | } |
125 | |
126 | bool MockGamepadProvider::setMockGamepadButtonValue(unsigned index, unsigned buttonIndex, double value) |
127 | { |
128 | if (index >= m_mockGamepadVector.size() || !m_mockGamepadVector[index]) { |
129 | LOG_ERROR("MockGamepadProvider: Attempt to set button value on a fake gamepad that doesn't exist (%u)" , index); |
130 | return false; |
131 | } |
132 | |
133 | m_mockGamepadVector[index]->setButtonValue(buttonIndex, value); |
134 | setShouldMakeGamepadsVisibile(); |
135 | gamepadInputActivity(); |
136 | return true; |
137 | } |
138 | |
139 | void MockGamepadProvider::gamepadInputActivity() |
140 | { |
141 | if (!m_shouldScheduleActivityCallback) |
142 | return; |
143 | |
144 | m_shouldScheduleActivityCallback = false; |
145 | callOnMainThread([this]() { |
146 | dispatchPlatformGamepadInputActivity(); |
147 | |
148 | m_shouldScheduleActivityCallback = true; |
149 | }); |
150 | } |
151 | |
152 | } // namespace WebCore |
153 | |
154 | #endif // ENABLE(GAMEPAD) |
155 | |