OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "base/android/jni_android.h" | |
8 #include "base/android/jni_array.h" | |
9 #include "base/android/jni_string.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 | |
14 #include "jni/GamepadList_jni.h" | |
15 | |
16 #include "third_party/WebKit/public/platform/WebGamepads.h" | |
17 | |
18 using base::android::AttachCurrentThread; | |
19 using base::android::CheckException; | |
20 using base::android::ClearException; | |
21 using base::android::ConvertJavaStringToUTF8; | |
22 using base::android::ScopedJavaLocalRef; | |
23 using blink::WebGamepad; | |
24 using blink::WebGamepads; | |
25 | |
26 namespace content { | |
27 | |
28 bool | |
29 GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid( | |
30 JNIEnv* env) { | |
31 return RegisterNativesImpl(env); | |
32 } | |
33 | |
34 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { | |
35 PauseHint(false); | |
36 } | |
37 | |
38 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { | |
39 } | |
40 | |
41 void GamepadPlatformDataFetcherAndroid::GetGamepadData(blink::WebGamepads* pads, | |
42 bool) { | |
43 JNIEnv* env = AttachCurrentThread(); | |
44 if (!env) | |
45 return; | |
46 | |
47 pads->length = WebGamepads::itemsLengthCap; | |
jdduke (slow)
2014/03/19 09:44:58
Hmm, are we guaranteed to always populate |itemLen
SaurabhK
2014/03/19 13:15:52
On 2014/03/19 09:44:58, jdduke wrote:
Yes, incorp
| |
48 Java_GamepadList_getGamepadData(env, reinterpret_cast<intptr_t>(pads)); | |
49 } | |
50 | |
51 void GamepadPlatformDataFetcherAndroid::PauseHint(bool isaccesspaused) { | |
52 JNIEnv* env = AttachCurrentThread(); | |
53 if (!env) | |
54 return; | |
55 | |
56 Java_GamepadList_notifyForGamepadsAccess(env, isaccesspaused); | |
57 } | |
58 | |
59 static void SetGamepadData(JNIEnv* env, | |
60 jobject obj, | |
61 jlong gamepads, | |
62 jint index, | |
63 jstring mapping, | |
64 jboolean connected, | |
65 jstring devicename, | |
66 jlong timestamp, | |
67 jfloatArray jaxes, | |
68 jfloatArray jbuttons) { | |
69 | |
70 blink::WebGamepad& pad = | |
71 reinterpret_cast<WebGamepads*>(gamepads)->items[index]; | |
72 pad.connected = connected; | |
73 | |
74 pad.timestamp = timestamp; | |
75 | |
76 // Do not set gamepad parameters for all the gamepad devices that are not | |
77 // attached. | |
78 if (!connected) | |
79 return; | |
80 | |
81 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | |
82 // be mapped to vendor and product information but it is only available at | |
83 // kernel level and it can not be queried using class | |
84 // android.hardware.input.InputManager. | |
85 | |
86 base::string16 device_name; | |
87 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); | |
88 const size_t name_to_copy = | |
89 std::min(device_name.size(), WebGamepad::idLengthCap - 1); | |
90 memcpy(pad.id, device_name.data(), name_to_copy); | |
91 pad.id[name_to_copy] = 0; | |
92 | |
93 base::string16 mapping_name; | |
94 base::android::ConvertJavaStringToUTF16(env, devicename, &mapping_name); | |
95 const size_t mapping_to_copy = | |
96 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); | |
97 memcpy(pad.mapping, mapping_name.data(), mapping_to_copy); | |
98 pad.mapping[mapping_to_copy] = 0; | |
99 | |
100 pad.timestamp = timestamp; | |
101 | |
102 std::vector<float> axes; | |
103 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | |
104 | |
105 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
106 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
107 // is larger than axesLengthCap. | |
108 pad.axesLength = std::min(axes.size(), WebGamepad::axesLengthCap); | |
109 | |
110 memcpy(pad.axes, axes.begin(), pad.axesLength * sizeof(float)); | |
111 | |
112 std::vector<float> buttons; | |
113 base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | |
114 | |
115 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
116 // device. Only return the first buttonsLengthCap if axeslength captured by | |
117 // GamepadList is larger than buttonsLengthCap. | |
118 pad.buttonsLength = std::min(buttons.size(), WebGamepad::buttonsLengthCap); | |
119 | |
120 // Copy buttons state to the WebGamepad buttons[]. | |
121 for (unsigned int j = 0; j < pad.buttonsLength; j++) { | |
122 pad.buttons[j].pressed = buttons[j]; | |
123 pad.buttons[j].value = buttons[j]; | |
124 } | |
125 } | |
126 | |
127 } // namespace content | |
OLD | NEW |