diff options
| author | 2024-02-24 00:38:13 -0500 | |
|---|---|---|
| committer | 2024-02-24 00:38:13 -0500 | |
| commit | d1e0039bc87a28e42b05b324d0f15dc063a898b1 (patch) | |
| tree | c17bd7729a3e6ec78a9e261c38a7dd013a4f3832 /src/input_common/drivers/android.cpp | |
| parent | Merge pull request #13146 from wheremyfoodat/patch-1 (diff) | |
| parent | android: Play vibrations asynchronously (diff) | |
| download | yuzu-d1e0039bc87a28e42b05b324d0f15dc063a898b1.tar.gz yuzu-d1e0039bc87a28e42b05b324d0f15dc063a898b1.tar.xz yuzu-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 @@ | |||
| 3 | 3 | ||
| 4 | #include <set> | 4 | #include <set> |
| 5 | #include <common/settings_input.h> | 5 | #include <common/settings_input.h> |
| 6 | #include <common/thread.h> | ||
| 6 | #include <jni.h> | 7 | #include <jni.h> |
| 7 | #include "common/android/android_common.h" | 8 | #include "common/android/android_common.h" |
| 8 | #include "common/android/id_cache.h" | 9 | #include "common/android/id_cache.h" |
| @@ -10,7 +11,18 @@ | |||
| 10 | 11 | ||
| 11 | namespace InputCommon { | 12 | namespace InputCommon { |
| 12 | 13 | ||
| 13 | Android::Android(std::string input_engine_) : InputEngine(std::move(input_engine_)) {} | 14 | Android::Android(std::string input_engine_) : InputEngine(std::move(input_engine_)) { |
| 15 | vibration_thread = std::jthread([this](std::stop_token token) { | ||
| 16 | Common::SetCurrentThreadName("Android_Vibration"); | ||
| 17 | auto env = Common::Android::GetEnvForThread(); | ||
| 18 | using namespace std::chrono_literals; | ||
| 19 | while (!token.stop_requested()) { | ||
| 20 | SendVibrations(env, token); | ||
| 21 | } | ||
| 22 | }); | ||
| 23 | } | ||
| 24 | |||
| 25 | Android::~Android() = default; | ||
| 14 | 26 | ||
| 15 | void Android::RegisterController(jobject j_input_device) { | 27 | void Android::RegisterController(jobject j_input_device) { |
| 16 | auto env = Common::Android::GetEnvForThread(); | 28 | auto env = Common::Android::GetEnvForThread(); |
| @@ -57,17 +69,11 @@ void Android::SetMotionState(std::string guid, size_t port, u64 delta_timestamp, | |||
| 57 | Common::Input::DriverResult Android::SetVibration( | 69 | Common::Input::DriverResult Android::SetVibration( |
| 58 | [[maybe_unused]] const PadIdentifier& identifier, | 70 | [[maybe_unused]] const PadIdentifier& identifier, |
| 59 | [[maybe_unused]] const Common::Input::VibrationStatus& vibration) { | 71 | [[maybe_unused]] const Common::Input::VibrationStatus& vibration) { |
| 60 | auto device = input_devices.find(identifier); | 72 | vibration_queue.Push(VibrationRequest{ |
| 61 | if (device != input_devices.end()) { | 73 | .identifier = identifier, |
| 62 | Common::Android::RunJNIOnFiber<void>([&](JNIEnv* env) { | 74 | .vibration = vibration, |
| 63 | float average_intensity = | 75 | }); |
| 64 | static_cast<float>((vibration.high_amplitude + vibration.low_amplitude) / 2.0); | 76 | return Common::Input::DriverResult::Success; |
| 65 | env->CallVoidMethod(device->second, Common::Android::GetYuzuDeviceVibrate(), | ||
| 66 | average_intensity); | ||
| 67 | }); | ||
| 68 | return Common::Input::DriverResult::Success; | ||
| 69 | } | ||
| 70 | return Common::Input::DriverResult::NotSupported; | ||
| 71 | } | 77 | } |
| 72 | 78 | ||
| 73 | bool Android::IsVibrationEnabled([[maybe_unused]] const PadIdentifier& identifier) { | 79 | bool Android::IsVibrationEnabled([[maybe_unused]] const PadIdentifier& identifier) { |
| @@ -347,4 +353,15 @@ PadIdentifier Android::GetIdentifier(const std::string& guid, size_t port) const | |||
| 347 | }; | 353 | }; |
| 348 | } | 354 | } |
| 349 | 355 | ||
| 356 | void Android::SendVibrations(JNIEnv* env, std::stop_token token) { | ||
| 357 | VibrationRequest request = vibration_queue.PopWait(token); | ||
| 358 | auto device = input_devices.find(request.identifier); | ||
| 359 | if (device != input_devices.end()) { | ||
| 360 | float average_intensity = static_cast<float>( | ||
| 361 | (request.vibration.high_amplitude + request.vibration.low_amplitude) / 2.0); | ||
| 362 | env->CallVoidMethod(device->second, Common::Android::GetYuzuDeviceVibrate(), | ||
| 363 | average_intensity); | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 350 | } // namespace InputCommon | 367 | } // namespace InputCommon |