2016-11-20 17:04:31 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
2021-09-21 14:59:48 -04:00
|
|
|
#include <atomic>
|
2016-11-20 17:04:31 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2016-12-05 14:41:00 -05:00
|
|
|
#include <map>
|
2016-12-22 16:11:03 -05:00
|
|
|
#include <mutex>
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
#include "common.hpp"
|
2023-05-01 08:58:52 -04:00
|
|
|
#include "components/config.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "components/types.hpp"
|
2016-11-25 07:55:15 -05:00
|
|
|
#include "errors.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "utils/concurrency.hpp"
|
|
|
|
#include "utils/inotify.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
POLYBAR_NS
|
|
|
|
|
2016-11-20 18:18:08 -05:00
|
|
|
namespace chrono = std::chrono;
|
|
|
|
using namespace std::chrono_literals;
|
2021-09-21 14:59:48 -04:00
|
|
|
using std::atomic;
|
2016-12-05 14:41:00 -05:00
|
|
|
using std::map;
|
2016-11-20 18:18:08 -05:00
|
|
|
|
2016-11-20 17:04:31 -05: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>;
|
Add units support (POINT, PIXEL, SPACE) (#2578)
* add units support (POINT, PIXEL, SPACE) for polybar
- add a size_with_unit struct
- add a geometry_format_values struct
- move dpi initialisation from renderer.cpp to bar.cpp
- add a string to size_with_unit converter
- add point support (with pt)
- add pixel support (with px)
* Fix unit test compilation
* clang-format
* Better names
The old names didn't really capture the purpose of the structs and
function.
space_type -> spacing_type
space_size -> spacing_val
size_type -> extent_type
geometry -> extent_val
geometry_format_values -> percentage_with_offset
* Remove parse_size_with_unit
No longer needed. The convert<spacing_val> function in config.cpp
already does all the work for us and always setting the type to pixel
was wrong.
In addition, line-size should not be of type spacing_val but extent_val.
* Cleanup
I tried to address most of my comments on the old PR
* Fix renderer width calculation
We can't just blindly add the x difference to the width because for
example the width should increase if x < width and the increase keeps
x < width.
Similarly, we can't just add the offset to the width.
* Rename geom_format_to_pixels to percentage_with_offset_to_pixel
* Cleanup
* Apply suggested changes from Patrick on GitHub
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/bar.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/config.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* config: Use stod for parsing percentage
* Use stof instead of strtof
* units: Fix test edge cases
* Remove unnecessary clang-format toggle
* Use percentage_with_offset for margin-{top,bottom}
* Support negative extent values
* Rename unit to units and create a cpp file
* Move percentage_with_offset_to_pixel unit test to units
* Add unit tests for units_utils
* Clarify when and how negative spacing/extent is allowed
Negative spacing is never allowed and produces a config error.
Extents allow negative values in theory, but only a few use-cases accept
it.
Only the extent value used for the `%{O}` tag and the offset value in
percentage_with_offset can be negative. Everything else is capped below
at 0.
The final pixel value of percentage_with_offset also caps below at 0.
* Fix parsing errors not being caught in config
* Print a proper error message for uncaught exceptions
* Cleanup module::get_output
All changes preserve the existing semantics
* Stop using remove_trailing_space in module::get_output
Instead, we first check if the current tag is built, and only if it is,
the spacing is prepended.
* Remove unused imports
* Restore old behavior
If there are two tags and the second one isn't built (module::build
returns false), the space in between them is removed.
For example in the mpd module:
format-online = <toggle> <label-song> foo
If mpd is not running, the mpd module will return false when trying to
build the `<label-song>` tag. If we don't remove the space between
`<toggle>` and `<label-song>`, we end up with two spaces between
`<toggle>` and `foo`.
This change is to match the old behavior where at least one trailing
space character was removed from the builder.
* Add changelog entry
* Remove unused setting
* Use percentage with offset for tray-offset
Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr>
Co-authored-by: Joe Groocock <github@frebib.net>
2022-02-20 15:08:57 -05:00
|
|
|
} // namespace drawtypes
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
class builder;
|
2016-12-22 16:11:03 -05:00
|
|
|
class config;
|
|
|
|
class logger;
|
|
|
|
class signal_emitter;
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
class action_router;
|
2016-11-20 17:04:31 -05:00
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace modules {
|
2021-01-04 04:25:52 -05:00
|
|
|
|
2016-11-20 17:04:31 -05: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 04:23:54 -05:00
|
|
|
string value{};
|
|
|
|
label_t prefix{};
|
|
|
|
label_t suffix{};
|
2019-10-27 17:41:18 -04:00
|
|
|
rgba fg{};
|
|
|
|
rgba bg{};
|
|
|
|
rgba ul{};
|
|
|
|
rgba ol{};
|
2017-01-10 19:42:55 -05:00
|
|
|
size_t ulsize{0};
|
|
|
|
size_t olsize{0};
|
Add units support (POINT, PIXEL, SPACE) (#2578)
* add units support (POINT, PIXEL, SPACE) for polybar
- add a size_with_unit struct
- add a geometry_format_values struct
- move dpi initialisation from renderer.cpp to bar.cpp
- add a string to size_with_unit converter
- add point support (with pt)
- add pixel support (with px)
* Fix unit test compilation
* clang-format
* Better names
The old names didn't really capture the purpose of the structs and
function.
space_type -> spacing_type
space_size -> spacing_val
size_type -> extent_type
geometry -> extent_val
geometry_format_values -> percentage_with_offset
* Remove parse_size_with_unit
No longer needed. The convert<spacing_val> function in config.cpp
already does all the work for us and always setting the type to pixel
was wrong.
In addition, line-size should not be of type spacing_val but extent_val.
* Cleanup
I tried to address most of my comments on the old PR
* Fix renderer width calculation
We can't just blindly add the x difference to the width because for
example the width should increase if x < width and the increase keeps
x < width.
Similarly, we can't just add the offset to the width.
* Rename geom_format_to_pixels to percentage_with_offset_to_pixel
* Cleanup
* Apply suggested changes from Patrick on GitHub
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/bar.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/config.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* config: Use stod for parsing percentage
* Use stof instead of strtof
* units: Fix test edge cases
* Remove unnecessary clang-format toggle
* Use percentage_with_offset for margin-{top,bottom}
* Support negative extent values
* Rename unit to units and create a cpp file
* Move percentage_with_offset_to_pixel unit test to units
* Add unit tests for units_utils
* Clarify when and how negative spacing/extent is allowed
Negative spacing is never allowed and produces a config error.
Extents allow negative values in theory, but only a few use-cases accept
it.
Only the extent value used for the `%{O}` tag and the offset value in
percentage_with_offset can be negative. Everything else is capped below
at 0.
The final pixel value of percentage_with_offset also caps below at 0.
* Fix parsing errors not being caught in config
* Print a proper error message for uncaught exceptions
* Cleanup module::get_output
All changes preserve the existing semantics
* Stop using remove_trailing_space in module::get_output
Instead, we first check if the current tag is built, and only if it is,
the spacing is prepended.
* Remove unused imports
* Restore old behavior
If there are two tags and the second one isn't built (module::build
returns false), the space in between them is removed.
For example in the mpd module:
format-online = <toggle> <label-song> foo
If mpd is not running, the mpd module will return false when trying to
build the `<label-song>` tag. If we don't remove the space between
`<toggle>` and `<label-song>`, we end up with two spaces between
`<toggle>` and `foo`.
This change is to match the old behavior where at least one trailing
space character was removed from the builder.
* Add changelog entry
* Remove unused setting
* Use percentage with offset for tray-offset
Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr>
Co-authored-by: Joe Groocock <github@frebib.net>
2022-02-20 15:08:57 -05:00
|
|
|
spacing_val spacing{ZERO_SPACE};
|
|
|
|
spacing_val padding{ZERO_SPACE};
|
|
|
|
spacing_val margin{ZERO_SPACE};
|
|
|
|
extent_val offset{ZERO_PX_EXTENT};
|
2019-01-13 16:10:34 -05:00
|
|
|
int font{0};
|
2016-11-20 17:04:31 -05: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 09:55:13 -05:00
|
|
|
void add_optional(string name, vector<string>&& tags, vector<string>&& whitelist = {});
|
2016-11-25 07:55:15 -05:00
|
|
|
bool has(const string& tag, const string& format_name);
|
|
|
|
bool has(const string& tag);
|
2020-12-02 09:55:13 -05:00
|
|
|
bool has_format(const string& format_name);
|
2016-11-25 07:55:15 -05:00
|
|
|
shared_ptr<module_format> get(const string& format_name);
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
protected:
|
2020-12-02 09:55:13 -05:00
|
|
|
void add_value(string&& name, string&& value, vector<string>&& tags, vector<string>&& whitelist);
|
|
|
|
|
2016-11-20 17:04:31 -05: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 13:59:08 -04:00
|
|
|
/**
|
|
|
|
* The type users have to specify in the module section `type` key
|
|
|
|
*/
|
|
|
|
virtual string type() const = 0;
|
|
|
|
|
2020-05-30 18:09:44 -04:00
|
|
|
/**
|
|
|
|
* Module name w/o 'module/' prefix
|
|
|
|
*/
|
|
|
|
virtual string name_raw() const = 0;
|
2016-11-20 17:04:31 -05:00
|
|
|
virtual string name() const = 0;
|
|
|
|
virtual bool running() const = 0;
|
2020-12-27 10:05:26 -05:00
|
|
|
virtual bool visible() const = 0;
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2020-11-22 17:05:45 -05:00
|
|
|
/**
|
|
|
|
* Handle action, possibly with data attached
|
|
|
|
*
|
|
|
|
* Any implementation is free to ignore the data, if the action does not
|
|
|
|
* require additional data.
|
|
|
|
*
|
2022-02-20 15:40:48 -05:00
|
|
|
* @returns true if the action is supported and false otherwise
|
2020-11-22 17:05:45 -05:00
|
|
|
*/
|
|
|
|
virtual bool input(const string& action, const string& data) = 0;
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
virtual void start() = 0;
|
2019-06-01 19:23:36 -04:00
|
|
|
virtual void join() = 0;
|
2016-11-20 17:04:31 -05:00
|
|
|
virtual void stop() = 0;
|
|
|
|
virtual void halt(string error_message) = 0;
|
|
|
|
virtual string contents() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// class definition : module {{{
|
|
|
|
|
|
|
|
template <class Impl>
|
2020-11-22 17:05:45 -05:00
|
|
|
class module : public module_interface {
|
2016-11-20 17:04:31 -05:00
|
|
|
public:
|
2023-05-01 08:58:52 -04:00
|
|
|
module(const bar_settings& bar, string name, const config&);
|
2016-11-20 17:04:31 -05:00
|
|
|
~module() noexcept;
|
|
|
|
|
2021-07-07 15:43:49 -04:00
|
|
|
static constexpr auto EVENT_MODULE_TOGGLE = "module_toggle";
|
|
|
|
static constexpr auto EVENT_MODULE_SHOW = "module_show";
|
|
|
|
static constexpr auto EVENT_MODULE_HIDE = "module_hide";
|
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
string type() const override;
|
2020-05-15 13:59:08 -04:00
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
string name_raw() const override;
|
|
|
|
string name() const override;
|
|
|
|
bool running() const override;
|
2020-12-27 10:05:26 -05:00
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
bool visible() const override;
|
2020-12-27 10:05:26 -05:00
|
|
|
|
2021-10-15 04:33:10 -04:00
|
|
|
void start() override;
|
2019-06-01 19:23:36 -04:00
|
|
|
void join() final override;
|
2021-01-04 04:38:43 -05:00
|
|
|
void stop() override;
|
|
|
|
void halt(string error_message) override;
|
2016-11-20 17:04:31 -05:00
|
|
|
void teardown();
|
2021-01-04 04:38:43 -05:00
|
|
|
string contents() override;
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
bool input(const string& action, const string& data) final override;
|
2019-12-02 13:14:26 -05:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
protected:
|
|
|
|
void broadcast();
|
|
|
|
void idle();
|
2017-01-09 23:12:53 -05:00
|
|
|
void sleep(chrono::duration<double> duration);
|
2020-06-20 16:15:25 -04:00
|
|
|
template <class Clock, class Duration>
|
|
|
|
void sleep_until(chrono::time_point<Clock, Duration> point);
|
2021-02-17 13:48:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wakes up the module.
|
|
|
|
*
|
|
|
|
* It should be possible to interrupt any blocking operation inside a
|
|
|
|
* module using this function.
|
|
|
|
*
|
|
|
|
* In addition, after a wake up whatever was woken up should immediately
|
|
|
|
* check whether the module is still running.
|
|
|
|
*
|
|
|
|
* Modules that don't follow this, could stall the operation of whatever
|
|
|
|
* code called this function.
|
|
|
|
*/
|
2016-11-20 17:04:31 -05:00
|
|
|
void wakeup();
|
|
|
|
string get_format() const;
|
|
|
|
string get_output();
|
|
|
|
|
2021-07-07 15:43:49 -04:00
|
|
|
void set_visible(bool value);
|
|
|
|
|
|
|
|
void action_module_toggle();
|
|
|
|
void action_module_show();
|
|
|
|
void action_module_hide();
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
protected:
|
2016-12-22 16:11:03 -05:00
|
|
|
signal_emitter& m_sig;
|
2022-03-14 17:11:46 -04:00
|
|
|
const bar_settings& m_bar;
|
2016-11-20 17:04:31 -05:00
|
|
|
const logger& m_log;
|
|
|
|
const config& m_conf;
|
|
|
|
|
2021-10-02 20:57:49 -04:00
|
|
|
unique_ptr<action_router> m_router;
|
2021-01-04 04:25:52 -05:00
|
|
|
|
2016-12-22 23:18:58 -05:00
|
|
|
mutex m_buildlock;
|
|
|
|
mutex m_updatelock;
|
|
|
|
mutex m_sleeplock;
|
2016-11-20 17:04:31 -05:00
|
|
|
std::condition_variable m_sleephandler;
|
|
|
|
|
2020-05-15 13:59:08 -04:00
|
|
|
const string m_name;
|
|
|
|
const string m_name_raw;
|
2016-11-20 17:04:31 -05:00
|
|
|
unique_ptr<builder> m_builder;
|
|
|
|
unique_ptr<module_formatter> m_formatter;
|
|
|
|
vector<thread> m_threads;
|
|
|
|
thread m_mainthread;
|
|
|
|
|
2017-04-02 12:12:07 -04:00
|
|
|
bool m_handle_events{true};
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
private:
|
2021-10-15 04:33:10 -04:00
|
|
|
atomic<bool> m_enabled{false};
|
2020-12-27 10:05:26 -05:00
|
|
|
atomic<bool> m_visible{true};
|
2016-12-22 23:19:45 -05:00
|
|
|
atomic<bool> m_changed{true};
|
2016-11-20 17:04:31 -05:00
|
|
|
string m_cache;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
Add units support (POINT, PIXEL, SPACE) (#2578)
* add units support (POINT, PIXEL, SPACE) for polybar
- add a size_with_unit struct
- add a geometry_format_values struct
- move dpi initialisation from renderer.cpp to bar.cpp
- add a string to size_with_unit converter
- add point support (with pt)
- add pixel support (with px)
* Fix unit test compilation
* clang-format
* Better names
The old names didn't really capture the purpose of the structs and
function.
space_type -> spacing_type
space_size -> spacing_val
size_type -> extent_type
geometry -> extent_val
geometry_format_values -> percentage_with_offset
* Remove parse_size_with_unit
No longer needed. The convert<spacing_val> function in config.cpp
already does all the work for us and always setting the type to pixel
was wrong.
In addition, line-size should not be of type spacing_val but extent_val.
* Cleanup
I tried to address most of my comments on the old PR
* Fix renderer width calculation
We can't just blindly add the x difference to the width because for
example the width should increase if x < width and the increase keeps
x < width.
Similarly, we can't just add the offset to the width.
* Rename geom_format_to_pixels to percentage_with_offset_to_pixel
* Cleanup
* Apply suggested changes from Patrick on GitHub
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/bar.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/config.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* Update src/components/builder.cpp
Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
* config: Use stod for parsing percentage
* Use stof instead of strtof
* units: Fix test edge cases
* Remove unnecessary clang-format toggle
* Use percentage_with_offset for margin-{top,bottom}
* Support negative extent values
* Rename unit to units and create a cpp file
* Move percentage_with_offset_to_pixel unit test to units
* Add unit tests for units_utils
* Clarify when and how negative spacing/extent is allowed
Negative spacing is never allowed and produces a config error.
Extents allow negative values in theory, but only a few use-cases accept
it.
Only the extent value used for the `%{O}` tag and the offset value in
percentage_with_offset can be negative. Everything else is capped below
at 0.
The final pixel value of percentage_with_offset also caps below at 0.
* Fix parsing errors not being caught in config
* Print a proper error message for uncaught exceptions
* Cleanup module::get_output
All changes preserve the existing semantics
* Stop using remove_trailing_space in module::get_output
Instead, we first check if the current tag is built, and only if it is,
the spacing is prepended.
* Remove unused imports
* Restore old behavior
If there are two tags and the second one isn't built (module::build
returns false), the space in between them is removed.
For example in the mpd module:
format-online = <toggle> <label-song> foo
If mpd is not running, the mpd module will return false when trying to
build the `<label-song>` tag. If we don't remove the space between
`<toggle>` and `<label-song>`, we end up with two spaces between
`<toggle>` and `foo`.
This change is to match the old behavior where at least one trailing
space character was removed from the builder.
* Add changelog entry
* Remove unused setting
* Use percentage with offset for tray-offset
Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr>
Co-authored-by: Joe Groocock <github@frebib.net>
2022-02-20 15:08:57 -05:00
|
|
|
} // namespace modules
|
2016-11-20 17:04:31 -05:00
|
|
|
|
|
|
|
POLYBAR_NS_END
|