2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/xbacklight.hpp"
|
2016-11-19 09:49:03 -05:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
#include "utils/math.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "x11/connection.hpp"
|
2017-01-24 02:49:27 -05:00
|
|
|
#include "x11/winspec.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.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<xbacklight_module>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct module
|
|
|
|
*/
|
2016-12-21 02:00:09 -05:00
|
|
|
xbacklight_module::xbacklight_module(const bar_settings& bar, string name_)
|
|
|
|
: static_module<xbacklight_module>(bar, move(name_)), m_connection(connection::make()) {
|
2016-12-30 17:32:05 -05:00
|
|
|
auto output = m_conf.get(name(), "output", m_bar.monitor->name);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2019-06-24 11:59:24 -04:00
|
|
|
auto monitors = randr_util::get_monitors(m_connection, m_connection.root(), bar.monitor_strict, false);
|
2018-12-02 19:21:20 -05:00
|
|
|
|
|
|
|
m_output = randr_util::match_monitor(monitors, output, bar.monitor_exact);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
// If we didn't get a match we stop the module
|
|
|
|
if (!m_output) {
|
|
|
|
throw module_error("No matching output found for \"" + output + "\", stopping module...");
|
|
|
|
}
|
|
|
|
|
2016-11-12 03:40:14 -05:00
|
|
|
// Get flag to check if we should add scroll handlers for changing value
|
2016-12-30 17:32:05 -05:00
|
|
|
m_scroll = m_conf.get(name(), "enable-scroll", m_scroll);
|
2016-11-12 03:40:14 -05:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
// Query randr for the backlight max and min value
|
|
|
|
try {
|
|
|
|
auto& backlight = m_output->backlight;
|
|
|
|
randr_util::get_backlight_range(m_connection, m_output, backlight);
|
|
|
|
randr_util::get_backlight_value(m_connection, m_output, backlight);
|
2016-11-12 03:40:14 -05:00
|
|
|
} catch (const exception& err) {
|
2017-01-12 21:52:56 -05:00
|
|
|
m_log.err("%s: Could not get data (err: %s)", name(), err.what());
|
2018-12-02 19:21:20 -05:00
|
|
|
throw module_error("Not supported for \"" + m_output->name + "\"");
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 03:40:14 -05:00
|
|
|
// Create window that will proxy all RandR notify events
|
2017-01-24 02:49:27 -05:00
|
|
|
// clang-format off
|
|
|
|
m_proxy = winspec(m_connection)
|
|
|
|
<< cw_size(1, 1)
|
|
|
|
<< cw_pos(-1, -1)
|
|
|
|
<< cw_flush(true);
|
|
|
|
// clang-format on
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-12 03:40:14 -05:00
|
|
|
// Connect with the event registry and make sure we get
|
|
|
|
// notified when a RandR output property gets modified
|
|
|
|
m_connection.select_input_checked(m_proxy, XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for XCB_RANDR_NOTIFY events
|
|
|
|
*/
|
|
|
|
void xbacklight_module::handle(const evt::randr_notify& evt) {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (evt->subCode != XCB_RANDR_NOTIFY_OUTPUT_PROPERTY) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (evt->u.op.status != XCB_PROPERTY_NEW_VALUE) {
|
2016-11-12 03:40:14 -05:00
|
|
|
return;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (evt->u.op.window != m_proxy) {
|
2016-11-12 03:40:14 -05:00
|
|
|
return;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (evt->u.op.output != m_output->output) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (evt->u.op.atom != m_output->backlight.atom) {
|
2016-11-12 03:40:14 -05:00
|
|
|
return;
|
2016-11-26 00:13:20 -05:00
|
|
|
} else {
|
|
|
|
update();
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query the RandR extension for the new values
|
|
|
|
*/
|
|
|
|
void xbacklight_module::update() {
|
|
|
|
auto& bl = m_output->backlight;
|
|
|
|
randr_util::get_backlight_value(m_connection, m_output, bl);
|
2017-01-10 22:16:33 -05:00
|
|
|
m_percentage = math_util::nearest_5(math_util::percentage<double>(bl.val, bl.min, bl.max));
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
// Update label tokens
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Emit a broadcast notification so that
|
|
|
|
// the new data will be drawn to the bar
|
|
|
|
broadcast();
|
|
|
|
}
|
|
|
|
|
2016-11-12 03:40:14 -05:00
|
|
|
/**
|
|
|
|
* Generate the module output
|
|
|
|
*/
|
|
|
|
string xbacklight_module::get_output() {
|
2016-12-04 23:32:10 -05:00
|
|
|
// Get the module output early so that
|
2016-12-20 01:04:13 -05:00
|
|
|
// the format prefix/suffix also gets wrapped
|
2016-12-04 23:32:10 -05:00
|
|
|
// with the cmd handlers
|
|
|
|
string output{module::get_output()};
|
|
|
|
|
2018-02-10 20:56:11 -05:00
|
|
|
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, "");
|
2018-02-10 20:56:11 -05:00
|
|
|
}
|
2016-11-24 22:10:26 -05:00
|
|
|
|
2016-12-04 23:32:10 -05:00
|
|
|
m_builder->append(output);
|
2016-11-12 03:40:14 -05:00
|
|
|
|
2020-05-24 12:35:12 -04:00
|
|
|
m_builder->action_close();
|
|
|
|
m_builder->action_close();
|
2016-12-20 01:04:13 -05:00
|
|
|
|
2016-11-12 03:40:14 -05:00
|
|
|
return m_builder->flush();
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
/**
|
|
|
|
* Output content as defined in the config
|
|
|
|
*/
|
2016-11-25 07:55:15 -05:00
|
|
|
bool xbacklight_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-12 03:40:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process scroll events by changing backlight value
|
|
|
|
*/
|
2020-05-31 15:11:36 -04:00
|
|
|
bool xbacklight_module::input(const string& action, const string&) {
|
2017-01-10 22:16:33 -05:00
|
|
|
double value_mod{0.0};
|
2016-11-12 03:40:14 -05:00
|
|
|
|
2020-05-24 12:35:12 -04:00
|
|
|
if (action == EVENT_INC) {
|
2017-01-10 22:16:33 -05:00
|
|
|
value_mod = 5.0;
|
2016-11-12 03:40:14 -05:00
|
|
|
m_log.info("%s: Increasing value by %i%", name(), value_mod);
|
2020-05-24 12:35:12 -04:00
|
|
|
} else if (action == EVENT_DEC) {
|
2017-01-10 22:16:33 -05:00
|
|
|
value_mod = -5.0;
|
2016-11-12 03:40:14 -05:00
|
|
|
m_log.info("%s: Decreasing value by %i%", name(), -value_mod);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-01-10 22:16:33 -05:00
|
|
|
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
|
|
|
|
const int values[1]{math_util::percentage_to_value<int>(rounded, m_output->backlight.max)};
|
2016-11-12 03:40:14 -05:00
|
|
|
|
|
|
|
m_connection.change_output_property_checked(
|
2016-11-12 14:31:39 -05:00
|
|
|
m_output->output, m_output->backlight.atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, values);
|
2016-11-12 03:40:14 -05:00
|
|
|
} catch (const exception& err) {
|
|
|
|
m_log.err("%s: %s", name(), err.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|