Bug 1657404 - Change how AndroidGamepadManager handles gamepad service IDs r=geckoview-reviewers,agi

Currently, the AndroidGamepadManager uses a single function to both add and
remove gamepad service entries (Even though their functionality is different),
and it also uses a JNI callback to set the ID rather than simply returning it

This fixes both of those things.

Depends on D96270

Differential Revision: https://phabricator.services.mozilla.com/D96272
This commit is contained in:
Chris Martin 2020-11-10 23:02:36 +00:00
parent cc59ccf077
commit d14d817428
2 changed files with 37 additions and 32 deletions

View File

@ -4,6 +4,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// TODO: Bug 680289, implement gamepad haptics for Android.
// TODO: Bug 1523355, implement gamepad lighindicator and touch for
// Android.
#include "mozilla/java/AndroidGamepadManagerNatives.h"
#include "mozilla/java/GeckoAppShellWrappers.h"
#include "nsThreadUtils.h"
@ -16,28 +20,31 @@ class AndroidGamepadManager final
AndroidGamepadManager() = delete;
public:
static void OnGamepadChange(int32_t aID, bool aAdded) {
static int32_t NativeAddGamepad() {
RefPtr<GamepadPlatformService> service =
GamepadPlatformService::GetParentService();
MOZ_RELEASE_ASSERT(service);
const uint32_t gamepadId = service->AddGamepad(
"android", GamepadMappingType::Standard, GamepadHand::_empty,
kStandardGamepadButtons, kStandardGamepadAxes, 0, 0, 0);
MOZ_RELEASE_ASSERT(gamepadId <= INT32_MAX);
return static_cast<int32_t>(gamepadId);
}
static void NativeRemoveGamepad(int32_t aGamepadId) {
RefPtr<GamepadPlatformService> service =
GamepadPlatformService::GetParentService();
if (!service) {
return;
}
if (aAdded) {
const int svc_id = service->AddGamepad(
"android", GamepadMappingType::Standard, GamepadHand::_empty,
kStandardGamepadButtons, kStandardGamepadAxes, 0, 0,
0); // TODO: Bug 680289, implement gamepad haptics for Android.
// TODO: Bug 1523355, implement gamepad lighindicator and touch for
// Android.
java::AndroidGamepadManager::OnGamepadAdded(aID, svc_id);
} else {
service->RemoveGamepad(aID);
}
service->RemoveGamepad(aGamepadId);
}
static void OnButtonChange(int32_t aID, int32_t aButton, bool aPressed,
static void OnButtonChange(int32_t aGamepadId, int32_t aButton, bool aPressed,
float aValue) {
RefPtr<GamepadPlatformService> service =
GamepadPlatformService::GetParentService();
@ -45,10 +52,10 @@ class AndroidGamepadManager final
return;
}
service->NewButtonEvent(aID, aButton, aPressed, aValue);
service->NewButtonEvent(aGamepadId, aButton, aPressed, aValue);
}
static void OnAxisChange(int32_t aID, jni::BooleanArray::Param aValid,
static void OnAxisChange(int32_t aGamepadId, jni::BooleanArray::Param aValid,
jni::FloatArray::Param aValues) {
RefPtr<GamepadPlatformService> service =
GamepadPlatformService::GetParentService();
@ -62,7 +69,7 @@ class AndroidGamepadManager final
for (size_t i = 0; i < values.Length(); i++) {
if (valid[i]) {
service->NewAxisMoveEvent(aID, i, values[i]);
service->NewAxisMoveEvent(aGamepadId, i, values[i]);
}
}
}

View File

@ -128,11 +128,13 @@ public class AndroidGamepadManager {
}
@WrapForJNI(calledFrom = "ui")
private static native void onGamepadChange(int id, boolean added);
private static native int nativeAddGamepad();
@WrapForJNI(calledFrom = "ui")
private static native void onButtonChange(int id, int button, boolean pressed, float value);
private static native void nativeRemoveGamepad(int aGamepadId);
@WrapForJNI(calledFrom = "ui")
private static native void onAxisChange(int id, boolean[] valid, float[] values);
private static native void onButtonChange(int aGamepadId, int aButton, boolean aPressed, float aValue);
@WrapForJNI(calledFrom = "ui")
private static native void onAxisChange(int aGamepadId, boolean[] aValid, float[] aValues);
private static boolean sStarted;
private static final SparseArray<Gamepad> sGamepads = new SparseArray<>();
@ -182,16 +184,6 @@ public class AndroidGamepadManager {
}
}
@WrapForJNI
private static void onGamepadAdded(final int deviceId, final int serviceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
handleGamepadAdded(deviceId, serviceId);
}
});
}
/* package */ static void handleGamepadAdded(final int deviceId, final int serviceId) {
ThreadUtils.assertOnUiThread();
if (!sStarted) {
@ -351,12 +343,18 @@ public class AndroidGamepadManager {
private static void addGamepad(final InputDevice device) {
sPendingGamepads.put(device.getId(), new ArrayList<KeyEvent>());
onGamepadChange(device.getId(), true);
final int gamepadId = nativeAddGamepad();
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
handleGamepadAdded(device.getId(), gamepadId);
}
});
}
private static void removeGamepad(final int deviceId) {
Gamepad gamepad = sGamepads.get(deviceId);
onGamepadChange(gamepad.id, false);
nativeRemoveGamepad(gamepad.id);
sGamepads.remove(deviceId);
}