diff options
Diffstat (limited to 'src/ipc')
| -rw-r--r-- | src/ipc/client.rs | 12 | ||||
| -rw-r--r-- | src/ipc/server.rs | 9 |
2 files changed, 20 insertions, 1 deletions
diff --git a/src/ipc/client.rs b/src/ipc/client.rs index 7754187a..b2004a7b 100644 --- a/src/ipc/client.rs +++ b/src/ipc/client.rs @@ -19,8 +19,9 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { let mut stream = UnixStream::connect(socket_path).context("error connecting to {socket_path}")?; - let request = match msg { + let request = match &msg { Msg::Outputs => Request::Outputs, + Msg::Action { action } => Request::Action(action.clone()), }; let mut buf = serde_json::to_vec(&request).unwrap(); stream @@ -35,6 +36,14 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { .read_to_end(&mut buf) .context("error reading IPC response")?; + if matches!(msg, Msg::Action { .. }) { + if buf.is_empty() { + return Ok(()); + } else { + bail!("unexpected response: expected no response, got {buf:?}"); + } + } + let response = serde_json::from_slice(&buf).context("error parsing IPC response")?; match msg { Msg::Outputs => { @@ -100,6 +109,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { println!(); } } + Msg::Action { .. } => unreachable!(), } Ok(()) diff --git a/src/ipc/server.rs b/src/ipc/server.rs index d493e861..bedfe48f 100644 --- a/src/ipc/server.rs +++ b/src/ipc/server.rs @@ -22,6 +22,7 @@ pub struct IpcServer { } struct ClientCtx { + event_loop: LoopHandle<'static, State>, ipc_outputs: Rc<RefCell<HashMap<String, niri_ipc::Output>>>, } @@ -85,6 +86,7 @@ fn on_new_ipc_client(state: &mut State, stream: UnixStream) { }; let ctx = ClientCtx { + event_loop: state.niri.event_loop.clone(), ipc_outputs: state.backend.ipc_outputs(), }; @@ -115,6 +117,13 @@ async fn handle_client(ctx: ClientCtx, stream: Async<'_, UnixStream>) -> anyhow: let ipc_outputs = ctx.ipc_outputs.borrow().clone(); Response::Outputs(ipc_outputs) } + Request::Action(action) => { + let action = niri_config::Action::from(action); + ctx.event_loop.insert_idle(move |state| { + state.do_action(action); + }); + return Ok(()); + } }; let buf = serde_json::to_vec(&response).context("error formatting response")?; |
