niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·a9fcbf8·Ivan Molodetskikh·2 years ago·671 commits
| 1 | use std::cell::RefCell; |
| 2 | use std::collections::HashMap; |
| 3 | use std::os::unix::net::{UnixListener, UnixStream}; |
| 4 | use std::path::PathBuf; |
| 5 | use std::rc::Rc; |
| 6 | use std::{env, io, process}; |
| 7 | |
| 8 | use anyhow::Context; |
| 9 | use calloop::io::Async; |
| 10 | use directories::BaseDirs; |
| 11 | use futures_util::io::{AsyncReadExt, BufReader}; |
| 12 | use futures_util::{AsyncBufReadExt, AsyncWriteExt}; |
| 13 | use niri_ipc::{Request, Response}; |
| 14 | use smithay::reexports::calloop::generic::Generic; |
| 15 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 16 | use smithay::reexports::rustix::fs::unlink; |
| 17 | |
| 18 | use crate::niri::State; |
| 19 | |
| 20 | pub struct IpcServer { |
| 21 | pub socket_path: PathBuf, |
| 22 | } |
| 23 | |
| 24 | struct ClientCtx { |
| 25 | event_loop: LoopHandle<'static, State>, |
| 26 | ipc_outputs: Rc<RefCell<HashMap<String, niri_ipc::Output>>>, |
| 27 | } |
| 28 | |
| 29 | impl IpcServer { |
| 30 | pub fn start( |
| 31 | event_loop: &LoopHandle<'static, State>, |
| 32 | wayland_socket_name: &str, |
| 33 | ) -> anyhow::Result<Self> { |
| 34 | let _span = tracy_client::span!("Ipc::start"); |
| 35 | |
| 36 | let socket_name = format!("niri.{wayland_socket_name}.{}.sock", process::id()); |
| 37 | let mut socket_path = socket_dir(); |
| 38 | socket_path.push(socket_name); |
| 39 | |
| 40 | let listener = UnixListener::bind(&socket_path).context("error binding socket")?; |
| 41 | listener |
| 42 | .set_nonblocking(true) |
| 43 | .context("error setting socket to non-blocking")?; |
| 44 | |
| 45 | let source = Generic::new(listener, Interest::READ, Mode::Level); |
| 46 | event_loop |
| 47 | .insert_source(source, |_, socket, state| { |
| 48 | match socket.accept() { |
| 49 | Ok((stream, _)) => on_new_ipc_client(state, stream), |
| 50 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => (), |
| 51 | Err(e) => return Err(e), |
| 52 | } |
| 53 | |
| 54 | Ok(PostAction::Continue) |
| 55 | }) |
| 56 | .unwrap(); |
| 57 | |
| 58 | Ok(Self { socket_path }) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl Drop for IpcServer { |
| 63 | fn drop(&mut self) { |
| 64 | let _ = unlink(&self.socket_path); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn socket_dir() -> PathBuf { |
| 69 | BaseDirs::new() |
| 70 | .as_ref() |
| 71 | .and_then(|x| x.runtime_dir()) |
| 72 | .map(|x| x.to_owned()) |
| 73 | .unwrap_or_else(env::temp_dir) |
| 74 | } |
| 75 | |
| 76 | fn on_new_ipc_client(state: &mut State, stream: UnixStream) { |
| 77 | let _span = tracy_client::span!("on_new_ipc_client"); |
| 78 | trace!("new IPC client connected"); |
| 79 | |
| 80 | let stream = match state.niri.event_loop.adapt_io(stream) { |
| 81 | Ok(stream) => stream, |
| 82 | Err(err) => { |
| 83 | warn!("error making IPC stream async: {err:?}"); |
| 84 | return; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | let ctx = ClientCtx { |
| 89 | event_loop: state.niri.event_loop.clone(), |
| 90 | ipc_outputs: state.backend.ipc_outputs(), |
| 91 | }; |
| 92 | |
| 93 | let future = async move { |
| 94 | if let Err(err) = handle_client(ctx, stream).await { |
| 95 | warn!("error handling IPC client: {err:?}"); |
| 96 | } |
| 97 | }; |
| 98 | if let Err(err) = state.niri.scheduler.schedule(future) { |
| 99 | warn!("error scheduling IPC stream future: {err:?}"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow::Result<()> { |
| 104 | let (read, mut write) = stream.split(); |
| 105 | let mut buf = String::new(); |
| 106 | |
| 107 | // Read a single line to allow extensibility in the future to keep reading. |
| 108 | BufReader::new(read) |
| 109 | .read_line(&mut buf) |
| 110 | .await |
| 111 | .context("error reading request")?; |
| 112 | |
| 113 | let reply = process(&ctx, &buf).map_err(|err| { |
| 114 | warn!("error processing IPC request: {err:?}"); |
| 115 | err.to_string() |
| 116 | }); |
| 117 | |
| 118 | let buf = serde_json::to_vec(&reply).context("error formatting reply")?; |
| 119 | write.write_all(&buf).await.context("error writing reply")?; |
| 120 | |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | fn process(ctx: &ClientCtx, buf: &str) -> anyhow::Result<Response> { |
| 125 | let request: Request = serde_json::from_str(buf).context("error parsing request")?; |
| 126 | |
| 127 | let response = match request { |
| 128 | Request::Outputs => { |
| 129 | let ipc_outputs = ctx.ipc_outputs.borrow().clone(); |
| 130 | Response::Outputs(ipc_outputs) |
| 131 | } |
| 132 | Request::Action(action) => { |
| 133 | let action = niri_config::Action::from(action); |
| 134 | ctx.event_loop.insert_idle(move |state| { |
| 135 | state.do_action(action); |
| 136 | }); |
| 137 | Response::Handled |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | Ok(response) |
| 142 | } |