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

Side by Side Diff: content/browser/gamepad/gamepad_platform_data_fetcher_android.cc

Issue 1586663006: Refactoring gamepad polling to support dynamically added sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h" 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h" 10 #include "base/android/jni_array.h"
(...skipping 17 matching lines...) Expand all
28 28
29 namespace content { 29 namespace content {
30 30
31 bool 31 bool
32 GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid( 32 GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid(
33 JNIEnv* env) { 33 JNIEnv* env) {
34 return RegisterNativesImpl(env); 34 return RegisterNativesImpl(env);
35 } 35 }
36 36
37 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { 37 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() {
38 PauseHint(false);
39 } 38 }
40 39
41 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { 40 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() {
42 PauseHint(true); 41 PauseHint(true);
43 } 42 }
44 43
44 void GamepadPlatformDataFetcherAndroid::OnAddedToProvider() {
45 PauseHint(false);
46 }
47
45 void GamepadPlatformDataFetcherAndroid::GetGamepadData( 48 void GamepadPlatformDataFetcherAndroid::GetGamepadData(
46 blink::WebGamepads* pads,
47 bool devices_changed_hint) { 49 bool devices_changed_hint) {
48 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); 50 TRACE_EVENT0("GAMEPAD", "GetGamepadData");
49 51
50 pads->length = 0;
51
52 JNIEnv* env = AttachCurrentThread(); 52 JNIEnv* env = AttachCurrentThread();
53 if (!env) 53 if (!env)
54 return; 54 return;
55 55
56 Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(pads)); 56 Java_GamepadList_updateGamepadData(env, reinterpret_cast<intptr_t>(this));
57 } 57 }
58 58
59 void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) { 59 void GamepadPlatformDataFetcherAndroid::PauseHint(bool paused) {
60 JNIEnv* env = AttachCurrentThread(); 60 JNIEnv* env = AttachCurrentThread();
61 if (!env) 61 if (!env)
62 return; 62 return;
63 63
64 Java_GamepadList_setGamepadAPIActive(env, !paused); 64 Java_GamepadList_setGamepadAPIActive(env, !paused);
65 } 65 }
66 66
67 static void SetGamepadData(JNIEnv* env, 67 static void SetGamepadData(JNIEnv* env,
68 const JavaParamRef<jobject>& obj, 68 const JavaParamRef<jobject>& obj,
69 jlong gamepads, 69 jlong data_fetcher,
70 jint index, 70 jint index,
71 jboolean mapping, 71 jboolean mapping,
72 jboolean connected, 72 jboolean connected,
73 const JavaParamRef<jstring>& devicename, 73 const JavaParamRef<jstring>& devicename,
74 jlong timestamp, 74 jlong timestamp,
75 const JavaParamRef<jfloatArray>& jaxes, 75 const JavaParamRef<jfloatArray>& jaxes,
76 const JavaParamRef<jfloatArray>& jbuttons) { 76 const JavaParamRef<jfloatArray>& jbuttons) {
77 DCHECK(gamepads); 77 DCHECK(data_fetcher);
78 blink::WebGamepads* pads = reinterpret_cast<WebGamepads*>(gamepads); 78 GamepadPlatformDataFetcherAndroid* fetcher =
79 DCHECK_EQ(pads->length, unsigned(index)); 79 reinterpret_cast<GamepadPlatformDataFetcherAndroid*>(data_fetcher);
80 DCHECK_LT(index, static_cast<int>(blink::WebGamepads::itemsLengthCap)); 80 DCHECK_LT(index, static_cast<int>(blink::WebGamepads::itemsLengthCap));
81 81
82 ++pads->length;
83
84 blink::WebGamepad& pad = pads->items[index];
85
86 pad.connected = connected;
87
88 pad.timestamp = timestamp;
89
90 // Do not set gamepad parameters for all the gamepad devices that are not 82 // Do not set gamepad parameters for all the gamepad devices that are not
91 // attached. 83 // attached.
92 if (!connected) 84 if (!connected)
93 return; 85 return;
94 86
95 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should 87 PadState* state = fetcher->provider()->GetPadState(
96 // be mapped to vendor and product information but it is only available at 88 GAMEPAD_SOURCE_ANDROID, index);
97 // kernel level and it can not be queried using class
98 // android.hardware.input.InputManager.
99 // TODO(SaurabhK): Store a cached WebGamePad object in
100 // GamepadPlatformDataFetcherAndroid and only update constant WebGamepad
101 // values when a device has changed.
102 base::string16 device_name;
103 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name);
104 const size_t name_to_copy =
105 std::min(device_name.size(), WebGamepad::idLengthCap - 1);
106 memcpy(pad.id,
107 device_name.data(),
108 name_to_copy * sizeof(base::string16::value_type));
109 pad.id[name_to_copy] = 0;
110 89
111 base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : ""); 90 if (!state)
112 const size_t mapping_to_copy = 91 return;
113 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); 92
114 memcpy(pad.mapping, 93 blink::WebGamepad& pad = state->data;
115 mapping_name.data(), 94
116 mapping_to_copy * sizeof(base::string16::value_type)); 95 // Is this the first time we've seen this device?
117 pad.mapping[mapping_to_copy] = 0; 96 if (state->active_state == GAMEPAD_NEWLY_ACTIVE) {
97 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should
98 // be mapped to vendor and product information but it is only available at
99 // kernel level and it can not be queried using class
100 // android.hardware.input.InputManager.
101 base::string16 device_name;
102 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name);
103 const size_t name_to_copy =
104 std::min(device_name.size(), WebGamepad::idLengthCap - 1);
105 memcpy(pad.id,
106 device_name.data(),
107 name_to_copy * sizeof(base::string16::value_type));
108 pad.id[name_to_copy] = 0;
109
110 base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : "");
111 const size_t mapping_to_copy =
112 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1);
113 memcpy(pad.mapping,
114 mapping_name.data(),
115 mapping_to_copy * sizeof(base::string16::value_type));
116 pad.mapping[mapping_to_copy] = 0;
117 }
118 118
119 pad.timestamp = timestamp; 119 pad.timestamp = timestamp;
120 120
121 std::vector<float> axes; 121 std::vector<float> axes;
122 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); 122 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes);
123 123
124 // Set WebGamepad axeslength to total number of axes on the gamepad device. 124 // Set WebGamepad axeslength to total number of axes on the gamepad device.
125 // Only return the first axesLengthCap if axeslength captured by GamepadList 125 // Only return the first axesLengthCap if axeslength captured by GamepadList
126 // is larger than axesLengthCap. 126 // is larger than axesLengthCap.
127 pad.axesLength = std::min(static_cast<int>(axes.size()), 127 pad.axesLength = std::min(static_cast<int>(axes.size()),
(...skipping 14 matching lines...) Expand all
142 static_cast<int>(WebGamepad::buttonsLengthCap)); 142 static_cast<int>(WebGamepad::buttonsLengthCap));
143 143
144 // Copy buttons state to the WebGamepad buttons[]. 144 // Copy buttons state to the WebGamepad buttons[].
145 for (unsigned int j = 0; j < pad.buttonsLength; j++) { 145 for (unsigned int j = 0; j < pad.buttonsLength; j++) {
146 pad.buttons[j].pressed = buttons[j]; 146 pad.buttons[j].pressed = buttons[j];
147 pad.buttons[j].value = buttons[j]; 147 pad.buttons[j].value = buttons[j];
148 } 148 }
149 } 149 }
150 150
151 } // namespace content 151 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698