fix(config): Trigger reload on correct event

Fixes #85
This commit is contained in:
Michael Carlberg 2016-11-25 11:40:32 +01:00
parent 8db3e79919
commit 4268817c51
3 changed files with 22 additions and 10 deletions

View File

@ -28,17 +28,21 @@ namespace inotify_util {
void remove(); void remove();
bool poll(int wait_ms = 1000); bool poll(int wait_ms = 1000);
unique_ptr<event_t> get_event(); unique_ptr<event_t> get_event();
bool await_match();
const string path() const; const string path() const;
protected: protected:
string m_path; string m_path;
int m_fd = -1; int m_fd = -1;
int m_wd = -1; int m_wd = -1;
int m_mask = 0;
}; };
using watch_t = unique_ptr<inotify_watch>; using watch_t = unique_ptr<inotify_watch>;
watch_t make_watch(string path); watch_t make_watch(string path);
bool match(const event_t* evt, int mask);
} }
POLYBAR_NS_END POLYBAR_NS_END

View File

@ -254,9 +254,7 @@ void controller::install_confwatch() {
return; return;
} }
m_threads.emplace_back([this] { m_threads.emplace_back([&] {
this_thread::sleep_for(chrono::seconds{1});
try { try {
if (!m_running) if (!m_running)
return; return;
@ -265,13 +263,10 @@ void controller::install_confwatch() {
m_confwatch->attach(IN_MODIFY); m_confwatch->attach(IN_MODIFY);
m_log.trace("controller: Wait for config file inotify event"); m_log.trace("controller: Wait for config file inotify event");
m_confwatch->get_event(); if (m_confwatch->await_match() && m_running) {
m_log.info("Configuration file changed");
if (!m_running) kill(getpid(), SIGUSR1);
return; }
m_log.info("Configuration file changed");
kill(getpid(), SIGUSR1);
} catch (const system_error& err) { } catch (const system_error& err) {
m_log.err(err.what()); m_log.err(err.what());
m_log.trace("controller: Reset config watch"); m_log.trace("controller: Reset config watch");

View File

@ -24,6 +24,7 @@ namespace inotify_util {
throw system_error("Failed to allocate inotify fd"); throw system_error("Failed to allocate inotify fd");
if ((m_wd = inotify_add_watch(m_fd, m_path.c_str(), mask)) == -1) if ((m_wd = inotify_add_watch(m_fd, m_path.c_str(), mask)) == -1)
throw system_error("Failed to attach inotify watch"); 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) if (inotify_rm_watch(m_fd, m_wd) == -1)
throw system_error("Failed to remove inotify watch"); throw system_error("Failed to remove inotify watch");
m_wd = -1; m_wd = -1;
m_mask = 0;
} }
/** /**
@ -81,6 +83,13 @@ namespace inotify_util {
return event; return event;
} }
/**
* Wait for matching event
*/
bool inotify_watch::await_match() {
return (get_event()->mask & m_mask) == m_mask;
}
/** /**
* Get watch file path * Get watch file path
*/ */
@ -92,6 +101,10 @@ namespace inotify_util {
di::injector<watch_t> injector = di::make_injector(di::bind<>().to(path)); di::injector<watch_t> injector = di::make_injector(di::bind<>().to(path));
return injector.create<watch_t>(); return injector.create<watch_t>();
} }
bool match(const event_t* evt, int mask) {
return (evt->mask & mask) == mask;
}
} }
POLYBAR_NS_END POLYBAR_NS_END