This source file includes following definitions.
- sampleGamepad
- sampleGamepads
- from
- from
- webkitGetGamepads
- getGamepads
- webkitGamepads
- gamepads
- trace
- didConnectOrDisconnectGamepad
- DOMWindowLifecycleObserver
- supplementName
- willDestroyGlobalObjectInFrame
- willDetachGlobalObjectFromFrame
- registerWithDispatcher
- unregisterWithDispatcher
- hasLastData
- getLastEvent
- isNullEvent
- didAddEventListener
- didRemoveEventListener
- didRemoveAllEventListeners
#include "config.h"
#include "modules/gamepad/NavigatorGamepad.h"
#include "RuntimeEnabledFeatures.h"
#include "core/dom/Document.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Navigator.h"
#include "core/page/Page.h"
#include "modules/gamepad/GamepadDispatcher.h"
#include "modules/gamepad/GamepadEvent.h"
#include "modules/gamepad/GamepadList.h"
#include "modules/gamepad/WebKitGamepadList.h"
namespace WebCore {
template<typename T>
static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& webGamepad)
{
gamepad.setId(webGamepad.id);
gamepad.setIndex(index);
gamepad.setConnected(webGamepad.connected);
gamepad.setTimestamp(webGamepad.timestamp);
gamepad.setMapping(webGamepad.mapping);
gamepad.setAxes(webGamepad.axesLength, webGamepad.axes);
gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons);
}
template<typename GamepadType, typename ListType>
static void sampleGamepads(ListType* into)
{
blink::WebGamepads gamepads;
GamepadDispatcher::instance().sampleGamepads(gamepads);
for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
blink::WebGamepad& webGamepad = gamepads.items[i];
if (i < gamepads.length && webGamepad.connected) {
RefPtrWillBeRawPtr<GamepadType> gamepad = into->item(i);
if (!gamepad)
gamepad = GamepadType::create();
sampleGamepad(i, *gamepad, webGamepad);
into->set(i, gamepad);
} else {
into->set(i, nullptr);
}
}
}
NavigatorGamepad* NavigatorGamepad::from(Document& document)
{
if (!document.frame() || !document.frame()->domWindow())
return 0;
Navigator& navigator = document.frame()->domWindow()->navigator();
return &from(navigator);
}
NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
{
NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupplement<Navigator>::from(navigator, supplementName()));
if (!supplement) {
supplement = new NavigatorGamepad(*navigator.frame()->document());
provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement));
}
return *supplement;
}
WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
{
return NavigatorGamepad::from(navigator).webkitGamepads();
}
GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
{
return NavigatorGamepad::from(navigator).gamepads();
}
WebKitGamepadList* NavigatorGamepad::webkitGamepads()
{
startUpdating();
if (!m_webkitGamepads)
m_webkitGamepads = WebKitGamepadList::create();
sampleGamepads<WebKitGamepad>(m_webkitGamepads.get());
return m_webkitGamepads.get();
}
GamepadList* NavigatorGamepad::gamepads()
{
startUpdating();
if (!m_gamepads)
m_gamepads = GamepadList::create();
sampleGamepads<Gamepad>(m_gamepads.get());
return m_gamepads.get();
}
void NavigatorGamepad::trace(Visitor* visitor)
{
visitor->trace(m_gamepads);
visitor->trace(m_webkitGamepads);
}
void NavigatorGamepad::didConnectOrDisconnectGamepad(unsigned index, const blink::WebGamepad& webGamepad, bool connected)
{
ASSERT(index < blink::WebGamepads::itemsLengthCap);
ASSERT(connected == webGamepad.connected);
ASSERT(window());
if (!m_hasEventListener)
return;
if (window()->document()->activeDOMObjectsAreStopped() || window()->document()->activeDOMObjectsAreSuspended())
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);
const AtomicString& eventName = connected ? EventTypeNames::gamepadconnected : EventTypeNames::gamepaddisconnected;
RefPtrWillBeRawPtr<GamepadEvent> event = GamepadEvent::create(eventName, false, true, gamepad.get());
window()->dispatchEvent(event);
}
NavigatorGamepad::NavigatorGamepad(Document& document)
: DOMWindowProperty(document.frame())
, DeviceSensorEventController(document)
, DOMWindowLifecycleObserver(document.frame()->domWindow())
{
}
NavigatorGamepad::~NavigatorGamepad()
{
}
const char* NavigatorGamepad::supplementName()
{
return "NavigatorGamepad";
}
void NavigatorGamepad::willDestroyGlobalObjectInFrame()
{
stopUpdating();
DOMWindowProperty::willDestroyGlobalObjectInFrame();
}
void NavigatorGamepad::willDetachGlobalObjectFromFrame()
{
stopUpdating();
DOMWindowProperty::willDetachGlobalObjectFromFrame();
}
void NavigatorGamepad::registerWithDispatcher()
{
GamepadDispatcher::instance().addClient(this);
}
void NavigatorGamepad::unregisterWithDispatcher()
{
GamepadDispatcher::instance().removeClient(this);
}
bool NavigatorGamepad::hasLastData()
{
return false;
}
PassRefPtrWillBeRawPtr<Event> NavigatorGamepad::getLastEvent()
{
ASSERT_NOT_REACHED();
return nullptr;
}
bool NavigatorGamepad::isNullEvent(Event*)
{
ASSERT_NOT_REACHED();
return false;
}
void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& eventType)
{
if (RuntimeEnabledFeatures::gamepadEnabled() && (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeNames::gamepaddisconnected)) {
if (page() && page()->visibilityState() == PageVisibilityStateVisible)
startUpdating();
m_hasEventListener = true;
}
}
void NavigatorGamepad::didRemoveEventListener(DOMWindow*, const AtomicString& eventType)
{
if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeNames::gamepaddisconnected)
m_hasEventListener = false;
}
void NavigatorGamepad::didRemoveAllEventListeners(DOMWindow*)
{
m_hasEventListener = false;
}
}