2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/x11/connection.hpp"
|
|
|
|
#include "utils/memory.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
2016-10-11 10:38:15 +00:00
|
|
|
struct randr_output {
|
2016-06-15 03:32:35 +00:00
|
|
|
string name;
|
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
};
|
|
|
|
|
2016-10-11 10:38:15 +00:00
|
|
|
using monitor_t = shared_ptr<randr_output>;
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
namespace randr_util {
|
|
|
|
/**
|
|
|
|
* Define monitor
|
|
|
|
*/
|
2016-10-11 10:38:15 +00:00
|
|
|
inline monitor_t make_monitor(string name, int w, int h, int x, int y) {
|
|
|
|
monitor_t mon{new monitor_t::element_type{}};
|
2016-06-15 03:32:35 +00: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 10:38:15 +00:00
|
|
|
inline vector<monitor_t> get_monitors(connection& conn, xcb_window_t root) {
|
|
|
|
vector<monitor_t> monitors;
|
2016-06-15 03:32:35 +00:00
|
|
|
auto outputs = conn.get_screen_resources(root).outputs();
|
|
|
|
|
|
|
|
for (auto it = outputs.begin(); it != outputs.end(); it++) {
|
2016-10-18 15:31:56 +00: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()};
|
|
|
|
monitors.emplace_back(make_monitor(name, crtc->width, crtc->height, crtc->x, crtc->y));
|
|
|
|
} catch (const xpp::randr::error::bad_crtc&) {
|
|
|
|
} catch (const xpp::randr::error::bad_output&) {
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the same sort algo as lemonbar, to match the defaults
|
2016-10-18 15:31:56 +00: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-15 03:32:35 +00:00
|
|
|
|
|
|
|
return monitors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|