7#include <nlohmann/json.hpp>
12namespace platf::kwin::detached {
19 inline void validate(
mode_t requested) {
20 if (requested.width < 64 || requested.width > 16384 ||
21 requested.height < 64 || requested.height > 16384 ||
22 requested.refresh_mhz < 1000 || requested.refresh_mhz > 1000000) {
23 throw std::runtime_error(
"Invalid private output mode");
36 std::vector<advertised_mode_t> modes;
41 return actual.width == requested.width && actual.height == requested.height &&
42 std::abs(actual.refresh_mhz - requested.refresh_mhz) <= 250;
45 inline output_t parse_output(
const std::string &text) {
46 const auto data = nlohmann::json::parse(text);
47 const auto &outputs = data.at(
"outputs");
48 if (!outputs.is_array() || outputs.size() != 1) {
49 throw std::runtime_error(
"Private KWin must expose exactly one output");
51 const auto &entry = outputs.at(0);
53 result.
name = entry.at(
"name").get<std::string>();
54 const int id = entry.at(
"id").get<
int>();
55 if (
id <= 0 || !result.name.starts_with(
"Virtual-") || !entry.at(
"enabled").get<
bool>()) {
56 throw std::runtime_error(
"Refusing to configure an unexpected private output");
58 result.id = std::to_string(
id);
59 result.current = entry.at(
"currentModeId").get<std::string>();
60 for (
const auto &mode : entry.at(
"modes")) {
61 const auto mode_id = mode.at(
"id").get<std::string>();
62 if (mode_id.empty() || !std::all_of(mode_id.begin(), mode_id.end(), [](
unsigned char c) {
63 return (c >=
'0' && c <=
'9') || (c >=
'a' && c <=
'z') ||
64 (c >=
'A' && c <=
'Z') || c ==
'-' || c ==
'_';
66 throw std::runtime_error(
"Invalid private output mode identifier");
68 const double hz = mode.at(
"refreshRate").get<
double>();
69 if (!std::isfinite(hz) || hz < 1 || hz > 1000) {
70 throw std::runtime_error(
"Invalid private output refresh rate");
72 const auto &size = mode.at(
"size");
73 mode_t value {size.at(
"width").get<
int>(), size.at(
"height").get<
int>(),
static_cast<int>(std::lround(hz * 1000))};
75 result.modes.push_back({mode_id, value});
77 if (std::none_of(result.modes.begin(), result.modes.end(), [&](
const auto &mode) {
78 return mode.id == result.current;
80 throw std::runtime_error(
"Private output has no advertised current mode");
88 auto output = parse_output(run(
"-j"));
89 const auto identity = output;
90 auto read_back = [&]() {
91 output = parse_output(run(
"-j"));
92 if (output.name != identity.name || output.id != identity.id) {
93 throw std::runtime_error(
"Private output changed during mode application");
96 auto current_matches = [&]() {
97 return std::any_of(output.modes.begin(), output.modes.end(), [&](
const auto &mode) {
98 return mode.id == output.current && matches(mode.value, requested);
101 auto find_mode = [&]() {
102 return std::find_if(output.modes.begin(), output.modes.end(), [&](
const auto &mode) {
103 return matches(mode.value, requested);
106 if (!current_matches()) {
107 if (find_mode() == output.modes.end()) {
108 run(
"output." + output.id +
".addCustomMode." + std::to_string(requested.width) +
"." + std::to_string(requested.height) +
"." + std::to_string(requested.refresh_mhz) +
".full");
111 const auto selected = find_mode();
112 if (selected == output.modes.end()) {
113 throw std::runtime_error(
"KWin did not advertise the requested custom mode; this KWin/KScreen version may not support it");
115 if (!current_matches()) {
116 run(
"output." + output.id +
".mode." + selected->id);
121 if (!current_matches()) {
122 throw std::runtime_error(
"KWin did not activate the requested private output mode");
125 return std::find_if(output.modes.begin(), output.modes.end(), [&](
const auto &mode) {
126 return mode.id == output.current;
std::string name
Connector name, e.g. "HDMI-A-1".