niri_ipc/
socket.rs

1//! Helper for blocking communication over the niri socket.
2
3use std::env;
4use std::io::{self, BufRead, BufReader, Write};
5use std::net::Shutdown;
6use std::os::unix::net::UnixStream;
7use std::path::Path;
8
9use crate::{Event, Reply, Request};
10
11/// Name of the environment variable containing the niri IPC socket path.
12pub const SOCKET_PATH_ENV: &str = "NIRI_SOCKET";
13
14/// Helper for blocking communication over the niri socket.
15///
16/// This struct is used to communicate with the niri IPC server. It handles the socket connection
17/// and serialization/deserialization of messages.
18pub struct Socket {
19    stream: UnixStream,
20}
21
22impl Socket {
23    /// Connects to the default niri IPC socket.
24    ///
25    /// This is equivalent to calling [`Self::connect_to`] with the path taken from the
26    /// [`SOCKET_PATH_ENV`] environment variable.
27    pub fn connect() -> io::Result<Self> {
28        let socket_path = env::var_os(SOCKET_PATH_ENV).ok_or_else(|| {
29            io::Error::new(
30                io::ErrorKind::NotFound,
31                format!("{SOCKET_PATH_ENV} is not set, are you running this within niri?"),
32            )
33        })?;
34        Self::connect_to(socket_path)
35    }
36
37    /// Connects to the niri IPC socket at the given path.
38    pub fn connect_to(path: impl AsRef<Path>) -> io::Result<Self> {
39        let stream = UnixStream::connect(path.as_ref())?;
40        Ok(Self { stream })
41    }
42
43    /// Sends a request to niri and returns the response.
44    ///
45    /// Return values:
46    ///
47    /// * `Ok(Ok(response))`: successful [`Response`](crate::Response) from niri
48    /// * `Ok(Err(message))`: error message from niri
49    /// * `Err(error)`: error communicating with niri
50    ///
51    /// This method also returns a blocking function that you can call to keep reading [`Event`]s
52    /// after requesting an [`EventStream`][Request::EventStream]. This function is not useful
53    /// otherwise.
54    pub fn send(self, request: Request) -> io::Result<(Reply, impl FnMut() -> io::Result<Event>)> {
55        let Self { mut stream } = self;
56
57        let mut buf = serde_json::to_string(&request).unwrap();
58        stream.write_all(buf.as_bytes())?;
59        stream.shutdown(Shutdown::Write)?;
60
61        let mut reader = BufReader::new(stream);
62
63        buf.clear();
64        reader.read_line(&mut buf)?;
65
66        let reply = serde_json::from_str(&buf)?;
67
68        let events = move || {
69            buf.clear();
70            reader.read_line(&mut buf)?;
71            let event = serde_json::from_str(&buf)?;
72            Ok(event)
73        };
74
75        Ok((reply, events))
76    }
77}