Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/android/gamepad_reader_impl.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 | |
| 13 #include "jni/GamepadList_jni.h" | |
| 14 | |
| 15 using base::android::AttachCurrentThread; | |
| 16 using base::android::CheckException; | |
| 17 using base::android::ClearException; | |
| 18 using base::android::ConvertJavaStringToUTF8; | |
| 19 using base::android::ScopedJavaLocalRef; | |
| 20 using blink::WebGamepad; | |
| 21 using blink::WebGamepads; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 bool GamepadsReader::RegisterGamepadsReader(JNIEnv* env) { | |
| 26 return RegisterNativesImpl(env); | |
| 27 } | |
| 28 | |
| 29 GamepadsReader::GamepadsReader() { | |
| 30 JNIEnv* env = AttachCurrentThread(); | |
| 31 CreateJavaObject(env); | |
| 32 } | |
| 33 | |
| 34 GamepadsReader::~GamepadsReader() { CHECK(java_gamepadList_object_.is_null()); } | |
| 35 | |
| 36 GamepadsReader* GamepadsReader::GetInstance() { | |
| 37 return Singleton<GamepadsReader>::get(); | |
| 38 } | |
| 39 | |
| 40 void GamepadsReader::GetGamepadData(blink::WebGamepads* pads) { | |
| 41 JNIEnv* env = AttachCurrentThread(); | |
| 42 if (!env) | |
| 43 return; | |
| 44 | |
| 45 gamepads_data_ = pads; | |
| 46 Java_GamepadList_getGamepadData(env, java_gamepadList_object_.obj()); | |
| 47 } | |
| 48 | |
| 49 void GamepadsReader::UpdateDeviceCount(JNIEnv* env, | |
| 50 jobject obj, | |
| 51 int devicecount) { | |
| 52 gamepads_data_->length = devicecount; | |
| 53 } | |
| 54 | |
| 55 void GamepadsReader::SetGamepadData(JNIEnv* env, | |
| 56 jobject obj, | |
| 57 int index, | |
| 58 jstring mapping, | |
| 59 bool connected, | |
| 60 jstring devicename, | |
| 61 int64 timestamp, | |
| 62 jfloatArray jaxes, | |
| 63 jfloatArray jbuttons) { | |
| 64 | |
| 65 blink::WebGamepad& pad = gamepads_data_->items[index]; | |
| 66 pad.connected = connected; | |
| 67 | |
| 68 pad.timestamp = timestamp; | |
| 69 | |
| 70 // Do not set gamepad parameters for all the gamepad devices that are not | |
| 71 // attached. | |
| 72 if (!connected) | |
| 73 return; | |
| 74 | |
| 75 // Map the Gamepad DeviceName String to the WebGamepad Id. Ideally it should | |
| 76 // be mapped to vendor and product information but it is only available at | |
| 77 // kernel level and it can not be queried using class | |
| 78 // android.hardware.input.InputManager. | |
| 79 | |
| 80 base::string16 device_name; | |
| 81 base::android::ConvertJavaStringToUTF16(env, devicename, &device_name); | |
| 82 const size_t name_to_copy = | |
| 83 std::min(device_name.size(), WebGamepad::idLengthCap - 1); | |
| 84 memcpy(pad.id, device_name.data(), name_to_copy); | |
| 85 pad.id[name_to_copy] = 0; | |
| 86 pad.id[name_to_copy] = 0; | |
|
jdduke (slow)
2014/03/18 09:43:15
Looks like we have an extra assignment here, also
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Oh yes, som
| |
| 87 | |
| 88 base::string16 mapping_name; | |
| 89 base::android::ConvertJavaStringToUTF16(env, devicename, &mapping_name); | |
| 90 const size_t mapping_to_copy = | |
| 91 std::min(mapping_name.size(), WebGamepad::mappingLengthCap - 1); | |
| 92 memcpy(pad.mapping, mapping_name.data(), mapping_to_copy); | |
| 93 pad.mapping[mapping_to_copy] = 0; | |
| 94 pad.mapping[mapping_to_copy] = 0; | |
| 95 | |
| 96 pad.timestamp = timestamp; | |
| 97 | |
| 98 std::vector<float> axes; | |
| 99 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | |
| 100 | |
| 101 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
| 102 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
| 103 // is larger than axesLengthCap. | |
| 104 pad.axesLength = (axes.size() > WebGamepad::axesLengthCap) | |
|
jdduke (slow)
2014/03/18 09:43:15
Why not std::min?
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Using it no
| |
| 105 ? WebGamepad::axesLengthCap | |
| 106 : axes.size(); | |
| 107 memcpy(pad.axes, axes.begin(), pad.axesLength * sizeof(float)); | |
| 108 | |
| 109 std::vector<float> buttons; | |
| 110 base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | |
| 111 | |
| 112 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
| 113 // device. Only return the first buttonsLengthCap if axeslength captured by | |
| 114 // GamepadList is larger than buttonsLengthCap. | |
| 115 | |
| 116 pad.buttonsLength = (buttons.size() > WebGamepad::buttonsLengthCap) | |
|
jdduke (slow)
2014/03/18 09:43:15
Also std::min.
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Chainging i
| |
| 117 ? WebGamepad::buttonsLengthCap | |
| 118 : buttons.size(); | |
| 119 // Copy buttons state to the WebGamepad buttons[]. | |
| 120 for (unsigned int j = 0; j < pad.buttonsLength; j++) { | |
| 121 pad.buttons[j].pressed = buttons[j]; | |
| 122 pad.buttons[j].value = buttons[j]; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void GamepadsReader::NotifyForGamepadsAccess(bool access_paused) { | |
| 127 JNIEnv* env = AttachCurrentThread(); | |
| 128 if (!env) | |
| 129 return; | |
| 130 | |
| 131 CHECK(!java_gamepadList_object_.is_null()); | |
| 132 Java_GamepadList_notifyForGamepadsAccess( | |
| 133 env, java_gamepadList_object_.obj(), access_paused); | |
| 134 } | |
| 135 | |
| 136 void GamepadsReader::CreateJavaObject(JNIEnv* env) { | |
| 137 // Create the Java GamepadsReader object and attach it with GamepadList | |
| 138 // object. | |
| 139 java_gamepadList_object_.Reset( | |
| 140 Java_GamepadList_getInstance(env, reinterpret_cast<intptr_t>(this))); | |
| 141 CHECK(!java_gamepadList_object_.is_null()); | |
| 142 } | |
| 143 | |
| 144 } // namespace content | |
| OLD | NEW |