diff options
| author | 2024-02-24 00:38:13 -0500 | |
|---|---|---|
| committer | 2024-02-24 00:38:13 -0500 | |
| commit | d1e0039bc87a28e42b05b324d0f15dc063a898b1 (patch) | |
| tree | c17bd7729a3e6ec78a9e261c38a7dd013a4f3832 /src | |
| 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')
| -rw-r--r-- | src/input_common/drivers/android.cpp | 41 | ||||
| -rw-r--r-- | src/input_common/drivers/android.h | 10 | ||||
| -rw-r--r-- | src/input_common/drivers/sdl_driver.h | 5 | ||||
| -rw-r--r-- | src/input_common/input_engine.h | 5 |
4 files changed, 44 insertions, 17 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 |
diff --git a/src/input_common/drivers/android.h b/src/input_common/drivers/android.h index 8a386c1b1..03e2b2c98 100644 --- a/src/input_common/drivers/android.h +++ b/src/input_common/drivers/android.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <set> | 6 | #include <set> |
| 7 | #include <common/threadsafe_queue.h> | ||
| 7 | #include <jni.h> | 8 | #include <jni.h> |
| 8 | #include "input_common/input_engine.h" | 9 | #include "input_common/input_engine.h" |
| 9 | 10 | ||
| @@ -16,6 +17,8 @@ class Android final : public InputEngine { | |||
| 16 | public: | 17 | public: |
| 17 | explicit Android(std::string input_engine_); | 18 | explicit Android(std::string input_engine_); |
| 18 | 19 | ||
| 20 | ~Android() override; | ||
| 21 | |||
| 19 | /** | 22 | /** |
| 20 | * Registers controller number to accept new inputs. | 23 | * Registers controller number to accept new inputs. |
| 21 | * @param j_input_device YuzuInputDevice object from the Android frontend to register. | 24 | * @param j_input_device YuzuInputDevice object from the Android frontend to register. |
| @@ -89,6 +92,9 @@ private: | |||
| 89 | /// Returns the correct identifier corresponding to the player index | 92 | /// Returns the correct identifier corresponding to the player index |
| 90 | PadIdentifier GetIdentifier(const std::string& guid, size_t port) const; | 93 | PadIdentifier GetIdentifier(const std::string& guid, size_t port) const; |
| 91 | 94 | ||
| 95 | /// Takes all vibrations from the queue and sends the command to the controller | ||
| 96 | void SendVibrations(JNIEnv* env, std::stop_token token); | ||
| 97 | |||
| 92 | static constexpr s32 AXIS_X = 0; | 98 | static constexpr s32 AXIS_X = 0; |
| 93 | static constexpr s32 AXIS_Y = 1; | 99 | static constexpr s32 AXIS_Y = 1; |
| 94 | static constexpr s32 AXIS_Z = 11; | 100 | static constexpr s32 AXIS_Z = 11; |
| @@ -133,6 +139,10 @@ private: | |||
| 133 | redmagic_vid, backbone_labs_vid, xbox_vid}; | 139 | redmagic_vid, backbone_labs_vid, xbox_vid}; |
| 134 | const std::vector<std::string> flipped_xy_vids{sony_vid, razer_vid, redmagic_vid, | 140 | const std::vector<std::string> flipped_xy_vids{sony_vid, razer_vid, redmagic_vid, |
| 135 | backbone_labs_vid, xbox_vid}; | 141 | backbone_labs_vid, xbox_vid}; |
| 142 | |||
| 143 | /// Queue of vibration request to controllers | ||
| 144 | Common::SPSCQueue<VibrationRequest> vibration_queue; | ||
| 145 | std::jthread vibration_thread; | ||
| 136 | }; | 146 | }; |
| 137 | 147 | ||
| 138 | } // namespace InputCommon | 148 | } // namespace InputCommon |
diff --git a/src/input_common/drivers/sdl_driver.h b/src/input_common/drivers/sdl_driver.h index 08e49a0da..a140ad072 100644 --- a/src/input_common/drivers/sdl_driver.h +++ b/src/input_common/drivers/sdl_driver.h | |||
| @@ -69,11 +69,6 @@ public: | |||
| 69 | bool IsVibrationEnabled(const PadIdentifier& identifier) override; | 69 | bool IsVibrationEnabled(const PadIdentifier& identifier) override; |
| 70 | 70 | ||
| 71 | private: | 71 | private: |
| 72 | struct VibrationRequest { | ||
| 73 | PadIdentifier identifier; | ||
| 74 | Common::Input::VibrationStatus vibration; | ||
| 75 | }; | ||
| 76 | |||
| 77 | void InitJoystick(int joystick_index); | 72 | void InitJoystick(int joystick_index); |
| 78 | void CloseJoystick(SDL_Joystick* sdl_joystick); | 73 | void CloseJoystick(SDL_Joystick* sdl_joystick); |
| 79 | 74 | ||
diff --git a/src/input_common/input_engine.h b/src/input_common/input_engine.h index c2d0cbb34..3e328509b 100644 --- a/src/input_common/input_engine.h +++ b/src/input_common/input_engine.h | |||
| @@ -46,6 +46,11 @@ enum class EngineInputType { | |||
| 46 | Nfc, | 46 | Nfc, |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | struct VibrationRequest { | ||
| 50 | PadIdentifier identifier; | ||
| 51 | Common::Input::VibrationStatus vibration; | ||
| 52 | }; | ||
| 53 | |||
| 49 | namespace std { | 54 | namespace std { |
| 50 | // Hash used to create lists from PadIdentifier data | 55 | // Hash used to create lists from PadIdentifier data |
| 51 | template <> | 56 | template <> |