Skip to content

Commit

Permalink
feat: add thread sleep to prevent continuous retry (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
grtsinry43 authored Feb 1, 2025
1 parent 0469a9c commit 47943bd
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions src/get_active_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,47 +77,61 @@ impl WindowTitle {
}

pub fn get_active_window_process_and_title() -> Result<String, Box<dyn Error>> {
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;
}
}
}

0 comments on commit 47943bd

Please sign in to comment.