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: BufReader<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        let stream = BufReader::new(stream);
41        Ok(Self { stream })
42    }
43
44    /// Sends a request to niri and returns the response.
45    ///
46    /// Return values:
47    ///
48    /// * `Ok(Ok(response))`: successful [`Response`](crate::Response) from niri
49    /// * `Ok(Err(message))`: error message from niri
50    /// * `Err(error)`: error communicating with niri
51    pub fn send(&mut self, request: Request) -> io::Result<Reply> {
52        let mut buf = serde_json::to_string(&request).unwrap();
53        buf.push('\n');
54        self.stream.get_mut().write_all(buf.as_bytes())?;
55
56        buf.clear();
57        self.stream.read_line(&mut buf)?;
58
59        let reply = serde_json::from_str(&buf)?;
60        Ok(reply)
61    }
62
63    /// Starts reading event stream [`Event`]s from the socket.
64    ///
65    /// The returned function will block until the next [`Event`] arrives, then return it.
66    ///
67    /// Use this only after requesting an [`EventStream`][Request::EventStream].
68    ///
69    /// # Examples
70    ///
71    /// ```no_run
72    /// use niri_ipc::{Request, Response};
73    /// use niri_ipc::socket::Socket;
74    ///
75    /// fn main() -> std::io::Result<()> {
76    ///     let mut socket = Socket::connect()?;
77    ///
78    ///     let reply = socket.send(Request::EventStream)?;
79    ///     if matches!(reply, Ok(Response::Handled)) {
80    ///         let mut read_event = socket.read_events();
81    ///         while let Ok(event) = read_event() {
82    ///             println!("Received event: {event:?}");
83    ///         }
84    ///     }
85    ///
86    ///     Ok(())
87    /// }
88    /// ```
89    pub fn read_events(self) -> impl FnMut() -> io::Result<Event> {
90        let Self { mut stream } = self;
91        let _ = stream.get_mut().shutdown(Shutdown::Write);
92
93        let mut buf = String::new();
94        move || {
95            buf.clear();
96            stream.read_line(&mut buf)?;
97            let event = serde_json::from_str(&buf)?;
98            Ok(event)
99        }
100    }
101}