From 3d16bb6cf8b08d55bc517d96d0c5396580a3d0b6 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 07:34:58 +0000 Subject: [PATCH] display: parse custom xrandr modes (1920x1080_120.00) and ask xrandr for them by name Found on the KDE neon test VM, whose current mode is a custom one: the parser skipped it, so the current mode was unknown and the restore after a game would have picked the 60 Hz mode of the same size. --- crates/summit-cli/src/main.rs | 9 +++---- crates/summit-core/src/display.rs | 39 +++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/summit-cli/src/main.rs b/crates/summit-cli/src/main.rs index b5b4616..a43c409 100644 --- a/crates/summit-cli/src/main.rs +++ b/crates/summit-cli/src/main.rs @@ -201,7 +201,7 @@ fn main() -> Result<()> { Err(e) => println!("outputs: {e}"), } if let Some(id) = id { - let cmds = display::plan(s, d, &profile::load(&id)?); + let cmds = display::plan_for(s, d, &profile::load(&id)?, &display::outputs(s, d).unwrap_or_default()); if cmds.is_empty() { println!("no display commands for this profile on this session (nothing set, or unsupported compositor)"); } for c in cmds { println!("would run: {}", c.join(" ")); } } @@ -215,13 +215,14 @@ fn main() -> Result<()> { // Found before the game starts, so a missing engine never leaves a game running without it. let eng = if enhance { Some(engine::find_engine().ok_or_else(|| anyhow::anyhow!("engine not found: set SUMMIT_ENGINE to the engine/ folder"))?) } else { None }; let (s, d) = (display::detect_session(), display::detect_desktop()); - let plan = display::plan(s, d, &p); + let outs = if p.monitor.is_some() { display::outputs(s, d) } else { Ok(vec![]) }; + let plan = display::plan_for(s, d, &p, outs.as_deref().unwrap_or_default()); if plan.is_empty() && p.monitor.is_some() { eprintln!("note: no display control for {s:?}/{d:?}; launching without mode change"); } // The mode in use now, to put back when the game is over. A detached game without the engine runs on // after this command returns, so there is no "after" to restore in. let restore = if plan.is_empty() || (detached && eng.is_none()) { None } else { - match display::outputs(s, d) { - Ok(outs) => display::restore_profile(&outs, &p).map(|r| display::plan(s, d, &r)), + match &outs { + Ok(o) => display::restore_profile(o, &p).map(|r| display::plan_for(s, d, &r, o)), Err(e) => { eprintln!("note: could not read the current monitor mode ({e}); it will not be restored"); None } } }; diff --git a/crates/summit-core/src/display.rs b/crates/summit-core/src/display.rs index 015f030..ffc4cf2 100644 --- a/crates/summit-core/src/display.rs +++ b/crates/summit-core/src/display.rs @@ -88,12 +88,17 @@ pub struct Output { } #[derive(Debug, Clone, PartialEq, Default)] -pub struct Mode { pub width: u32, pub height: u32, pub rates: Vec } +pub struct Mode { + pub width: u32, pub height: u32, pub rates: Vec, + /// (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. + pub named: Vec<(f32, String)>, +} fn push_mode(modes: &mut Vec, w: u32, h: u32, r: Option) { let m = match modes.iter_mut().position(|m| m.width == w && m.height == h) { Some(i) => &mut modes[i], - None => { modes.push(Mode { width: w, height: h, rates: vec![] }); modes.last_mut().unwrap() } + None => { modes.push(Mode { width: w, height: h, ..Default::default() }); modes.last_mut().unwrap() } }; if let Some(r) = r { if !m.rates.iter().any(|x| (x - r).abs() < 0.005) { m.rates.push(r); } } } @@ -118,11 +123,16 @@ pub fn parse_xrandr(text: &str) -> Vec { let mut w = line.split_whitespace(); let Some(size) = w.next() else { continue }; let Some((ws, hs)) = size.split_once('x') else { continue }; + // custom modes are named like "1920x1080_120.00"; the size is what counts + let hs = hs.split('_').next().unwrap_or_default(); let (Ok(wd), Ok(ht)) = (ws.parse::(), hs.parse::()) else { continue }; for tok in w { let cur = tok.contains('*'); let Ok(r) = tok.trim_end_matches(['*', '+']).parse::() else { continue }; push_mode(&mut o.modes, wd, ht, Some(r)); + if size != format!("{wd}x{ht}") { + if let Some(m) = o.modes.iter_mut().find(|m| m.width == wd && m.height == ht) { m.named.push((r, size.to_string())); } + } if cur { o.current = Some((wd, ht, r)); } } } @@ -199,6 +209,20 @@ pub fn restore_profile(outs: &[Output], p: &Profile) -> Option { 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. +pub fn plan_for(session: Session, desktop: Desktop, p: &Profile, outs: &[Output]) -> Vec { + 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; } + } + cmds +} + pub fn apply(cmds: &[Cmd]) -> Vec<(Cmd, Result<(), String>)> { cmds.iter().map(|cmd| { let r = Command::new(&cmd[0]).args(&cmd[1..]).status() @@ -243,6 +267,17 @@ HDMI-1 disconnected (normal left inverted right x axis y axis) assert_eq!(o[0].modes[1].rates, vec![60.0, 59.94, 50.0]); assert_eq!((o[1].name.as_str(), o[1].connected, o[1].current), ("HDMI-1", false, None)); } + #[test] fn xrandr_custom_mode_names() { + // as on the test VM: the mode in use is a custom one, with its own name + let o = parse_xrandr("Virtual-1 connected primary 1920x1080+0+0 (normal) 325mm x 203mm\n 1280x800 74.99 +\n 1920x1080_120.00 119.93*\n 1920x1080 60.00 50.00\n"); + assert_eq!(o[0].current, Some((1920, 1080, 119.93))); + assert_eq!(o[0].modes[0].rates, vec![119.93, 60.0, 50.0]); + let p = Profile { monitor: Some("Virtual-1".into()), width: Some(1280), height: Some(800), refresh_hz: Some(74.99), ..Default::default() }; + let back = restore_profile(&o, &p).unwrap(); + assert_eq!(plan_for(Session::X11, Desktop::Kde, &back, &o)[0].join(" "), "xrandr --output Virtual-1 --mode 1920x1080_120.00 --rate 119.93"); + let sixty = Profile { refresh_hz: Some(60.0), ..back }; + assert_eq!(plan_for(Session::X11, Desktop::Kde, &sixty, &o)[0].join(" "), "xrandr --output Virtual-1 --mode 1920x1080 --rate 60"); + } #[test] fn kscreen_outputs() { let j = r#"{"outputs":[{"name":"DP-1","connected":true,"enabled":true,"currentModeId":"2","modes":[ {"id":"1","name":"3840x2160@60","refreshRate":59.997,"size":{"width":3840,"height":2160}},