From 0d65114ee6a5098cdec5537cb5bca30a176bedfe Mon Sep 17 00:00:00 2001 From: Mr_Goldberg Date: Sun, 7 Aug 2022 23:06:43 -0400 Subject: [PATCH] Fix controller rumble code. --- dll/steam_controller.h | 47 ++++++++++++------------------------------ 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/dll/steam_controller.h b/dll/steam_controller.h index 64b7f4f..db04d60 100644 --- a/dll/steam_controller.h +++ b/dll/steam_controller.h @@ -66,7 +66,7 @@ struct Controller_Action { struct Rumble_Thread_Data { std::condition_variable rumble_thread_cv; - std::atomic_bool kill_rumble_thread; + bool kill_rumble_thread; std::mutex rumble_mutex; struct Rumble_Data { @@ -232,38 +232,21 @@ public: static void background_rumble(Rumble_Thread_Data *data) { - std::mutex mtx; - std::unique_lock lck(mtx); - bool rumbled = false; while (true) { - bool new_data = false; - if (rumbled) { - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - data->rumble_mutex.lock(); - for (int i = 0; i < GAMEPAD_COUNT; ++i) { - if (data->data[i].new_data) { - new_data = true; - break; - } + unsigned short left, right; + unsigned int rumble_length_ms; + int gamepad = -1; + while (gamepad == -1) { + std::unique_lock lck(data->rumble_mutex); + if (data->kill_rumble_thread) { + return; } - data->rumble_mutex.unlock(); + data->rumble_thread_cv.wait_for(lck, std::chrono::milliseconds(1000)); if (data->kill_rumble_thread) { return; } - } - bool x = new_data || data->rumble_thread_cv.wait_for(lck, std::chrono::milliseconds(100)) != std::cv_status::timeout; - if (data->kill_rumble_thread) { - return; - } - - rumbled = false; - while (true) { - unsigned short left, right; - unsigned int rumble_length_ms; - int gamepad = -1; - data->rumble_mutex.lock(); for (int i = 0; i < GAMEPAD_COUNT; ++i) { if (data->data[i].new_data) { left = data->data[i].left; @@ -278,15 +261,9 @@ static void background_rumble(Rumble_Thread_Data *data) } } } - - data->rumble_mutex.unlock(); - if (gamepad == -1) { - break; - } - - GamepadSetRumble((GAMEPAD_DEVICE)gamepad, ((double)left) / 65535.0, ((double)right) / 65535.0, rumble_length_ms); - rumbled = true; } + + GamepadSetRumble((GAMEPAD_DEVICE)gamepad, ((double)left) / 65535.0, ((double)right) / 65535.0, rumble_length_ms); } } @@ -370,7 +347,9 @@ bool Shutdown() } controllers = std::map(); + rumble_thread_data->rumble_mutex.lock(); rumble_thread_data->kill_rumble_thread = true; + rumble_thread_data->rumble_mutex.unlock(); rumble_thread_data->rumble_thread_cv.notify_one(); background_rumble_thread.join(); delete rumble_thread_data;