display: KDE Wayland plan is one kscreen-doctor call, picks the mode by id, skips enable for an output that is on

On Plasma 6.8.2 two modes of one monitor share the name 1920x1080@120 (119.88 and 119.98 Hz), so the old
name-based plan could restore the wrong one. Test data is that monitor's kscreen-doctor -j.
This commit is contained in:
alex committed 2026-11-24 16:47:19 +00:00
1 parent b50f87b13c
commit 05df3abcfa
1 file changed
+62 -16
+62 -16
View File
@@ -42,14 +42,13 @@ pub fn plan(session: Session, desktop: Desktop, p: &Profile) -> Vec<Cmd> {
let mode = match (p.width, p.height) { (Some(w), Some(h)) => Some((w, h)), _ => None };
match (session, desktop) {
(Session::Wayland, Desktop::Kde) => {
let mut arg = format!("output.{mon}.enable");
let mut v = vec![c(&["kscreen-doctor", &arg])];
// One call: kscreen-doctor applies all of its arguments as one configuration.
let mut v = vec!["kscreen-doctor".to_string(), format!("output.{mon}.enable")];
if let Some((w, h)) = mode {
let m = match p.refresh_hz { Some(r) => format!("{w}x{h}@{}", r.round() as u32), None => format!("{w}x{h}") };
arg = format!("output.{mon}.mode.{m}");
v.push(c(&["kscreen-doctor", &arg]));
v.push(format!("output.{mon}.mode.{m}"));
}
v
vec![v]
}
(Session::Wayland, Desktop::Wlroots) => match mode {
Some((w, h)) => {
@@ -90,8 +89,9 @@ pub struct Output {
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Mode {
pub width: u32, pub height: u32, pub rates: Vec<f32>,
/// (rate, mode name) for xrandr modes whose name is not just "WxH" (custom modes like "1920x1080_120.00"):
/// xrandr picks a mode by its name, so these need it.
/// (rate, what the tool picks this mode by) when "WxH" is not enough. xrandr: the names of custom modes
/// ("1920x1080_120.00"). kscreen-doctor: the mode id, as its names ("1920x1080@120") can be shared by two
/// modes (119.88 and 119.98 Hz on one real monitor).
pub named: Vec<(f32, String)>,
}
@@ -154,6 +154,7 @@ pub fn parse_kscreen_json(text: &str) -> Vec<Output> {
let r = m["refreshRate"].as_f64().map(|r| (r * 100.0).round() as f32 / 100.0);
push_mode(&mut x.modes, w, h, r);
let id = match &m["id"] { serde_json::Value::String(s) => s.clone(), n => n.to_string() };
if let (Some(r), Some(mm)) = (r, x.modes.iter_mut().find(|mm| mm.width == w && mm.height == h)) { mm.named.push((r, id.clone())); }
if enabled && id == cur_id { x.current = Some((w, h, r.unwrap_or(0.0))); }
}
sort_modes(&mut x.modes);
@@ -209,16 +210,36 @@ pub fn restore_profile(outs: &[Output], p: &Profile) -> Option<Profile> {
Some(Profile { monitor: Some(mon.into()), width: Some(w), height: Some(h), refresh_hz: if r > 0.0 { Some(r) } else { None }, ..Default::default() })
}
/// `plan()`, with the outputs this machine has: on X11 a custom mode is asked for by its name, as xrandr needs.
/// `plan()`, with the outputs this machine has: on X11 a custom mode is asked for by its name, as xrandr needs; on KDE
/// Wayland the mode by its id (the closest rate within 0.5 Hz; the highest rate when the profile names none) and no
/// `enable` for an output that is already on.
pub fn plan_for(session: Session, desktop: Desktop, p: &Profile, outs: &[Output]) -> Vec<Cmd> {
let mut cmds = plan(session, desktop, p);
if session != Session::X11 { return cmds; }
let (Some(mon), Some(w), Some(h), Some(r)) = (p.monitor.as_deref(), p.width, p.height, p.refresh_hz) else { return cmds };
let name = outs.iter().find(|o| o.name == mon)
.and_then(|o| o.modes.iter().find(|m| m.width == w && m.height == h))
.and_then(|m| m.named.iter().find(|(x, _)| (x - r).abs() < 0.01)).map(|(_, n)| n.clone());
if let (Some(name), Some(cmd)) = (name, cmds.first_mut()) {
if let Some(i) = cmd.iter().position(|a| a == "--mode") { cmd[i + 1] = name; }
let Some(mon) = p.monitor.as_deref() else { return cmds };
let out = outs.iter().find(|o| o.name == mon);
let mode = |w: u32, h: u32| out.and_then(|o| o.modes.iter().find(|m| m.width == w && m.height == h));
match (session, desktop) {
(Session::X11, _) => {
let (Some(w), Some(h), Some(r)) = (p.width, p.height, p.refresh_hz) else { return cmds };
let name = mode(w, h).and_then(|m| m.named.iter().find(|(x, _)| (x - r).abs() < 0.01)).map(|(_, n)| n.clone());
if let (Some(name), Some(cmd)) = (name, cmds.first_mut()) {
if let Some(i) = cmd.iter().position(|a| a == "--mode") { cmd[i + 1] = name; }
}
}
(Session::Wayland, Desktop::Kde) => {
let Some(cmd) = cmds.first_mut() else { return cmds };
if let (Some(w), Some(h)) = (p.width, p.height) {
let id = mode(w, h).and_then(|m| match p.refresh_hz {
Some(r) => m.named.iter().filter(|(x, _)| (x - r).abs() < 0.5)
.min_by(|a, b| (a.0 - r).abs().total_cmp(&(b.0 - r).abs())).map(|(_, id)| id.clone()),
None => m.named.iter().max_by(|a, b| a.0.total_cmp(&b.0)).map(|(_, id)| id.clone()),
});
if let (Some(id), Some(last)) = (id, cmd.last_mut()) { *last = format!("output.{mon}.mode.{id}"); }
}
if out.is_some_and(|o| o.current.is_some()) { cmd.retain(|a| a != &format!("output.{mon}.enable")); }
if cmd.len() == 1 { cmds.clear(); }
}
_ => {}
}
cmds
}
@@ -243,7 +264,7 @@ mod tests {
}
#[test] fn kde_wayland_plan() {
let p = plan(Session::Wayland, detect_desktop_from("KDE"), &prof());
assert_eq!(p[1], vec!["kscreen-doctor", "output.HDMI-A-1.mode.3840x2160@60"]);
assert_eq!(p, vec![vec!["kscreen-doctor", "output.HDMI-A-1.enable", "output.HDMI-A-1.mode.3840x2160@60"]]);
}
#[test] fn x11_plan() {
let p = plan(Session::X11, Desktop::Other, &prof());
@@ -303,4 +324,29 @@ HDMI-1 disconnected (normal left inverted right x axis y axis)
assert_eq!(restore_profile(&o, &same), None); // nothing changes, nothing to undo
assert_eq!(restore_profile(&o, &Profile { monitor: Some("Virtual-1".into()), ..Default::default() }), None);
}
// From the owner's desktop (Plasma 6.8.2, `kscreen-doctor -j`, trimmed): two modes share the name "1920x1080@120".
const KSCREEN_REAL: &str = r#"{"outputs":[{"name":"HDMI-A-1","connected":true,"enabled":true,"currentModeId":"49","modes":[
{"id":"46","name":"1920x1080@144","refreshRate":143.9810028076172,"size":{"height":1080,"width":1920}},
{"id":"48","name":"1920x1080@120","refreshRate":119.87899780273438,"size":{"height":1080,"width":1920}},
{"id":"49","name":"1920x1080@120","refreshRate":119.98200225830078,"size":{"height":1080,"width":1920}},
{"id":"58","name":"1280x720@60","refreshRate":60,"size":{"height":720,"width":1280}}]},
{"name":"DP-3","connected":true,"enabled":false,"currentModeId":"","modes":[
{"id":"5","name":"1920x1080@60","refreshRate":60,"size":{"height":1080,"width":1920}}]}]}"#;
#[test] fn kde_plan_uses_mode_ids_and_restores_the_exact_mode() {
let o = parse_kscreen_json(KSCREEN_REAL);
assert_eq!(o[0].current, Some((1920, 1080, 119.98)));
let p = Profile { monitor: Some("HDMI-A-1".into()), width: Some(1280), height: Some(720), refresh_hz: Some(60.0), ..Default::default() };
// the output is on: no `enable`, one call, the mode by id
assert_eq!(plan_for(Session::Wayland, Desktop::Kde, &p, &o), vec![vec!["kscreen-doctor", "output.HDMI-A-1.mode.58"]]);
// back to 119.98 Hz, not the 119.88 Hz mode that has the same name
let back = restore_profile(&o, &p).unwrap();
assert_eq!(plan_for(Session::Wayland, Desktop::Kde, &back, &o), vec![vec!["kscreen-doctor", "output.HDMI-A-1.mode.49"]]);
let fast = Profile { width: Some(1920), height: Some(1080), refresh_hz: None, ..p.clone() };
assert_eq!(plan_for(Session::Wayland, Desktop::Kde, &fast, &o), vec![vec!["kscreen-doctor", "output.HDMI-A-1.mode.46"]]);
// only a monitor, already on: nothing to run
assert!(plan_for(Session::Wayland, Desktop::Kde, &Profile { monitor: Some("HDMI-A-1".into()), ..Default::default() }, &o).is_empty());
// an output that is off is turned on in the same call
let off = Profile { monitor: Some("DP-3".into()), width: Some(1920), height: Some(1080), refresh_hz: Some(60.0), ..Default::default() };
assert_eq!(plan_for(Session::Wayland, Desktop::Kde, &off, &o), vec![vec!["kscreen-doctor", "output.DP-3.enable", "output.DP-3.mode.5"]]);
}
}