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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND |
| 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE | 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE |
| 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 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/DOMWindow.h" | |
| 30 #include "core/frame/LocalFrame.h" | |
| 29 #include "core/frame/Navigator.h" | 31 #include "core/frame/Navigator.h" |
| 32 #include "modules/gamepad/GamepadEvent.h" | |
| 30 #include "modules/gamepad/GamepadList.h" | 33 #include "modules/gamepad/GamepadList.h" |
| 31 #include "modules/gamepad/WebKitGamepadList.h" | 34 #include "modules/gamepad/WebKitGamepadList.h" |
| 32 #include "public/platform/Platform.h" | 35 #include "public/platform/Platform.h" |
| 33 #include "wtf/PassOwnPtr.h" | 36 #include "wtf/PassOwnPtr.h" |
| 34 | 37 |
| 35 namespace WebCore { | 38 namespace WebCore { |
| 36 | 39 |
| 37 template<typename T> | 40 template<typename T> |
| 38 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) | 41 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad) |
| 39 { | 42 { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 60 if (!gamepad) | 63 if (!gamepad) |
| 61 gamepad = GamepadType::create(); | 64 gamepad = GamepadType::create(); |
| 62 sampleGamepad(i, *gamepad, webGamepad); | 65 sampleGamepad(i, *gamepad, webGamepad); |
| 63 into->set(i, gamepad); | 66 into->set(i, gamepad); |
| 64 } else { | 67 } else { |
| 65 into->set(i, nullptr); | 68 into->set(i, nullptr); |
| 66 } | 69 } |
| 67 } | 70 } |
| 68 } | 71 } |
| 69 | 72 |
| 70 NavigatorGamepad::NavigatorGamepad() | 73 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame) |
| 74 : DOMWindowProperty(frame) | |
| 71 { | 75 { |
| 76 blink::Platform::current()->setGamepadListener(this); | |
| 72 } | 77 } |
| 73 | 78 |
| 74 NavigatorGamepad::~NavigatorGamepad() | 79 NavigatorGamepad::~NavigatorGamepad() |
| 75 { | 80 { |
| 81 blink::Platform::current()->setGamepadListener(0); | |
| 76 } | 82 } |
| 77 | 83 |
| 78 const char* NavigatorGamepad::supplementName() | 84 const char* NavigatorGamepad::supplementName() |
| 79 { | 85 { |
| 80 return "NavigatorGamepad"; | 86 return "NavigatorGamepad"; |
| 81 } | 87 } |
| 82 | 88 |
| 83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) | 89 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
| 84 { | 90 { |
| 85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); | 91 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); |
| 86 if (!supplement) { | 92 if (!supplement) { |
| 87 supplement = new NavigatorGamepad(); | 93 supplement = new NavigatorGamepad(navigator.frame()); |
| 88 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 94 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
| 89 } | 95 } |
| 90 return *supplement; | 96 return *supplement; |
| 91 } | 97 } |
| 92 | 98 |
| 93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) | 99 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) |
| 94 { | 100 { |
| 95 return NavigatorGamepad::from(navigator).webkitGamepads(); | 101 return NavigatorGamepad::from(navigator).webkitGamepads(); |
| 96 } | 102 } |
| 97 | 103 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 109 } | 115 } |
| 110 | 116 |
| 111 GamepadList* NavigatorGamepad::gamepads() | 117 GamepadList* NavigatorGamepad::gamepads() |
| 112 { | 118 { |
| 113 if (!m_gamepads) | 119 if (!m_gamepads) |
| 114 m_gamepads = GamepadList::create(); | 120 m_gamepads = GamepadList::create(); |
| 115 sampleGamepads<Gamepad>(m_gamepads.get()); | 121 sampleGamepads<Gamepad>(m_gamepads.get()); |
| 116 return m_gamepads.get(); | 122 return m_gamepads.get(); |
| 117 } | 123 } |
| 118 | 124 |
| 125 void NavigatorGamepad::onGamepadConnected(unsigned index, const blink::WebGamepa d& webGamepad) | |
| 126 { | |
| 127 if (index >= blink::WebGamepads::itemsLengthCap || !webGamepad.connected) | |
| 128 return; | |
| 129 | |
| 130 if (!m_gamepads) | |
| 131 m_gamepads = GamepadList::create(); | |
| 132 | |
| 133 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
| 134 if (!gamepad) | |
| 135 gamepad = Gamepad::create(); | |
| 136 sampleGamepad(index, *gamepad, webGamepad); | |
| 137 m_gamepads->set(index, gamepad); | |
| 138 | |
| 139 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepadcon nected, false, true, gamepad.get()); | |
| 140 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
| |
| 141 } | |
| 142 | |
| 143 void NavigatorGamepad::onGamepadDisconnected(unsigned index, const blink::WebGam epad& webGamepad) | |
| 144 { | |
| 145 if (index >= blink::WebGamepads::itemsLengthCap) | |
| 146 return; | |
| 147 | |
| 148 if (!m_gamepads) | |
| 149 m_gamepads = GamepadList::create(); | |
| 150 | |
| 151 RefPtrWillBeRawPtr<Gamepad> gamepad = m_gamepads->item(index); | |
| 152 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
| |
| 153 gamepad = Gamepad::create(); | |
| 154 sampleGamepad(index, *gamepad, webGamepad); | |
| 155 | |
| 156 RefPtr<GamepadEvent> event = GamepadEvent::create(EventTypeNames::gamepaddis connected, false, true, gamepad); | |
| 157 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.
| |
| 158 } | |
| 159 | |
| 119 } // namespace WebCore | 160 } // namespace WebCore |
| OLD | NEW |