From db7e6bfa42f95335ed52ba77af2fd22cd8d98c27 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 8 Apr 2019 09:53:53 +0200 Subject: [PATCH] Added only secondary screen option --- src/lib.rs | 28 ++++++++++++++++++++++++++-- src/tv.rs | 3 ++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 96207a7..3e320c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -309,6 +309,22 @@ impl MultiScreen { } } + /// Returns the arguments for enabling only the second screen. + pub fn only_secondary_args(&self) -> Option> { + if let Some((ref main, ref second)) = self.main_screens() { + Some(vec![ + "--output".to_owned(), + main.name.clone(), + "--off".to_owned(), + "--output".to_owned(), + second.name.clone(), + "--auto".to_owned(), + ]) + } else { + None + } + } + /// 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()) @@ -325,7 +341,16 @@ impl MultiScreen { /// Calls the xrandr program to set two screens. pub fn two_screens(&self, side: Side) -> Result<(), Error> { - if let Some(args) = self.two_screen_args(side){ + if let Some(args) = self.two_screen_args(side) { + self.run_command(args) + } else { + Err(Error::NoScreenDetected) + } + } + + /// Calls the xrandr program to enable only the second screen. + pub fn only_secondary(&self) -> Result<(), Error> { + if let Some(args) = self.only_secondary_args() { self.run_command(args) } else { Err(Error::NoScreenDetected) @@ -334,7 +359,6 @@ impl MultiScreen { /// Run an xrandr command with its args. pub fn run_command(&self, args: Vec) -> Result<(), Error> { - Command::new("xrandr") .args(args) .spawn()? diff --git a/src/tv.rs b/src/tv.rs index ffe00fe..84a0837 100644 --- a/src/tv.rs +++ b/src/tv.rs @@ -10,7 +10,7 @@ fn main() { .help("Action to do") .takes_value(true) .value_name("ACTION") - .possible_values(&["disable", "duplicate", "enable"]) + .possible_values(&["disable", "duplicate", "enable","only-secondary"]) .required(true)) .arg(Arg::with_name("position") .help("Specify the position of the second screen") @@ -35,6 +35,7 @@ fn main() { let side = matches.value_of("position").unwrap().parse().unwrap(); screens.two_screens(side) }, + Some("only-secondary") => screens.only_secondary(), // Can never happen, clap will crash before this _ => Ok(()) };