2016-11-20 22:04:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2016-12-05 19:41:00 +00:00
|
|
|
#include <map>
|
2016-12-22 21:11:03 +00:00
|
|
|
#include <mutex>
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/types.hpp"
|
2016-11-25 12:55:15 +00:00
|
|
|
#include "errors.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/concurrency.hpp"
|
|
|
|
#include "utils/functional.hpp"
|
|
|
|
#include "utils/inotify.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
POLYBAR_NS
|
|
|
|
|
2016-11-20 23:18:08 +00:00
|
|
|
namespace chrono = std::chrono;
|
|
|
|
using namespace std::chrono_literals;
|
2016-12-05 19:41:00 +00:00
|
|
|
using std::map;
|
2016-11-20 23:18:08 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#define DEFAULT_FORMAT "format"
|
|
|
|
|
|
|
|
#define CONST_MOD(name) static_cast<name const&>(*this)
|
|
|
|
#define CAST_MOD(name) static_cast<name*>(this)
|
|
|
|
|
|
|
|
// fwd decl {{{
|
|
|
|
|
|
|
|
namespace drawtypes {
|
|
|
|
class ramp;
|
|
|
|
using ramp_t = shared_ptr<ramp>;
|
|
|
|
class progressbar;
|
|
|
|
using progressbar_t = shared_ptr<progressbar>;
|
|
|
|
class animation;
|
|
|
|
using animation_t = shared_ptr<animation>;
|
|
|
|
class iconset;
|
|
|
|
using iconset_t = shared_ptr<iconset>;
|
2020-06-20 20:15:25 +00:00
|
|
|
} // namespace drawtypes
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
class builder;
|
2016-12-22 21:11:03 +00:00
|
|
|
class config;
|
|
|
|
class logger;
|
|
|
|
class signal_emitter;
|
2016-11-20 22:04:31 +00:00
|
|
|
|
2021-01-04 09:25:52 +00:00
|
|
|
template <typename Impl>
|
|
|
|
class action_router;
|
2016-11-20 22:04:31 +00:00
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace modules {
|
2021-01-04 09:25:52 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
using namespace drawtypes;
|
|
|
|
|
|
|
|
DEFINE_ERROR(module_error);
|
|
|
|
DEFINE_CHILD_ERROR(undefined_format, module_error);
|
|
|
|
DEFINE_CHILD_ERROR(undefined_format_tag, module_error);
|
|
|
|
|
|
|
|
// class definition : module_format {{{
|
|
|
|
|
|
|
|
struct module_format {
|
2016-12-16 09:23:54 +00:00
|
|
|
string value{};
|
|
|
|
vector<string> tags{};
|
|
|
|
label_t prefix{};
|
|
|
|
label_t suffix{};
|
2019-10-27 21:41:18 +00:00
|
|
|
rgba fg{};
|
|
|
|
rgba bg{};
|
|
|
|
rgba ul{};
|
|
|
|
rgba ol{};
|
2017-01-11 00:42:55 +00:00
|
|
|
size_t ulsize{0};
|
|
|
|
size_t olsize{0};
|
|
|
|
size_t spacing{0};
|
|
|
|
size_t padding{0};
|
|
|
|
size_t margin{0};
|
|
|
|
int offset{0};
|
2019-01-13 21:10:34 +00:00
|
|
|
int font{0};
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
string decorate(builder* builder, string output);
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// class definition : module_formatter {{{
|
|
|
|
|
|
|
|
class module_formatter {
|
|
|
|
public:
|
|
|
|
explicit module_formatter(const config& conf, string modname) : m_conf(conf), m_modname(modname) {}
|
|
|
|
|
|
|
|
void add(string name, string fallback, vector<string>&& tags, vector<string>&& whitelist = {});
|
2020-12-02 14:55:13 +00:00
|
|
|
void add_optional(string name, vector<string>&& tags, vector<string>&& whitelist = {});
|
2016-11-25 12:55:15 +00:00
|
|
|
bool has(const string& tag, const string& format_name);
|
|
|
|
bool has(const string& tag);
|
2020-12-02 14:55:13 +00:00
|
|
|
bool has_format(const string& format_name);
|
2016-11-25 12:55:15 +00:00
|
|
|
shared_ptr<module_format> get(const string& format_name);
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
protected:
|
2020-12-02 14:55:13 +00:00
|
|
|
void add_value(string&& name, string&& value, vector<string>&& tags, vector<string>&& whitelist);
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
const config& m_conf;
|
|
|
|
string m_modname;
|
|
|
|
map<string, shared_ptr<module_format>> m_formats;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
// class definition : module_interface {{{
|
|
|
|
|
|
|
|
struct module_interface {
|
|
|
|
public:
|
|
|
|
virtual ~module_interface() {}
|
|
|
|
|
2020-05-15 17:59:08 +00:00
|
|
|
/**
|
|
|
|
* The type users have to specify in the module section `type` key
|
|
|
|
*/
|
|
|
|
virtual string type() const = 0;
|
|
|
|
|
2020-05-30 22:09:44 +00:00
|
|
|
/**
|
|
|
|
* Module name w/o 'module/' prefix
|
|
|
|
*/
|
|
|
|
virtual string name_raw() const = 0;
|
2016-11-20 22:04:31 +00:00
|
|
|
virtual string name() const = 0;
|
|
|
|
virtual bool running() const = 0;
|
2020-12-27 15:05:26 +00:00
|
|
|
virtual bool visible() const = 0;
|
|
|
|
virtual void set_visible(bool value) = 0;
|
2016-11-20 22:04:31 +00:00
|
|
|
|
2020-11-22 22:05:45 +00:00
|
|
|
/**
|
|
|
|
* Handle action, possibly with data attached
|
|
|
|
*
|
|
|
|
* Any implementation is free to ignore the data, if the action does not
|
|
|
|
* require additional data.
|
|
|
|
*
|
|
|
|
* \returns true if the action is supported and false otherwise
|
|
|
|
*/
|
|
|
|
virtual bool input(const string& action, const string& data) = 0;
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
virtual void start() = 0;
|
|
|
|
virtual void stop() = 0;
|
|
|
|
virtual void halt(string error_message) = 0;
|
|
|
|
virtual string contents() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// class definition : module {{{
|
|
|
|
|
|
|
|
template <class Impl>
|
2020-11-22 22:05:45 +00:00
|
|
|
class module : public module_interface {
|
2016-11-20 22:04:31 +00:00
|
|
|
public:
|
2016-12-09 08:40:46 +00:00
|
|
|
module(const bar_settings bar, string name);
|
2016-11-20 22:04:31 +00:00
|
|
|
~module() noexcept;
|
|
|
|
|
2021-01-04 09:38:43 +00:00
|
|
|
string type() const override;
|
2020-05-15 17:59:08 +00:00
|
|
|
|
2021-01-04 09:38:43 +00:00
|
|
|
string name_raw() const override;
|
|
|
|
string name() const override;
|
|
|
|
bool running() const override;
|
2020-12-27 15:05:26 +00:00
|
|
|
|
2021-01-04 09:38:43 +00:00
|
|
|
bool visible() const override;
|
|
|
|
void set_visible(bool value) override;
|
2020-12-27 15:05:26 +00:00
|
|
|
|
2021-01-04 09:38:43 +00:00
|
|
|
void stop() override;
|
|
|
|
void halt(string error_message) override;
|
2016-11-20 22:04:31 +00:00
|
|
|
void teardown();
|
2021-01-04 09:38:43 +00:00
|
|
|
string contents() override;
|
2016-11-20 22:04:31 +00:00
|
|
|
|
2021-01-04 09:38:43 +00:00
|
|
|
bool input(const string& action, const string& data) final override;
|
2019-12-02 18:14:26 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
protected:
|
|
|
|
void broadcast();
|
|
|
|
void idle();
|
2017-01-10 04:12:53 +00:00
|
|
|
void sleep(chrono::duration<double> duration);
|
2020-06-20 20:15:25 +00:00
|
|
|
template <class Clock, class Duration>
|
|
|
|
void sleep_until(chrono::time_point<Clock, Duration> point);
|
2016-11-20 22:04:31 +00:00
|
|
|
void wakeup();
|
|
|
|
string get_format() const;
|
|
|
|
string get_output();
|
|
|
|
|
|
|
|
protected:
|
2016-12-22 21:11:03 +00:00
|
|
|
signal_emitter& m_sig;
|
2016-11-20 22:04:31 +00:00
|
|
|
const bar_settings m_bar;
|
|
|
|
const logger& m_log;
|
|
|
|
const config& m_conf;
|
|
|
|
|
2021-01-04 09:25:52 +00:00
|
|
|
unique_ptr<action_router<Impl>> m_router;
|
|
|
|
|
2016-12-23 04:18:58 +00:00
|
|
|
mutex m_buildlock;
|
|
|
|
mutex m_updatelock;
|
|
|
|
mutex m_sleeplock;
|
2016-11-20 22:04:31 +00:00
|
|
|
std::condition_variable m_sleephandler;
|
|
|
|
|
2020-05-15 17:59:08 +00:00
|
|
|
const string m_name;
|
|
|
|
const string m_name_raw;
|
2016-11-20 22:04:31 +00:00
|
|
|
unique_ptr<builder> m_builder;
|
|
|
|
unique_ptr<module_formatter> m_formatter;
|
|
|
|
vector<thread> m_threads;
|
|
|
|
thread m_mainthread;
|
|
|
|
|
2017-04-02 16:12:07 +00:00
|
|
|
bool m_handle_events{true};
|
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
private:
|
2016-12-23 04:19:45 +00:00
|
|
|
atomic<bool> m_enabled{true};
|
2020-12-27 15:05:26 +00:00
|
|
|
atomic<bool> m_visible{true};
|
2016-12-23 04:19:45 +00:00
|
|
|
atomic<bool> m_changed{true};
|
2016-11-20 22:04:31 +00:00
|
|
|
string m_cache;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2020-06-20 20:15:25 +00:00
|
|
|
} // namespace modules
|
2016-11-20 22:04:31 +00:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|