feat: New module xbacklight

Better implementation for showing backlight information.
This one is using the XRandR extension instead of
reading from the raw file handlers.
This commit is contained in:
Michael Carlberg 2016-10-19 14:46:43 +02:00
parent 3a5ee90bb5
commit 56b4657c3f
6 changed files with 250 additions and 33 deletions

View File

@ -19,6 +19,7 @@
#include "utils/throttle.hpp" #include "utils/throttle.hpp"
#include "modules/backlight.hpp" #include "modules/backlight.hpp"
#include "modules/xbacklight.hpp"
#include "modules/battery.hpp" #include "modules/battery.hpp"
#include "modules/bspwm.hpp" #include "modules/bspwm.hpp"
#include "modules/counter.hpp" #include "modules/counter.hpp"
@ -237,10 +238,14 @@ class controller {
} }
} }
m_connection.flush();
m_log.trace("controller: Listen for X events"); m_log.trace("controller: Listen for X events");
while (m_running) { while (m_running) {
m_connection.flush(); auto evt = m_connection.wait_for_event();
m_connection.dispatch_event(m_connection.wait_for_event());
if (evt != nullptr)
m_connection.dispatch_event(evt);
} }
}); });
@ -375,6 +380,8 @@ class controller {
modules.emplace_back(new counter_module(bar, m_log, m_conf, module_name)); modules.emplace_back(new counter_module(bar, m_log, m_conf, module_name));
else if (type == "internal/backlight") else if (type == "internal/backlight")
modules.emplace_back(new backlight_module(bar, m_log, m_conf, module_name)); modules.emplace_back(new backlight_module(bar, m_log, m_conf, module_name));
else if (type == "internal/xbacklight")
modules.emplace_back(new xbacklight_module(bar, m_log, m_conf, module_name));
else if (type == "internal/battery") else if (type == "internal/battery")
modules.emplace_back(new battery_module(bar, m_log, m_conf, module_name)); modules.emplace_back(new battery_module(bar, m_log, m_conf, module_name));
else if (type == "internal/bspwm") else if (type == "internal/bspwm")

View File

@ -31,9 +31,11 @@ static xcb_atom_t MANAGER;
static xcb_atom_t WM_STATE; static xcb_atom_t WM_STATE;
static xcb_atom_t _NET_SYSTEM_TRAY_ORIENTATION; static xcb_atom_t _NET_SYSTEM_TRAY_ORIENTATION;
static xcb_atom_t WM_TAKE_FOCUS; static xcb_atom_t WM_TAKE_FOCUS;
static xcb_atom_t Backlight;
static xcb_atom_t BACKLIGHT;
// clang-format off // clang-format off
static cached_atom ATOMS[22] = { static cached_atom ATOMS[24] = {
{"_NET_WM_NAME", sizeof("_NET_WM_NAME") - 1, &_NET_WM_NAME}, {"_NET_WM_NAME", sizeof("_NET_WM_NAME") - 1, &_NET_WM_NAME},
{"_NET_WM_DESKTOP", sizeof("_NET_WM_DESKTOP") - 1, &_NET_WM_DESKTOP}, {"_NET_WM_DESKTOP", sizeof("_NET_WM_DESKTOP") - 1, &_NET_WM_DESKTOP},
{"_NET_WM_WINDOW_TYPE", sizeof("_NET_WM_WINDOW_TYPE") - 1, &_NET_WM_WINDOW_TYPE}, {"_NET_WM_WINDOW_TYPE", sizeof("_NET_WM_WINDOW_TYPE") - 1, &_NET_WM_WINDOW_TYPE},
@ -56,5 +58,7 @@ static cached_atom ATOMS[22] = {
{"WM_STATE", sizeof("WM_STATE") - 1, &WM_STATE}, {"WM_STATE", sizeof("WM_STATE") - 1, &WM_STATE},
{"_NET_SYSTEM_TRAY_ORIENTATION", sizeof("_NET_SYSTEM_TRAY_ORIENTATION") - 1, &_NET_SYSTEM_TRAY_ORIENTATION}, {"_NET_SYSTEM_TRAY_ORIENTATION", sizeof("_NET_SYSTEM_TRAY_ORIENTATION") - 1, &_NET_SYSTEM_TRAY_ORIENTATION},
{"WM_TAKE_FOCUS", sizeof("WM_TAKE_FOCUS") - 1, &WM_TAKE_FOCUS}, {"WM_TAKE_FOCUS", sizeof("WM_TAKE_FOCUS") - 1, &WM_TAKE_FOCUS},
{"Backlight", sizeof("Backlight") - 1, &Backlight},
{"BACKLIGHT", sizeof("BACKLIGHT") - 1, &BACKLIGHT},
}; };
// clang-format on // clang-format on

View File

@ -6,12 +6,20 @@
LEMONBUDDY_NS LEMONBUDDY_NS
struct backlight_values {
uint32_t min = 0;
uint32_t max = 0;
uint32_t val = 0;
};
struct randr_output { struct randr_output {
xcb_randr_output_t randr_output;
string name; string name;
int w = 0; int w = 0;
int h = 0; int h = 0;
int x = 0; int x = 0;
int y = 0; int y = 0;
backlight_values backlight;
}; };
using monitor_t = shared_ptr<randr_output>; using monitor_t = shared_ptr<randr_output>;
@ -20,8 +28,9 @@ namespace randr_util {
/** /**
* Define monitor * Define monitor
*/ */
inline monitor_t make_monitor(string name, int w, int h, int x, int y) { inline monitor_t make_monitor(xcb_randr_output_t randr, string name, int w, int h, int x, int y) {
monitor_t mon{new monitor_t::element_type{}}; monitor_t mon{new monitor_t::element_type{}};
mon->randr_output = randr;
mon->name = name; mon->name = name;
mon->x = x; mon->x = x;
mon->y = y; mon->y = y;
@ -44,7 +53,7 @@ namespace randr_util {
continue; continue;
auto crtc = conn.get_crtc_info(info->crtc); auto crtc = conn.get_crtc_info(info->crtc);
string name{info.name().begin(), info.name().end()}; string name{info.name().begin(), info.name().end()};
monitors.emplace_back(make_monitor(name, crtc->width, crtc->height, crtc->x, crtc->y)); monitors.emplace_back(make_monitor(*it, name, crtc->width, crtc->height, crtc->x, crtc->y));
} catch (const xpp::randr::error::bad_crtc&) { } catch (const xpp::randr::error::bad_crtc&) {
} catch (const xpp::randr::error::bad_output&) { } catch (const xpp::randr::error::bad_output&) {
} }
@ -61,6 +70,33 @@ namespace randr_util {
return monitors; return monitors;
} }
inline void get_backlight_range(connection& conn, const monitor_t& mon, backlight_values& dst) {
auto reply = conn.query_output_property(mon->randr_output, Backlight);
if (!reply->range || reply->length != 2)
reply = conn.query_output_property(mon->randr_output, BACKLIGHT);
if (!reply->range || reply->length != 2)
return;
auto range = reply.valid_values().begin();
dst.min = *range++;
dst.max = *range;
}
inline void get_backlight_value(connection& conn, const monitor_t& mon, backlight_values& dst) {
auto reply = conn.get_output_property(mon->randr_output, Backlight, XCB_ATOM_NONE, 0, 4, 0, 0);
if (!reply->num_items)
reply = conn.get_output_property(mon->randr_output, BACKLIGHT, XCB_ATOM_NONE, 0, 4, 0, 0);
if(!reply->num_items)
return;
dst.val = *reply.data().begin();
}
} }
LEMONBUDDY_NS_END LEMONBUDDY_NS_END

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "config.hpp" #include "config.hpp"
#include "components/config.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp" #include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp" #include "drawtypes/ramp.hpp"
@ -9,6 +10,21 @@
LEMONBUDDY_NS LEMONBUDDY_NS
namespace modules { 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;
};
class backlight_module : public inotify_module<backlight_module> { class backlight_module : public inotify_module<backlight_module> {
public: public:
using inotify_module::inotify_module; using inotify_module::inotify_module;
@ -28,29 +44,22 @@ namespace modules {
m_ramp = get_config_ramp(m_conf, name(), TAG_RAMP); m_ramp = get_config_ramp(m_conf, name(), TAG_RAMP);
// Build path to the file where the current/maximum brightness value is located // Build path to the file where the current/maximum brightness value is located
m_path_val = string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card); m_val.filepath(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
m_path_max = string_util::replace(PATH_BACKLIGHT_MAX, "%card%", card); m_max.filepath(string_util::replace(PATH_BACKLIGHT_MAX, "%card%", card));
if (!file_util::exists(m_path_val))
throw module_error("backlight_module: The file '" + m_path_val + "' does not exist");
if (!file_util::exists(m_path_max))
throw module_error("backlight_module: The file '" + m_path_max + "' does not exist");
// Add inotify watch // Add inotify watch
watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card)); watch(string_util::replace(PATH_BACKLIGHT_VAL, "%card%", card));
} }
void idle() {
sleep(75ms);
}
bool on_event(inotify_event* event) { bool on_event(inotify_event* event) {
if (event != nullptr) if (event != nullptr)
m_log.trace("%s: %s", name(), event->filename); m_log.trace("%s: %s", name(), event->filename);
auto val = file_util::get_contents(m_path_val); m_percentage = static_cast<int>(m_val.read() / m_max.read() * 100.0f + 0.5f);
m_val = std::stoull(val.c_str(), 0, 10);
auto max = file_util::get_contents(m_path_max);
m_max = std::stoull(max.c_str(), 0, 10);
m_percentage = static_cast<int>(float(m_val) / float(m_max) * 100.0f + 0.5f);
if (m_label) { if (m_label) {
m_label->reset_tokens(); m_label->reset_tokens();
@ -72,10 +81,6 @@ namespace modules {
return true; return true;
} }
void idle() {
this_thread::sleep_for(75ms);
}
private: private:
static constexpr auto TAG_LABEL = "<label>"; static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_BAR = "<bar>"; static constexpr auto TAG_BAR = "<bar>";
@ -85,12 +90,10 @@ namespace modules {
label_t m_label; label_t m_label;
progressbar_t m_progressbar; progressbar_t m_progressbar;
string m_path_val; brightness_handle m_val;
string m_path_max; brightness_handle m_max;
float m_val = 0;
float m_max = 0;
int m_percentage; int m_percentage = 0;
}; };
} }

View File

@ -0,0 +1,159 @@
#pragma once
#include "components/config.hpp"
#include "components/x11/connection.hpp"
#include "components/x11/randr.hpp"
#include "components/x11/xutils.hpp"
#include "config.hpp"
#include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp"
#include "modules/meta.hpp"
#include "utils/throttle.hpp"
LEMONBUDDY_NS
namespace modules {
/**
* Backlight module built using the RandR X extension.
*
* This is built as a replacement for the old backlight
* module that was set up using with inotify watches listening
* for changes to the raw file handle.
*
* This module is alot faster, it's more responsive and it will
* be dormant until new values are reported. Inotify watches
* are a bit random when it comes to proc-/sysfs.
*
* TODO: Implement backlight configuring using scroll events
*/
class xbacklight_module : public static_module<xbacklight_module>,
public xpp::event::sink<evt::randr_notify> {
public:
using static_module::static_module;
/**
* Bootstrap the module by grabbing all required components
*/
void setup() {
auto output = m_conf.get<string>(name(), "output", m_bar.monitor->name);
// 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())) {
if (mon->name == output) {
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...");
}
// 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 std::exception& err) {
throw module_error("No backlight data found for \"" + output + "\", stopping module...");
}
// Connect with the event registry and tell randr that we
// want to get notified when an output property gets modified
m_connection.attach_sink(this, 1);
m_connection.select_input_checked(
m_connection.screen()->root, XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
// Create a throttle so that we limit the amount of events
// to handle since randr can burst out quite a few
// We will allow 1 event per 60 ms. The updates still look smooth
// using this setting which is important.
m_throttler = throttle_util::make_throttler(1, 60ms);
// Add formats and elements
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP});
if (m_formatter->has(TAG_LABEL))
m_label = get_optional_config_label(m_conf, name(), TAG_LABEL, "%percentage%");
if (m_formatter->has(TAG_BAR))
m_progressbar = get_config_bar(m_bar, m_conf, name(), TAG_BAR);
if (m_formatter->has(TAG_RAMP))
m_ramp = get_config_ramp(m_conf, name(), TAG_RAMP);
// Trigger the initial draw event
update();
}
/**
* Handler for XCB_RANDR_NOTIFY events
*/
void handle(const evt::randr_notify& evt) {
if (evt->subCode != XCB_RANDR_NOTIFY_OUTPUT_PROPERTY)
return;
else if (evt->u.op.output != m_output->randr_output)
return;
else if (evt->u.op.atom == Backlight)
update();
else if (evt->u.op.atom == BACKLIGHT)
update();
}
/**
* Query the RandR extension for the new values
*/
void update() {
// Test if we are allowed to handle the event
if (!m_throttler->passthrough(throttle_util::strategy::try_once_or_leave_yolo{}))
return;
// 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<float>(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();
}
/**
* Output content as defined in the config
*/
bool build(builder* builder, string tag) const {
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);
else
return false;
return true;
}
private:
static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_BAR = "<bar>";
static constexpr auto TAG_RAMP = "<ramp>";
throttle_util::throttle_t m_throttler;
connection& m_connection{connection::configure().create<connection&>()};
monitor_t m_output;
ramp_t m_ramp;
label_t m_label;
progressbar_t m_progressbar;
int m_percentage = 0;
};
}
LEMONBUDDY_NS_END

View File

@ -6,18 +6,26 @@
LEMONBUDDY_NS LEMONBUDDY_NS
namespace math_util namespace math_util {
{
/** /**
* Limit value T by min and max bounds * Limit value T by min and max bounds
*/ */
template <typename T> template <typename T>
T cap(T value, T min_value, T max_value) T cap(T value, T min_value, T max_value) {
{
value = std::min<T>(value, max_value); value = std::min<T>(value, max_value);
value = std::max<T>(value, min_value); value = std::max<T>(value, min_value);
return value; return value;
} }
/**
* Calculate the percentage for a value
* within given range
*/
template <typename T>
int percentage(T value, T min_value, T max_value) {
T percentage = ((value - min_value) / (max_value - min_value)) * 100.0f + 0.5f;
return cap<T>(std::ceil(percentage), 0, 100);
}
} }
LEMONBUDDY_NS_END LEMONBUDDY_NS_END