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" |
33 #include "wtf/PassOwnPtr.h" | 36 #include "wtf/PassOwnPtr.h" |
34 | 37 |
35 namespace WebCore { | 38 namespace WebCore { |
36 | 39 |
37 template<typename T> | 40 template<typename T> |
38 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) | 41 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) |
39 { | 42 { |
(...skipping 20 matching lines...) Expand all Loading... | |
60 if (!gamepad) | 63 if (!gamepad) |
61 gamepad = GamepadType::create(); | 64 gamepad = GamepadType::create(); |
62 sampleGamepad(i, *gamepad, webGamepad); | 65 sampleGamepad(i, *gamepad, webGamepad); |
63 into->set(i, gamepad); | 66 into->set(i, gamepad); |
64 } else { | 67 } else { |
65 into->set(i, nullptr); | 68 into->set(i, nullptr); |
66 } | 69 } |
67 } | 70 } |
68 } | 71 } |
69 | 72 |
70 NavigatorGamepad::NavigatorGamepad() | 73 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame) |
74 : DOMWindowProperty(frame) | |
75 , DOMWindowLifecycleObserver(frame ? frame->domWindow() : 0) | |
71 { | 76 { |
72 } | 77 } |
73 | 78 |
74 NavigatorGamepad::~NavigatorGamepad() | 79 NavigatorGamepad::~NavigatorGamepad() |
75 { | 80 { |
81 blink::Platform::current()->setGamepadListener(0); | |
abarth-chromium
2014/03/17 18:23:46
This line sets a static field in the embedder. Ho
kbalazs
2014/03/17 23:49:27
Added a singleton mediator that implements WebGame
| |
76 } | 82 } |
77 | 83 |
78 const char* NavigatorGamepad::supplementName() | 84 const char* NavigatorGamepad::supplementName() |
79 { | 85 { |
80 return "NavigatorGamepad"; | 86 return "NavigatorGamepad"; |
81 } | 87 } |
82 | 88 |
83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) | 89 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
84 { | 90 { |
85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); | 91 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); |
86 if (!supplement) { | 92 if (!supplement) { |
87 supplement = new NavigatorGamepad(); | 93 supplement = new NavigatorGamepad(navigator.frame()); |
88 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 94 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
89 } | 95 } |
90 return *supplement; | 96 return *supplement; |
91 } | 97 } |
92 | 98 |
93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) | 99 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) |
94 { | 100 { |
95 return NavigatorGamepad::from(navigator).webkitGamepads(); | 101 return NavigatorGamepad::from(navigator).webkitGamepads(); |
96 } | 102 } |
97 | 103 |
(...skipping 11 matching lines...) Expand all Loading... | |
109 } | 115 } |
110 | 116 |
111 GamepadList* NavigatorGamepad::gamepads() | 117 GamepadList* NavigatorGamepad::gamepads() |
112 { | 118 { |
113 if (!m_gamepads) | 119 if (!m_gamepads) |
114 m_gamepads = GamepadList::create(); | 120 m_gamepads = GamepadList::create(); |
115 sampleGamepads<Gamepad>(m_gamepads.get()); | 121 sampleGamepads<Gamepad>(m_gamepads.get()); |
116 return m_gamepads.get(); | 122 return m_gamepads.get(); |
117 } | 123 } |
118 | 124 |
125 void NavigatorGamepad::onGamepadConnected(unsigned index, const blink::WebGamepa d& webGamepad) | |
126 { | |
127 if (index >= blink::WebGamepads::itemsLengthCap || !webGamepad.connected) | |
128 return; | |
129 | |
130 if (!m_gamepads) | |
131 m_gamepads = GamepadList::create(); | |
132 | |
133 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
134 if (!gamepad) | |
135 gamepad = Gamepad::create(); | |
136 sampleGamepad(index, *gamepad, webGamepad); | |
137 m_gamepads->set(index, gamepad); | |
138 | |
139 if (frame() && frame()->domWindow()) { | |
140 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepa dconnected, false, true, gamepad.get()); | |
141 frame()->domWindow()->dispatchEvent(event); | |
142 } | |
143 } | |
144 | |
145 void NavigatorGamepad::onGamepadDisconnected(unsigned index, const blink::WebGam epad& webGamepad) | |
146 { | |
147 if (index >= blink::WebGamepads::itemsLengthCap) | |
148 return; | |
149 | |
150 if (!m_gamepads) | |
151 m_gamepads = GamepadList::create(); | |
152 | |
153 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
154 if (!gamepad) | |
155 gamepad = Gamepad::create(); | |
156 sampleGamepad(index, *gamepad, webGamepad); | |
157 | |
158 if (frame() && frame()->domWindow()) { | |
159 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepa ddisconnected, false, true, gamepad); | |
160 frame()->domWindow()->dispatchEvent(event); | |
161 } | |
162 } | |
163 | |
164 void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& event Type) | |
165 { | |
166 // If an event listener is added for gamepad events set ourself as listener and sample the gamepads once to start monitoring gamepad state. | |
167 if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeN ames::gamepaddisconnected) { | |
168 blink::Platform::current()->setGamepadListener(this); | |
abarth-chromium
2014/03/17 18:23:46
Again, this sets a static field, which doesn't see
kbalazs
2014/03/17 23:49:27
Done.
| |
169 gamepads(); | |
170 } | |
171 } | |
172 | |
119 } // namespace WebCore | 173 } // namespace WebCore |
OLD | NEW |