| 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 15 matching lines...) Expand all Loading... |
| 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/dom/Document.h" | 29 #include "core/dom/Document.h" |
| 30 #include "core/frame/LocalDOMWindow.h" | 30 #include "core/frame/LocalDOMWindow.h" |
| 31 #include "core/frame/LocalFrame.h" | 31 #include "core/frame/LocalFrame.h" |
| 32 #include "core/frame/Navigator.h" | 32 #include "core/frame/Navigator.h" |
| 33 #include "core/page/Page.h" | 33 #include "core/page/Page.h" |
| 34 #include "modules/gamepad/GamepadDispatcher.h" | 34 #include "modules/gamepad/GamepadDispatcher.h" |
| 35 #include "modules/gamepad/GamepadEvent.h" | 35 #include "modules/gamepad/GamepadEvent.h" |
| 36 #include "modules/gamepad/GamepadList.h" | |
| 37 | 36 |
| 38 namespace blink { | 37 namespace blink { |
| 39 | 38 |
| 40 template<typename T> | 39 static Member<Gamepad> sampleGamepad(unsigned index, Gamepad& gamepad, const Web
Gamepad& webGamepad) |
| 41 static void sampleGamepad(unsigned index, T& gamepad, const WebGamepad& webGamep
ad) | |
| 42 { | 40 { |
| 41 Member<Gamepad> gamepad = Gamepad::create(); |
| 43 gamepad.setId(webGamepad.id); | 42 gamepad.setId(webGamepad.id); |
| 44 gamepad.setIndex(index); | 43 gamepad.setIndex(index); |
| 45 gamepad.setConnected(webGamepad.connected); | 44 gamepad.setConnected(webGamepad.connected); |
| 46 gamepad.setTimestamp(webGamepad.timestamp); | 45 gamepad.setTimestamp(webGamepad.timestamp); |
| 47 gamepad.setMapping(webGamepad.mapping); | 46 gamepad.setMapping(webGamepad.mapping); |
| 48 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); | 47 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes); |
| 49 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); | 48 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons); |
| 50 } | 49 } |
| 51 | 50 |
| 52 template<typename GamepadType, typename ListType> | 51 static HeapVector<Member<Gamepad>> sampleGamepads() |
| 53 static void sampleGamepads(ListType* into) | |
| 54 { | 52 { |
| 55 WebGamepads gamepads; | 53 WebGamepads webGamepads; |
| 54 GamepadDispatcher::instance().sampleGamepads(webGamepads); |
| 56 | 55 |
| 57 GamepadDispatcher::instance().sampleGamepads(gamepads); | 56 HeapVector<Member<Gamepad>> gamepads; |
| 58 | 57 gamepads.reserveCapacity(webGamepads.length); |
| 59 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 58 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 60 WebGamepad& webGamepad = gamepads.items[i]; | 59 WebGamepad& webGamepad = webGamepads.items[i]; |
| 61 if (i < gamepads.length && webGamepad.connected) { | 60 if (i < webGamepads.length && webGamepad.connected) { |
| 62 GamepadType* gamepad = into->item(i); | 61 Gamepad* gamepad = gamepads->item(i); |
| 63 if (!gamepad) | 62 if (!gamepad) |
| 64 gamepad = GamepadType::create(); | 63 gamepad = Gamepad::create(); |
| 65 sampleGamepad(i, *gamepad, webGamepad); | 64 sampleGamepad(i, *gamepad, webGamepad); |
| 66 into->set(i, gamepad); | 65 gamepads->set(i, gamepad); |
| 67 } else { | 66 } else { |
| 68 into->set(i, 0); | 67 gamepads->set(i, 0); |
| 69 } | 68 } |
| 70 } | 69 } |
| 70 |
| 71 return gamepads; |
| 71 } | 72 } |
| 72 | |
| 73 NavigatorGamepad* NavigatorGamepad::from(Document& document) | 73 NavigatorGamepad* NavigatorGamepad::from(Document& document) |
| 74 { | 74 { |
| 75 if (!document.frame() || !document.frame()->domWindow()) | 75 if (!document.frame() || !document.frame()->domWindow()) |
| 76 return 0; | 76 return 0; |
| 77 Navigator& navigator = *document.frame()->domWindow()->navigator(); | 77 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
| 78 return &from(navigator); | 78 return &from(navigator); |
| 79 } | 79 } |
| 80 | 80 |
| 81 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) | 81 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) |
| 82 { | 82 { |
| 83 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp
lement<Navigator>::from(navigator, supplementName())); | 83 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp
lement<Navigator>::from(navigator, supplementName())); |
| 84 if (!supplement) { | 84 if (!supplement) { |
| 85 supplement = new NavigatorGamepad(navigator.frame()); | 85 supplement = new NavigatorGamepad(navigator.frame()); |
| 86 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); | 86 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); |
| 87 } | 87 } |
| 88 return *supplement; | 88 return *supplement; |
| 89 } | 89 } |
| 90 | 90 |
| 91 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) | 91 HeapVector<Member<Gamepad>> NavigatorGamepad::getGamepads(Navigator& navigator) |
| 92 { | 92 { |
| 93 return NavigatorGamepad::from(navigator).gamepads(); | 93 return NavigatorGamepad::from(navigator).getGamepads(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 GamepadList* NavigatorGamepad::gamepads() | 96 HeapVector<Member<Gamepad>> NavigatorGamepad::getGamepads() |
| 97 { | 97 { |
| 98 if (!m_gamepads) | 98 startUpdatingIfAttached(); |
| 99 m_gamepads = GamepadList::create(); | 99 return sampleGamepads(); |
| 100 if (startUpdatingIfAttached()) | |
| 101 sampleGamepads<Gamepad>(m_gamepads.get()); | |
| 102 return m_gamepads.get(); | |
| 103 } | 100 } |
| 104 | 101 |
| 105 void NavigatorGamepad::trace(Visitor* visitor) | 102 void NavigatorGamepad::trace(Visitor* visitor) |
| 106 { | 103 { |
| 107 visitor->trace(m_gamepads); | 104 visitor->trace(m_gamepads); |
| 108 visitor->trace(m_pendingEvents); | 105 visitor->trace(m_pendingEvents); |
| 109 WillBeHeapSupplement<Navigator>::trace(visitor); | 106 WillBeHeapSupplement<Navigator>::trace(visitor); |
| 110 DOMWindowProperty::trace(visitor); | 107 DOMWindowProperty::trace(visitor); |
| 111 PlatformEventController::trace(visitor); | 108 PlatformEventController::trace(visitor); |
| 112 DOMWindowLifecycleObserver::trace(visitor); | 109 DOMWindowLifecycleObserver::trace(visitor); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) { | 276 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) { |
| 280 m_pendingEvents.append(newGamepad); | 277 m_pendingEvents.append(newGamepad); |
| 281 } | 278 } |
| 282 } | 279 } |
| 283 | 280 |
| 284 if (!m_pendingEvents.isEmpty()) | 281 if (!m_pendingEvents.isEmpty()) |
| 285 m_dispatchOneEventRunner.runAsync(); | 282 m_dispatchOneEventRunner.runAsync(); |
| 286 } | 283 } |
| 287 | 284 |
| 288 } // namespace blink | 285 } // namespace blink |
| OLD | NEW |