use crate::client::{Client, WindowInfo};
use anyhow::bail;
use niri_ipc::{socket::Socket, Request, Response};
pub struct NiriClient;
impl NiriClient {
pub fn new() -> NiriClient {
NiriClient {}
}
fn get_active_window() -> Option<niri_ipc::Window> {
let mut socket = match Socket::connect() {
Ok(socket) => socket,
Err(_) => return None,
};
let response = match socket.send(Request::Windows) {
Ok(Ok(response)) => response,
Ok(Err(_)) => return None,
Err(_) => return None,
};
match response {
Response::Windows(windows) => windows.into_iter().find(|w| w.is_focused),
_ => None,
}
}
}
impl Client for NiriClient {
fn supported(&mut self) -> bool {
let socket_path = match std::env::var("NIRI_SOCKET") {
Ok(path) => path,
Err(_) => return false,
};
if !std::path::Path::new(&socket_path).exists() {
return false;
}
Socket::connect().is_ok()
}
fn current_window(&mut self) -> Option<String> {
Self::get_active_window().and_then(|win| {
win.title.or_else(|| win.app_id.clone())
})
}
fn current_application(&mut self) -> Option<String> {
Self::get_active_window().and_then(|win| {
win.app_id.or_else(|| win.title.clone())
})
}
fn window_list(&mut self) -> anyhow::Result<Vec<WindowInfo>> {
bail!("window_list not implemented for NIRI")
}
}