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

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: 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
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"
34 #include "modules/gamepad/WebKitGamepadList.h"
31 #include "public/platform/Platform.h" 35 #include "public/platform/Platform.h"
32 #include "wtf/PassOwnPtr.h" 36 #include "wtf/PassOwnPtr.h"
33 37
34 namespace WebCore { 38 namespace WebCore {
35 39
36 static void sampleGamepads(GamepadList* into) 40 static void sampleGamepad(unsigned index, GamepadBase* gamepad, const blink::Web Gamepad& webGamepad)
41 {
42 gamepad->id(webGamepad.id);
43 gamepad->index(index);
44 gamepad->connected(webGamepad.connected);
45 gamepad->timestamp(webGamepad.timestamp);
46 gamepad->mapping(webGamepad.mapping);
47 gamepad->axes(webGamepad.axesLength, webGamepad.axes);
48 gamepad->buttons(webGamepad.buttonsLength, webGamepad.buttons);
49 }
50
51 static void sampleWebKitGamepads(WebKitGamepadList* into)
37 { 52 {
38 blink::WebGamepads gamepads; 53 blink::WebGamepads gamepads;
39 54
55 blink::Platform::current()->sampleGamepads(gamepads);
56
57 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
58 blink::WebGamepad& webGamepad = gamepads.items[i];
59 if (i < gamepads.length && webGamepad.connected) {
60 RefPtrWillBeRawPtr<WebKitGamepad> gamepad = into->item(i);
61 if (!gamepad)
62 gamepad = WebKitGamepad::create();
63 sampleGamepad(i, gamepad.get(), webGamepad);
64 into->set(i, gamepad);
65 } else {
66 into->set(i, nullptr);
67 }
68 }
69 }
70
71 static void sampleGamepads(GamepadList* into, LocalFrame* frame)
72 {
73 blink::WebGamepads gamepads;
74
40 blink::Platform::current()->sampleGamepads(gamepads); 75 blink::Platform::current()->sampleGamepads(gamepads);
41 76
42 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 77 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
43 blink::WebGamepad& webGamepad = gamepads.items[i]; 78 blink::WebGamepad& webGamepad = gamepads.items[i];
44 if (i < gamepads.length && webGamepad.connected) { 79 if (i < gamepads.length && webGamepad.connected) {
45 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i); 80 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i);
46 if (!gamepad) 81 if (!gamepad)
47 gamepad = Gamepad::create(); 82 gamepad = Gamepad::create();
48 gamepad->id(webGamepad.id); 83 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); 84 into->set(i, gamepad);
58 } else { 85 } else {
59 into->set(i, nullptr); 86 into->set(i, nullptr);
60 } 87 }
61 } 88 }
62 } 89 }
63 90
64 NavigatorGamepad::NavigatorGamepad() 91 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame)
92 : DOMWindowProperty(frame)
65 { 93 {
66 } 94 }
67 95
68 NavigatorGamepad::~NavigatorGamepad() 96 NavigatorGamepad::~NavigatorGamepad()
69 { 97 {
70 } 98 }
71 99
72 const char* NavigatorGamepad::supplementName() 100 const char* NavigatorGamepad::supplementName()
73 { 101 {
74 return "NavigatorGamepad"; 102 return "NavigatorGamepad";
75 } 103 }
76 104
77 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) 105 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
78 { 106 {
79 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); 107 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName()));
80 if (!supplement) { 108 if (!supplement) {
81 supplement = new NavigatorGamepad(); 109 supplement = new NavigatorGamepad(navigator.frame());
82 provideTo(navigator, supplementName(), adoptPtr(supplement)); 110 provideTo(navigator, supplementName(), adoptPtr(supplement));
83 } 111 }
84 return *supplement; 112 return *supplement;
85 } 113 }
86 114
87 GamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) 115 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
116 {
117 return NavigatorGamepad::from(navigator).webkitGamepads();
118 }
119
120 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
88 { 121 {
89 return NavigatorGamepad::from(navigator).gamepads(); 122 return NavigatorGamepad::from(navigator).gamepads();
90 } 123 }
91 124
125 WebKitGamepadList* NavigatorGamepad::webkitGamepads()
126 {
127 if (!m_webkitGamepads)
128 m_webkitGamepads = WebKitGamepadList::create();
129 sampleWebKitGamepads(m_webkitGamepads.get());
130 return m_webkitGamepads.get();
131 }
132
92 GamepadList* NavigatorGamepad::gamepads() 133 GamepadList* NavigatorGamepad::gamepads()
93 { 134 {
94 if (!m_gamepads) 135 if (!m_gamepads)
95 m_gamepads = GamepadList::create(); 136 m_gamepads = GamepadList::create();
96 sampleGamepads(m_gamepads.get()); 137 sampleGamepads(m_gamepads.get(), frame());
97 return m_gamepads.get(); 138 return m_gamepads.get();
98 } 139 }
99 140
100 } // namespace WebCore 141 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698