Index: content/browser/android/gamepad_reader_impl.cc |
diff --git a/content/browser/android/gamepad_reader_impl.cc b/content/browser/android/gamepad_reader_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5000a23bb8d33dd5b68ee69f5b9612bbf09fe564 |
--- /dev/null |
+++ b/content/browser/android/gamepad_reader_impl.cc |
@@ -0,0 +1,144 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/android/gamepad_reader_impl.h" |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+ |
+#include "jni/GamepadList_jni.h" |
+ |
+using base::android::AttachCurrentThread; |
+using base::android::CheckException; |
+using base::android::ClearException; |
+using base::android::ConvertJavaStringToUTF8; |
+using base::android::ScopedJavaLocalRef; |
+using blink::WebGamepad; |
+using blink::WebGamepads; |
+ |
+namespace content { |
+ |
+bool GamepadsReader::RegisterGamepadsReader(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+GamepadsReader::GamepadsReader() { |
+ JNIEnv* env = AttachCurrentThread(); |
+ CreateJavaObject(env); |
+} |
+ |
+GamepadsReader::~GamepadsReader() { CHECK(java_gamepadList_object_.is_null()); } |
+ |
+GamepadsReader* GamepadsReader::GetInstance() { |
+ return Singleton<GamepadsReader>::get(); |
+} |
+ |
+void GamepadsReader::GetGamepadData(blink::WebGamepads* pads) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ if (!env) |
+ return; |
+ |
+ gamepads_data_ = pads; |
+ Java_GamepadList_getGamepadData(env, java_gamepadList_object_.obj()); |
+} |
+ |
+void GamepadsReader::UpdateDeviceCount(JNIEnv* env, |
+ jobject obj, |
+ int devicecount) { |
+ gamepads_data_->length = devicecount; |
+} |
+ |
+void GamepadsReader::SetGamepadData(JNIEnv* env, |
+ jobject obj, |
+ int index, |
+ jstring mapping, |
+ bool connected, |
+ jstring devicename, |
+ int64 timestamp, |
+ jfloatArray jaxes, |
+ jfloatArray jbuttons) { |
+ |
+ blink::WebGamepad& pad = gamepads_data_->items[index]; |
+ pad.connected = connected; |
+ |
+ pad.timestamp = timestamp; |
+ |
+ // Do not set gamepad parameters for all the gamepad devices that are not |
+ // attached. |
+ if (!connected) |
+ return; |
+ |
+ // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should |
+ // be mapped to vendor and product information but it is only available at |
+ // kernel level and it can not be queried using class |
+ // android.hardware.input.InputManager. |
+ |
+ base::string16 device_name; |
+ base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); |
+ const size_t name_to_copy = |
+ std::min(device_name.size(), WebGamepad::idLengthCap - 1); |
+ memcpy(pad.id, device_name.data(), name_to_copy); |
+ pad.id[name_to_copy] = 0; |
+ pad.id[name_to_copy] = 0; |
jdduke (slow)
2014/03/18 09:43:15
Looks like we have an extra assignment here, also
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Oh yes, som
|
+ |
+ base::string16 mapping_name; |
+ base::android::ConvertJavaStringToUTF16(env, devicename, &mapping_name); |
+ const size_t mapping_to_copy = |
+ std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); |
+ memcpy(pad.mapping, mapping_name.data(), mapping_to_copy); |
+ pad.mapping[mapping_to_copy] = 0; |
+ pad.mapping[mapping_to_copy] = 0; |
+ |
+ pad.timestamp = timestamp; |
+ |
+ std::vector<float> axes; |
+ base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); |
+ |
+ // Set WebGamepad axeslength to total number of axes on the gamepad device. |
+ // Only return the first axesLengthCap if axeslength captured by GamepadList |
+ // is larger than axesLengthCap. |
+ pad.axesLength = (axes.size() > WebGamepad::axesLengthCap) |
jdduke (slow)
2014/03/18 09:43:15
Why not std::min?
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Using it no
|
+ ? WebGamepad::axesLengthCap |
+ : axes.size(); |
+ memcpy(pad.axes, axes.begin(), pad.axesLength * sizeof(float)); |
+ |
+ std::vector<float> buttons; |
+ base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); |
+ |
+ // Set WebGamepad buttonslength to total number of axes on the gamepad |
+ // device. Only return the first buttonsLengthCap if axeslength captured by |
+ // GamepadList is larger than buttonsLengthCap. |
+ |
+ pad.buttonsLength = (buttons.size() > WebGamepad::buttonsLengthCap) |
jdduke (slow)
2014/03/18 09:43:15
Also std::min.
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Chainging i
|
+ ? WebGamepad::buttonsLengthCap |
+ : buttons.size(); |
+ // Copy buttons state to the WebGamepad buttons[]. |
+ for (unsigned int j = 0; j < pad.buttonsLength; j++) { |
+ pad.buttons[j].pressed = buttons[j]; |
+ pad.buttons[j].value = buttons[j]; |
+ } |
+} |
+ |
+void GamepadsReader::NotifyForGamepadsAccess(bool access_paused) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ if (!env) |
+ return; |
+ |
+ CHECK(!java_gamepadList_object_.is_null()); |
+ Java_GamepadList_notifyForGamepadsAccess( |
+ env, java_gamepadList_object_.obj(), access_paused); |
+} |
+ |
+void GamepadsReader::CreateJavaObject(JNIEnv* env) { |
+ // Create the Java GamepadsReader object and attach it with GamepadList |
+ // object. |
+ java_gamepadList_object_.Reset( |
+ Java_GamepadList_getInstance(env, reinterpret_cast<intptr_t>(this))); |
+ CHECK(!java_gamepadList_object_.is_null()); |
+} |
+ |
+} // namespace content |