Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
monitor_restore.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <map>
9#include <optional>
10#include <string>
11#include <vector>
12
13namespace VDISPLAY {
16 std::string name;
17 bool connected {false};
18 bool enabled {false};
19 int priority {0};
20 int x {0};
21 int y {0};
22 int width {0};
23 int height {0};
24 };
25
26 namespace monitor_restore {
27 using targets_t = std::map<std::string, int>;
28 using snapshot_t = std::optional<std::vector<kscreen_output_t>>;
29
37 template<class Query, class Apply>
38 bool restore(const targets_t &saved, Query query, Apply apply) {
39 if (saved.empty()) {
40 return true;
41 }
42 const auto before = query();
43 if (!before) {
44 return false;
45 }
46 targets_t connected;
47 bool already_restored = true;
48 for (const auto &output : *before) {
49 const auto target = saved.find(output.name);
50 if (target != saved.end() && output.connected) {
51 connected.emplace(*target);
52 already_restored &= output.enabled &&
53 (target->second <= 0 || output.priority == target->second);
54 }
55 }
56 if (connected.empty() || already_restored) {
57 return true;
58 }
59
60 // Always verify, even if the command failed after partially applying.
61 apply(connected);
62 const auto after = query();
63 if (!after) {
64 return false;
65 }
66 for (const auto &[name, priority] : connected) {
67 const auto output = std::find_if(after->begin(), after->end(), [&name](const auto &entry) {
68 return entry.name == name;
69 });
70 if (output != after->end() && output->connected && !output->enabled) {
71 return false;
72 }
73 }
74 return true;
75 }
76 } // namespace monitor_restore
77} // namespace VDISPLAY
bool restore(const targets_t &saved, Query query, Apply apply)
One output as kscreen-doctor -j reports it.
std::string name
Connector name, e.g. "HDMI-A-1".
bool connected
The connector exists in this session.
bool enabled
KWin is composing on it.
int height
Logical height.
int priority
KScreen priority; 1 marks the primary output.