Merge branch 'master' into overlay-haptics

This commit is contained in:
codokie
2026-04-06 09:50:39 +03:00
committed by GitHub
3344 changed files with 556197 additions and 803331 deletions

View File

@@ -31,6 +31,8 @@
namespace
{
std::string SOURCE = "Android";
jclass s_list_class;
jmethodID s_list_get;
jmethodID s_list_size;
@@ -442,6 +444,25 @@ std::shared_ptr<ciface::Core::Device> FindDevice(jint device_id)
return device;
}
void RegisterDevicesChangedCallbackIfNeeded(JNIEnv* env, jclass controller_interface_class)
{
static bool registered = false;
if (registered)
return;
registered = true;
const jclass global_controller_interface_class =
reinterpret_cast<jclass>(env->NewGlobalRef(controller_interface_class));
const jmethodID controller_interface_on_devices_changed =
env->GetStaticMethodID(global_controller_interface_class, "onDevicesChanged", "()V");
static Common::EventHook event_hook = g_controller_interface.RegisterDevicesChangedCallback(
[global_controller_interface_class, controller_interface_on_devices_changed] {
IDCache::GetEnvForThread()->CallStaticVoidMethod(global_controller_interface_class,
controller_interface_on_devices_changed);
});
}
} // namespace
namespace ciface::Android
@@ -449,13 +470,13 @@ namespace ciface::Android
class InputBackend final : public ciface::InputBackend
{
public:
InputBackend(ControllerInterface* controller_interface);
explicit InputBackend(ControllerInterface* controller_interface);
~InputBackend();
void PopulateDevices() override;
private:
void AddDevice(JNIEnv* env, int device_id);
void AddSensorDevice(JNIEnv* env);
static void AddDevice(JNIEnv* env, int device_id);
static void AddSensorDevice(JNIEnv* env);
static void RemoveDevice(int device_id);
};
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)
@@ -592,10 +613,11 @@ private:
class AndroidDevice final : public Core::Device
{
public:
AndroidDevice(JNIEnv* env, jobject input_device)
AndroidDevice(JNIEnv* env, jint device_id, jobject input_device)
: m_sensor_event_listener(AddSensors(env, input_device)),
m_source(env->CallIntMethod(input_device, s_input_device_get_sources)),
m_controller_number(env->CallIntMethod(input_device, s_input_device_get_controller_number))
m_controller_number(env->CallIntMethod(input_device, s_input_device_get_controller_number)),
m_device_id(device_id)
{
jstring j_name =
reinterpret_cast<jstring>(env->CallObjectMethod(input_device, s_input_device_get_name));
@@ -612,7 +634,7 @@ public:
// Constructor for the device added by Dolphin to contain sensor inputs
AndroidDevice(JNIEnv* env, std::string name)
: m_sensor_event_listener(AddSensors(env, nullptr)), m_source(AINPUT_SOURCE_SENSOR),
m_controller_number(0), m_name(std::move(name))
m_controller_number(0), m_device_id(std::nullopt), m_name(std::move(name))
{
AddSystemMotors(env);
}
@@ -625,7 +647,7 @@ public:
std::string GetName() const override { return m_name; }
std::string GetSource() const override { return "Android"; }
std::string GetSource() const override { return SOURCE; }
std::optional<int> GetPreferredId() const override
{
@@ -647,6 +669,8 @@ public:
return -3;
}
std::optional<jint> GetDeviceID() const { return m_device_id; }
jobject GetSensorEventListener() { return m_sensor_event_listener; }
private:
@@ -783,6 +807,7 @@ private:
const jobject m_sensor_event_listener;
const int m_source;
const int m_controller_number;
const std::optional<jint> m_device_id;
std::string m_name;
};
@@ -904,6 +929,8 @@ InputBackend::InputBackend(ControllerInterface* controller_interface)
env->CallStaticVoidMethod(s_controller_interface_class,
s_controller_interface_register_input_device_listener);
RegisterDevicesChangedCallbackIfNeeded(env, s_controller_interface_class);
}
InputBackend::~InputBackend()
@@ -924,8 +951,12 @@ InputBackend::~InputBackend()
env->DeleteGlobalRef(s_keycodes_array);
}
void InputBackend::AddDevice(JNIEnv* env, int device_id)
void InputBackend::AddDevice(JNIEnv* env, jint device_id)
{
// Remove the device in case it already exists (maybe it's possible for a device to connect,
// be processed by PopulateDevices, and then be processed by onInputDeviceAdded)
RemoveDevice(device_id);
jobject input_device =
env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device, device_id);
@@ -935,14 +966,14 @@ void InputBackend::AddDevice(JNIEnv* env, int device_id)
return;
}
auto device = std::make_shared<AndroidDevice>(env, input_device);
auto device = std::make_shared<AndroidDevice>(env, device_id, input_device);
env->DeleteLocalRef(input_device);
if (device->Inputs().empty() && device->Outputs().empty())
return;
GetControllerInterface().AddDevice(device);
g_controller_interface.AddDevice(device);
Core::DeviceQualifier qualifier;
qualifier.FromDevice(device.get());
@@ -967,7 +998,7 @@ void InputBackend::AddSensorDevice(JNIEnv* env)
if (device->Inputs().empty() && device->Outputs().empty())
return;
GetControllerInterface().AddDevice(device);
g_controller_interface.AddDevice(device);
Core::DeviceQualifier qualifier;
qualifier.FromDevice(device.get());
@@ -980,6 +1011,16 @@ void InputBackend::AddSensorDevice(JNIEnv* env)
env->DeleteLocalRef(j_qualifier);
}
void InputBackend::RemoveDevice(jint device_id)
{
g_controller_interface.RemoveDevice([device_id](const ciface::Core::Device* device) {
return device->GetSource() == SOURCE &&
static_cast<const AndroidDevice*>(device)->GetDeviceID() == device_id;
});
s_device_id_to_device_qualifier.erase(device_id);
}
void InputBackend::PopulateDevices()
{
INFO_LOG_FMT(CONTROLLERINTERFACE, "Android populating devices");
@@ -1003,7 +1044,7 @@ void InputBackend::PopulateDevices()
extern "C" {
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchKeyEvent(
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchKeyEventNative(
JNIEnv* env, jclass, jobject key_event)
{
const jint action = env->CallIntMethod(key_event, s_key_event_get_action);
@@ -1047,7 +1088,7 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatch
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchGenericMotionEvent(
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchGenericMotionEventNative(
JNIEnv* env, jclass, jobject motion_event)
{
const jint device_id = env->CallIntMethod(motion_event, s_input_event_get_device_id);
@@ -1091,7 +1132,7 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatch
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchSensorEvent(
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchSensorEventNative(
JNIEnv* env, jclass, jstring j_device_qualifier, jstring j_axis_name, jfloat value)
{
ciface::Core::DeviceQualifier device_qualifier;
@@ -1143,17 +1184,32 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_notifySe
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_refreshDevices(JNIEnv* env,
jclass)
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_00024InputDeviceListener_onInputDeviceAdded(
JNIEnv* env, jobject, jint device_id)
{
g_controller_interface.RefreshDevices();
ciface::Android::InputBackend::AddDevice(env, device_id);
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_00024InputDeviceListener_onInputDeviceRemoved(
JNIEnv*, jobject, jint device_id)
{
ciface::Android::InputBackend::RemoveDevice(device_id);
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_00024InputDeviceListener_onInputDeviceChanged(
JNIEnv* env, jobject, jint device_id)
{
// AddDevice will automatically remove the existing device
ciface::Android::InputBackend::AddDevice(env, device_id);
}
JNIEXPORT jobjectArray JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_getAllDeviceStrings(
JNIEnv* env, jclass)
{
return VectorToJStringArray(env, g_controller_interface.GetAllDeviceStrings());
return SpanToJStringArray(env, g_controller_interface.GetAllDeviceStrings());
}
JNIEXPORT jobject JNICALL