diff --git a/include/util/chrono.hpp b/include/util/chrono.hpp index 828273a4..fdca5f47 100644 --- a/include/util/chrono.hpp +++ b/include/util/chrono.hpp @@ -23,35 +23,24 @@ struct SleeperThread { SleeperThread() = default; SleeperThread(std::function func) - : thread_{[this, func] { - while(true) { - { - std::lock_guard lock(mutex_); - if (!do_run_) { - break; - } - } - func(); - } + : do_run_(true), thread_{[this, func] { + while (do_run_) func(); }} {} SleeperThread& operator=(std::function func) { + do_run_ = true; thread_ = std::thread([this, func] { - while(true) { - { - std::lock_guard lock(mutex_); - if (!do_run_) { - break; - } - } - func(); - } + while (do_run_) func(); }); return *this; } + bool isRunnging() const + { + return do_run_; + } auto sleep_for(chrono::duration dur) { @@ -85,10 +74,10 @@ struct SleeperThread { } private: + bool do_run_ = false; std::thread thread_; std::condition_variable condvar_; std::mutex mutex_; - bool do_run_ = true; }; } diff --git a/src/modules/sway/ipc/client.cpp b/src/modules/sway/ipc/client.cpp index 85f7c656..4abe51fd 100644 --- a/src/modules/sway/ipc/client.cpp +++ b/src/modules/sway/ipc/client.cpp @@ -69,8 +69,7 @@ struct waybar::modules::sway::Ipc::ipc_response size_t total = 0; while (total < ipc_header_size_) { - ssize_t res = - ::recv(fd, header.data() + total, ipc_header_size_ - total, 0); + auto res = ::recv(fd, header.data() + total, ipc_header_size_ - total, 0); if (res <= 0) { throw std::runtime_error("Unable to receive IPC response"); } @@ -81,8 +80,7 @@ struct waybar::modules::sway::Ipc::ipc_response std::string payload; payload.reserve(data32[0] + 1); while (total < data32[0]) { - ssize_t res = - ::recv(fd, payload.data() + total, data32[0] - total, 0); + auto res = ::recv(fd, payload.data() + total, data32[0] - total, 0); if (res < 0) { throw std::runtime_error("Unable to receive IPC response"); } diff --git a/src/modules/sway/mode.cpp b/src/modules/sway/mode.cpp index 0d2bbee7..e0c73f8a 100644 --- a/src/modules/sway/mode.cpp +++ b/src/modules/sway/mode.cpp @@ -24,7 +24,7 @@ void waybar::modules::sway::Mode::worker() dp.emit(); } } catch (const std::exception& e) { - std::cerr << e.what() << std::endl; + std::cerr << "Mode: " << e.what() << std::endl; } }; } diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 067487cf..29d147a0 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -36,7 +36,7 @@ void waybar::modules::sway::Window::worker() dp.emit(); } } catch (const std::exception& e) { - std::cerr << e.what() << std::endl; + std::cerr << "Window: " << e.what() << std::endl; } }; } diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index d3e42e37..07ed0f1a 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -20,7 +20,7 @@ void waybar::modules::sway::Workspaces::worker() while (bar_.output_name.empty()) { thread_.sleep_for(chrono::milliseconds(150)); } - } else if (!workspaces_.empty()) { + } else if (thread_.isRunnging() && !workspaces_.empty()) { ipc_.handleEvent(); } { @@ -30,7 +30,7 @@ void waybar::modules::sway::Workspaces::worker() } dp.emit(); } catch (const std::exception& e) { - std::cerr << e.what() << std::endl; + std::cerr << "Workspaces: " << e.what() << std::endl; } }; }