| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/gamepad/GamepadDispatcher.h" | 6 #include "modules/gamepad/GamepadDispatcher.h" |
| 7 | 7 |
| 8 #include "modules/gamepad/NavigatorGamepad.h" | 8 #include "modules/gamepad/NavigatorGamepad.h" |
| 9 #include "public/platform/Platform.h" | 9 #include "public/platform/Platform.h" |
| 10 #include "wtf/TemporaryChange.h" | 10 #include "wtf/TemporaryChange.h" |
| 11 | 11 |
| 12 namespace WebCore { | 12 namespace WebCore { |
| 13 | 13 |
| 14 GamepadDispatcher& GamepadDispatcher::instance() | 14 GamepadDispatcher& GamepadDispatcher::instance() |
| 15 { | 15 { |
| 16 DEFINE_STATIC_LOCAL(GamepadDispatcher, gamepadDispatcher, ()); | 16 DEFINE_STATIC_LOCAL(GamepadDispatcher, gamepadDispatcher, ()); |
| 17 return gamepadDispatcher; | 17 return gamepadDispatcher; |
| 18 } | 18 } |
| 19 | 19 |
| 20 void GamepadDispatcher::addClient(NavigatorGamepad* client) | |
| 21 { | |
| 22 addController(client); | |
| 23 } | |
| 24 | |
| 25 void GamepadDispatcher::removeClient(NavigatorGamepad* client) | |
| 26 { | |
| 27 removeController(client); | |
| 28 } | |
| 29 | |
| 30 void GamepadDispatcher::sampleGamepads(blink::WebGamepads& gamepads) | 20 void GamepadDispatcher::sampleGamepads(blink::WebGamepads& gamepads) |
| 31 { | 21 { |
| 32 ASSERT(!m_controllers.isEmpty()); | |
| 33 blink::Platform::current()->sampleGamepads(gamepads); | 22 blink::Platform::current()->sampleGamepads(gamepads); |
| 34 } | 23 } |
| 35 | 24 |
| 36 GamepadDispatcher::GamepadDispatcher() | 25 GamepadDispatcher::GamepadDispatcher() |
| 37 { | 26 { |
| 38 } | 27 } |
| 39 | 28 |
| 40 GamepadDispatcher::~GamepadDispatcher() | 29 GamepadDispatcher::~GamepadDispatcher() |
| 41 { | 30 { |
| 42 } | 31 } |
| 43 | 32 |
| 44 void GamepadDispatcher::didConnectGamepad(unsigned index, const blink::WebGamepa
d& gamepad) | 33 void GamepadDispatcher::didConnectGamepad(unsigned index, const blink::WebGamepa
d& gamepad) |
| 45 { | 34 { |
| 46 dispatchDidConnectOrDisconnectGamepad(index, gamepad, true); | 35 dispatchDidConnectOrDisconnectGamepad(index, gamepad, true); |
| 47 } | 36 } |
| 48 | 37 |
| 49 void GamepadDispatcher::didDisconnectGamepad(unsigned index, const blink::WebGam
epad& gamepad) | 38 void GamepadDispatcher::didDisconnectGamepad(unsigned index, const blink::WebGam
epad& gamepad) |
| 50 { | 39 { |
| 51 dispatchDidConnectOrDisconnectGamepad(index, gamepad, false); | 40 dispatchDidConnectOrDisconnectGamepad(index, gamepad, false); |
| 52 } | 41 } |
| 53 | 42 |
| 54 void GamepadDispatcher::dispatchDidConnectOrDisconnectGamepad(unsigned index, co
nst blink::WebGamepad& gamepad, bool connected) | 43 void GamepadDispatcher::dispatchDidConnectOrDisconnectGamepad(unsigned index, co
nst blink::WebGamepad& gamepad, bool connected) |
| 55 { | 44 { |
| 56 { | 45 ASSERT(index < blink::WebGamepads::itemsLengthCap); |
| 57 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | 46 ASSERT(connected == gamepad.connected); |
| 58 // Don't fire controllers removed or added during event dispatch. | |
| 59 size_t size = m_controllers.size(); | |
| 60 for (size_t i = 0; i < size; ++i) { | |
| 61 if (m_controllers[i]) | |
| 62 static_cast<NavigatorGamepad*>(m_controllers[i])->didConnectOrDi
sconnectGamepad(index, gamepad, connected); | |
| 63 } | |
| 64 } | |
| 65 | 47 |
| 66 if (m_needsPurge) | 48 m_latestGamepad = gamepad; |
| 67 purgeControllers(); | 49 m_latestGamepadIndex = index; |
| 50 notifyControllers(); |
| 51 } |
| 52 |
| 53 const blink::WebGamepad& GamepadDispatcher::latestGamepadEventData(unsigned &ind
ex) const |
| 54 { |
| 55 index = m_latestGamepadIndex; |
| 56 return m_latestGamepad; |
| 68 } | 57 } |
| 69 | 58 |
| 70 void GamepadDispatcher::startListening() | 59 void GamepadDispatcher::startListening() |
| 71 { | 60 { |
| 72 blink::Platform::current()->setGamepadListener(this); | 61 blink::Platform::current()->setGamepadListener(this); |
| 73 } | 62 } |
| 74 | 63 |
| 75 void GamepadDispatcher::stopListening() | 64 void GamepadDispatcher::stopListening() |
| 76 { | 65 { |
| 77 blink::Platform::current()->setGamepadListener(0); | 66 blink::Platform::current()->setGamepadListener(0); |
| 78 } | 67 } |
| 79 | 68 |
| 80 } // namespace WebCore | 69 } // namespace WebCore |
| OLD | NEW |