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

200 lines
6.1 KiB
C++
Raw Normal View History

2016-11-02 15:22:45 -04:00
#include "modules/xbacklight.hpp"
#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"
#include "x11/graphics.hpp"
2016-11-26 00:13:20 -05:00
#include "x11/xutils.hpp"
2016-11-02 15:22:45 -04:00
2016-11-20 17:04:31 -05:00
#include "modules/meta/base.inl"
#include "modules/meta/static_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<xbacklight_module>;
template class static_module<xbacklight_module>;
/**
* Construct module
*/
2016-11-25 07:55:15 -05:00
xbacklight_module::xbacklight_module(const bar_settings& bar, const logger& logger, const config& config, string name)
2016-11-20 17:04:31 -05:00
: static_module<xbacklight_module>(bar, logger, config, name)
, m_connection(configure_connection().create<connection&>()) {}
2016-11-02 15:22:45 -04:00
/**
* Bootstrap the module by grabbing all required components
*/
void xbacklight_module::setup() {
auto output = m_conf.get<string>(name(), "output", m_bar.monitor->name);
auto strict = m_conf.get<bool>(name(), "monitor-strict", false);
2016-11-02 15:22:45 -04:00
// Grab a list of all outputs and try to find the one defined in the config
for (auto&& mon : randr_util::get_monitors(m_connection, m_connection.root(), strict)) {
if (mon->match(output, strict)) {
2016-11-02 15:22:45 -04:00
m_output.swap(mon);
break;
}
}
// 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...");
}
// Get flag to check if we should add scroll handlers for changing value
GET_CONFIG_VALUE(name(), m_scroll, "enable-scroll");
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);
} catch (const exception& err) {
2016-11-02 15:22:45 -04:00
throw module_error("No backlight data found for \"" + output + "\", stopping module...");
}
// Create window that will proxy all RandR notify events
if (!graphics_util::create_window(m_connection, &m_proxy, -1, -1, 1, 1)) {
throw module_error("Failed to create event proxy");
}
2016-11-02 15:22:45 -04:00
2016-11-26 00:13:20 -05:00
// Get the throttle time
m_randrnotify.offset = xutils::event_timer_ms(m_conf, xcb_randr_notify_event_t{});
2016-11-26 00:13:20 -05:00
// Connect with the event registry and make sure we get
// notified when a RandR output property gets modified
2016-12-03 07:00:40 -05:00
m_connection.attach_sink(this, SINK_PRIORITY_MODULE);
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)) {
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
// Trigger the initial draw event
update();
}
/**
* Disconnect from the event registry
*/
void xbacklight_module::teardown() {
2016-12-03 07:00:40 -05:00
m_connection.detach_sink(this, SINK_PRIORITY_MODULE);
}
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) {
return;
2016-11-25 07:55:15 -05:00
} else if (evt->u.op.window != m_proxy) {
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) {
return;
} else if (m_randrnotify.deny(evt->u.op.timestamp)) {
2016-11-26 00:13:20 -05:00
return m_log.trace_x("%s: Ignoring randr notify (throttled)...", name());
} 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() {
// Query for the new backlight value
auto& bl = m_output->backlight;
randr_util::get_backlight_value(m_connection, m_output, bl);
m_percentage = math_util::percentage(bl.val, bl.min, bl.max);
// Update label tokens
if (m_label) {
m_label->reset_tokens();
m_label->replace_token("%percentage%", to_string(m_percentage) + "%");
}
// Emit a broadcast notification so that
// the new data will be drawn to the bar
broadcast();
}
/**
* Generate the module output
*/
string xbacklight_module::get_output() {
2016-11-25 07:55:15 -05:00
if (m_scroll && m_percentage < 100) {
2016-11-24 22:10:26 -05:00
m_builder->cmd(mousebtn::SCROLL_UP, EVENT_SCROLLUP);
2016-11-25 07:55:15 -05:00
}
if (m_scroll && m_percentage > 0) {
2016-11-24 22:10:26 -05:00
m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_SCROLLDOWN);
2016-11-25 07:55:15 -05:00
}
2016-11-24 22:10:26 -05:00
m_builder->append(static_module::get_output());
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;
}
/**
* Process scroll events by changing backlight value
*/
bool xbacklight_module::handle_event(string cmd) {
int value_mod = 0;
if (cmd == EVENT_SCROLLUP) {
value_mod = 10;
m_log.info("%s: Increasing value by %i%", name(), value_mod);
} else if (cmd == EVENT_SCROLLDOWN) {
value_mod = -10;
m_log.info("%s: Decreasing value by %i%", name(), -value_mod);
} else {
return false;
}
try {
const int new_perc = math_util::cap(m_percentage + value_mod, 0, 100);
const int new_value = math_util::percentage_to_value<int>(new_perc, m_output->backlight.max);
const int values[1]{new_value};
m_connection.change_output_property_checked(
m_output->output, m_output->backlight.atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, values);
} 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