feat: full poc
This commit is contained in:
parent
b76036038e
commit
a8847b93cf
14 changed files with 630 additions and 323 deletions
37
daemon/socket.rs
Normal file
37
daemon/socket.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use anyhow::{Context, Result, anyhow};
|
||||
use niri_ipc::{Reply, Request, Response};
|
||||
use smol::{
|
||||
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
||||
net::unix::UnixStream,
|
||||
};
|
||||
use std::env;
|
||||
|
||||
pub async fn query(socket: &mut BufReader<UnixStream>, req: Request) -> Result<Reply> {
|
||||
let req = serde_json::to_string(&req)?;
|
||||
tracing::debug!("sending request: {}", req);
|
||||
socket.write_all(&[req.as_bytes(), b"\n"].concat()).await?;
|
||||
socket.flush().await?;
|
||||
let mut rep = String::new();
|
||||
socket.read_line(&mut rep).await?;
|
||||
Ok(serde_json::from_str(&rep)?)
|
||||
}
|
||||
|
||||
pub async fn tell(socket: &mut BufReader<UnixStream>, req: Request) -> Result<()> {
|
||||
let rep = query(socket, req).await?;
|
||||
if let Reply::Ok(Response::Handled) = rep {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Expected Reply::Ok(Response::Handled), got {}",
|
||||
rep.unwrap_err()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_niri_socket() -> Result<BufReader<UnixStream>> {
|
||||
let socket_path = env::var(niri_ipc::socket::SOCKET_PATH_ENV)
|
||||
.context("Couldn't find Niri socket path ($NIRI_SOCKET) in environment")?;
|
||||
tracing::debug!("socket path is: {}", socket_path);
|
||||
let raw = UnixStream::connect(&socket_path).await?;
|
||||
Ok(BufReader::new(raw))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue