 Chromium Code Reviews
 Chromium Code Reviews Issue 133943002:
  Gamepad API support for chrome on android  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master
    
  
    Issue 133943002:
  Gamepad API support for chrome on android  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 
Ted C
2014/04/16 23:36:53
no (c) here either
 
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:36:53, Ted C wrote:
Again, i hav
 | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_android.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/debug/trace_event.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 | |
| 15 #include "jni/GamepadList_jni.h" | |
| 16 | |
| 17 #include "third_party/WebKit/public/platform/WebGamepads.h" | |
| 18 | |
| 19 using base::android::AttachCurrentThread; | |
| 20 using base::android::CheckException; | |
| 21 using base::android::ClearException; | |
| 22 using base::android::ConvertJavaStringToUTF8; | |
| 23 using base::android::ScopedJavaLocalRef; | |
| 24 using blink::WebGamepad; | |
| 25 using blink::WebGamepads; | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 bool | |
| 30 GamepadPlatformDataFetcherAndroid::RegisterGamepadPlatformDataFetcherAndroid( | |
| 31 JNIEnv* env) { | |
| 32 return RegisterNativesImpl(env); | |
| 33 } | |
| 34 | |
| 35 GamepadPlatformDataFetcherAndroid::GamepadPlatformDataFetcherAndroid() { | |
| 36 PauseHint(false); | |
| 37 } | |
| 38 | |
| 39 GamepadPlatformDataFetcherAndroid::~GamepadPlatformDataFetcherAndroid() { | |
| 40 PauseHint(true); | |
| 41 } | |
| 42 | |
| 43 void GamepadPlatformDataFetcherAndroid::GetGamepadData(blink::WebGamepads* pads, | |
| 44 bool) { | |
| 
Ted C
2014/04/16 23:36:53
include the paramater name even if unused.
 
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:36:53, Ted C wrote:
Done.
 | |
| 45 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | |
| 46 | |
| 47 pads->length = 0; | |
| 48 | |
| 49 JNIEnv* env = AttachCurrentThread(); | |
| 50 if (!env) | |
| 51 return; | |
| 52 | |
| 53 Java_GamepadList_getGamepadData(env, reinterpret_cast<intptr_t>(pads)); | |
| 54 } | |
| 55 | |
| 56 void GamepadPlatformDataFetcherAndroid::PauseHint(bool isaccesspaused) { | |
| 
Ted C
2014/04/16 23:36:53
variable name should match the header
 
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:36:53, Ted C wrote:
Done.
 | |
| 57 JNIEnv* env = AttachCurrentThread(); | |
| 58 if (!env) | |
| 59 return; | |
| 60 | |
| 61 Java_GamepadList_notifyForGamepadsAccess(env, isaccesspaused); | |
| 62 } | |
| 63 | |
| 64 static void SetGamepadData(JNIEnv* env, | |
| 65 jobject obj, | |
| 66 jlong gamepads, | |
| 67 jint index, | |
| 68 jboolean mapping, | |
| 69 jboolean connected, | |
| 70 jstring devicename, | |
| 71 jlong timestamp, | |
| 72 jfloatArray jaxes, | |
| 73 jfloatArray jbuttons) { | |
| 74 DCHECK(gamepads); | |
| 75 blink::WebGamepads* pads = reinterpret_cast<WebGamepads*>(gamepads); | |
| 76 DCHECK_EQ(pads->length, unsigned(index)); | |
| 77 DCHECK_LT(unsigned(index), WebGamepads::itemsLengthCap); | |
| 78 | |
| 79 ++pads->length; | |
| 80 | |
| 81 blink::WebGamepad& pad = pads->items[index]; | |
| 82 | |
| 83 pad.connected = connected; | |
| 84 | |
| 85 pad.timestamp = timestamp; | |
| 86 | |
| 87 // Do not set gamepad parameters for all the gamepad devices that are not | |
| 88 // attached. | |
| 89 if (!connected) | |
| 90 return; | |
| 91 | |
| 92 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | |
| 93 // be mapped to vendor and product information but it is only available at | |
| 94 // kernel level and it can not be queried using class | |
| 95 // android.hardware.input.InputManager. | |
| 96 base::string16 device_name; | |
| 97 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); | |
| 
jdduke (slow)
2014/04/16 23:23:44
It's a shame to have to do this conversion every f
 
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:23:44, jdduke wrote:
Done.
 
jdduke (slow)
2014/04/21 19:53:58
Hmm, is the "Done." part in a patch that hasn't be
 
SaurabhK
2014/04/22 09:33:52
On 2014/04/21 19:53:58, jdduke wrote:
I meant tha
 | |
| 98 const size_t name_to_copy = | |
| 99 std::min(device_name.size(), WebGamepad::idLengthCap - 1); | |
| 100 memcpy(pad.id, | |
| 101 device_name.data(), | |
| 102 name_to_copy * sizeof(base::string16::value_type)); | |
| 103 pad.id[name_to_copy] = 0; | |
| 104 | |
| 105 base::string16 mapping_name = base::UTF8ToUTF16(mapping ? "standard" : ""); | |
| 106 const size_t mapping_to_copy = | |
| 107 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); | |
| 108 memcpy(pad.mapping, | |
| 109 mapping_name.data(), | |
| 110 mapping_to_copy * sizeof(base::string16::value_type)); | |
| 111 pad.mapping[mapping_to_copy] = 0; | |
| 112 | |
| 113 pad.timestamp = timestamp; | |
| 114 | |
| 115 std::vector<float> axes; | |
| 116 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | |
| 117 | |
| 118 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
| 119 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
| 120 // is larger than axesLengthCap. | |
| 121 pad.axesLength = std::min(axes.size(), WebGamepad::axesLengthCap); | |
| 122 | |
| 123 memcpy(pad.axes, axes.begin(), pad.axesLength * sizeof(float)); | |
| 124 | |
| 125 std::vector<float> buttons; | |
| 126 base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | |
| 127 | |
| 128 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
| 129 // device. Only return the first buttonsLengthCap if axeslength captured by | |
| 130 // GamepadList is larger than buttonsLengthCap. | |
| 131 pad.buttonsLength = std::min(buttons.size(), WebGamepad::buttonsLengthCap); | |
| 132 | |
| 133 // Copy buttons state to the WebGamepad buttons[]. | |
| 134 for (unsigned int j = 0; j < pad.buttonsLength; j++) { | |
| 135 pad.buttons[j].pressed = buttons[j]; | |
| 136 pad.buttons[j].value = buttons[j]; | |
| 
jdduke (slow)
2014/04/16 23:23:44
Should this be |pad.buttons[j].value = buttons[j]
 
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:23:44, jdduke wrote:
We already
 | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 } // namespace content | |
| OLD | NEW |