diff --git a/include/modules/meta/event_module.hpp b/include/modules/meta/event_module.hpp index ae77b983..c26bbcd1 100644 --- a/include/modules/meta/event_module.hpp +++ b/include/modules/meta/event_module.hpp @@ -1,8 +1,5 @@ #pragma once -// #include "components/types.hpp" -// #include "components/builder.hpp" - #include "modules/meta/base.hpp" POLYBAR_NS @@ -13,10 +10,44 @@ namespace modules { public: using module::module; - void start(); + void start() { + this->m_mainthread = thread(&event_module::runner, this); + } 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 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()); + } + } }; } diff --git a/include/modules/meta/event_module.inl b/include/modules/meta/event_module.inl deleted file mode 100644 index 890bf0a5..00000000 --- a/include/modules/meta/event_module.inl +++ /dev/null @@ -1,50 +0,0 @@ -POLYBAR_NS - -namespace modules { - // public {{{ - - template - void event_module::start() { - CAST_MOD(Impl)->m_mainthread = thread(&event_module::runner, this); - } - - // }}} - // protected {{{ - - template - void event_module::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 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 diff --git a/include/modules/meta/inotify_module.hpp b/include/modules/meta/inotify_module.hpp index 1f46dbcd..6e21692c 100644 --- a/include/modules/meta/inotify_module.hpp +++ b/include/modules/meta/inotify_module.hpp @@ -11,13 +11,87 @@ namespace modules { public: using module::module; - void start(); + void start() { + this->m_mainthread = thread(&inotify_module::runner, this); + } protected: - void runner(); - void watch(string path, int mask = IN_ALL_EVENTS); - void idle(); - void poll_events(); + void runner() { + try { + // Warm up module output and + // 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> 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 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: map m_watchlist; diff --git a/include/modules/meta/inotify_module.inl b/include/modules/meta/inotify_module.inl deleted file mode 100644 index 91d371f0..00000000 --- a/include/modules/meta/inotify_module.inl +++ /dev/null @@ -1,96 +0,0 @@ -POLYBAR_NS - -namespace modules { - // public {{{ - - template - void inotify_module::start() { - CAST_MOD(Impl)->m_mainthread = thread(&inotify_module::runner, this); - } - - // }}} - // protected {{{ - - template - void inotify_module::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 - void inotify_module::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 - void inotify_module::idle() { - CAST_MOD(Impl)->sleep(200ms); - } - - template - void inotify_module::poll_events() { - vector> 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 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 diff --git a/include/modules/meta/static_module.hpp b/include/modules/meta/static_module.hpp index 10aa83cb..f926c934 100644 --- a/include/modules/meta/static_module.hpp +++ b/include/modules/meta/static_module.hpp @@ -1,8 +1,5 @@ #pragma once -// #include "components/builder.hpp" -// #include "components/types.hpp" - #include "modules/meta/base.hpp" POLYBAR_NS @@ -13,8 +10,13 @@ namespace modules { public: using module::module; - void start(); - bool build(builder*, string) const; + void start() { + CAST_MOD(Impl)->broadcast(); + } + + bool build(builder*, string) const { + return true; + } }; } diff --git a/include/modules/meta/static_module.inl b/include/modules/meta/static_module.inl deleted file mode 100644 index db4c32f4..00000000 --- a/include/modules/meta/static_module.inl +++ /dev/null @@ -1,15 +0,0 @@ -POLYBAR_NS - -namespace modules { - template - void static_module::start() { - CAST_MOD(Impl)->broadcast(); - } - - template - bool static_module::build(builder*, string) const { - return true; - } -} - -POLYBAR_NS_END diff --git a/include/modules/meta/timer_module.hpp b/include/modules/meta/timer_module.hpp index 8e46bf26..73f6cd59 100644 --- a/include/modules/meta/timer_module.hpp +++ b/include/modules/meta/timer_module.hpp @@ -16,12 +16,32 @@ namespace modules { public: using module::module; - void start(); + void start() { + this->m_mainthread = thread(&timer_module::runner, this); + } protected: interval_t m_interval{1}; - void runner(); + void runner() { + try { + while (CONST_MOD(Impl).running()) { + { + std::lock_guard 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()); + } + } }; } diff --git a/include/modules/meta/timer_module.inl b/include/modules/meta/timer_module.inl deleted file mode 100644 index 2eb6ea27..00000000 --- a/include/modules/meta/timer_module.inl +++ /dev/null @@ -1,38 +0,0 @@ -POLYBAR_NS - -namespace modules { - // public {{{ - - template - void timer_module::start() { - CAST_MOD(Impl)->m_mainthread = thread(&timer_module::runner, this); - } - - // }}} - // protected {{{ - - template - void timer_module::runner() { - try { - while (CONST_MOD(Impl).running()) { - { - std::lock_guard 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 diff --git a/include/modules/unsupported.hpp b/include/modules/unsupported.hpp index bd32633a..0325107b 100644 --- a/include/modules/unsupported.hpp +++ b/include/modules/unsupported.hpp @@ -6,16 +6,6 @@ #include "modules/meta/base.hpp" #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 namespace modules { @@ -39,12 +29,6 @@ namespace modules { string contents() { \ return ""; \ } \ - bool handle_event(string) { \ - return false; \ - } \ - bool receive_events() const { \ - return false; \ - } \ void set_update_cb(callback<>&&) {} \ void set_stop_cb(callback<>&&) {} \ } diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index bb0f60fd..12068246 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -6,13 +6,11 @@ #include "utils/file.hpp" #include "modules/meta/base.inl" -#include "modules/meta/inotify_module.inl" POLYBAR_NS namespace modules { template class module; - template class inotify_module; void backlight_module::brightness_handle::filepath(const string& path) { if (!file_util::exists(path)) { diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index ef7d8572..bc0b188f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -9,13 +9,11 @@ #include "utils/math.hpp" #include "modules/meta/base.inl" -#include "modules/meta/inotify_module.inl" POLYBAR_NS namespace modules { template class module; - template class inotify_module; /** * Bootstrap module by setting up required components diff --git a/src/modules/bspwm.cpp b/src/modules/bspwm.cpp index dcfc1b4a..cbffe3aa 100644 --- a/src/modules/bspwm.cpp +++ b/src/modules/bspwm.cpp @@ -7,7 +7,6 @@ #include "utils/file.hpp" #include "modules/meta/base.inl" -#include "modules/meta/event_module.inl" POLYBAR_NS @@ -37,7 +36,6 @@ namespace { namespace modules { template class module; - template class event_module; bspwm_module::bspwm_module(const bar_settings& bar, string name_) : event_module(bar, move(name_)) { auto socket_path = bspwm_util::get_socket_path(); diff --git a/src/modules/counter.cpp b/src/modules/counter.cpp index b37733a8..25c7ae9e 100644 --- a/src/modules/counter.cpp +++ b/src/modules/counter.cpp @@ -1,13 +1,11 @@ #include "modules/counter.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; counter_module::counter_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 2a6cd0fe..18b9ba3e 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -9,13 +9,11 @@ #include "utils/math.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; cpu_module::cpu_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { m_interval = chrono::duration(m_conf.get(name(), "interval", 1)); diff --git a/src/modules/date.cpp b/src/modules/date.cpp index e202bc14..ea4401f2 100644 --- a/src/modules/date.cpp +++ b/src/modules/date.cpp @@ -2,13 +2,11 @@ #include "drawtypes/label.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; date_module::date_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { if (!m_bar.locale.empty()) { diff --git a/src/modules/fs.cpp b/src/modules/fs.cpp index fdbc567b..f261a841 100644 --- a/src/modules/fs.cpp +++ b/src/modules/fs.cpp @@ -11,13 +11,11 @@ #include "utils/string.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; /** * Bootstrap the module by reading config values and diff --git a/src/modules/github.cpp b/src/modules/github.cpp index e462d772..ea9879bb 100644 --- a/src/modules/github.cpp +++ b/src/modules/github.cpp @@ -2,13 +2,11 @@ #include "drawtypes/label.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; /** * Construct module diff --git a/src/modules/i3.cpp b/src/modules/i3.cpp index af3b0e79..90a943c3 100644 --- a/src/modules/i3.cpp +++ b/src/modules/i3.cpp @@ -7,13 +7,11 @@ #include "utils/file.hpp" #include "modules/meta/base.inl" -#include "modules/meta/event_module.inl" POLYBAR_NS namespace modules { template class module; - template class event_module; i3_module::i3_module(const bar_settings& bar, string name_) : event_module(bar, move(name_)) { auto socket_path = i3ipc::get_socketpath(); diff --git a/src/modules/ipc.cpp b/src/modules/ipc.cpp index 566fab2e..c886e45e 100644 --- a/src/modules/ipc.cpp +++ b/src/modules/ipc.cpp @@ -3,13 +3,11 @@ #include "components/ipc.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; /** * Load user-defined ipc hooks and diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index d8163b90..30143195 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -7,13 +7,11 @@ #include "drawtypes/progressbar.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; memory_module::memory_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { m_interval = chrono::duration(m_conf.get(name(), "interval", 1)); diff --git a/src/modules/menu.cpp b/src/modules/menu.cpp index a1752a62..d1228c64 100644 --- a/src/modules/menu.cpp +++ b/src/modules/menu.cpp @@ -5,13 +5,11 @@ #include "utils/scope.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; menu_module::menu_module(const bar_settings& bar, string name_) : static_module(bar, move(name_)) { string default_format{TAG_LABEL_TOGGLE + string{" "} + TAG_MENU}; diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index 35e3208b..fd7bec59 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -6,7 +6,6 @@ #include "utils/factory.hpp" #include "modules/meta/base.inl" -#include "modules/meta/event_module.inl" POLYBAR_NS @@ -14,7 +13,6 @@ using namespace mpd; namespace modules { template class module; - template class event_module; mpd_module::mpd_module(const bar_settings& bar, string name_) : event_module(bar, move(name_)) { m_host = m_conf.get(name(), "host", m_host); diff --git a/src/modules/network.cpp b/src/modules/network.cpp index e9da1c8a..566e5e5d 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -6,13 +6,11 @@ #include "utils/factory.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; network_module::network_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { diff --git a/src/modules/script.cpp b/src/modules/script.cpp index 3f79454c..f3859010 100644 --- a/src/modules/script.cpp +++ b/src/modules/script.cpp @@ -2,13 +2,11 @@ #include "drawtypes/label.hpp" #include "modules/meta/base.inl" -#include "modules/meta/event_module.inl" POLYBAR_NS namespace modules { template class module; - template class event_module; script_module::script_module(const bar_settings& bar, string name_) : event_module(bar, move(name_)) { REQ_CONFIG_VALUE(name(), m_exec, "exec"); diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index b8745edc..543f2470 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -6,13 +6,11 @@ #include "utils/math.hpp" #include "modules/meta/base.inl" -#include "modules/meta/timer_module.inl" POLYBAR_NS namespace modules { template class module; - template class timer_module; temperature_module::temperature_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) { diff --git a/src/modules/text.cpp b/src/modules/text.cpp index bdd28432..752175fd 100644 --- a/src/modules/text.cpp +++ b/src/modules/text.cpp @@ -1,13 +1,11 @@ #include "modules/text.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; text_module::text_module(const bar_settings& bar, string name_) : static_module(bar, move(name_)) { m_formatter->add("content", "", {}); diff --git a/src/modules/volume.cpp b/src/modules/volume.cpp index 98be9083..15884e35 100644 --- a/src/modules/volume.cpp +++ b/src/modules/volume.cpp @@ -8,7 +8,6 @@ #include "utils/math.hpp" #include "modules/meta/base.inl" -#include "modules/meta/event_module.inl" POLYBAR_NS @@ -16,7 +15,6 @@ using namespace alsa; namespace modules { template class module; - template class event_module; volume_module::volume_module(const bar_settings& bar, string name_) : event_module(bar, move(name_)) { // Load configuration values diff --git a/src/modules/xbacklight.cpp b/src/modules/xbacklight.cpp index f2eb327c..54b34d8a 100644 --- a/src/modules/xbacklight.cpp +++ b/src/modules/xbacklight.cpp @@ -8,13 +8,11 @@ #include "x11/xutils.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; /** * Construct module diff --git a/src/modules/xkeyboard.cpp b/src/modules/xkeyboard.cpp index ac8454e6..65fbd12b 100644 --- a/src/modules/xkeyboard.cpp +++ b/src/modules/xkeyboard.cpp @@ -6,13 +6,11 @@ #include "x11/connection.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; /** * Construct module diff --git a/src/modules/xwindow.cpp b/src/modules/xwindow.cpp index e51d1f9b..80a0051d 100644 --- a/src/modules/xwindow.cpp +++ b/src/modules/xwindow.cpp @@ -6,13 +6,11 @@ #include "x11/graphics.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; /** * Wrapper used to update the event mask of the diff --git a/src/modules/xworkspaces.cpp b/src/modules/xworkspaces.cpp index 870de3b1..88da4eb2 100644 --- a/src/modules/xworkspaces.cpp +++ b/src/modules/xworkspaces.cpp @@ -9,13 +9,11 @@ #include "x11/connection.hpp" #include "modules/meta/base.inl" -#include "modules/meta/static_module.inl" POLYBAR_NS namespace modules { template class module; - template class static_module; /** * Construct module