2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/backlight.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2016-11-19 09:49:03 -05:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.inl"
|
2020-01-15 10:32:17 -05:00
|
|
|
#include "utils/file.hpp"
|
|
|
|
#include "utils/math.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
|
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>;
|
|
|
|
|
2016-12-21 02:38:44 -05:00
|
|
|
void backlight_module::brightness_handle::filepath(const string& path) {
|
|
|
|
if (!file_util::exists(path)) {
|
|
|
|
throw module_error("The file '" + path + "' does not exist");
|
|
|
|
}
|
|
|
|
m_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
float backlight_module::brightness_handle::read() const {
|
2016-12-30 22:20:46 -05:00
|
|
|
return std::strtof(file_util::contents(m_path).c_str(), nullptr);
|
2016-12-21 02:38:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 02:00:09 -05:00
|
|
|
backlight_module::backlight_module(const bar_settings& bar, string name_)
|
|
|
|
: inotify_module<backlight_module>(bar, move(name_)) {
|
2021-10-02 20:57:49 -04:00
|
|
|
m_router->register_action(EVENT_DEC, [this]() { action_dec(); });
|
|
|
|
m_router->register_action(EVENT_INC, [this]() { action_inc(); });
|
2021-01-04 04:25:52 -05:00
|
|
|
|
2016-12-30 17:32:05 -05:00
|
|
|
auto card = m_conf.get(name(), "card");
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2020-01-15 10:32:17 -05:00
|
|
|
// Get flag to check if we should add scroll handlers for changing value
|
|
|
|
m_scroll = m_conf.get(name(), "enable-scroll", m_scroll);
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
// 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)) {
|
2017-01-13 14:03:08 -05: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
|
|
|
|
2019-10-15 10:39:18 -04:00
|
|
|
// Build path to the sysfs folder the current/maximum brightness values are located
|
2020-01-15 10:32:17 -05:00
|
|
|
m_path_backlight = string_util::replace(PATH_BACKLIGHT, "%card%", card);
|
2019-10-15 10:39:18 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* amdgpu drivers set the actual_brightness in a different scale than [0, max_brightness]
|
|
|
|
* The only sensible way is to use the 'brightness' file instead
|
|
|
|
* Ref: https://github.com/Alexays/Waybar/issues/335
|
|
|
|
*/
|
2021-02-16 04:02:33 -05:00
|
|
|
bool card_is_amdgpu = (card.substr(0, 9) == "amdgpu_bl");
|
|
|
|
m_use_actual_brightness = m_conf.get(name(), "use-actual-brightness", !card_is_amdgpu);
|
|
|
|
|
|
|
|
std::string brightness_type = (m_use_actual_brightness ? "actual_brightness" : "brightness");
|
2020-06-05 15:48:17 -04:00
|
|
|
auto path_backlight_val = m_path_backlight + "/" + brightness_type;
|
2019-10-15 10:39:18 -04:00
|
|
|
|
|
|
|
m_val.filepath(path_backlight_val);
|
2020-01-15 10:32:17 -05:00
|
|
|
m_max.filepath(m_path_backlight + "/max_brightness");
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
// Add inotify watch
|
2019-10-15 10:39:18 -04:00
|
|
|
watch(path_backlight_val);
|
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
|
|
|
|
2020-01-15 10:32:17 -05:00
|
|
|
m_max_brightness = m_max.read();
|
|
|
|
m_percentage = static_cast<int>(m_val.read() / m_max_brightness * 100.0f + 0.5f);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
2017-01-13 14:03:08 -05:00
|
|
|
m_label->replace_token("%percentage%", to_string(m_percentage));
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-15 10:32:17 -05:00
|
|
|
string backlight_module::get_output() {
|
|
|
|
// Get the module output early so that
|
|
|
|
// the format prefix/suffix also gets wrapped
|
|
|
|
// with the cmd handlers
|
|
|
|
string output{module::get_output()};
|
|
|
|
|
|
|
|
if (m_scroll) {
|
2020-05-23 18:36:16 -04:00
|
|
|
m_builder->action(mousebtn::SCROLL_UP, *this, EVENT_INC, "");
|
|
|
|
m_builder->action(mousebtn::SCROLL_DOWN, *this, EVENT_DEC, "");
|
2020-01-15 10:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m_builder->append(std::move(output));
|
|
|
|
|
2020-05-24 12:35:12 -04:00
|
|
|
m_builder->action_close();
|
|
|
|
m_builder->action_close();
|
2020-01-15 10:32:17 -05:00
|
|
|
|
|
|
|
return m_builder->flush();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2020-01-15 10:32:17 -05:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void backlight_module::action_inc() {
|
|
|
|
change_value(5);
|
|
|
|
}
|
2020-01-15 10:32:17 -05:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void backlight_module::action_dec() {
|
|
|
|
change_value(-5);
|
|
|
|
}
|
2020-01-15 10:32:17 -05:00
|
|
|
|
2021-01-04 04:25:52 -05:00
|
|
|
void backlight_module::change_value(int value_mod) {
|
|
|
|
m_log.info("%s: Changing value by %d%", name(), value_mod);
|
2020-01-15 10:32:17 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
|
|
|
|
int value = math_util::percentage_to_value<int>(rounded, m_max_brightness);
|
|
|
|
file_util::write_contents(m_path_backlight + "/brightness", to_string(value));
|
|
|
|
} catch (const exception& err) {
|
|
|
|
m_log.err(
|
|
|
|
"%s: Unable to change backlight value. Your system may require additional "
|
|
|
|
"configuration. Please read the module documentation.\n(reason: %s)",
|
|
|
|
name(), err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace modules
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|