diff options
author | liamwhite <[email protected]> | 2024-02-24 00:38:13 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-24 00:38:13 -0500 |
commit | d1e0039bc87a28e42b05b324d0f15dc063a898b1 (patch) | |
tree | c17bd7729a3e6ec78a9e261c38a7dd013a4f3832 /src/input_common/drivers/android.cpp | |
parent | 7a51eaa727f703a6a6583df678b00332ff4d6418 (diff) | |
parent | 0369c65870c4ffa1223dc3c6c7234ae1f12f6a04 (diff) | |
download | yuzu-android-d1e0039bc87a28e42b05b324d0f15dc063a898b1.tar.gz yuzu-android-d1e0039bc87a28e42b05b324d0f15dc063a898b1.zip |
Merge pull request #13142 from t895/vibration-queue
android: Play vibrations asynchronously
Diffstat (limited to 'src/input_common/drivers/android.cpp')
-rw-r--r-- | src/input_common/drivers/android.cpp | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/src/input_common/drivers/android.cpp b/src/input_common/drivers/android.cpp index e859cc538..d40fa66ae 100644 --- a/src/input_common/drivers/android.cpp +++ b/src/input_common/drivers/android.cpp @@ -3,6 +3,7 @@ #include <set> #include <common/settings_input.h> +#include <common/thread.h> #include <jni.h> #include "common/android/android_common.h" #include "common/android/id_cache.h" @@ -10,7 +11,18 @@ namespace InputCommon { -Android::Android(std::string input_engine_) : InputEngine(std::move(input_engine_)) {} +Android::Android(std::string input_engine_) : InputEngine(std::move(input_engine_)) { + vibration_thread = std::jthread([this](std::stop_token token) { + Common::SetCurrentThreadName("Android_Vibration"); + auto env = Common::Android::GetEnvForThread(); + using namespace std::chrono_literals; + while (!token.stop_requested()) { + SendVibrations(env, token); + } + }); +} + +Android::~Android() = default; void Android::RegisterController(jobject j_input_device) { auto env = Common::Android::GetEnvForThread(); @@ -57,17 +69,11 @@ void Android::SetMotionState(std::string guid, size_t port, u64 delta_timestamp, Common::Input::DriverResult Android::SetVibration( [[maybe_unused]] const PadIdentifier& identifier, [[maybe_unused]] const Common::Input::VibrationStatus& vibration) { - auto device = input_devices.find(identifier); - if (device != input_devices.end()) { - Common::Android::RunJNIOnFiber<void>([&](JNIEnv* env) { - float average_intensity = - static_cast<float>((vibration.high_amplitude + vibration.low_amplitude) / 2.0); - env->CallVoidMethod(device->second, Common::Android::GetYuzuDeviceVibrate(), - average_intensity); - }); - return Common::Input::DriverResult::Success; - } - return Common::Input::DriverResult::NotSupported; + vibration_queue.Push(VibrationRequest{ + .identifier = identifier, + .vibration = vibration, + }); + return Common::Input::DriverResult::Success; } bool Android::IsVibrationEnabled([[maybe_unused]] const PadIdentifier& identifier) { @@ -347,4 +353,15 @@ PadIdentifier Android::GetIdentifier(const std::string& guid, size_t port) const }; } +void Android::SendVibrations(JNIEnv* env, std::stop_token token) { + VibrationRequest request = vibration_queue.PopWait(token); + auto device = input_devices.find(request.identifier); + if (device != input_devices.end()) { + float average_intensity = static_cast<float>( + (request.vibration.high_amplitude + request.vibration.low_amplitude) / 2.0); + env->CallVoidMethod(device->second, Common::Android::GetYuzuDeviceVibrate(), + average_intensity); + } +} + } // namespace InputCommon |