diff --git a/include/utils/inotify.hpp b/include/utils/inotify.hpp index e018d5f4..458f5ea6 100644 --- a/include/utils/inotify.hpp +++ b/include/utils/inotify.hpp @@ -28,17 +28,21 @@ namespace inotify_util { void remove(); bool poll(int wait_ms = 1000); unique_ptr get_event(); + bool await_match(); const string path() const; protected: string m_path; int m_fd = -1; int m_wd = -1; + int m_mask = 0; }; using watch_t = unique_ptr; watch_t make_watch(string path); + + bool match(const event_t* evt, int mask); } POLYBAR_NS_END diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 7b4283a4..dcf0a1a5 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -254,9 +254,7 @@ void controller::install_confwatch() { return; } - m_threads.emplace_back([this] { - this_thread::sleep_for(chrono::seconds{1}); - + m_threads.emplace_back([&] { try { if (!m_running) return; @@ -265,13 +263,10 @@ void controller::install_confwatch() { m_confwatch->attach(IN_MODIFY); m_log.trace("controller: Wait for config file inotify event"); - m_confwatch->get_event(); - - if (!m_running) - return; - - m_log.info("Configuration file changed"); - kill(getpid(), SIGUSR1); + if (m_confwatch->await_match() && m_running) { + m_log.info("Configuration file changed"); + kill(getpid(), SIGUSR1); + } } catch (const system_error& err) { m_log.err(err.what()); m_log.trace("controller: Reset config watch"); diff --git a/src/utils/inotify.cpp b/src/utils/inotify.cpp index cc967b54..e1b57391 100644 --- a/src/utils/inotify.cpp +++ b/src/utils/inotify.cpp @@ -24,6 +24,7 @@ namespace inotify_util { throw system_error("Failed to allocate inotify fd"); if ((m_wd = inotify_add_watch(m_fd, m_path.c_str(), mask)) == -1) throw system_error("Failed to attach inotify watch"); + m_mask |= mask; } /** @@ -33,6 +34,7 @@ namespace inotify_util { if (inotify_rm_watch(m_fd, m_wd) == -1) throw system_error("Failed to remove inotify watch"); m_wd = -1; + m_mask = 0; } /** @@ -81,6 +83,13 @@ namespace inotify_util { return event; } + /** + * Wait for matching event + */ + bool inotify_watch::await_match() { + return (get_event()->mask & m_mask) == m_mask; + } + /** * Get watch file path */ @@ -92,6 +101,10 @@ namespace inotify_util { di::injector injector = di::make_injector(di::bind<>().to(path)); return injector.create(); } + + bool match(const event_t* evt, int mask) { + return (evt->mask & mask) == mask; + } } POLYBAR_NS_END