Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
detached_mode.h
1// SPDX-License-Identifier: GPL-3.0-only
2// Mode application for a verified private KWin connection. No host fallback.
3#pragma once
4
5#include <algorithm>
6#include <cmath>
7#include <nlohmann/json.hpp>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12namespace platf::kwin::detached {
13 struct mode_t {
14 int width;
15 int height;
16 int refresh_mhz;
17 };
18
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");
24 }
25 }
26
28 std::string id;
29 mode_t value;
30 };
31
32 struct output_t {
33 std::string name;
34 std::string id;
35 std::string current;
36 std::vector<advertised_mode_t> modes;
37 };
38
39 inline bool matches(mode_t actual, mode_t requested) {
40 // CVT pixel-clock rounding can produce 119.98 Hz for a 120 Hz request.
41 return actual.width == requested.width && actual.height == requested.height &&
42 std::abs(actual.refresh_mhz - requested.refresh_mhz) <= 250;
43 }
44
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");
50 }
51 const auto &entry = outputs.at(0);
52 output_t result;
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");
57 }
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 == '_';
65 })) {
66 throw std::runtime_error("Invalid private output mode identifier");
67 }
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");
71 }
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))};
74 validate(value);
75 result.modes.push_back({mode_id, value});
76 }
77 if (std::none_of(result.modes.begin(), result.modes.end(), [&](const auto &mode) {
78 return mode.id == result.current;
79 })) {
80 throw std::runtime_error("Private output has no advertised current mode");
81 }
82 return result;
83 }
84
85 template<class Run>
86 mode_t configure(mode_t requested, Run run) {
87 validate(requested);
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");
94 }
95 };
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);
99 });
100 };
101 auto find_mode = [&]() {
102 return std::find_if(output.modes.begin(), output.modes.end(), [&](const auto &mode) {
103 return matches(mode.value, requested);
104 });
105 };
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");
109 read_back();
110 }
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");
114 }
115 if (!current_matches()) {
116 run("output." + output.id + ".mode." + selected->id);
117 read_back();
118 }
119 // kscreen-doctor can return zero after an apply failure. Trust the mode
120 // read from KWin, never the command exit status alone.
121 if (!current_matches()) {
122 throw std::runtime_error("KWin did not activate the requested private output mode");
123 }
124 }
125 return std::find_if(output.modes.begin(), output.modes.end(), [&](const auto &mode) {
126 return mode.id == output.current;
127 })
128 ->value;
129 }
130} // namespace platf::kwin::detached
std::string name
Connector name, e.g. "HDMI-A-1".