Index: Source/modules/gamepad/NavigatorGamepad.cpp |
diff --git a/Source/modules/gamepad/NavigatorGamepad.cpp b/Source/modules/gamepad/NavigatorGamepad.cpp |
index 27c1e53322ceece8349fc24a3b5d8f530ab8e7e2..8eae2466c71d00f6da7702e0137e89d3cebb4367 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) |
{ |
+ blink::Platform::current()->setGamepadListener(this); |
} |
NavigatorGamepad::~NavigatorGamepad() |
{ |
+ blink::Platform::current()->setGamepadListener(0); |
} |
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,39 @@ 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); |
+ |
+ RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepadconnected, false, true, gamepad.get()); |
+ frame()->domWindow()->dispatchEvent(event); |
Inactive
2014/03/14 19:46:08
We probably need a null check for the frame
kbalazs
2014/03/14 23:51:27
Yep, null check added for frame() and frame()->dom
|
+} |
+ |
+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->id().characters16() != webGamepad.id) |
Inactive
2014/03/14 19:46:08
Comparison pointers instead of comparing strings?
kbalazs
2014/03/14 23:51:27
Oops, I didn't really thought it over. I don't thi
|
+ gamepad = Gamepad::create(); |
+ sampleGamepad(index, *gamepad, webGamepad); |
+ |
+ RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepaddisconnected, false, true, gamepad); |
+ frame()->domWindow()->dispatchEvent(event); |
Inactive
2014/03/14 19:46:08
We probably need a null check for the frame
kbalazs
2014/03/14 23:51:27
Done.
|
+} |
+ |
} // namespace WebCore |