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

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..7b747e54fe9611ade69ebcbc070389e390d260df
--- /dev/null
+++ b/content/browser/android/gamepad_reader_impl.cc
@@ -0,0 +1,136 @@
+// 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;
kbalazs 2014/02/20 00:48:24 I don't think we need this, other platforms just i
+}
+
+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 and then convert
+ // the utf-8 id string to WebUChar. 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.
+
+ std::string device_name =
+ base::android::ConvertJavaStringToUTF8(env, devicename);
+ base::TruncateUTF8ToByteSize(
+ device_name, blink::WebGamepad::idLengthCap - 1, &device_name);
+ base::string16 tmp16 = base::UTF8ToUTF16(device_name);
kbalazs 2014/02/20 00:48:24 Can we get rid of converting back and forth (utf16
+ memset(pad.id, 0, sizeof(pad.id));
+ tmp16.copy(pad.id, arraysize(pad.id) - 1);
+
+ 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.
+
+ std::vector<float> axes;
+ base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes);
+ pad.axesLength = (axes.size() > WebGamepad::axesLengthCap)
+ ? WebGamepad::axesLengthCap
+ : axes.size();
+
+ // Copy axes state to the WebGamepad axes[].
+ for (unsigned int j = 0; j < pad.axesLength; j++)
kbalazs 2014/02/20 00:48:24 This can also be done with only one copy instead o
+ pad.axes[j] = axes[j];
+
+ // 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.
+
+ std::vector<float> buttons;
+ base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons);
kbalazs 2014/02/20 00:48:24 Ditto.
+ pad.buttonsLength = (buttons.size() > WebGamepad::buttonsLengthCap)
+ ? WebGamepad::buttonsLengthCap
+ : buttons.size();
+
+ // Copy buttons state to the WebGamepad buttons[].
+ for (unsigned int j = 0; j < pad.buttonsLength; j++)
+ pad.buttons[j] = buttons[j];
+}
+
+void GamepadsReader::NotifyForGamepadsAccess(bool isaccesspaused) {
kbalazs 2014/02/20 00:48:24 is_access_paused? Or just access_paused?
+ JNIEnv* env = AttachCurrentThread();
+ if (!env)
+ return;
+
+ CHECK(!java_gamepadList_object_.is_null());
+ Java_GamepadList_notifyForGamepadsAccess(
+ env, java_gamepadList_object_.obj(), isaccesspaused);
+}
+
+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