1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/src/modules/backlight.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

2016-11-02 15:22:45 -04:00
#include "modules/backlight.hpp"
2016-11-20 17:04:31 -05:00
#include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp"
2016-11-02 15:22:45 -04:00
#include "utils/file.hpp"
2016-11-20 17:04:31 -05:00
#include "modules/meta/base.inl"
#include "modules/meta/inotify_module.inl"
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-11-02 15:22:45 -04:00
namespace modules {
2016-11-20 17:04:31 -05:00
template class module<backlight_module>;
template class inotify_module<backlight_module>;
backlight_module::backlight_module(const bar_settings& bar, string name_)
: inotify_module<backlight_module>(bar, move(name_)) {
2016-11-02 15:22:45 -04:00
auto card = m_conf.get<string>(name(), "card");
// Add formats and elements
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP});
2016-11-25 07:55:15 -05:00
if (m_formatter->has(TAG_LABEL)) {
2016-11-02 15:22:45 -04:00
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%");
2016-11-25 07:55:15 -05:00
}
if (m_formatter->has(TAG_BAR)) {
2016-11-02 15:22:45 -04:00
m_progressbar = load_progressbar(m_bar, m_conf, name(), TAG_BAR);
2016-11-25 07:55:15 -05:00
}
if (m_formatter->has(TAG_RAMP)) {
2016-11-02 15:22:45 -04:00
m_ramp = load_ramp(m_conf, name(), TAG_RAMP);
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
// Build path to the file where the current/maximum brightness value is located
m_val.filepath(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
m_max.filepath(string_util::replace(PATH_BACKLIGHT_MAX, "%card%", card));
// Add inotify watch
watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
}
void brightness_handle::filepath(const string& path) {
if (!file_util::exists(path)) {
throw module_error("The file '" + path + "' does not exist");
}
m_path = path;
}
float brightness_handle::read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), nullptr);
}
2016-11-02 15:22:45 -04:00
void backlight_module::idle() {
sleep(75ms);
}
bool backlight_module::on_event(inotify_event* event) {
2016-11-25 07:55:15 -05:00
if (event != nullptr) {
2016-11-02 15:22:45 -04:00
m_log.trace("%s: %s", name(), event->filename);
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
m_percentage = static_cast<int>(m_val.read() / m_max.read() * 100.0f + 0.5f);
if (m_label) {
m_label->reset_tokens();
m_label->replace_token("%percentage%", to_string(m_percentage) + "%");
}
return true;
}
2016-11-25 07:55:15 -05:00
bool backlight_module::build(builder* builder, const string& tag) const {
if (tag == TAG_BAR) {
2016-11-02 15:22:45 -04:00
builder->node(m_progressbar->output(m_percentage));
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_RAMP) {
2016-11-02 15:22:45 -04:00
builder->node(m_ramp->get_by_percentage(m_percentage));
2016-11-25 07:55:15 -05:00
} else if (tag == TAG_LABEL) {
2016-11-02 15:22:45 -04:00
builder->node(m_label);
2016-11-25 07:55:15 -05:00
} else {
2016-11-02 15:22:45 -04:00
return false;
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
return true;
}
}
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END