Chromium Code Reviews| 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 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 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/Navigator.h" | 29 #include "core/frame/Navigator.h" |
| 30 #include "modules/gamepad/GamepadList.h" | 30 #include "modules/gamepad/GamepadList.h" |
| 31 #include "modules/gamepad/WebKitGamepadList.h" | |
| 31 #include "public/platform/Platform.h" | 32 #include "public/platform/Platform.h" |
| 32 #include "wtf/PassOwnPtr.h" | 33 #include "wtf/PassOwnPtr.h" |
| 33 | 34 |
| 34 namespace WebCore { | 35 namespace WebCore { |
| 35 | 36 |
| 36 static void sampleGamepads(GamepadList* into) | 37 template<typename T> |
| 38 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) | |
| 39 { | |
| 40 gamepad.setId(webGamepad.id); | |
| 41 gamepad.setIndex(index); | |
| 42 gamepad.setConnected(webGamepad.connected); | |
| 43 gamepad.setTimestamp(webGamepad.timestamp); | |
| 44 gamepad.setMapping(webGamepad.mapping); | |
| 45 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); | |
| 46 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); | |
| 47 } | |
| 48 | |
| 49 template<typename GamepadType, typename ListType> | |
| 50 static void sampleGamepads(ListType* into) | |
| 37 { | 51 { |
| 38 blink::WebGamepads gamepads; | 52 blink::WebGamepads gamepads; |
| 39 | 53 |
| 40 blink::Platform::current()->sampleGamepads(gamepads); | 54 blink::Platform::current()->sampleGamepads(gamepads); |
| 41 | 55 |
| 42 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { | 56 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { |
| 43 blink::WebGamepad& webGamepad = gamepads.items[i]; | 57 blink::WebGamepad& webGamepad = gamepads.items[i]; |
| 44 if (i < gamepads.length && webGamepad.connected) { | 58 if (i < gamepads.length && webGamepad.connected) { |
| 45 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i); | 59 RefPtrWillBeRawPtr<GamepadType> gamepad = into->item(i); |
| 46 if (!gamepad) | 60 if (!gamepad) |
| 47 gamepad = Gamepad::create(); | 61 gamepad = GamepadType::create(); |
|
eseidel
2014/03/12 22:25:26
Unfortunate that we need to do a malloc on every s
| |
| 48 gamepad->id(webGamepad.id); | 62 sampleGamepad(i, *gamepad, webGamepad); |
| 49 gamepad->index(i); | |
| 50 gamepad->connected(webGamepad.connected); | |
| 51 gamepad->timestamp(webGamepad.timestamp); | |
| 52 #if defined(ENABLE_NEW_GAMEPAD_API) | |
| 53 gamepad->mapping(webGamepad.mapping); | |
| 54 #endif | |
| 55 gamepad->axes(webGamepad.axesLength, webGamepad.axes); | |
| 56 gamepad->buttons(webGamepad.buttonsLength, webGamepad.buttons); | |
| 57 into->set(i, gamepad); | 63 into->set(i, gamepad); |
| 58 } else { | 64 } else { |
| 59 into->set(i, nullptr); | 65 into->set(i, nullptr); |
| 60 } | 66 } |
| 61 } | 67 } |
| 62 } | 68 } |
| 63 | 69 |
| 64 NavigatorGamepad::NavigatorGamepad() | 70 NavigatorGamepad::NavigatorGamepad() |
| 65 { | 71 { |
| 72 | |
|
eseidel
2014/03/12 22:25:26
spurious.
| |
| 66 } | 73 } |
| 67 | 74 |
| 68 NavigatorGamepad::~NavigatorGamepad() | 75 NavigatorGamepad::~NavigatorGamepad() |
| 69 { | 76 { |
| 77 | |
| 70 } | 78 } |
| 71 | 79 |
| 72 const char* NavigatorGamepad::supplementName() | 80 const char* NavigatorGamepad::supplementName() |
| 73 { | 81 { |
| 74 return "NavigatorGamepad"; | 82 return "NavigatorGamepad"; |
| 75 } | 83 } |
| 76 | 84 |
| 77 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) | 85 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
| 78 { | 86 { |
| 79 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); | 87 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); |
| 80 if (!supplement) { | 88 if (!supplement) { |
| 81 supplement = new NavigatorGamepad(); | 89 supplement = new NavigatorGamepad(); |
| 82 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 90 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
| 83 } | 91 } |
| 84 return *supplement; | 92 return *supplement; |
| 85 } | 93 } |
| 86 | 94 |
| 87 GamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) | 95 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) |
| 96 { | |
| 97 return NavigatorGamepad::from(navigator).webkitGamepads(); | |
| 98 } | |
| 99 | |
| 100 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) | |
| 88 { | 101 { |
| 89 return NavigatorGamepad::from(navigator).gamepads(); | 102 return NavigatorGamepad::from(navigator).gamepads(); |
| 90 } | 103 } |
| 91 | 104 |
| 105 WebKitGamepadList* NavigatorGamepad::webkitGamepads() | |
| 106 { | |
| 107 if (!m_webkitGamepads) | |
| 108 m_webkitGamepads = WebKitGamepadList::create(); | |
| 109 sampleGamepads<WebKitGamepad>(m_webkitGamepads.get()); | |
|
eseidel
2014/03/12 22:25:26
Woh, crazy. So they can sample multiple times per
| |
| 110 return m_webkitGamepads.get(); | |
| 111 } | |
| 112 | |
| 92 GamepadList* NavigatorGamepad::gamepads() | 113 GamepadList* NavigatorGamepad::gamepads() |
| 93 { | 114 { |
| 94 if (!m_gamepads) | 115 if (!m_gamepads) |
| 95 m_gamepads = GamepadList::create(); | 116 m_gamepads = GamepadList::create(); |
| 96 sampleGamepads(m_gamepads.get()); | 117 sampleGamepads<Gamepad>(m_gamepads.get()); |
| 97 return m_gamepads.get(); | 118 return m_gamepads.get(); |
| 98 } | 119 } |
| 99 | 120 |
| 100 } // namespace WebCore | 121 } // namespace WebCore |
| OLD | NEW |