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; | |
|
kbalazs
2014/02/20 00:48:24
I don't think we need this, other platforms just i
| |
| 53 } | |
| 54 | |
| 55 void GamepadsReader::SetGamepadData(JNIEnv* env, | |
| 56 jobject obj, | |
| 57 int index, | |
| 58 bool connected, | |
| 59 jstring devicename, | |
| 60 int64 timestamp, | |
| 61 jfloatArray jaxes, | |
| 62 jfloatArray jbuttons) { | |
| 63 | |
| 64 blink::WebGamepad& pad = gamepads_data_->items[index]; | |
| 65 pad.connected = connected; | |
| 66 | |
| 67 pad.timestamp = timestamp; | |
| 68 | |
| 69 // Do not set gamepad parameters for all the gamepad devices that are not | |
| 70 // attached. | |
| 71 if (!connected) | |
| 72 return; | |
| 73 | |
| 74 // Map the Gamepad DeviceName String to the WebGamepad Id and then convert | |
| 75 // the utf-8 id string to WebUChar. Ideally it should be mapped to vendor and | |
| 76 // product information but it is only available at kernel level and it can | |
| 77 // not be queried using class android.hardware.input.InputManager. | |
| 78 | |
| 79 std::string device_name = | |
| 80 base::android::ConvertJavaStringToUTF8(env, devicename); | |
| 81 base::TruncateUTF8ToByteSize( | |
| 82 device_name, blink::WebGamepad::idLengthCap - 1, &device_name); | |
| 83 base::string16 tmp16 = base::UTF8ToUTF16(device_name); | |
|
kbalazs
2014/02/20 00:48:24
Can we get rid of converting back and forth (utf16
| |
| 84 memset(pad.id, 0, sizeof(pad.id)); | |
| 85 tmp16.copy(pad.id, arraysize(pad.id) - 1); | |
| 86 | |
| 87 pad.timestamp = timestamp; | |
| 88 | |
| 89 // Set WebGamepad axeslength to total number of axes on the gamepad device. | |
| 90 // Only return the first axesLengthCap if axeslength captured by GamepadList | |
| 91 // is larger than axesLengthCap. | |
| 92 | |
| 93 std::vector<float> axes; | |
| 94 base::android::JavaFloatArrayToFloatVector(env, jaxes, &axes); | |
| 95 pad.axesLength = (axes.size() > WebGamepad::axesLengthCap) | |
| 96 ? WebGamepad::axesLengthCap | |
| 97 : axes.size(); | |
| 98 | |
| 99 // Copy axes state to the WebGamepad axes[]. | |
| 100 for (unsigned int j = 0; j < pad.axesLength; j++) | |
|
kbalazs
2014/02/20 00:48:24
This can also be done with only one copy instead o
| |
| 101 pad.axes[j] = axes[j]; | |
| 102 | |
| 103 // Set WebGamepad buttonslength to total number of axes on the gamepad | |
| 104 // device. Only return the first buttonsLengthCap if axeslength captured by | |
| 105 // GamepadList is larger than buttonsLengthCap. | |
| 106 | |
| 107 std::vector<float> buttons; | |
| 108 base::android::JavaFloatArrayToFloatVector(env, jbuttons, &buttons); | |
|
kbalazs
2014/02/20 00:48:24
Ditto.
| |
| 109 pad.buttonsLength = (buttons.size() > WebGamepad::buttonsLengthCap) | |
| 110 ? WebGamepad::buttonsLengthCap | |
| 111 : buttons.size(); | |
| 112 | |
| 113 // Copy buttons state to the WebGamepad buttons[]. | |
| 114 for (unsigned int j = 0; j < pad.buttonsLength; j++) | |
| 115 pad.buttons[j] = buttons[j]; | |
| 116 } | |
| 117 | |
| 118 void GamepadsReader::NotifyForGamepadsAccess(bool isaccesspaused) { | |
|
kbalazs
2014/02/20 00:48:24
is_access_paused? Or just access_paused?
| |
| 119 JNIEnv* env = AttachCurrentThread(); | |
| 120 if (!env) | |
| 121 return; | |
| 122 | |
| 123 CHECK(!java_gamepadList_object_.is_null()); | |
| 124 Java_GamepadList_notifyForGamepadsAccess( | |
| 125 env, java_gamepadList_object_.obj(), isaccesspaused); | |
| 126 } | |
| 127 | |
| 128 void GamepadsReader::CreateJavaObject(JNIEnv* env) { | |
| 129 // Create the Java GamepadsReader object and attach it with GamepadList | |
| 130 // object. | |
| 131 java_gamepadList_object_.Reset( | |
| 132 Java_GamepadList_getInstance(env, reinterpret_cast<intptr_t>(this))); | |
| 133 CHECK(!java_gamepadList_object_.is_null()); | |
| 134 } | |
| 135 | |
| 136 } // namespace content | |
| OLD | NEW |