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

Unified Diff: device/gamepad/gamepad_platform_data_fetcher_android.cc

Issue 2129003002: Refactored gamepad polling to support dynamic sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed unit test issue and Mac XBoxDataFetcher constructor Created 4 years, 5 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: device/gamepad/gamepad_platform_data_fetcher_android.cc
diff --git a/device/gamepad/gamepad_platform_data_fetcher_android.cc b/device/gamepad/gamepad_platform_data_fetcher_android.cc
index 9a14515f608e5275e1574dbde315b25a8e1faa05..58e3fd94e01a33bec70f8cb7f504b26ad258d74e 100644
--- a/device/gamepad/gamepad_platform_data_fetcher_android.cc
+++ b/device/gamepad/gamepad_platform_data_fetcher_android.cc
@@ -34,25 +34,29 @@ bool GamepadPlatformDataFetcherAndroid::
}
GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() {
- PauseHint(false);
}
GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() {
PauseHint(true);
}
+GamepadSource GamepadPlatformDataFetcherAndroid::source() {
+ return Factory::static_source();
+}
+
+void GamepadPlatformDataFetcherAndroid::OnAddedToProvider() {
+ PauseHint(false);
+}
+
void GamepadPlatformDataFetcherAndroid::GetGamepadData(
- blink::WebGamepads* pads,
bool devices_changed_hint) {
TRACE_EVENT0("GAMEPAD", "GetGamepadData");
- pads->length = 0;
-
JNIEnv* env = AttachCurrentThread();
if (!env)
return;
- Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(pads));
+ Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(this));
}
void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) {
@@ -65,7 +69,7 @@ void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) {
static void SetGamepadData(JNIEnv* env,
const JavaParamRef<jobject>& obj,
- jlong gamepads,
+ jlong data_fetcher,
jint index,
jboolean mapping,
jboolean connected,
@@ -73,45 +77,44 @@ static void SetGamepadData(JNIEnv* env,
jlong timestamp,
const JavaParamRef<jfloatArray>& jaxes,
const JavaParamRef<jfloatArray>& jbuttons) {
- DCHECK(gamepads);
- blink::WebGamepads* pads = reinterpret_cast<WebGamepads*>(gamepads);
- DCHECK_EQ(pads->length, unsigned(index));
+ DCHECK(data_fetcher);
+ GamepadPlatformDataFetcherAndroid* fetcher =
+ reinterpret_cast<GamepadPlatformDataFetcherAndroid*>(data_fetcher);
DCHECK_LT(index, static_cast<int>(blink::WebGamepads::itemsLengthCap));
- ++pads->length;
-
- blink::WebGamepad& pad = pads->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.
- // TODO(SaurabhK): Store a cached WebGamePad object in
- // GamepadPlatformDataFetcherAndroid and only update constant WebGamepad
- // values when a device has changed.
- 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 * sizeof(base::string16::value_type));
- pad.id[name_to_copy] = 0;
-
- base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : "");
- const size_t mapping_to_copy =
- std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1);
- memcpy(pad.mapping, mapping_name.data(),
- mapping_to_copy * sizeof(base::string16::value_type));
- pad.mapping[mapping_to_copy] = 0;
+ PadState* state = fetcher->GetPadState(index);
+
+ if (!state)
+ return;
+
+ blink::WebGamepad& pad = state->data;
+
+ // Is this the first time we've seen this device?
+ if (state->active_state == GAMEPAD_NEWLY_ACTIVE) {
+ // 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 * sizeof(base::string16::value_type));
+ pad.id[name_to_copy] = 0;
+
+ base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : "");
+ const size_t mapping_to_copy =
+ std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1);
+ memcpy(pad.mapping, mapping_name.data(),
+ mapping_to_copy * sizeof(base::string16::value_type));
+ pad.mapping[mapping_to_copy] = 0;
+ }
pad.timestamp = timestamp;

Powered by Google App Engine
This is Rietveld 408576698