|
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + env, |
| 4 | + error::Error as Std_Error, |
| 5 | + fs, |
| 6 | + io::{Error, ErrorKind}, |
| 7 | + process::Command, |
| 8 | + thread, |
| 9 | +}; |
| 10 | + |
| 11 | +use procfs::process::Process; |
| 12 | +use sysinfo::{Pid, System}; |
| 13 | + |
| 14 | +use niri_ipc::socket::Socket; |
| 15 | +use niri_ipc::{Request, Response}; |
| 16 | + |
| 17 | +fn is_x11_session() -> bool { |
| 18 | + env::var("XDG_SESSION_TYPE").map_or(false, |session_type| session_type == "x11") |
| 19 | +} |
| 20 | + |
| 21 | +fn is_niri_session() -> bool { |
| 22 | + env::var("NIRI_SOCKET").is_ok() |
| 23 | +} |
| 24 | + |
| 25 | +fn get_active_window_x11() -> Result<(String, String), Box<dyn Std_Error>> { |
| 26 | + // Get active window ID using xprop |
| 27 | + let active_output = Command::new("xprop") |
| 28 | + .args(["-root", "_NET_ACTIVE_WINDOW"]) |
| 29 | + .output()?; |
| 30 | + |
| 31 | + if !active_output.status.success() { |
| 32 | + return Err("xprop failed to get active window".into()); |
| 33 | + } |
| 34 | + |
| 35 | + let active_line = String::from_utf8_lossy(&active_output.stdout); |
| 36 | + let window_id = active_line |
| 37 | + .split_whitespace() |
| 38 | + .last() |
| 39 | + .ok_or("Could not parse window ID")?; |
| 40 | + |
| 41 | + // Get window list with PIDs using wmctrl |
| 42 | + let output = Command::new("wmctrl").args(["-l", "-p"]).output()?; |
| 43 | + |
| 44 | + if !output.status.success() { |
| 45 | + return Err("wmctrl failed".into()); |
| 46 | + } |
| 47 | + |
| 48 | + let window_list = String::from_utf8_lossy(&output.stdout); |
| 49 | + |
| 50 | + // Find the active window in the list |
| 51 | + for line in window_list.lines() { |
| 52 | + if line.starts_with(window_id) { |
| 53 | + let parts: Vec<&str> = line.split_whitespace().collect(); |
| 54 | + if parts.len() >= 3 { |
| 55 | + if let Ok(pid) = parts[2].parse::<i32>() { |
| 56 | + if let Ok(process) = Process::new(pid) { |
| 57 | + if let Ok(exe) = process.exe() { |
| 58 | + let path = exe.to_string_lossy().into_owned(); |
| 59 | + let name = exe |
| 60 | + .file_name() |
| 61 | + .map(|n| n.to_string_lossy().into_owned()) |
| 62 | + .unwrap_or_else(|| "unknown".to_string()); |
| 63 | + return Ok((path, name)); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Err("Could not determine active window".into()) |
| 72 | +} |
| 73 | + |
| 74 | +fn get_active_window_niri() -> Result<(String, String), Box<dyn Std_Error>> { |
| 75 | + let mut socket = Socket::connect()?; |
| 76 | + |
| 77 | + socket |
| 78 | + .send(Request::Windows)? |
| 79 | + .ok() |
| 80 | + .and_then(|resp| { |
| 81 | + if let Response::Windows(ws) = resp { |
| 82 | + Some(ws) |
| 83 | + } else { |
| 84 | + None |
| 85 | + } |
| 86 | + }) |
| 87 | + .and_then(|windows| windows.into_iter().find(|w| w.is_focused)) |
| 88 | + .and_then(|window| window.pid) |
| 89 | + .and_then(|pid| { |
| 90 | + let sys = System::new_all(); |
| 91 | + let sys_pid = Pid::from(pid as usize); |
| 92 | + |
| 93 | + sys.process(sys_pid).map(|proc| { |
| 94 | + let exe = proc |
| 95 | + .exe() |
| 96 | + .map(|exe| exe.to_string_lossy().into_owned()) |
| 97 | + .unwrap_or_default(); |
| 98 | + |
| 99 | + let cmd = proc |
| 100 | + .cmd() |
| 101 | + .iter() |
| 102 | + .map(|os| os.to_string_lossy()) |
| 103 | + .collect::<Vec<_>>() |
| 104 | + .join(" "); |
| 105 | + |
| 106 | + (exe, cmd) |
| 107 | + }) |
| 108 | + }) |
| 109 | + .ok_or("Could not determine active window".into()) |
| 110 | +} |
| 111 | + |
| 112 | +pub fn ux_are_processes_running<'a>( |
| 113 | + processes: &'a [String], |
| 114 | +) -> Result<HashMap<&'a String, bool>, Error> { |
| 115 | + let mut map = HashMap::new(); |
| 116 | + let sys = System::new_all(); |
| 117 | + |
| 118 | + for (_, process) in sys.processes() { |
| 119 | + let process_name = process.name().to_str().unwrap_or(""); |
| 120 | + for target_process in processes { |
| 121 | + if target_process == &format!("{}.exe", process_name) { |
| 122 | + map.insert(target_process, true); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Ok(map) |
| 128 | +} |
| 129 | + |
| 130 | +pub fn ux_get_foreground_meta() -> (Option<String>, Option<String>) { |
| 131 | + if is_x11_session() { |
| 132 | + match get_active_window_x11() { |
| 133 | + Ok((path, name)) => (Some(path), Some(name)), |
| 134 | + Err(_) => (None, None), |
| 135 | + } |
| 136 | + } else { |
| 137 | + if is_niri_session() { |
| 138 | + match get_active_window_niri() { |
| 139 | + Ok((path, name)) => (Some(path), Some(name)), |
| 140 | + Err(_) => (None, None), |
| 141 | + } |
| 142 | + } else { |
| 143 | + (None, None) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +pub fn init_tray() { |
| 149 | + thread::spawn(move || {}); |
| 150 | +} |
0 commit comments