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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/gamepad/gamepad_platform_data_fetcher_android.cc
diff --git a/content/browser/gamepad/gamepad_platform_data_fetcher_android.cc b/content/browser/gamepad/gamepad_platform_data_fetcher_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..25e08f03ae6fa9f2a4356a760fe5d8539d000804
--- /dev/null
+++ b/content/browser/gamepad/gamepad_platform_data_fetcher_android.cc
@@ -0,0 +1,127 @@
+// Copyright (c) 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/gamepad/gamepad_platform_data_fetcher_android.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_array.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"
+
+#include "third_party/WebKit/public/platform/WebGamepads.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
+GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid(
+ JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() {
+ PauseHint(false);
+}
+
+GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() {
+}
+
+void GamepadPlatformDataFetcherAndroid::GetGamepadData(blink::WebGamepads* pads,
+ bool) {
+ JNIEnv* env = AttachCurrentThread();
+ if (!env)
+ return;
+
+ 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
+ Java_GamepadList_getGamepadData(env, reinterpret_cast<intptr_t>(pads));
+}
+
+void GamepadPlatformDataFetcherAndroid::PauseHint(bool isaccesspaused) {
+ JNIEnv* env = AttachCurrentThread();
+ if (!env)
+ return;
+
+ Java_GamepadList_notifyForGamepadsAccess(env, isaccesspaused);
+}
+
+static void SetGamepadData(JNIEnv* env,
+ jobject obj,
+ jlong gamepads,
+ jint index,
+ jstring mapping,
+ jboolean connected,
+ jstring devicename,
+ jlong timestamp,
+ jfloatArray jaxes,
+ jfloatArray jbuttons) {
+
+ blink::WebGamepad& pad =
+ reinterpret_cast<WebGamepads*>(gamepads)->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;
+
+ 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.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 = std::min(axes.size(), WebGamepad::axesLengthCap);
+
+ 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 = std::min(buttons.size(), WebGamepad::buttonsLengthCap);
+
+ // 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];
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698