1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Display profile provider — per-display refresh/HDR/color/scale.
//!
//! Builds on the existing [`crate::display::DisplayMonitor`]. Each connected
//! display is exposed as its own profile group with the actively-applied
//! mode + HDR/scale settings.
use super::{ProfileGroup, ProfileProvider, Setting, SettingRisk, SettingValue, Subsystem};
pub struct DisplayProfileProvider {
_private: (),
}
impl DisplayProfileProvider {
pub fn new() -> Self {
Self { _private: () }
}
}
impl Default for DisplayProfileProvider {
fn default() -> Self {
Self::new()
}
}
impl ProfileProvider for DisplayProfileProvider {
fn subsystem(&self) -> Subsystem {
Subsystem::Display
}
fn snapshot(&mut self) -> Result<Vec<ProfileGroup>, String> {
let (mut groups, edid_unread) = super::split_scan(super::edid::scan_edid_groups());
let monitor = match crate::display::DisplayMonitor::new() {
Ok(m) => m,
Err(e) => {
// The EDID scan found nothing and the display stack failed: no
// read happened at all, so the provider fails and names every
// reason. Otherwise keep the EDID groups and say which part is
// missing. This used to return them with no note.
if groups.is_empty() {
let mut reasons = edid_unread;
reasons.push(format!("display enumeration failed: {e}"));
return Err(reasons.join("; "));
}
let mut g = ProfileGroup::new(
Subsystem::Display,
"Display stack",
"DisplayMonitor",
"DisplayMonitor",
);
g.note(format!("not read: display enumeration failed: {e}"));
groups.push(g);
groups.extend(super::not_read_groups(
Subsystem::Display,
"EDID",
&edid_unread,
));
return Ok(groups);
}
};
for d in monitor.displays() {
let label = d.name.clone().unwrap_or_else(|| d.id.clone());
let mut g = ProfileGroup::new(
Subsystem::Display,
&label,
if d.is_primary == Some(true) {
"Primary display"
} else {
"Display"
},
"DisplayMonitor",
);
if let Some(mfg) = &d.manufacturer {
g.push(Setting::info(
"manufacturer",
"Manufacturer",
SettingValue::Text(mfg.clone()),
));
}
// Two of the three displays here report a connection the reader
// cannot classify, and `{:?}` printed the literal word "Unknown" as
// the value. The ontology already turns that into an absence with a
// reason --- `push_str_as` refuses the word --- and this surface was
// publishing it as though it were a kind of connector. Dropped
// instead, the way every other row here behaves when it has nothing.
let connection = format!("{:?}", d.connection);
if !connection.eq_ignore_ascii_case("unknown") {
g.push(Setting::info(
"connection",
"Connection",
SettingValue::Text(connection),
));
}
// "0x0" was published here as a resolution. A mode that was not
// read gets no row, the same as the aspect ratio below.
if let (Some(w), Some(h)) = (d.width, d.height) {
g.push(Setting::info(
"resolution",
"Resolution",
SettingValue::Text(format!("{w}x{h}")),
));
}
if let Some(ratio) = d.aspect_ratio() {
g.push(Setting::info(
"aspect_ratio",
"Aspect Ratio",
SettingValue::Text(ratio),
));
}
// The Linux and macOS readers do not parse a rate at all, so this
// row said "0 Hz" on both platforms for every display.
if let Some(hz) = d.refresh_rate {
g.push(
Setting::info(
"refresh_rate_hz",
"Refresh Rate",
SettingValue::Float(hz as f64),
)
.with_unit("Hz")
.with_risk(SettingRisk::Safe)
.with_source("DisplayMonitor"),
);
}
// Windows reports whether HDR is supported and on, but not which
// format is in use, so those are their own rows and the mode stays
// unreadable rather than being inferred from "on".
for (key, label, value) in [
("hdr_supported", "HDR Supported", d.hdr_supported),
("hdr_enabled", "HDR Enabled", d.hdr_enabled),
] {
g.push(
Setting::info(
key,
label,
match value {
Some(on) => SettingValue::Bool(on),
None => SettingValue::Unreadable(
"HDR state is not read on this platform".into(),
),
},
)
.with_risk(SettingRisk::Safe),
);
}
g.push(
Setting::info(
"hdr_mode",
"HDR Mode",
// `None` is "not read", shown as unreadable rather than as a mode.
match d.hdr {
Some(mode) => SettingValue::Text(format!("{mode:?}")),
None => SettingValue::Unreadable(
"the HDR format in use (HDR10, Dolby Vision) is not reported".into(),
),
},
)
.with_risk(SettingRisk::Safe),
);
if let Some(b) = d.brightness {
g.push(
Setting::info("brightness", "Brightness", SettingValue::Float(b as f64))
.with_unit("%")
.with_risk(SettingRisk::Safe),
);
}
if let Some(s) = d.scale_factor {
g.push(
Setting::info("scale_factor", "DPI Scale", SettingValue::Float(s))
.with_risk(SettingRisk::Safe),
);
}
if let Some(bpp) = d.bits_per_pixel {
g.push(Setting::info(
"bpp",
"Bits Per Pixel",
SettingValue::Uint(bpp as u64),
));
}
if let (Some(w), Some(h)) = (d.physical_width_mm, d.physical_height_mm) {
g.push(Setting::info(
"physical_mm",
"Physical Size",
SettingValue::Text(format!("{}×{} mm", w, h)),
));
}
// Only a read answer becomes a setting. An unread flag is left out
// rather than shown as "false".
if let Some(primary) = d.is_primary {
g.push(Setting::info(
"is_primary",
"Primary",
SettingValue::Bool(primary),
));
}
groups.push(g);
}
groups.extend(super::not_read_groups(
Subsystem::Display,
"EDID",
&edid_unread,
));
Ok(groups)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn smoke() {
let mut p = DisplayProfileProvider::new();
let _ = p.snapshot();
}
}