refactor(modules): Get rid of .inl files

This commit is contained in:
Michael Carlberg 2016-12-21 09:21:35 +01:00
parent 7979f5b3d4
commit 788a7ca3a3
31 changed files with 144 additions and 276 deletions

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// #include "components/types.hpp"
// #include "components/builder.hpp"
#include "modules/meta/base.hpp" #include "modules/meta/base.hpp"
POLYBAR_NS POLYBAR_NS
@ -13,10 +10,44 @@ namespace modules {
public: public:
using module<Impl>::module; using module<Impl>::module;
void start(); void start() {
this->m_mainthread = thread(&event_module::runner, this);
}
protected: protected:
void runner(); void runner() {
try {
// Warm up module output and
// send broadcast before entering
// the update loop
if (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->update();
CAST_MOD(Impl)->broadcast();
}
while (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->idle();
if (!CONST_MOD(Impl).running()) {
break;
}
std::lock_guard<std::mutex> guard(this->m_updatelock);
if (!CAST_MOD(Impl)->has_event()) {
continue;
} else if (!CONST_MOD(Impl).running()) {
break;
} else if (!CAST_MOD(Impl)->update()) {
continue;
}
CAST_MOD(Impl)->broadcast();
}
} catch (const exception& err) {
CAST_MOD(Impl)->halt(err.what());
}
}
}; };
} }

View File

@ -1,50 +0,0 @@
POLYBAR_NS
namespace modules {
// public {{{
template <class Impl>
void event_module<Impl>::start() {
CAST_MOD(Impl)->m_mainthread = thread(&event_module::runner, this);
}
// }}}
// protected {{{
template <class Impl>
void event_module<Impl>::runner() {
try {
// Send initial broadcast to warmup cache
if (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->update();
CAST_MOD(Impl)->broadcast();
}
while (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->idle();
if (!CONST_MOD(Impl).running())
break;
std::lock_guard<std::mutex> guard(this->m_updatelock);
{
if (!CAST_MOD(Impl)->has_event())
continue;
if (!CONST_MOD(Impl).running())
break;
if (!CAST_MOD(Impl)->update())
continue;
CAST_MOD(Impl)->broadcast();
}
}
} catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) {
CAST_MOD(Impl)->halt(err.what());
}
}
// }}}
}
POLYBAR_NS_END

View File

@ -11,13 +11,87 @@ namespace modules {
public: public:
using module<Impl>::module; using module<Impl>::module;
void start(); void start() {
this->m_mainthread = thread(&inotify_module::runner, this);
}
protected: protected:
void runner(); void runner() {
void watch(string path, int mask = IN_ALL_EVENTS); try {
void idle(); // Warm up module output and
void poll_events(); // send broadcast before entering
// the update loop
if (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->on_event(nullptr);
CAST_MOD(Impl)->broadcast();
}
while (CONST_MOD(Impl).running()) {
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) {
this->m_log.trace("%s: Attach inotify at %s", CONST_MOD(Impl).name(), path);
m_watchlist.insert(make_pair(path, mask));
}
void idle() {
this->sleep(200ms);
}
void poll_events() {
vector<unique_ptr<inotify_watch>> watches;
try {
for (auto&& w : m_watchlist) {
watches.emplace_back(inotify_util::make_watch(w.first));
watches.back()->attach(w.second);
}
} catch (const system_error& e) {
watches.clear();
this->m_log.err("%s: Error while creating inotify watch (what: %s)", CONST_MOD(Impl).name(), e.what());
CAST_MOD(Impl)->sleep(0.1s);
return;
}
while (CONST_MOD(Impl).running()) {
std::unique_lock<std::mutex> guard(this->m_updatelock);
{
for (auto&& w : watches) {
this->m_log.trace_x("%s: Poll inotify watch %s", CONST_MOD(Impl).name(), w->path());
if (w->poll(1000 / watches.size())) {
auto event = w->get_event();
for (auto&& w : watches) {
try {
w->remove();
} catch (const system_error&) {
}
}
if (CAST_MOD(Impl)->on_event(event.get()))
CAST_MOD(Impl)->broadcast();
CAST_MOD(Impl)->idle();
return;
}
if (!CONST_MOD(Impl).running())
break;
}
}
guard.unlock();
CAST_MOD(Impl)->idle();
}
}
private: private:
map<string, int> m_watchlist; map<string, int> m_watchlist;

View File

@ -1,96 +0,0 @@
POLYBAR_NS
namespace modules {
// public {{{
template <class Impl>
void inotify_module<Impl>::start() {
CAST_MOD(Impl)->m_mainthread = thread(&inotify_module::runner, this);
}
// }}}
// protected {{{
template <class Impl>
void inotify_module<Impl>::runner() {
try {
// Send initial broadcast to warmup cache
if (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->on_event(nullptr);
CAST_MOD(Impl)->broadcast();
}
while (CONST_MOD(Impl).running()) {
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());
}
}
template <class Impl>
void inotify_module<Impl>::watch(string path, int mask) {
this->m_log.trace("%s: Attach inotify at %s", CONST_MOD(Impl).name(), path);
m_watchlist.insert(make_pair(path, mask));
}
template <class Impl>
void inotify_module<Impl>::idle() {
CAST_MOD(Impl)->sleep(200ms);
}
template <class Impl>
void inotify_module<Impl>::poll_events() {
vector<unique_ptr<inotify_watch>> watches;
try {
for (auto&& w : m_watchlist) {
watches.emplace_back(inotify_util::make_watch(w.first));
watches.back()->attach(w.second);
}
} catch (const system_error& e) {
watches.clear();
this->m_log.err("%s: Error while creating inotify watch (what: %s)", CONST_MOD(Impl).name(), e.what());
CAST_MOD(Impl)->sleep(0.1s);
return;
}
while (CONST_MOD(Impl).running()) {
std::unique_lock<std::mutex> guard(this->m_updatelock);
{
for (auto&& w : watches) {
this->m_log.trace_x("%s: Poll inotify watch %s", CONST_MOD(Impl).name(), w->path());
if (w->poll(1000 / watches.size())) {
auto event = w->get_event();
for (auto&& w : watches) {
try {
w->remove();
} catch (const system_error&) {
}
}
if (CAST_MOD(Impl)->on_event(event.get()))
CAST_MOD(Impl)->broadcast();
CAST_MOD(Impl)->idle();
return;
}
if (!CONST_MOD(Impl).running())
break;
}
}
guard.unlock();
CAST_MOD(Impl)->idle();
}
}
// }}}
}
POLYBAR_NS_END

View File

@ -1,8 +1,5 @@
#pragma once #pragma once
// #include "components/builder.hpp"
// #include "components/types.hpp"
#include "modules/meta/base.hpp" #include "modules/meta/base.hpp"
POLYBAR_NS POLYBAR_NS
@ -13,8 +10,13 @@ namespace modules {
public: public:
using module<Impl>::module; using module<Impl>::module;
void start(); void start() {
bool build(builder*, string) const; CAST_MOD(Impl)->broadcast();
}
bool build(builder*, string) const {
return true;
}
}; };
} }

View File

@ -1,15 +0,0 @@
POLYBAR_NS
namespace modules {
template <typename Impl>
void static_module<Impl>::start() {
CAST_MOD(Impl)->broadcast();
}
template <typename Impl>
bool static_module<Impl>::build(builder*, string) const {
return true;
}
}
POLYBAR_NS_END

View File

@ -16,12 +16,32 @@ namespace modules {
public: public:
using module<Impl>::module; using module<Impl>::module;
void start(); void start() {
this->m_mainthread = thread(&timer_module::runner, this);
}
protected: protected:
interval_t m_interval{1}; interval_t m_interval{1};
void runner(); void runner() {
try {
while (CONST_MOD(Impl).running()) {
{
std::lock_guard<std::mutex> guard(this->m_updatelock);
if (CAST_MOD(Impl)->update())
this->broadcast();
}
if (CONST_MOD(Impl).running()) {
this->sleep(m_interval);
}
}
} catch (const module_error& err) {
this->halt(err.what());
} catch (const std::exception& err) {
this->halt(err.what());
}
}
}; };
} }

View File

@ -1,38 +0,0 @@
POLYBAR_NS
namespace modules {
// public {{{
template <typename Impl>
void timer_module<Impl>::start() {
CAST_MOD(Impl)->m_mainthread = thread(&timer_module::runner, this);
}
// }}}
// protected {{{
template <typename Impl>
void timer_module<Impl>::runner() {
try {
while (CONST_MOD(Impl).running()) {
{
std::lock_guard<std::mutex> guard(this->m_updatelock);
if (CAST_MOD(Impl)->update())
CAST_MOD(Impl)->broadcast();
}
if (CONST_MOD(Impl).running()) {
CAST_MOD(Impl)->sleep(m_interval);
}
}
} catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) {
CAST_MOD(Impl)->halt(err.what());
}
}
// }}}
}
POLYBAR_NS_END

View File

@ -6,16 +6,6 @@
#include "modules/meta/base.hpp" #include "modules/meta/base.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#if not(ENABLE_ALSA && ENABLE_I3 && ENABLE_MPD)
#include "modules/meta/event_module.inl"
#endif
#if not(ENABLE_NETWORK && ENABLE_CURL)
#include "modules/meta/timer_module.inl"
#endif
#if not WITH_XKB
#include "modules/meta/static_module.inl"
#endif
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
@ -39,12 +29,6 @@ namespace modules {
string contents() { \ string contents() { \
return ""; \ return ""; \
} \ } \
bool handle_event(string) { \
return false; \
} \
bool receive_events() const { \
return false; \
} \
void set_update_cb(callback<>&&) {} \ void set_update_cb(callback<>&&) {} \
void set_stop_cb(callback<>&&) {} \ void set_stop_cb(callback<>&&) {} \
} }

View File

@ -6,13 +6,11 @@
#include "utils/file.hpp" #include "utils/file.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/inotify_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<backlight_module>; template class module<backlight_module>;
template class inotify_module<backlight_module>;
void backlight_module::brightness_handle::filepath(const string& path) { void backlight_module::brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) { if (!file_util::exists(path)) {

View File

@ -9,13 +9,11 @@
#include "utils/math.hpp" #include "utils/math.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/inotify_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<battery_module>; template class module<battery_module>;
template class inotify_module<battery_module>;
/** /**
* Bootstrap module by setting up required components * Bootstrap module by setting up required components

View File

@ -7,7 +7,6 @@
#include "utils/file.hpp" #include "utils/file.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
POLYBAR_NS POLYBAR_NS
@ -37,7 +36,6 @@ namespace {
namespace modules { namespace modules {
template class module<bspwm_module>; template class module<bspwm_module>;
template class event_module<bspwm_module>;
bspwm_module::bspwm_module(const bar_settings& bar, string name_) : event_module<bspwm_module>(bar, move(name_)) { bspwm_module::bspwm_module(const bar_settings& bar, string name_) : event_module<bspwm_module>(bar, move(name_)) {
auto socket_path = bspwm_util::get_socket_path(); auto socket_path = bspwm_util::get_socket_path();

View File

@ -1,13 +1,11 @@
#include "modules/counter.hpp" #include "modules/counter.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<counter_module>; template class module<counter_module>;
template class timer_module<counter_module>;
counter_module::counter_module(const bar_settings& bar, string name_) counter_module::counter_module(const bar_settings& bar, string name_)
: timer_module<counter_module>(bar, move(name_)) { : timer_module<counter_module>(bar, move(name_)) {

View File

@ -9,13 +9,11 @@
#include "utils/math.hpp" #include "utils/math.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<cpu_module>; template class module<cpu_module>;
template class timer_module<cpu_module>;
cpu_module::cpu_module(const bar_settings& bar, string name_) : timer_module<cpu_module>(bar, move(name_)) { cpu_module::cpu_module(const bar_settings& bar, string name_) : timer_module<cpu_module>(bar, move(name_)) {
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1)); m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));

View File

@ -2,13 +2,11 @@
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<date_module>; template class module<date_module>;
template class timer_module<date_module>;
date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) { date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) {
if (!m_bar.locale.empty()) { if (!m_bar.locale.empty()) {

View File

@ -11,13 +11,11 @@
#include "utils/string.hpp" #include "utils/string.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<fs_module>; template class module<fs_module>;
template class timer_module<fs_module>;
/** /**
* Bootstrap the module by reading config values and * Bootstrap the module by reading config values and

View File

@ -2,13 +2,11 @@
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<github_module>; template class module<github_module>;
template class timer_module<github_module>;
/** /**
* Construct module * Construct module

View File

@ -7,13 +7,11 @@
#include "utils/file.hpp" #include "utils/file.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<i3_module>; template class module<i3_module>;
template class event_module<i3_module>;
i3_module::i3_module(const bar_settings& bar, string name_) : event_module<i3_module>(bar, move(name_)) { i3_module::i3_module(const bar_settings& bar, string name_) : event_module<i3_module>(bar, move(name_)) {
auto socket_path = i3ipc::get_socketpath(); auto socket_path = i3ipc::get_socketpath();

View File

@ -3,13 +3,11 @@
#include "components/ipc.hpp" #include "components/ipc.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<ipc_module>; template class module<ipc_module>;
template class static_module<ipc_module>;
/** /**
* Load user-defined ipc hooks and * Load user-defined ipc hooks and

View File

@ -7,13 +7,11 @@
#include "drawtypes/progressbar.hpp" #include "drawtypes/progressbar.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<memory_module>; template class module<memory_module>;
template class timer_module<memory_module>;
memory_module::memory_module(const bar_settings& bar, string name_) : timer_module<memory_module>(bar, move(name_)) { memory_module::memory_module(const bar_settings& bar, string name_) : timer_module<memory_module>(bar, move(name_)) {
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1)); m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));

View File

@ -5,13 +5,11 @@
#include "utils/scope.hpp" #include "utils/scope.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<menu_module>; template class module<menu_module>;
template class static_module<menu_module>;
menu_module::menu_module(const bar_settings& bar, string name_) : static_module<menu_module>(bar, move(name_)) { menu_module::menu_module(const bar_settings& bar, string name_) : static_module<menu_module>(bar, move(name_)) {
string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU}; string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU};

View File

@ -6,7 +6,6 @@
#include "utils/factory.hpp" #include "utils/factory.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
POLYBAR_NS POLYBAR_NS
@ -14,7 +13,6 @@ using namespace mpd;
namespace modules { namespace modules {
template class module<mpd_module>; template class module<mpd_module>;
template class event_module<mpd_module>;
mpd_module::mpd_module(const bar_settings& bar, string name_) : event_module<mpd_module>(bar, move(name_)) { mpd_module::mpd_module(const bar_settings& bar, string name_) : event_module<mpd_module>(bar, move(name_)) {
m_host = m_conf.get<string>(name(), "host", m_host); m_host = m_conf.get<string>(name(), "host", m_host);

View File

@ -6,13 +6,11 @@
#include "utils/factory.hpp" #include "utils/factory.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<network_module>; template class module<network_module>;
template class timer_module<network_module>;
network_module::network_module(const bar_settings& bar, string name_) network_module::network_module(const bar_settings& bar, string name_)
: timer_module<network_module>(bar, move(name_)) { : timer_module<network_module>(bar, move(name_)) {

View File

@ -2,13 +2,11 @@
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<script_module>; template class module<script_module>;
template class event_module<script_module>;
script_module::script_module(const bar_settings& bar, string name_) : event_module<script_module>(bar, move(name_)) { script_module::script_module(const bar_settings& bar, string name_) : event_module<script_module>(bar, move(name_)) {
REQ_CONFIG_VALUE(name(), m_exec, "exec"); REQ_CONFIG_VALUE(name(), m_exec, "exec");

View File

@ -6,13 +6,11 @@
#include "utils/math.hpp" #include "utils/math.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<temperature_module>; template class module<temperature_module>;
template class timer_module<temperature_module>;
temperature_module::temperature_module(const bar_settings& bar, string name_) temperature_module::temperature_module(const bar_settings& bar, string name_)
: timer_module<temperature_module>(bar, move(name_)) { : timer_module<temperature_module>(bar, move(name_)) {

View File

@ -1,13 +1,11 @@
#include "modules/text.hpp" #include "modules/text.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<text_module>; template class module<text_module>;
template class static_module<text_module>;
text_module::text_module(const bar_settings& bar, string name_) : static_module<text_module>(bar, move(name_)) { text_module::text_module(const bar_settings& bar, string name_) : static_module<text_module>(bar, move(name_)) {
m_formatter->add("content", "", {}); m_formatter->add("content", "", {});

View File

@ -8,7 +8,6 @@
#include "utils/math.hpp" #include "utils/math.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/event_module.inl"
POLYBAR_NS POLYBAR_NS
@ -16,7 +15,6 @@ using namespace alsa;
namespace modules { namespace modules {
template class module<volume_module>; template class module<volume_module>;
template class event_module<volume_module>;
volume_module::volume_module(const bar_settings& bar, string name_) : event_module<volume_module>(bar, move(name_)) { volume_module::volume_module(const bar_settings& bar, string name_) : event_module<volume_module>(bar, move(name_)) {
// Load configuration values // Load configuration values

View File

@ -8,13 +8,11 @@
#include "x11/xutils.hpp" #include "x11/xutils.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<xbacklight_module>; template class module<xbacklight_module>;
template class static_module<xbacklight_module>;
/** /**
* Construct module * Construct module

View File

@ -6,13 +6,11 @@
#include "x11/connection.hpp" #include "x11/connection.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<xkeyboard_module>; template class module<xkeyboard_module>;
template class static_module<xkeyboard_module>;
/** /**
* Construct module * Construct module

View File

@ -6,13 +6,11 @@
#include "x11/graphics.hpp" #include "x11/graphics.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<xwindow_module>; template class module<xwindow_module>;
template class static_module<xwindow_module>;
/** /**
* Wrapper used to update the event mask of the * Wrapper used to update the event mask of the

View File

@ -9,13 +9,11 @@
#include "x11/connection.hpp" #include "x11/connection.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/static_module.inl"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
template class module<xworkspaces_module>; template class module<xworkspaces_module>;
template class static_module<xworkspaces_module>;
/** /**
* Construct module * Construct module