2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/x11/connection.hpp"
|
|
|
|
#include "utils/memory.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
2016-10-19 08:46:43 -04:00
|
|
|
struct backlight_values {
|
|
|
|
uint32_t min = 0;
|
|
|
|
uint32_t max = 0;
|
|
|
|
uint32_t val = 0;
|
|
|
|
};
|
|
|
|
|
2016-10-11 06:38:15 -04:00
|
|
|
struct randr_output {
|
2016-10-19 08:46:43 -04:00
|
|
|
xcb_randr_output_t randr_output;
|
2016-06-14 23:32:35 -04:00
|
|
|
string name;
|
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2016-10-19 08:46:43 -04:00
|
|
|
backlight_values backlight;
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
2016-10-11 06:38:15 -04:00
|
|
|
using monitor_t = shared_ptr<randr_output>;
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace randr_util {
|
|
|
|
/**
|
|
|
|
* Define monitor
|
|
|
|
*/
|
2016-10-19 08:46:43 -04:00
|
|
|
inline monitor_t make_monitor(xcb_randr_output_t randr, string name, int w, int h, int x, int y) {
|
2016-10-11 06:38:15 -04:00
|
|
|
monitor_t mon{new monitor_t::element_type{}};
|
2016-10-19 08:46:43 -04:00
|
|
|
mon->randr_output = randr;
|
2016-06-14 23:32:35 -04:00
|
|
|
mon->name = name;
|
|
|
|
mon->x = x;
|
|
|
|
mon->y = y;
|
|
|
|
mon->h = h;
|
|
|
|
mon->w = w;
|
|
|
|
return mon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a list of all available randr outputs
|
|
|
|
*/
|
2016-10-11 06:38:15 -04:00
|
|
|
inline vector<monitor_t> get_monitors(connection& conn, xcb_window_t root) {
|
|
|
|
vector<monitor_t> monitors;
|
2016-06-14 23:32:35 -04:00
|
|
|
auto outputs = conn.get_screen_resources(root).outputs();
|
|
|
|
|
|
|
|
for (auto it = outputs.begin(); it != outputs.end(); it++) {
|
2016-10-18 11:31:56 -04:00
|
|
|
try {
|
|
|
|
auto info = conn.get_output_info(*it);
|
|
|
|
if (info->connection != XCB_RANDR_CONNECTION_CONNECTED)
|
|
|
|
continue;
|
|
|
|
auto crtc = conn.get_crtc_info(info->crtc);
|
|
|
|
string name{info.name().begin(), info.name().end()};
|
2016-10-19 08:46:43 -04:00
|
|
|
monitors.emplace_back(make_monitor(*it, name, crtc->width, crtc->height, crtc->x, crtc->y));
|
2016-10-18 11:31:56 -04:00
|
|
|
} catch (const xpp::randr::error::bad_crtc&) {
|
|
|
|
} catch (const xpp::randr::error::bad_output&) {
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the same sort algo as lemonbar, to match the defaults
|
2016-10-18 11:31:56 -04:00
|
|
|
sort(monitors.begin(), monitors.end(), [](monitor_t& m1, monitor_t& m2) -> bool {
|
|
|
|
if (m1->x < m2->x || m1->y + m1->h <= m2->y)
|
|
|
|
return 1;
|
|
|
|
if (m1->x > m2->x || m1->y + m1->h > m2->y)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
});
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
return monitors;
|
|
|
|
}
|
2016-10-19 08:46:43 -04:00
|
|
|
|
|
|
|
inline void get_backlight_range(connection& conn, const monitor_t& mon, backlight_values& dst) {
|
|
|
|
auto reply = conn.query_output_property(mon->randr_output, Backlight);
|
|
|
|
|
2016-10-25 10:39:50 -04:00
|
|
|
dst.min = 0;
|
|
|
|
dst.max = 0;
|
|
|
|
|
2016-10-19 08:46:43 -04:00
|
|
|
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();
|
|
|
|
|
2016-10-25 10:39:50 -04:00
|
|
|
dst.min = static_cast<uint32_t>(*range++);
|
|
|
|
dst.max = static_cast<uint32_t>(*range);
|
2016-10-19 08:46:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-10-25 10:39:50 -04:00
|
|
|
if (reply->num_items != 1 || reply->format != 32 || reply->type != XCB_ATOM_INTEGER)
|
2016-10-19 08:46:43 -04:00
|
|
|
reply = conn.get_output_property(mon->randr_output, BACKLIGHT, XCB_ATOM_NONE, 0, 4, 0, 0);
|
2016-10-25 10:39:50 -04:00
|
|
|
if (reply->num_items == 1 && reply->format == 32 && reply->type == XCB_ATOM_INTEGER)
|
|
|
|
dst.val = *reinterpret_cast<uint32_t*>(xcb_randr_get_output_property_data(reply.get().get()));
|
|
|
|
else
|
|
|
|
dst.val = 0;
|
2016-10-19 08:46:43 -04:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|