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/android/gamepad_reader_impl.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 | |
13 #include "jni/GamepadList_jni.h" | |
14 | |
15 using base::android::AttachCurrentThread; | |
16 using base::android::CheckException; | |
17 using base::android::ClearException; | |
18 using base::android::ConvertJavaStringToUTF8; | |
19 using base::android::ScopedJavaLocalRef; | |
20 using blink::WebGamepad; | |
21 using blink::WebGamepads; | |
22 | |
23 namespace content { | |
24 | |
25 bool GamepadsReader::RegisterGamepadsReader(JNIEnv* env) { | |
26 return RegisterNativesImpl(env); | |
27 } | |
28 | |
29 GamepadsReader::GamepadsReader() { | |
30 JNIEnv* env = AttachCurrentThread(); | |
31 CreateJavaObject(env); | |
32 } | |
33 | |
34 GamepadsReader::~GamepadsReader() { CHECK(java_gamepadList_object_.is_null()); } | |
35 | |
36 GamepadsReader* GamepadsReader::GetInstance() { | |
37 return Singleton<GamepadsReader>::get(); | |
38 } | |
39 | |
40 void GamepadsReader::GetGamepadData(blink::WebGamepads* pads) { | |
41 JNIEnv* env = AttachCurrentThread(); | |
42 if (!env) | |
43 return; | |
44 | |
45 gamepads_data_ = pads; | |
46 Java_GamepadList_getGamepadData(env, java_gamepadList_object_.obj()); | |
47 } | |
48 | |
49 void GamepadsReader::UpdateDeviceCount(JNIEnv* env, | |
50 jobject obj, | |
51 int devicecount) { | |
52 gamepads_data_->length = devicecount; | |
53 } | |
54 | |
55 void GamepadsReader::SetGamepadData(JNIEnv* env, | |
56 jobject obj, | |
57 int index, | |
58 bool connected, | |
59 jstring devicename, | |
60 int64 timestamp, | |
61 jfloatArray jaxes, | |
62 jfloatArray jbuttons) { | |
63 | |
64 blink::WebGamepad& pad = gamepads_data_->items[index]; | |
65 pad.connected = connected; | |
66 | |
67 pad.timestamp = timestamp; | |
68 | |
69 // Do not set gamepad parameters for all the gamepad devices that are not | |
70 // attached. | |
71 if (!connected) | |
72 return; | |
73 | |
74 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | |
75 // be mapped to vendor and product information but it is only available at | |
76 // kernel level and it can not be queried using class | |
77 // android.hardware.input.InputManager. | |
78 | |
79 jsize device_name_length = env->GetStringLength(devicename); | |
80 const jchar* device_name_utf16 = env->GetStringChars(devicename, 0); | |
81 COMPILE_ASSERT(sizeof(jchar) == sizeof(blink::WebUChar), | |
82 jchar_and_WebUChar_are_same_size); | |
83 const size_t bytes_to_copy = | |
84 std::min(device_name_length * sizeof(jchar), WebGamepad::idLengthCap); | |
85 memcpy(pad.id, device_name_utf16, bytes_to_copy); | |
86 pad.id[bytes_to_copy] = 0; | |
87 env->ReleaseStringChars(devicename, device_name_utf16); | |
88 | |
89 pad.timestamp = timestamp; | |
90 | |
91 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
92 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
93 // is larger than axesLengthCap. | |
94 | |
95 pad.axesLength = std::min(env->GetArrayLength(jaxes), | |
96 static_cast<int>(WebGamepad::axesLengthCap)); | |
97 jfloat* raw_axes = env->GetFloatArrayElements(jaxes, 0); | |
98 memcpy(pad.axes, raw_axes, pad.axesLength * sizeof(float)); | |
99 env->ReleaseFloatArrayElements(jaxes, raw_axes, 0); | |
100 | |
101 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
102 // device. Only return the first buttonsLengthCap if axeslength captured by | |
103 // GamepadList is larger than buttonsLengthCap. | |
104 | |
105 pad.buttonsLength = std::min(env->GetArrayLength(jbuttons), | |
106 static_cast<int>(WebGamepad::buttonsLengthCap)); | |
107 jfloat* raw_buttons = env->GetFloatArrayElements(jbuttons, 0); | |
108 memcpy(pad.buttons, raw_buttons, pad.buttonsLength * sizeof(float)); | |
bajones
2014/02/28 23:56:11
This needs to be rebased. The current code no long
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
Done.
| |
109 env->ReleaseFloatArrayElements(jbuttons, raw_buttons, 0); | |
110 } | |
111 | |
112 void GamepadsReader::NotifyForGamepadsAccess(bool access_paused) { | |
113 JNIEnv* env = AttachCurrentThread(); | |
114 if (!env) | |
115 return; | |
116 | |
117 CHECK(!java_gamepadList_object_.is_null()); | |
118 Java_GamepadList_notifyForGamepadsAccess( | |
119 env, java_gamepadList_object_.obj(), access_paused); | |
120 } | |
121 | |
122 void GamepadsReader::CreateJavaObject(JNIEnv* env) { | |
123 // Create the Java GamepadsReader object and attach it with GamepadList | |
124 // object. | |
125 java_gamepadList_object_.Reset( | |
126 Java_GamepadList_getInstance(env, reinterpret_cast<intptr_t>(this))); | |
127 CHECK(!java_gamepadList_object_.is_null()); | |
128 } | |
129 | |
130 } // namespace content | |
OLD | NEW |