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

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

Issue 196503004: Share Gamepad and WebKitGamepad implementation (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Compiles and links 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 15 matching lines...) Expand all
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 "public/platform/Platform.h" 31 #include "public/platform/Platform.h"
32 #include "wtf/PassOwnPtr.h" 32 #include "wtf/PassOwnPtr.h"
33 33
34 namespace WebCore { 34 namespace WebCore {
35 35
36 static void sampleGamepads(GamepadList* into) 36 template<typename T>
37 static void sampleGamepad(unsigned index, T& gamepad, const blink::WebGamepad& w ebGamepad)
38 {
39 gamepad.setId(webGamepad.id);
40 gamepad.setIndex(index);
41 gamepad.setConnected(webGamepad.connected);
42 gamepad.setTimestamp(webGamepad.timestamp);
43 gamepad.setMapping(webGamepad.mapping);
44 gamepad.setAxes(webGamepad.axesLength, webGamepad.axes);
45 gamepad.setButtons(webGamepad.buttonsLength, webGamepad.buttons);
46 }
47
48 template<typename GamepadType, typename ListType>
49 static void sampleGamepads(ListType* into)
37 { 50 {
38 blink::WebGamepads gamepads; 51 blink::WebGamepads gamepads;
39 52
40 blink::Platform::current()->sampleGamepads(gamepads); 53 blink::Platform::current()->sampleGamepads(gamepads);
41 54
42 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) { 55 for (unsigned i = 0; i < blink::WebGamepads::itemsLengthCap; ++i) {
43 blink::WebGamepad& webGamepad = gamepads.items[i]; 56 blink::WebGamepad& webGamepad = gamepads.items[i];
44 if (i < gamepads.length && webGamepad.connected) { 57 if (i < gamepads.length && webGamepad.connected) {
45 RefPtrWillBeRawPtr<Gamepad> gamepad = into->item(i); 58 RefPtrWillBeRawPtr<GamepadType> gamepad = into->item(i);
46 if (!gamepad) 59 if (!gamepad)
47 gamepad = Gamepad::create(); 60 gamepad = GamepadType::create();
48 gamepad->id(webGamepad.id); 61 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); 62 into->set(i, gamepad);
58 } else { 63 } else {
59 into->set(i, nullptr); 64 into->set(i, nullptr);
60 } 65 }
61 } 66 }
62 } 67 }
63 68
64 NavigatorGamepad::NavigatorGamepad() 69 NavigatorGamepad::NavigatorGamepad()
65 { 70 {
71
66 } 72 }
67 73
68 NavigatorGamepad::~NavigatorGamepad() 74 NavigatorGamepad::~NavigatorGamepad()
69 { 75 {
76
70 } 77 }
71 78
72 const char* NavigatorGamepad::supplementName() 79 const char* NavigatorGamepad::supplementName()
73 { 80 {
74 return "NavigatorGamepad"; 81 return "NavigatorGamepad";
75 } 82 }
76 83
77 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) 84 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
78 { 85 {
79 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName())); 86 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(Supplement<Nav igator>::from(navigator, supplementName()));
80 if (!supplement) { 87 if (!supplement) {
81 supplement = new NavigatorGamepad(); 88 supplement = new NavigatorGamepad();
82 provideTo(navigator, supplementName(), adoptPtr(supplement)); 89 provideTo(navigator, supplementName(), adoptPtr(supplement));
83 } 90 }
84 return *supplement; 91 return *supplement;
85 } 92 }
86 93
87 GamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) 94 GamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
88 { 95 {
96 return NavigatorGamepad::from(navigator).webkitGamepads();
97 }
98
99 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
100 {
89 return NavigatorGamepad::from(navigator).gamepads(); 101 return NavigatorGamepad::from(navigator).gamepads();
90 } 102 }
91 103
104 GamepadList* NavigatorGamepad::webkitGamepads()
105 {
106 if (!m_webkitGamepads)
107 m_webkitGamepads = GamepadList::create();
108 sampleGamepads<Gamepad>(m_webkitGamepads.get());
109 return m_webkitGamepads.get();
110 }
111
92 GamepadList* NavigatorGamepad::gamepads() 112 GamepadList* NavigatorGamepad::gamepads()
93 { 113 {
94 if (!m_gamepads) 114 if (!m_gamepads)
95 m_gamepads = GamepadList::create(); 115 m_gamepads = GamepadList::create();
96 sampleGamepads(m_gamepads.get()); 116 sampleGamepads<Gamepad>(m_gamepads.get());
97 return m_gamepads.get(); 117 return m_gamepads.get();
98 } 118 }
99 119
100 } // namespace WebCore 120 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698