OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011, Google Inc. All rights reserved. | 2 * Copyright (C) 2011, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are met: | 5 * modification, are permitted provided that the following conditions are met: |
6 * | 6 * |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE | 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
23 * DAMAGE. | 23 * DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "modules/gamepad/NavigatorGamepad.h" | 27 #include "modules/gamepad/NavigatorGamepad.h" |
28 | 28 |
29 #include "core/frame/DOMWindow.h" | |
30 #include "core/frame/LocalFrame.h" | |
29 #include "core/frame/Navigator.h" | 31 #include "core/frame/Navigator.h" |
32 #include "modules/gamepad/GamepadEvent.h" | |
30 #include "modules/gamepad/GamepadList.h" | 33 #include "modules/gamepad/GamepadList.h" |
31 #include "modules/gamepad/WebKitGamepadList.h" | 34 #include "modules/gamepad/WebKitGamepadList.h" |
32 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
36 #include "public/platform/WebGamepadListener.h" | |
37 #include "wtf/HashSet.h" | |
33 #include "wtf/PassOwnPtr.h" | 38 #include "wtf/PassOwnPtr.h" |
34 | 39 |
35 namespace WebCore { | 40 namespace WebCore { |
36 | 41 |
42 class GlobalGamepadListener : public blink::WebGamepadListener { | |
abarth-chromium
2014/03/19 18:00:33
Can you put this class in its own file? For consi
| |
43 public: | |
44 GlobalGamepadListener(); | |
45 | |
46 void addClient(NavigatorGamepad* observer); | |
47 void removeClient(NavigatorGamepad* observer); | |
48 | |
49 private: | |
50 virtual void didConnectGamepad(unsigned index, const blink::WebGamepad&) OVE RRIDE; | |
51 virtual void didDisconnectGamepad(unsigned index, const blink::WebGamepad&) OVERRIDE; | |
52 | |
53 typedef WTF::HashSet<NavigatorGamepad*> ClientSet; | |
54 ClientSet m_clients; | |
55 }; | |
56 | |
57 GlobalGamepadListener::GlobalGamepadListener() | |
58 { | |
59 blink::Platform::current()->setGamepadListener(this); | |
abarth-chromium
2014/03/19 18:00:33
If you look at how DeviceOrientationDispatcher, yo
kbalazs
2014/03/20 18:57:21
Good point. After looking at orientation and motio
| |
60 } | |
61 | |
62 void GlobalGamepadListener::addClient(NavigatorGamepad* observer) | |
63 { | |
64 m_clients.add(observer); | |
65 } | |
66 | |
67 void GlobalGamepadListener::removeClient(NavigatorGamepad* observer) | |
68 { | |
69 m_clients.remove(observer); | |
70 } | |
71 | |
72 void GlobalGamepadListener::didConnectGamepad(unsigned index, const blink::WebGa mepad& gamepad) | |
73 { | |
74 for (ClientSet::iterator it = m_clients.begin(), end = m_clients.end(); it ! = end; ++it) | |
75 (*it)->didConnectGamepad(index, gamepad); | |
abarth-chromium
2014/03/19 18:00:33
What stops m_clients from being mutated during thi
kbalazs
2014/03/20 18:57:21
Solved by using DeviceSensorDispatcher and DeviceS
| |
76 } | |
77 | |
78 void GlobalGamepadListener::didDisconnectGamepad(unsigned index, const blink::We bGamepad& gamepad) | |
79 { | |
80 for (ClientSet::iterator it = m_clients.begin(), end = m_clients.end(); it ! = end; ++it) | |
81 (*it)->didDisconnectGamepad(index, gamepad); | |
82 } | |
83 | |
84 static GlobalGamepadListener& globalListener() | |
85 { | |
86 DEFINE_STATIC_LOCAL(GlobalGamepadListener, listener, ()); | |
87 return listener; | |
88 } | |
89 | |
37 template<typename T> | 90 template<typename T> |
38 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) | 91 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) |
39 { | 92 { |
40 gamepad.setId(webGamepad.id); | 93 gamepad.setId(webGamepad.id); |
41 gamepad.setIndex(index); | 94 gamepad.setIndex(index); |
42 gamepad.setConnected(webGamepad.connected); | 95 gamepad.setConnected(webGamepad.connected); |
43 gamepad.setTimestamp(webGamepad.timestamp); | 96 gamepad.setTimestamp(webGamepad.timestamp); |
44 gamepad.setMapping(webGamepad.mapping); | 97 gamepad.setMapping(webGamepad.mapping); |
45 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); | 98 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); |
46 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); | 99 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); |
(...skipping 13 matching lines...) Expand all Loading... | |
60 if (!gamepad) | 113 if (!gamepad) |
61 gamepad = GamepadType::create(); | 114 gamepad = GamepadType::create(); |
62 sampleGamepad(i, *gamepad, webGamepad); | 115 sampleGamepad(i, *gamepad, webGamepad); |
63 into->set(i, gamepad); | 116 into->set(i, gamepad); |
64 } else { | 117 } else { |
65 into->set(i, nullptr); | 118 into->set(i, nullptr); |
66 } | 119 } |
67 } | 120 } |
68 } | 121 } |
69 | 122 |
70 NavigatorGamepad::NavigatorGamepad() | 123 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame) |
124 : DOMWindowLifecycleObserver(frame ? frame->domWindow() : 0) | |
71 { | 125 { |
72 } | 126 } |
73 | 127 |
74 NavigatorGamepad::~NavigatorGamepad() | 128 NavigatorGamepad::~NavigatorGamepad() |
75 { | 129 { |
130 globalListener().removeClient(this); | |
abarth-chromium
2014/03/19 18:00:33
The problem with only calling removeClient in the
kbalazs
2014/03/20 18:57:21
Orientation and motion unregisters when the event
| |
76 } | 131 } |
77 | 132 |
78 const char* NavigatorGamepad::supplementName() | 133 const char* NavigatorGamepad::supplementName() |
79 { | 134 { |
80 return "NavigatorGamepad"; | 135 return "NavigatorGamepad"; |
81 } | 136 } |
82 | 137 |
83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) | 138 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
84 { | 139 { |
85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); | 140 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); |
86 if (!supplement) { | 141 if (!supplement) { |
87 supplement = new NavigatorGamepad(); | 142 supplement = new NavigatorGamepad(navigator.frame()); |
88 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 143 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
89 } | 144 } |
90 return *supplement; | 145 return *supplement; |
91 } | 146 } |
92 | 147 |
93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) | 148 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) |
94 { | 149 { |
95 return NavigatorGamepad::from(navigator).webkitGamepads(); | 150 return NavigatorGamepad::from(navigator).webkitGamepads(); |
96 } | 151 } |
97 | 152 |
(...skipping 11 matching lines...) Expand all Loading... | |
109 } | 164 } |
110 | 165 |
111 GamepadList* NavigatorGamepad::gamepads() | 166 GamepadList* NavigatorGamepad::gamepads() |
112 { | 167 { |
113 if (!m_gamepads) | 168 if (!m_gamepads) |
114 m_gamepads = GamepadList::create(); | 169 m_gamepads = GamepadList::create(); |
115 sampleGamepads<Gamepad>(m_gamepads.get()); | 170 sampleGamepads<Gamepad>(m_gamepads.get()); |
116 return m_gamepads.get(); | 171 return m_gamepads.get(); |
117 } | 172 } |
118 | 173 |
174 void NavigatorGamepad::didConnectGamepad(unsigned index, const blink::WebGamepad & webGamepad) | |
175 { | |
176 if (index >= blink::WebGamepads::itemsLengthCap || !webGamepad.connected) | |
177 return; | |
abarth-chromium
2014/03/19 18:00:33
How could index >= blink::WebGamepads::itemsLength
kbalazs
2014/03/20 18:57:21
Done.
| |
178 | |
179 if (!m_gamepads) | |
180 m_gamepads = GamepadList::create(); | |
181 | |
182 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
183 if (!gamepad) | |
184 gamepad = Gamepad::create(); | |
185 sampleGamepad(index, *gamepad, webGamepad); | |
186 m_gamepads->set(index, gamepad); | |
187 | |
188 if (window()) { | |
abarth-chromium
2014/03/19 18:00:33
Do we want to do the work above if we don't have a
kbalazs
2014/03/20 18:57:21
Done.
| |
189 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepa dconnected, false, true, gamepad.get()); | |
190 window()->dispatchEvent(event); | |
191 } | |
192 } | |
193 | |
194 void NavigatorGamepad::didDisconnectGamepad(unsigned index, const blink::WebGame pad& webGamepad) | |
195 { | |
196 if (index >= blink::WebGamepads::itemsLengthCap) | |
197 return; | |
198 | |
199 if (!m_gamepads) | |
200 m_gamepads = GamepadList::create(); | |
201 | |
202 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
203 if (!gamepad) | |
204 gamepad = Gamepad::create(); | |
205 sampleGamepad(index, *gamepad, webGamepad); | |
206 | |
207 if (window()) { | |
208 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepa ddisconnected, false, true, gamepad.get()); | |
209 window()->dispatchEvent(event); | |
210 } | |
211 } | |
212 | |
213 void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& event Type) | |
214 { | |
215 if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeN ames::gamepaddisconnected) | |
216 globalListener() | |
217 } | |
218 | |
119 } // namespace WebCore | 219 } // namespace WebCore |
OLD | NEW |