Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: Source/modules/gamepad/NavigatorGamepad.cpp

Issue 183313003: Added non-prefixed navigator.getGamepads() with updated API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Added LayoutTests Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
37 static void sampleGamepad(unsigned index, GamepadBase* gamepad, const blink::Web Gamepad& webGamepad)
Inactive 2014/03/06 02:54:00 nit: gamedpad would be passed by reference here to
38 {
39 gamepad->id(webGamepad.id);
40 gamepad->index(index);
41 gamepad->connected(webGamepad.connected);
42 gamepad->timestamp(webGamepad.timestamp);
43 gamepad->mapping(webGamepad.mapping);
44 gamepad->axes(webGamepad.axesLength, webGamepad.axes);
45 gamepad->buttons(webGamepad.buttonsLength, webGamepad.buttons);
46 }
47
48 static void sampleWebKitGamepads(WebKitGamepadList* into)
49 {
50 blink::WebGamepads gamepads;
51
52 blink::Platform::current()->sampleGamepads(gamepads);
53
54 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
55 blink::WebGamepad& webGamepad = gamepads.items[i];
56 if (i < gamepads.length && webGamepad.connected) {
57 RefPtrWillBeRawPtr<WebKitGamepad> gamepad = into->item(i);
58 if (!gamepad)
59 gamepad = WebKitGamepad::create();
60 sampleGamepad(i, gamepad.get(), webGamepad);
Inactive 2014/03/06 02:54:00 nit: *gamepad (see comment above)
61 into->set(i, gamepad);
62 } else {
63 into->set(i, nullptr);
64 }
65 }
66 }
67
36 static void sampleGamepads(GamepadList* into) 68 static void sampleGamepads(GamepadList* into)
37 { 69 {
38 blink::WebGamepads gamepads; 70 blink::WebGamepads gamepads;
39 71
40 blink::Platform::current()->sampleGamepads(gamepads); 72 blink::Platform::current()->sampleGamepads(gamepads);
41 73
42 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 74 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
43 blink::WebGamepad& webGamepad = gamepads.items[i]; 75 blink::WebGamepad& webGamepad = gamepads.items[i];
44 if (i < gamepads.length && webGamepad.connected) { 76 if (i < gamepads.length && webGamepad.connected) {
45 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i); 77 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i);
46 if (!gamepad) 78 if (!gamepad)
47 gamepad = Gamepad::create(); 79 gamepad = Gamepad::create();
48 gamepad->id(webGamepad.id); 80 sampleGamepad(i, gamepad.get(), 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); 81 into->set(i, gamepad);
58 } else { 82 } else {
59 into->set(i, nullptr); 83 into->set(i, nullptr);
60 } 84 }
61 } 85 }
62 } 86 }
63 87
64 NavigatorGamepad::NavigatorGamepad() 88 NavigatorGamepad::NavigatorGamepad()
65 { 89 {
90
66 } 91 }
67 92
68 NavigatorGamepad::~NavigatorGamepad() 93 NavigatorGamepad::~NavigatorGamepad()
69 { 94 {
95
70 } 96 }
71 97
72 const char* NavigatorGamepad::supplementName() 98 const char* NavigatorGamepad::supplementName()
73 { 99 {
74 return "NavigatorGamepad"; 100 return "NavigatorGamepad";
75 } 101 }
76 102
77 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) 103 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
78 { 104 {
79 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); 105 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName()));
80 if (!supplement) { 106 if (!supplement) {
81 supplement = new NavigatorGamepad(); 107 supplement = new NavigatorGamepad();
82 provideTo(navigator, supplementName(), adoptPtr(supplement)); 108 provideTo(navigator, supplementName(), adoptPtr(supplement));
83 } 109 }
84 return *supplement; 110 return *supplement;
85 } 111 }
86 112
87 GamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) 113 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
114 {
115 return NavigatorGamepad::from(navigator).webkitGamepads();
116 }
117
118 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
88 { 119 {
89 return NavigatorGamepad::from(navigator).gamepads(); 120 return NavigatorGamepad::from(navigator).gamepads();
90 } 121 }
91 122
123 WebKitGamepadList* NavigatorGamepad::webkitGamepads()
124 {
125 if (!m_webkitGamepads)
126 m_webkitGamepads = WebKitGamepadList::create();
127 sampleWebKitGamepads(m_webkitGamepads.get());
128 return m_webkitGamepads.get();
129 }
130
92 GamepadList* NavigatorGamepad::gamepads() 131 GamepadList* NavigatorGamepad::gamepads()
93 { 132 {
94 if (!m_gamepads) 133 if (!m_gamepads)
95 m_gamepads = GamepadList::create(); 134 m_gamepads = GamepadList::create();
96 sampleGamepads(m_gamepads.get()); 135 sampleGamepads(m_gamepads.get());
97 return m_gamepads.get(); 136 return m_gamepads.get();
98 } 137 }
99 138
100 } // namespace WebCore 139 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698