1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/include/modules/backlight.hpp

101 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
2016-05-19 10:41:06 -04:00
#include "components/config.hpp"
2016-10-25 01:10:03 -04:00
#include "config.hpp"
2016-05-19 10:41:06 -04:00
#include "drawtypes/label.hpp"
2016-06-14 23:32:35 -04:00
#include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp"
#include "modules/meta.hpp"
2016-05-19 10:41:06 -04:00
2016-06-14 23:32:35 -04:00
LEMONBUDDY_NS
namespace modules {
struct brightness_handle {
void filepath(string path) {
if (!file_util::exists(path))
throw module_error("The file '" + path + "' does not exist");
m_path = path;
}
float read() const {
return std::strtof(file_util::get_contents(m_path).c_str(), 0);
}
private:
string m_path;
};
2016-06-14 23:32:35 -04:00
class backlight_module : public inotify_module<backlight_module> {
public:
using inotify_module::inotify_module;
void setup() {
// Load configuration values
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});
if (m_formatter->has(TAG_LABEL))
2016-10-25 01:10:03 -04:00
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%");
2016-06-14 23:32:35 -04:00
if (m_formatter->has(TAG_BAR))
2016-10-25 01:10:03 -04:00
m_progressbar = load_progressbar(m_bar, m_conf, name(), TAG_BAR);
2016-06-14 23:32:35 -04:00
if (m_formatter->has(TAG_RAMP))
2016-10-25 01:10:03 -04:00
m_ramp = load_ramp(m_conf, name(), TAG_RAMP);
2016-06-14 23:32:35 -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));
2016-06-14 23:32:35 -04:00
// Add inotify watch
watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
}
2016-05-19 10:41:06 -04:00
void idle() {
sleep(75ms);
}
2016-06-14 23:32:35 -04:00
bool on_event(inotify_event* event) {
if (event != nullptr)
m_log.trace("%s: %s", name(), event->filename);
2016-05-19 10:41:06 -04:00
m_percentage = static_cast<int>(m_val.read() / m_max.read() * 100.0f + 0.5f);
2016-05-19 10:41:06 -04:00
if (m_label) {
m_label->reset_tokens();
m_label->replace_token("%percentage%", to_string(m_percentage) + "%");
}
2016-06-14 23:32:35 -04:00
return true;
}
bool build(builder* builder, string tag) const {
2016-06-14 23:32:35 -04:00
if (tag == TAG_BAR)
builder->node(m_progressbar->output(m_percentage));
else if (tag == TAG_RAMP)
builder->node(m_ramp->get_by_percentage(m_percentage));
else if (tag == TAG_LABEL)
builder->node(m_label);
2016-06-14 23:32:35 -04:00
else
return false;
return true;
}
private:
static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_BAR = "<bar>";
static constexpr auto TAG_RAMP = "<ramp>";
ramp_t m_ramp;
label_t m_label;
progressbar_t m_progressbar;
brightness_handle m_val;
brightness_handle m_max;
2016-06-14 23:32:35 -04:00
int m_percentage = 0;
2016-05-19 10:41:06 -04:00
};
}
2016-06-14 23:32:35 -04:00
LEMONBUDDY_NS_END