diff --git a/src/get_active_window.rs b/src/get_active_window.rs index 2aa1192..7672c5c 100644 --- a/src/get_active_window.rs +++ b/src/get_active_window.rs @@ -12,6 +12,8 @@ use std::error::Error; use std::io::{BufRead, BufReader}; use std::process::{Command, Stdio}; +use std::thread; +use std::time::Duration; enum WindowTitle { Code, @@ -75,47 +77,61 @@ impl WindowTitle { } pub fn get_active_window_process_and_title() -> Result> { - let xprop_output = Command::new("xprop") - .arg("-root") - .arg("_NET_ACTIVE_WINDOW") - .stdout(Stdio::piped()) - .spawn()? - .stdout - .ok_or("Failed to capture xprop stdout")?; + let mut failure_count = 0; - let xprop_reader = BufReader::new(xprop_output); - let mut window_id = String::new(); - for line in xprop_reader.lines() { - let line = line?; - if line.contains("_NET_ACTIVE_WINDOW(WINDOW)") { - window_id = line.split_whitespace().nth(4).unwrap_or("").to_string(); - break; + loop { + let xprop_output = Command::new("xprop") + .arg("-root") + .arg("_NET_ACTIVE_WINDOW") + .stdout(Stdio::piped()) + .spawn()? + .stdout + .ok_or("Failed to capture xprop stdout")?; + + let xprop_reader = BufReader::new(xprop_output); + let mut window_id = String::new(); + for line in xprop_reader.lines() { + let line = line?; + if line.contains("_NET_ACTIVE_WINDOW(WINDOW)") { + window_id = line.split_whitespace().nth(4).unwrap_or("").to_string(); + break; + } } - } - if window_id.is_empty() { - return Err("Failed to get active window ID".into()); - } + if window_id.is_empty() { + failure_count += 1; + if failure_count >= 3 { + thread::sleep(Duration::from_secs(1)); + failure_count = 0; + } + continue; + } - let xprop_output = Command::new("xprop") - .arg("-id") - .arg(&window_id) - .arg("WM_CLASS") - .stdout(Stdio::piped()) - .spawn()? - .stdout - .ok_or("Failed to capture xprop stdout")?; + let xprop_output = Command::new("xprop") + .arg("-id") + .arg(&window_id) + .arg("WM_CLASS") + .stdout(Stdio::piped()) + .spawn()? + .stdout + .ok_or("Failed to capture xprop stdout")?; - let xprop_reader = BufReader::new(xprop_output); - for line in xprop_reader.lines() { - let line = line?; - if line.contains("WM_CLASS(STRING)") { - let class_name = line.split('"').nth(1).unwrap_or(""); - println!("class_name: {}", class_name); - let window_title_enum = WindowTitle::from_string(class_name); - return Ok(window_title_enum.to_string()); + let xprop_reader = BufReader::new(xprop_output); + for line in xprop_reader.lines() { + let line = line?; + if line.contains("WM_CLASS(STRING)") { + let class_name = line.split('"').nth(1).unwrap_or(""); + println!("class_name: {}", class_name); + let window_title_enum = WindowTitle::from_string(class_name); + return Ok(window_title_enum.to_string()); + } } - } - Err("Failed to get window class".into()) + failure_count += 1; + if failure_count >= 3 { + thread::sleep(Duration::from_secs(1)); + println!("It seems that the window is not found or it's not a valid window (Terminal)."); + failure_count = 0; + } + } }