2016-11-20 17:04:31 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "components/builder.hpp"
|
|
|
|
#include "modules/meta/base.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
template <class Impl>
|
|
|
|
class inotify_module : public module<Impl> {
|
|
|
|
public:
|
|
|
|
using module<Impl>::module;
|
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
void start() override {
|
2021-10-15 04:33:10 -04:00
|
|
|
this->module<Impl>::start();
|
2016-12-21 03:21:35 -05:00
|
|
|
this->m_mainthread = thread(&inotify_module::runner, this);
|
|
|
|
}
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
protected:
|
2016-12-21 03:21:35 -05:00
|
|
|
void runner() {
|
2017-01-27 07:29:10 -05:00
|
|
|
this->m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
|
2016-12-21 03:21:35 -05:00
|
|
|
try {
|
2016-12-23 09:54:06 -05:00
|
|
|
// Warm up module output before entering the loop
|
2017-01-27 07:29:10 -05:00
|
|
|
std::unique_lock<std::mutex> guard(this->m_updatelock);
|
2022-03-06 11:44:48 -05:00
|
|
|
CAST_MOD(Impl)->on_event({});
|
2016-12-23 09:54:06 -05:00
|
|
|
CAST_MOD(Impl)->broadcast();
|
2017-01-27 07:29:10 -05:00
|
|
|
guard.unlock();
|
2016-12-21 03:21:35 -05:00
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
while (this->running()) {
|
2016-12-30 22:20:46 -05:00
|
|
|
std::lock_guard<std::mutex> guard(this->m_updatelock);
|
2016-12-21 03:21:35 -05:00
|
|
|
CAST_MOD(Impl)->poll_events();
|
|
|
|
}
|
|
|
|
} catch (const module_error& err) {
|
|
|
|
CAST_MOD(Impl)->halt(err.what());
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
CAST_MOD(Impl)->halt(err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void watch(string path, int mask = IN_ALL_EVENTS) {
|
2016-12-21 17:22:02 -05:00
|
|
|
this->m_log.trace("%s: Attach inotify at %s", this->name(), path);
|
2016-12-21 03:21:35 -05:00
|
|
|
m_watchlist.insert(make_pair(path, mask));
|
|
|
|
}
|
|
|
|
|
|
|
|
void idle() {
|
|
|
|
this->sleep(200ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void poll_events() {
|
2022-03-06 11:44:48 -05:00
|
|
|
vector<inotify_watch> watches;
|
2016-12-21 03:21:35 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (auto&& w : m_watchlist) {
|
2022-03-06 11:44:48 -05:00
|
|
|
watches.emplace_back(w.first);
|
|
|
|
watches.back().attach(w.second);
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
|
|
|
} catch (const system_error& e) {
|
2016-12-21 17:22:02 -05:00
|
|
|
this->m_log.err("%s: Error while creating inotify watch (what: %s)", this->name(), e.what());
|
2016-12-21 03:21:35 -05:00
|
|
|
CAST_MOD(Impl)->sleep(0.1s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:22:02 -05:00
|
|
|
while (this->running()) {
|
|
|
|
for (auto&& w : watches) {
|
2022-03-06 11:44:48 -05:00
|
|
|
this->m_log.trace_x("%s: Poll inotify watch %s", this->name(), w.path());
|
2016-12-21 03:21:35 -05:00
|
|
|
|
2022-03-06 11:44:48 -05:00
|
|
|
if (w.poll(1000 / watches.size())) {
|
|
|
|
auto event = w.get_event();
|
|
|
|
if (CAST_MOD(Impl)->on_event(event)) {
|
2016-12-21 17:22:02 -05:00
|
|
|
CAST_MOD(Impl)->broadcast();
|
|
|
|
}
|
|
|
|
CAST_MOD(Impl)->idle();
|
|
|
|
return;
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
2016-12-21 17:22:02 -05:00
|
|
|
|
2022-10-15 17:21:40 -04:00
|
|
|
if (!this->running()) {
|
2016-12-21 17:22:02 -05:00
|
|
|
break;
|
2022-10-16 15:31:07 -04:00
|
|
|
}
|
2016-12-21 03:21:35 -05:00
|
|
|
}
|
|
|
|
CAST_MOD(Impl)->idle();
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
map<string, int> m_watchlist;
|
|
|
|
};
|
2022-03-06 11:44:48 -05:00
|
|
|
} // namespace modules
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|