OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_array.h" | |
11 #include "base/android/jni_string.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/trace_event/trace_event.h" | |
16 | |
17 #include "jni/GamepadList_jni.h" | |
18 | |
19 #include "third_party/WebKit/public/platform/WebGamepads.h" | |
20 | |
21 using base::android::AttachCurrentThread; | |
22 using base::android::CheckException; | |
23 using base::android::ClearException; | |
24 using base::android::ConvertJavaStringToUTF8; | |
25 using base::android::ScopedJavaLocalRef; | |
26 using blink::WebGamepad; | |
27 using blink::WebGamepads; | |
28 | |
29 namespace content { | |
30 | |
31 bool | |
32 GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid( | |
33 JNIEnv* env) { | |
34 return RegisterNativesImpl(env); | |
35 } | |
36 | |
37 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { | |
38 PauseHint(false); | |
39 } | |
40 | |
41 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { | |
42 PauseHint(true); | |
43 } | |
44 | |
45 void GamepadPlatformDataFetcherAndroid::GetGamepadData( | |
46 blink::WebGamepads* pads, | |
47 bool devices_changed_hint) { | |
48 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | |
49 | |
50 pads->length = 0; | |
51 | |
52 JNIEnv* env = AttachCurrentThread(); | |
53 if (!env) | |
54 return; | |
55 | |
56 Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(pads)); | |
57 } | |
58 | |
59 void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) { | |
60 JNIEnv* env = AttachCurrentThread(); | |
61 if (!env) | |
62 return; | |
63 | |
64 Java_GamepadList_setGamepadAPIActive(env, !paused); | |
65 } | |
66 | |
67 static void SetGamepadData(JNIEnv* env, | |
68 const JavaParamRef<jobject>& obj, | |
69 jlong gamepads, | |
70 jint index, | |
71 jboolean mapping, | |
72 jboolean connected, | |
73 const JavaParamRef<jstring>& devicename, | |
74 jlong timestamp, | |
75 const JavaParamRef<jfloatArray>& jaxes, | |
76 const JavaParamRef<jfloatArray>& jbuttons) { | |
77 DCHECK(gamepads); | |
78 blink::WebGamepads* pads = reinterpret_cast<WebGamepads*>(gamepads); | |
79 DCHECK_EQ(pads->length, unsigned(index)); | |
80 DCHECK_LT(index, static_cast<int>(blink::WebGamepads::itemsLengthCap)); | |
81 | |
82 ++pads->length; | |
83 | |
84 blink::WebGamepad& pad = pads->items[index]; | |
85 | |
86 pad.connected = connected; | |
87 | |
88 pad.timestamp = timestamp; | |
89 | |
90 // Do not set gamepad parameters for all the gamepad devices that are not | |
91 // attached. | |
92 if (!connected) | |
93 return; | |
94 | |
95 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | |
96 // be mapped to vendor and product information but it is only available at | |
97 // kernel level and it can not be queried using class | |
98 // android.hardware.input.InputManager. | |
99 // TODO(SaurabhK): Store a cached WebGamePad object in | |
100 // GamepadPlatformDataFetcherAndroid and only update constant WebGamepad | |
101 // values when a device has changed. | |
102 base::string16 device_name; | |
103 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); | |
104 const size_t name_to_copy = | |
105 std::min(device_name.size(), WebGamepad::idLengthCap - 1); | |
106 memcpy(pad.id, | |
107 device_name.data(), | |
108 name_to_copy * sizeof(base::string16::value_type)); | |
109 pad.id[name_to_copy] = 0; | |
110 | |
111 base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : ""); | |
112 const size_t mapping_to_copy = | |
113 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); | |
114 memcpy(pad.mapping, | |
115 mapping_name.data(), | |
116 mapping_to_copy * sizeof(base::string16::value_type)); | |
117 pad.mapping[mapping_to_copy] = 0; | |
118 | |
119 pad.timestamp = timestamp; | |
120 | |
121 std::vector<float> axes; | |
122 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | |
123 | |
124 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
125 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
126 // is larger than axesLengthCap. | |
127 pad.axesLength = std::min(static_cast<int>(axes.size()), | |
128 static_cast<int>(WebGamepad::axesLengthCap)); | |
129 | |
130 // Copy axes state to the WebGamepad axes[]. | |
131 for (unsigned int i = 0; i < pad.axesLength; i++) { | |
132 pad.axes[i] = static_cast<double>(axes[i]); | |
133 } | |
134 | |
135 std::vector<float> buttons; | |
136 base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | |
137 | |
138 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
139 // device. Only return the first buttonsLengthCap if axeslength captured by | |
140 // GamepadList is larger than buttonsLengthCap. | |
141 pad.buttonsLength = std::min(static_cast<int>(buttons.size()), | |
142 static_cast<int>(WebGamepad::buttonsLengthCap)); | |
143 | |
144 // Copy buttons state to the WebGamepad buttons[]. | |
145 for (unsigned int j = 0; j < pad.buttonsLength; j++) { | |
146 pad.buttons[j].pressed = buttons[j]; | |
147 pad.buttons[j].value = buttons[j]; | |
148 } | |
149 } | |
150 | |
151 } // namespace content | |
OLD | NEW |