mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Handle screens disconnection (#29)
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
#include <functional>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
#include <gtkmm.h>
|
||||
|
||||
namespace waybar::chrono {
|
||||
|
||||
@ -14,18 +15,6 @@ using clock = std::chrono::system_clock;
|
||||
using duration = clock::duration;
|
||||
using time_point = std::chrono::time_point<clock, duration>;
|
||||
|
||||
inline struct timespec to_timespec(time_point t) noexcept
|
||||
{
|
||||
long secs = duration_cast<seconds>(t.time_since_epoch()).count();
|
||||
long nsc = duration_cast<nanoseconds>(t.time_since_epoch() % seconds(1)).count();
|
||||
return {secs, nsc};
|
||||
}
|
||||
|
||||
inline time_point to_time_point(struct timespec t) noexcept
|
||||
{
|
||||
return time_point(duration_cast<duration>(seconds(t.tv_sec) + nanoseconds(t.tv_nsec)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace waybar::util {
|
||||
@ -34,59 +23,71 @@ struct SleeperThread {
|
||||
SleeperThread() = default;
|
||||
|
||||
SleeperThread(std::function<void()> func)
|
||||
: thread{[this, func] {
|
||||
do {
|
||||
: thread_{[this, func] {
|
||||
while(true) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!do_run_) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
func();
|
||||
} while (do_run);
|
||||
}
|
||||
}}
|
||||
{
|
||||
defined = true;
|
||||
}
|
||||
{}
|
||||
|
||||
SleeperThread& operator=(std::function<void()> func)
|
||||
{
|
||||
thread = std::thread([this, func] {
|
||||
do {
|
||||
thread_ = std::thread([this, func] {
|
||||
while(true) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!do_run_) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
func();
|
||||
} while (do_run);
|
||||
}
|
||||
});
|
||||
defined = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
auto sleep_for(chrono::duration dur)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_for(lock, dur);
|
||||
auto lock = std::unique_lock(mutex_);
|
||||
return condvar_.wait_for(lock, dur);
|
||||
}
|
||||
|
||||
auto sleep_until(chrono::time_point time)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_until(lock, time);
|
||||
auto lock = std::unique_lock(mutex_);
|
||||
return condvar_.wait_until(lock, time);
|
||||
}
|
||||
|
||||
auto wake_up()
|
||||
{
|
||||
condvar.notify_all();
|
||||
condvar_.notify_all();
|
||||
}
|
||||
|
||||
~SleeperThread()
|
||||
{
|
||||
do_run = false;
|
||||
if (defined) {
|
||||
condvar.notify_all();
|
||||
thread.join();
|
||||
do_run_ = false;
|
||||
condvar_.notify_all();
|
||||
auto native_handle = thread_.native_handle();
|
||||
pthread_cancel(native_handle);
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
sigc::signal<void> sig_update;
|
||||
|
||||
private:
|
||||
std::thread thread;
|
||||
std::condition_variable condvar;
|
||||
std::mutex mutex;
|
||||
bool defined = false;
|
||||
bool do_run = true;
|
||||
std::thread thread_;
|
||||
std::condition_variable condvar_;
|
||||
std::mutex mutex_;
|
||||
bool do_run_ = true;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -14,6 +14,9 @@ struct JsonParser {
|
||||
{
|
||||
Json::Value root;
|
||||
std::string err;
|
||||
if (_reader == nullptr) {
|
||||
throw std::runtime_error("Unable to parse");
|
||||
}
|
||||
bool res =
|
||||
_reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
if (!res)
|
||||
@ -24,6 +27,7 @@ struct JsonParser {
|
||||
~JsonParser()
|
||||
{
|
||||
delete _reader;
|
||||
_reader = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user