Chromium Code Reviews| 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..98c82d5ff51b21ca49b78e8c1b7fc5c011aea1cd |
| --- /dev/null |
| +++ b/content/browser/gamepad/gamepad_platform_data_fetcher_android.cc |
| @@ -0,0 +1,99 @@ |
| +// 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/gamepad/gamepad_platform_data_fetcher_android.h" |
| + |
| +#include "base/android/jni_array.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/debug/trace_event.h" |
| +#include "base/strings/string16.h" |
| +#include "jni/GamepadAdapter_jni.h" |
| +#include "third_party/WebKit/public/platform/WebGamepad.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +void CopyJavaStringToWebUCharArray( |
| + JNIEnv* env, jstring src, blink::WebUChar* array, size_t array_length) { |
| + COMPILE_ASSERT(sizeof(base::string16::value_type) == sizeof(blink::WebUChar), |
| + string16_and_WebUChar_are_same_size); |
| + base::string16 data; |
| + base::android::ConvertJavaStringToUTF16(env, src, &data); |
| + CHECK(array_length > 0); |
| + const size_t bytes_to_copy = std::min(data.size(), array_length - 1); |
| + memcpy(array, data.data(), bytes_to_copy); |
| + array[bytes_to_copy] = 0; |
| +} |
| + |
| +} |
| + |
| +using base::android::AttachCurrentThread; |
| +using blink::WebGamepad; |
| +using blink::WebGamepads; |
|
jdduke (slow)
2014/03/07 23:16:39
Please move the using statements above the initial
kbalazs
2014/03/12 00:17:36
Done.
|
| + |
| +GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { |
| + j_gamepad_adapter_.Reset( |
|
jdduke (slow)
2014/03/07 23:16:39
If the GamePadAdapter is a singleton in practice,
kbalazs
2014/03/12 00:17:36
Done.
|
| + Java_GamepadAdapter_attach(AttachCurrentThread(), |
| + reinterpret_cast<intptr_t>(this))); |
| +} |
| + |
| +GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { |
| +} |
| + |
| +void GamepadPlatformDataFetcherAndroid::GetGamepadData( |
| + blink::WebGamepads* pads, |
| + bool devices_changed_hint) { |
| + TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
| + data_ = pads; |
| + data_->length = WebGamepads::itemsLengthCap; |
|
jdduke (slow)
2014/03/07 23:16:39
Rather than storing |data_| and assuming we'll get
jdduke (slow)
2014/03/07 23:33:27
I see now, you definitely still need attach in som
kbalazs
2014/03/12 00:17:36
Done.
|
| + Java_GamepadAdapter_getGamepadData( |
| + AttachCurrentThread(), j_gamepad_adapter_.obj()); |
| + data_ = NULL; |
| +} |
| + |
| +void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) { |
| + Java_GamepadAdapter_setFetched( |
|
jdduke (slow)
2014/03/07 23:16:39
Same with the other function, make this a static c
kbalazs
2014/03/12 00:17:36
Done.
|
| + AttachCurrentThread(), j_gamepad_adapter_.obj(), !paused); |
| +} |
| + |
| +void GamepadPlatformDataFetcherAndroid::RefreshDevice( |
|
jdduke (slow)
2014/03/07 23:16:39
This function can be made static (and local to thi
|
| + JNIEnv* env, |
| + jobject obj, |
| + int index, |
| + bool connected, |
| + jstring id, |
| + jstring mapping, |
| + long timestamp, |
| + jfloatArray axes, |
| + jfloatArray buttons) { |
| + CHECK(data_); |
| + WebGamepad& pad = data_->items[index]; |
| + pad.connected = connected; |
| + if (!connected) |
| + return; |
| + CopyJavaStringToWebUCharArray(env, id, pad.id, WebGamepad::idLengthCap); |
| + CopyJavaStringToWebUCharArray(env, mapping, pad.mapping, |
| + WebGamepad::mappingLengthCap); |
| + pad.timestamp = timestamp; |
| + std::vector<float> axes_data; |
| + base::android::JavaFloatArrayToFloatVector(env, axes, &axes_data); |
| + pad.axesLength = std::min(axes_data.size(), WebGamepad::axesLengthCap); |
| + memcpy(pad.axes, axes_data.begin(), pad.axesLength * sizeof(float)); |
|
jdduke (slow)
2014/03/07 23:16:39
Could you add a line space in between each logical
kbalazs
2014/03/12 00:17:36
Done.
|
| + std::vector<float> buttons_data; |
| + base::android::JavaFloatArrayToFloatVector(env, buttons, &buttons_data); |
| + pad.buttonsLength = |
| + std::min(buttons_data.size(), WebGamepad::buttonsLengthCap); |
| + for (unsigned i = 0; i < pad.buttonsLength; ++i) { |
| + float value = buttons_data[i]; |
| + pad.buttons[i].pressed = value; |
| + pad.buttons[i].value = value; |
| + } |
| +} |
| + |
| +bool GamepadPlatformDataFetcherAndroid::RegisterGamepadAdapter(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} |