polybar-dwm/include/modules/base.hpp

427 lines
12 KiB
C++
Raw Normal View History

#pragma once
2016-05-19 14:41:06 +00:00
#include <chrono>
#include <memory>
#include <string>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <map>
#include <thread>
#include <algorithm>
#include "exception.hpp"
#include "services/builder.hpp"
#include "services/inotify.hpp"
#include "services/logger.hpp"
2016-06-21 02:21:29 +00:00
#include "services/event_throttler.hpp"
2016-05-19 14:41:06 +00:00
#include "utils/config.hpp"
#include "utils/string.hpp"
#include "utils/concurrency.hpp"
using namespace std::chrono_literals;
#define Concat(one, two) one ## two
#define _Stringify(expr) #expr
#define Stringify(expr) _Stringify(expr)
2016-05-19 14:41:06 +00:00
#define DefineModule(ModuleName, ModuleType) struct ModuleName : public ModuleType<ModuleName>
2016-06-21 02:34:11 +00:00
#define DefineModule2(ModuleName, ModuleType, P2) struct ModuleName : public ModuleType<ModuleName, P2>
#define DefineModule3(ModuleName, ModuleType, P2, P3) struct ModuleName : public ModuleType<ModuleName, P2, P3>
2016-05-19 14:41:06 +00:00
#define CastModule(ModuleName) static_cast<ModuleName *>(this)
#define ConstCastModule(ModuleName) static_cast<ModuleName const &>(*this)
#define DEFAULT_FORMAT "format"
2016-05-19 14:41:06 +00:00
DefineBaseException(ModuleError);
DefineChildException(UndefinedFormat, ModuleError);
DefineChildException(UndefinedFormatTag, ModuleError);
class ModuleFormatter
{
public:
struct Format
{
std::string value;
std::vector<std::string> tags;
std::string fg, bg, ul, ol;
int spacing, padding, margin, offset;
std::string decorate(Builder *builder, std::string output);
2016-05-19 14:41:06 +00:00
};
std::string module_name;
std::map<std::string, std::shared_ptr<Format>> formats;
2016-05-19 14:41:06 +00:00
public:
2016-06-21 01:59:43 +00:00
explicit ModuleFormatter(std::string module_name)
2016-05-19 14:41:06 +00:00
: module_name(module_name) {}
void add(std::string name, std::string fallback, std::vector<std::string>&& tags, std::vector<std::string>&& whitelist = {});
std::shared_ptr<Format> get(std::string format_name);
bool has(std::string tag, std::string format_name);
bool has(std::string tag);
2016-05-19 14:41:06 +00:00
};
namespace modules
{
2016-06-21 01:59:43 +00:00
void broadcast_module_update(std::string module_name);
std::string get_tag_name(std::string tag);
2016-05-19 14:41:06 +00:00
struct ModuleInterface
{
public:
virtual ~ModuleInterface(){}
virtual std::string name() const = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual void refresh() = 0;
virtual std::string operator()() = 0;
2016-06-21 01:59:43 +00:00
virtual bool handle_command(std::string cmd) = 0;
2016-05-19 14:41:06 +00:00
};
2016-06-21 02:34:11 +00:00
template<class ModuleImpl>
2016-05-19 14:41:06 +00:00
class Module : public ModuleInterface
{
2016-06-28 02:58:32 +00:00
concurrency::Atomic<bool> enabled_flag { false };
concurrency::Value<std::string> cache { "" };
2016-05-19 14:41:06 +00:00
protected:
concurrency::SpinLock output_lock;
concurrency::SpinLock broadcast_lock;
2016-06-21 02:26:42 +00:00
concurrency::SpinLock update_lock;
2016-05-19 14:41:06 +00:00
std::mutex sleep_lock;
std::condition_variable sleep_handler;
2016-05-19 14:41:06 +00:00
std::string name_;
2016-06-28 02:58:32 +00:00
std::shared_ptr<Logger> logger;
2016-05-19 14:41:06 +00:00
std::unique_ptr<Builder> builder;
2016-06-21 02:21:29 +00:00
std::unique_ptr<EventThrottler> broadcast_throttler;
2016-05-19 14:41:06 +00:00
std::unique_ptr<ModuleFormatter> formatter;
std::vector<std::thread> threads;
event_throttler::limit_t broadcast_throttler_limit() const {
return event_throttler::limit_t(1);
}
event_throttler::timewindow_t broadcast_throttler_timewindow() const {
return event_throttler::timewindow_t(25);
}
2016-05-19 14:41:06 +00:00
public:
2016-06-21 02:21:29 +00:00
Module(std::string name, bool lazy_builder = true)
: name_("module/"+ name)
2016-06-28 02:58:32 +00:00
, logger(get_logger())
2016-06-21 02:21:29 +00:00
, builder(std::make_unique<Builder>(lazy_builder))
, broadcast_throttler(std::make_unique<EventThrottler>(ConstCastModule(ModuleImpl).broadcast_throttler_limit(), ConstCastModule(ModuleImpl).broadcast_throttler_timewindow()))
2016-06-28 02:58:32 +00:00
, formatter(std::make_unique<ModuleFormatter>(ConstCastModule(ModuleImpl).name())) {}
2016-05-19 14:41:06 +00:00
~Module()
{
if (this->enabled())
this->stop();
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
2016-06-21 02:26:42 +00:00
{
for (auto &&t : this->threads) {
if (t.joinable())
t.join();
else
2016-06-28 02:58:32 +00:00
this->logger->warning("["+ ConstCastModule(ModuleImpl).name() +"] Runner thread not joinable");
2016-06-21 02:26:42 +00:00
}
this->threads.clear();
2016-05-19 14:41:06 +00:00
}
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, name());
2016-05-19 14:41:06 +00:00
}
std::string name() const {
return name_;
}
void stop()
{
2016-06-21 06:26:26 +00:00
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, name());
this->wakeup();
2016-05-19 14:41:06 +00:00
this->enable(false);
}
void refresh() {
this->cache = CastModule(ModuleImpl)->get_output();
}
std::string operator()() {
return this->cache();
}
2016-06-21 01:59:43 +00:00
bool handle_command(std::string cmd) {
2016-05-19 14:41:06 +00:00
return CastModule(ModuleImpl)->handle_command(cmd);
}
protected:
bool enabled() {
return this->enabled_flag();
}
void enable(bool state) {
this->enabled_flag = state;
}
void broadcast()
{
2016-05-19 14:41:06 +00:00
std::lock_guard<concurrency::SpinLock> lck(this->broadcast_lock);
2016-06-21 02:21:29 +00:00
if (!this->broadcast_throttler->passthrough()) {
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, "Throttled broadcast for: "+ this->name_);
2016-06-21 02:21:29 +00:00
return;
}
2016-05-19 14:41:06 +00:00
broadcast_module_update(ConstCastModule(ModuleImpl).name());
}
void sleep(std::chrono::duration<double> sleep_duration)
{
std::unique_lock<std::mutex> lck(this->sleep_lock);
2016-06-21 06:26:26 +00:00
this->sleep_handler.wait_for(lck, sleep_duration);
}
void wakeup()
{
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, "Releasing sleep lock for "+ this->name_);
this->sleep_handler.notify_all();
}
2016-05-19 14:41:06 +00:00
std::string get_format() {
return DEFAULT_FORMAT;
}
std::string get_output()
{
std::lock_guard<concurrency::SpinLock> lck(this->output_lock);
if (!this->enabled()) {
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, ConstCastModule(ModuleImpl).name() +" is disabled");
2016-05-19 14:41:06 +00:00
return "";
} else {
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, ConstCastModule(ModuleImpl).name());
2016-05-19 14:41:06 +00:00
}
auto format_name = CastModule(ModuleImpl)->get_format();
auto format = this->formatter->get(format_name);
2016-05-19 14:41:06 +00:00
int i = 0;
bool tag_built = true;
2016-05-19 14:41:06 +00:00
for (auto tag : string::split(format->value, ' ')) {
bool is_blankspace = tag.empty();
2016-05-19 14:41:06 +00:00
if (tag[0] == '<' && tag[tag.length()-1] == '>') {
if (i > 0)
this->builder->space(format->spacing);
if (!(tag_built = CastModule(ModuleImpl)->build(this->builder.get(), tag)) && i > 0)
2016-05-19 14:41:06 +00:00
this->builder->remove_trailing_space(format->spacing);
if (tag_built)
i++;
} else if (is_blankspace && tag_built) {
this->builder->node(" ");
} else if (!is_blankspace) {
2016-05-19 14:41:06 +00:00
this->builder->node(tag);
}
}
return format->decorate(this->builder.get(), this->builder->flush());
}
};
2016-06-21 02:34:11 +00:00
template<class ModuleImpl>
2016-05-19 14:41:06 +00:00
class StaticModule : public Module<ModuleImpl>
{
using Module<ModuleImpl>::Module;
2016-05-19 14:41:06 +00:00
public:
void start()
{
this->enable(true);
this->threads.emplace_back(std::thread(&StaticModule::broadcast, this));
}
bool build(Builder *builder, std::string tag) {
2016-05-19 14:41:06 +00:00
return true;
}
};
2016-06-21 02:34:11 +00:00
template<class ModuleImpl>
2016-05-19 14:41:06 +00:00
class TimerModule : public Module<ModuleImpl>
{
protected:
std::chrono::duration<double> interval = 1s;
void runner()
{
while (this->enabled()) {
{ std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
if (CastModule(ModuleImpl)->update())
CastModule(ModuleImpl)->broadcast();
}
this->sleep(this->interval);
2016-05-19 14:41:06 +00:00
}
}
public:
template<typename I>
2016-06-21 01:59:43 +00:00
TimerModule(std::string name, I const &interval)
2016-05-19 14:41:06 +00:00
: Module<ModuleImpl>(name), interval(interval) {}
void start()
{
this->enable(true);
this->threads.emplace_back(std::thread(&TimerModule::runner, this));
}
};
2016-06-21 02:34:11 +00:00
template<class ModuleImpl>
2016-05-19 14:41:06 +00:00
class EventModule : public Module<ModuleImpl>
{
using Module<ModuleImpl>::Module;
protected:
void runner()
{
// warmup
CastModule(ModuleImpl)->update();
CastModule(ModuleImpl)->broadcast();
while (this->enabled()) {
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
if (!CastModule(ModuleImpl)->has_event())
continue;
if (!CastModule(ModuleImpl)->update())
continue;
CastModule(ModuleImpl)->broadcast();
}
}
public:
void start()
{
this->enable(true);
this->threads.emplace_back(std::thread(&EventModule::runner, this));
}
};
2016-06-21 02:34:11 +00:00
template<class ModuleImpl, const int ThrottleLimit = 2, const int ThrottleWindowMs = 50>
2016-05-19 14:41:06 +00:00
class InotifyModule : public Module<ModuleImpl>
{
protected:
2016-06-21 02:34:11 +00:00
concurrency::SpinLock poll_lock;
2016-05-19 14:41:06 +00:00
2016-06-21 02:34:11 +00:00
std::unique_ptr<EventThrottler> poll_throttler;
std::map<std::string, int> watch_list;
2016-05-19 14:41:06 +00:00
void runner()
{
// warmup
2016-06-21 02:34:11 +00:00
CastModule(ModuleImpl)->on_event(nullptr);
CastModule(ModuleImpl)->broadcast();
2016-05-19 14:41:06 +00:00
while (this->enabled()) {
try {
2016-06-21 02:34:11 +00:00
std::lock_guard<concurrency::SpinLock> lck(this->poll_lock);
if (this->poll_throttler->passthrough())
this->poll_events();
2016-05-19 14:41:06 +00:00
} catch (InotifyException &e) {
get_logger()->fatal(e.what());
}
}
}
2016-06-21 02:34:11 +00:00
void idle() const {
// this->sleep(1s);
}
2016-05-19 14:41:06 +00:00
void poll_events()
{
std::vector<std::unique_ptr<InotifyWatch>> watches;
2016-06-14 04:26:46 +00:00
try {
for (auto &&w : this->watch_list)
watches.emplace_back(std::make_unique<InotifyWatch>(w.first, w.second));
} catch (InotifyException &e) {
watches.clear();
2016-06-21 02:34:11 +00:00
log_error(e.what());
this->sleep(0.1s);
2016-06-14 04:26:46 +00:00
return;
}
2016-05-19 14:41:06 +00:00
while (this->enabled()) {
ConstCastModule(ModuleImpl).idle();
2016-05-19 14:41:06 +00:00
for (auto &&w : watches) {
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, "Polling inotify event for watch at "+ (*w)());
2016-05-19 14:41:06 +00:00
if (w->has_event(500 / watches.size())) {
std::unique_ptr<InotifyEvent> event = w->get_event();
2016-05-19 14:41:06 +00:00
watches.clear();
2016-06-21 02:34:11 +00:00
std::lock_guard<concurrency::SpinLock> lck(this->update_lock);
if (!this->poll_throttler->passthrough())
return;
2016-05-19 14:41:06 +00:00
if (CastModule(ModuleImpl)->on_event(event.get()))
CastModule(ModuleImpl)->broadcast();
2016-05-19 14:41:06 +00:00
return;
}
}
}
}
2016-06-21 02:34:11 +00:00
void watch(std::string path, int mask = InotifyEvent::ALL)
2016-05-19 14:41:06 +00:00
{
2016-06-28 02:58:32 +00:00
log_trace2(this->logger, path);
2016-05-19 14:41:06 +00:00
this->watch_list.insert(std::make_pair(path, mask));
}
public:
explicit InotifyModule(std::string name)
2016-06-21 02:34:11 +00:00
: Module<ModuleImpl>(name)
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs))) {}
InotifyModule(std::string name, std::string path, int mask = InotifyEvent::ALL)
: Module<ModuleImpl>(name)
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs))) {
2016-05-19 14:41:06 +00:00
this->watch(path, mask);
}
2016-06-21 02:34:11 +00:00
InotifyModule(std::string name, std::vector<std::string> paths, int mask = InotifyEvent::ALL)
: Module<ModuleImpl>(name)
, poll_throttler(std::make_unique<EventThrottler>(event_throttler::limit_t(ThrottleLimit), event_throttler::timewindow_t(ThrottleWindowMs)))
2016-05-19 14:41:06 +00:00
{
for (auto &&path : paths)
this->watch(path, mask);
}
2016-06-21 02:34:11 +00:00
~InotifyModule() {
std::lock_guard<concurrency::SpinLock> lck(this->poll_lock);
}
2016-05-19 14:41:06 +00:00
void start()
{
this->enable(true);
this->threads.emplace_back(std::thread(&InotifyModule::runner, this));
}
};
}