Files
STARTLINER/rust/src/display.rs
2025-03-04 15:59:37 +01:00

104 lines
3.0 KiB
Rust

use crate::profile::Profile;
use anyhow::Result;
#[cfg(target_os = "windows")]
pub struct DisplayInfo {
primary: String,
target: String,
target_settings: displayz::DisplaySettings
}
#[allow(dead_code)]
#[cfg(not(target_os = "windows"))]
pub async fn prepare_display(_: &Profile) -> Result<()> {
Ok(())
}
#[cfg(target_os = "windows")]
pub async fn prepare_display(p: &Profile) -> Result<Option<DisplayInfo>> {
use anyhow::anyhow;
use displayz::{query_displays, Orientation, Resolution};
let display_name = p.get_str("display", "default");
let rotation = p.get_int("display-rotation", 0);
if display_name == "default" {
log::debug!("prepare display: skip");
return Ok(None);
}
let display_set = query_displays()?;
let primary = display_set
.displays()
.find(|display| display.is_primary())
.ok_or_else(|| anyhow!("Primary display not found"))?;
let target = display_set
.displays()
.find(|display| display.name() == display_name)
.ok_or_else(|| anyhow!("Display {} not found", display_name))?;
target.set_primary()?;
let settings = target.settings()
.as_ref()
.ok_or_else(|| anyhow!("Unable to query display settings"))?;
let res = DisplayInfo {
primary: primary.name().to_owned(),
target: target.name().to_owned(),
target_settings: settings.borrow().clone()
};
if rotation == 90 || rotation == 270 {
let rez = settings.borrow_mut().resolution;
settings.borrow_mut().orientation = if rotation == 90 { Orientation::PortraitFlipped } else { Orientation::Portrait };
if rez.height < rez.width {
settings.borrow_mut().resolution = Resolution::new(rez.height, rez.width);
}
}
if p.get_str("display-mode", "borderless") == "borderless" && p.get_bool("borderless-fullscreen", false) {
settings.borrow_mut().resolution = Resolution::new(
p.get_int("rez-w", 1080).try_into().expect("Negative resolution"),
p.get_int("rez-h", 1920).try_into().expect("Negative resolution")
);
}
display_set.apply()?;
displayz::refresh()?;
log::debug!("prepare display: done");
Ok(Some(res))
}
#[cfg(target_os = "windows")]
pub async fn undo_display(info: DisplayInfo) -> Result<()> {
use displayz::query_displays;
let display_set = query_displays()?;
let primary = display_set
.displays()
.find(|display| display.name() == info.primary)
.ok_or_else(|| anyhow!("Display {} not found", info.primary))?;
let target = display_set
.displays()
.find(|display| display.name() == info.target)
.ok_or_else(|| anyhow!("Display {} not found", info.target))?;
primary.set_primary()?;
let settings = target.settings()
.as_ref()
.ok_or_else(|| anyhow!("Unable to query display settings"))?;
settings.replace_with(|_| info.target_settings);
display_set.apply()?;
displayz::refresh()?;
log::debug!("undo display: done");
Ok(())
}