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

Side by Side Diff: content/browser/gamepad/gamepad_platform_data_fetcher_android.cc

Issue 133943002: Gamepad API support for chrome on android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 11 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
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of NVIDIA CORPORATION nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "base/strings/string_number_conversions.h"
30 #include "base/strings/string_util.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h"
33 #include "third_party/WebKit/public/platform/WebGamepads.h"
34
35 #if defined(OS_ANDROID)
36
37 namespace content {
38
39 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() {
40 }
41
42 void GamepadPlatformDataFetcherAndroid::GetGamepadData(
43 blink::WebGamepads* pads, bool) {
44
45 GamepadsReader* m_gamepadsReader = GamepadsReader::GetInstance();
46
47 pads->length = m_gamepadsReader->updateGamepadsCount();
48 if (pads->length == 0)
49 return;
50
51 for (unsigned int i = 0; i < WebGamepads::itemsLengthCap; i++) {
52 bool ifDeviceConnected = m_gamepadsReader->isDeviceConnected(i);
53 if (ifDeviceConnected) {
54
55 // Set connected flag to true for all the gamepad devices that are
56 // attached.
57 pads->items[i].connected = true;
58
59 // Map the Gamepad DeviceName String to the WebGamepad Id and then
60 // convert the utf-8 id string to WebUChar.
61 // Ideally it should be mapped to vendor and product information
62 // but it is only available at kernel level and it can not be
63 // queried using class android.hardware.input.InputManager.
64
65 std::string string1 = m_gamepadsReader->getDeviceName(i);
66 base::TruncateUTF8ToByteSize(string1, WebGamepad::idLengthCap - 1,
67 &string1);
68 base::string16 tmp16 = base::UTF8ToUTF16(string1);
69 memset(pads->items[i].id, 0, sizeof(pads->items[i].id));
70 tmp16.copy(pads->items[i].id, arraysize(pads->items[i].id) - 1);
71
72 // timestamp is queried from GamepadReader.
73 pads->items[i].timestamp = m_gamepadsReader->getDeviceTimestamp(i);
74
75 // A float vector is queried from GamepadReader that stores the
76 // state of axeses.
77 std::vector<float> axes = m_gamepadsReader->getDeviceAxes(i);
78
79 // Set WebGamepad axeslength to total number of axes on the gamepad
80 // device.
81 pads->items[i].axesLength = axes.size();
82
83 // A float vector is queried from GamepadReader that stores the
84 // button's state.
85 std::vector<float> buttons = m_gamepadsReader->getDeviceButtons(i);
86
87 // Set WebGamepad buttonslength to total numbers of buttons on the
88 // gamepad device.
89 pads->items[i].buttonsLength = buttons.size();
90
91 // Set WebGamepad connected flag to false if buttonslength or
92 // axeslength returned by
93 // JNI are larger than permitted value.
94 if (pads->items[i].axesLength > WebGamepad::axesLengthCap ||
95 pads->items[i].buttonsLength >
96 WebGamepad::buttonsLengthCap) {
97 pads->items[i].connected = false;
98 } else {
99 // Copy axes state to the WebGamepad axes[].
100 for (unsigned int j = 0; j < pads->items[i].axesLength; j++)
101 pads->items[i].axes[j] = axes[j];
102
103 // Copy buttons state to the WebGamepad buttons[].
104 for (unsigned int j = 0; j < pads->items[i].buttonsLength; j++)
105 pads->items[i].buttons[j] = buttons[j];
106 }
107 } else {
108 // Set connected flag to flase for all the gamepad devices that are
109 // not attached.
110 pads->items[i].connected = false;
111 }
112 }
113 }
114
115 void GamepadPlatformDataFetcherAndroid::PauseHint(bool isaccesspaused) {
116 GamepadsReader::GetInstance()->notifyForGamepadsAccess(isaccesspaused);
117 }
118
119 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() {
120 }
121
122 } // namespace content
123 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698