Index: Source/modules/gamepad/NavigatorGamepad.cpp |
diff --git a/Source/modules/gamepad/NavigatorGamepad.cpp b/Source/modules/gamepad/NavigatorGamepad.cpp |
index 27c1e53322ceece8349fc24a3b5d8f530ab8e7e2..161dd0629734ab2dac6b5f51ed3623f6193b2c0e 100644 |
--- a/Source/modules/gamepad/NavigatorGamepad.cpp |
+++ b/Source/modules/gamepad/NavigatorGamepad.cpp |
@@ -26,7 +26,10 @@ |
#include "config.h" |
#include "modules/gamepad/NavigatorGamepad.h" |
+#include "core/frame/DOMWindow.h" |
+#include "core/frame/LocalFrame.h" |
#include "core/frame/Navigator.h" |
+#include "modules/gamepad/GamepadEvent.h" |
#include "modules/gamepad/GamepadList.h" |
#include "modules/gamepad/WebKitGamepadList.h" |
#include "public/platform/Platform.h" |
@@ -67,12 +70,15 @@ static void sampleGamepads(ListType* into) |
} |
} |
-NavigatorGamepad::NavigatorGamepad() |
+NavigatorGamepad::NavigatorGamepad(LocalFrame* frame) |
+ : DOMWindowProperty(frame) |
+ , DOMWindowLifecycleObserver(frame ? frame->domWindow() : 0) |
{ |
} |
NavigatorGamepad::~NavigatorGamepad() |
{ |
+ 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
|
} |
const char* NavigatorGamepad::supplementName() |
@@ -84,7 +90,7 @@ NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
{ |
NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Navigator>::from(navigator, supplementName())); |
if (!supplement) { |
- supplement = new NavigatorGamepad(); |
+ supplement = new NavigatorGamepad(navigator.frame()); |
provideTo(navigator, supplementName(), adoptPtr(supplement)); |
} |
return *supplement; |
@@ -116,4 +122,52 @@ GamepadList* NavigatorGamepad::gamepads() |
return m_gamepads.get(); |
} |
+void NavigatorGamepad::onGamepadConnected(unsigned index, const blink::WebGamepad& webGamepad) |
+{ |
+ if (index >= blink::WebGamepads::itemsLengthCap || !webGamepad.connected) |
+ return; |
+ |
+ if (!m_gamepads) |
+ m_gamepads = GamepadList::create(); |
+ |
+ RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); |
+ if (!gamepad) |
+ gamepad = Gamepad::create(); |
+ sampleGamepad(index, *gamepad, webGamepad); |
+ m_gamepads->set(index, gamepad); |
+ |
+ if (frame() && frame()->domWindow()) { |
+ RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepadconnected, false, true, gamepad.get()); |
+ frame()->domWindow()->dispatchEvent(event); |
+ } |
+} |
+ |
+void NavigatorGamepad::onGamepadDisconnected(unsigned index, const blink::WebGamepad& webGamepad) |
+{ |
+ if (index >= blink::WebGamepads::itemsLengthCap) |
+ return; |
+ |
+ if (!m_gamepads) |
+ m_gamepads = GamepadList::create(); |
+ |
+ RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); |
+ if (!gamepad) |
+ gamepad = Gamepad::create(); |
+ sampleGamepad(index, *gamepad, webGamepad); |
+ |
+ if (frame() && frame()->domWindow()) { |
+ RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepaddisconnected, false, true, gamepad); |
+ frame()->domWindow()->dispatchEvent(event); |
+ } |
+} |
+ |
+void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& eventType) |
+{ |
+ // If an event listener is added for gamepad events set ourself as listener and sample the gamepads once to start monitoring gamepad state. |
+ if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeNames::gamepaddisconnected) { |
+ 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.
|
+ gamepads(); |
+ } |
+} |
+ |
} // namespace WebCore |