tvrs/src/lib.rs

199 lines
5.0 KiB
Rust

use std::io;
use std::collections::HashSet;
use std::process::Command;
use std::num::ParseIntError;
use std::string::FromUtf8Error;
/// The different kinds of errors that can happen.
#[derive(Debug)]
pub enum Error {
/// An io::Error happened while running the xrandr command.
IoError(io::Error),
/// An error happened while parsing the xrandr output to a string
Utf8Error(FromUtf8Error),
/// An error happened while parsing the resolution of a xrandr screen.
ParseIntError(ParseIntError),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::IoError(e)
}
}
impl From<FromUtf8Error> for Error {
fn from(e: FromUtf8Error) -> Error {
Error::Utf8Error(e)
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Error {
Error::ParseIntError(e)
}
}
/// A struct that represents a screen and its supported resolutions.
#[derive(Debug)]
pub struct Screen {
/// The name of the screen received from xrandr.
name: String,
/// The available resolutions of the screen.
resolutions: Vec<(u32, u32)>,
/// Whether the screen is primary or not
primary: bool,
/// Whether the screen is connected or not.
connected: bool,
}
impl Screen {
/// Creates a screen from the first line in the xrandr command.
pub fn from_line(line: &str) -> Screen {
let split = line.split_whitespace().collect::<Vec<_>>();
Screen {
name: split[0].to_string(),
resolutions: vec![],
primary: line.contains("primary"),
connected: line.contains(" connected"),
}
}
/// Finds the best common resolution between two screens.
fn best_common_resolution(&self, other: &Screen) -> Option<(u32, u32)> {
let self_res: HashSet<(u32, u32)> = self.resolutions.clone().into_iter().collect();
let other_res: HashSet<(u32, u32)> = other.resolutions.clone().into_iter().collect();
let intersection = self_res.intersection(&other_res);
let mut output = None;
for element in intersection {
if let Some(res) = output {
if element > &res {
output = Some(*element);
}
}
}
output
}
}
/// A struct that contains all the screens and allows to manage them.
#[derive(Debug)]
pub struct MultiScreen {
/// All the contained screens.
pub(crate) screens: Vec<Screen>,
}
impl MultiScreen {
/// Finds all the screens by spawning and parsing the output of xrandr.
pub fn detect() -> Result<MultiScreen, Error> {
let mut screens = vec![];
let xrandr = Command::new("xrandr")
.output()?;
let output = String::from_utf8(xrandr.stdout)?;
let output = output.split('\n').skip(1);
for line in output {
if line.is_empty() {
continue;
}
if line.contains("connected") {
screens.push(Screen::from_line(line));
} else if let Some(mut last) = screens.last_mut() {
// Get the first thing
let split = line.split_whitespace().collect::<Vec<_>>();
// Remove the _ with the frequency
let resolution = split[0]
.split("_").collect::<Vec<_>>()[0]
.split("x").collect::<Vec<_>>();
last.resolutions.push((
resolution[0].parse::<u32>()?,
resolution[1].parse::<u32>()?,
));
}
}
for screen in &mut screens {
screen.resolutions.sort();
}
Ok(MultiScreen { screens: screens })
}
/// Returns the arguments of xrandr that disable all screens except the primary.
pub fn only_primary_args(&self) -> Vec<&str> {
let mut primary_found = false;
let mut args = vec![];
for screen in &self.screens {
if ! screen.primary || primary_found {
args.push("--output");
args.push(&screen.name);
args.push("--off");
} else {
args.push("--output");
args.push(&screen.name);
args.push("--auto");
primary_found = true;
}
}
args
}
/// Returns the arguments to duplicate the screens.
pub fn duplicate_args(&self) -> Vec<&str> {
panic!("Not implemented yet");
vec![]
}
/// Calls the xrandr program to disable all screens except the primary.
pub fn only_primary(&self) -> Result<(), Error> {
self.run_command(self.only_primary_args())
}
/// Calls the xrandr program to duplicate the screens.
pub fn duplicate(&self) -> Result<(), Error> {
self.run_command(self.duplicate_args())
}
/// Run an xrandr command with its args.
pub fn run_command(&self, args: Vec<&str>) -> Result<(), Error> {
Command::new("xrandr")
.args(args)
.spawn()?
.wait()?;
Ok(())
}
}