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

Unified Diff: content/browser/android/gamepad_reader_impl.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, 10 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/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..f641322c2102ac815756037903a527b63f11a0cc
--- /dev/null
+++ b/content/browser/android/gamepad_reader_impl.cc
@@ -0,0 +1,130 @@
+// 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,
+ 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.
+
+ jsize device_name_length = env->GetStringLength(devicename);
+ const jchar* device_name_utf16 = env->GetStringChars(devicename, 0);
+ COMPILE_ASSERT(sizeof(jchar) == sizeof(blink::WebUChar),
+ jchar_and_WebUChar_are_same_size);
+ const size_t bytes_to_copy =
+ std::min(device_name_length * sizeof(jchar), WebGamepad::idLengthCap);
+ memcpy(pad.id, device_name_utf16, bytes_to_copy);
+ pad.id[bytes_to_copy] = 0;
+ env->ReleaseStringChars(devicename, device_name_utf16);
+
+ pad.timestamp = timestamp;
+
+ // 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(env->GetArrayLength(jaxes),
+ static_cast<int>(WebGamepad::axesLengthCap));
+ jfloat* raw_axes = env->GetFloatArrayElements(jaxes, 0);
+ memcpy(pad.axes, raw_axes, pad.axesLength * sizeof(float));
+ env->ReleaseFloatArrayElements(jaxes, raw_axes, 0);
+
+ // 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(env->GetArrayLength(jbuttons),
+ static_cast<int>(WebGamepad::buttonsLengthCap));
+ jfloat* raw_buttons = env->GetFloatArrayElements(jbuttons, 0);
+ 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.
+ env->ReleaseFloatArrayElements(jbuttons, raw_buttons, 0);
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698