Use Xorg process to determine if on X backend

I believe the old process name "x" was incorrect. This also moves the
list of backends to an enum instead of a string.
This commit is contained in:
mnussbaum 2019-09-05 17:28:26 -07:00
parent 25860caa84
commit 1e0cc3ed57
1 changed files with 48 additions and 41 deletions

View File

@ -7,8 +7,12 @@ use std::process::Command;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
fn main() { enum BackendMode {
let mut mode = ""; Sway,
Xorg,
}
fn main() -> Result<(), String> {
let mut old_state = "normal"; let mut old_state = "normal";
let mut new_state: &str; let mut new_state: &str;
let mut path_x: String = "".to_string(); let mut path_x: String = "".to_string();
@ -16,17 +20,21 @@ fn main() {
let mut matrix: [&str; 9]; let mut matrix: [&str; 9];
let mut x_state: &str; let mut x_state: &str;
let sway_pid = let mode = if String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout)
String::from_utf8(Command::new("pidof").arg("sway").output().unwrap().stdout).unwrap(); .unwrap()
.len()
let x_pid = String::from_utf8(Command::new("pidof").arg("x").output().unwrap().stdout).unwrap(); >= 1
{
if sway_pid.len() >= 1 { BackendMode::Sway
mode = "sway"; } else if String::from_utf8(Command::new("pidof").arg("Xorg").output().unwrap().stdout)
} .unwrap()
if x_pid.len() >= 1 { .len()
mode = "x"; >= 1
} {
BackendMode::Xorg
} else {
return Err("Unable to find Sway or Xorg procceses".to_owned());
};
let matches = App::new("rot8") let matches = App::new("rot8")
.version("0.1.1") .version("0.1.1")
@ -116,7 +124,8 @@ fn main() {
} }
if new_state != old_state { if new_state != old_state {
if mode == "sway" { match mode {
BackendMode::Sway => {
Command::new("swaymsg") Command::new("swaymsg")
.arg("output") .arg("output")
.arg(display) .arg(display)
@ -124,10 +133,8 @@ fn main() {
.arg(new_state) .arg(new_state)
.spawn() .spawn()
.expect("rotate command failed to start"); .expect("rotate command failed to start");
old_state = new_state;
} }
if mode == "x" { BackendMode::Xorg => {
Command::new("xrandr") Command::new("xrandr")
.arg("-o") .arg("-o")
.arg(x_state) .arg(x_state)
@ -143,10 +150,10 @@ fn main() {
.args(&matrix) .args(&matrix)
.spawn() .spawn()
.expect("rotate command failed to start"); .expect("rotate command failed to start");
old_state = new_state;
} }
} }
old_state = new_state;
}
thread::sleep(Duration::from_millis(sleep.parse::<u64>().unwrap_or(0))); thread::sleep(Duration::from_millis(sleep.parse::<u64>().unwrap_or(0)));
} }
} }