Implement IPC for the overview state (#1526)
Browse files at this commit5 changed files +101 -3
- niri-ipc/src/lib.rs +17 -0
- niri-ipc/src/state.rs +30 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +25 -2
- src/ipc/server.rs +27 -1
niri-ipc/src/lib.rsmodified+17 -0
| @@ -97,6 +97,8 @@ pub enum Request { | |||
| 97 | EventStream, | 97 | EventStream, |
| 98 | /// Respond with an error (for testing error handling). | 98 | /// Respond with an error (for testing error handling). |
| 99 | ReturnError, | 99 | ReturnError, |
| 100 | + /// Request information about the overview. | ||
| 101 | + OverviewState, | ||
| 100 | } | 102 | } |
| 101 | 103 | ||
| 102 | /// Reply from niri to client. | 104 | /// Reply from niri to client. |
| @@ -139,6 +141,16 @@ pub enum Response { | |||
| 139 | PickedColor(Option<PickedColor>), | 141 | PickedColor(Option<PickedColor>), |
| 140 | /// Output configuration change result. | 142 | /// Output configuration change result. |
| 141 | OutputConfigChanged(OutputConfigChanged), | 143 | OutputConfigChanged(OutputConfigChanged), |
| 144 | + /// Information about the overview. | ||
| 145 | + OverviewState(Overview), | ||
| 146 | +} | ||
| 147 | + | ||
| 148 | +/// Overview information. | ||
| 149 | +#[derive(Serialize, Deserialize, Debug, Clone)] | ||
| 150 | +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] | ||
| 151 | +pub struct Overview { | ||
| 152 | + /// Whether the overview is currently open. | ||
| 153 | + pub is_open: bool, | ||
| 142 | } | 154 | } |
| 143 | 155 | ||
| 144 | /// Color picked from the screen. | 156 | /// Color picked from the screen. |
| @@ -1269,6 +1281,11 @@ pub enum Event { | |||
| 1269 | /// Index of the newly active layout. | 1281 | /// Index of the newly active layout. |
| 1270 | idx: u8, | 1282 | idx: u8, |
| 1271 | }, | 1283 | }, |
| 1284 | + /// The overview was opened or closed. | ||
| 1285 | + OverviewOpenedOrClosed { | ||
| 1286 | + /// The new state of the overview. | ||
| 1287 | + is_open: bool, | ||
| 1288 | + }, | ||
| 1272 | } | 1289 | } |
| 1273 | 1290 | ||
| 1274 | impl FromStr for WorkspaceReferenceArg { | 1291 | impl FromStr for WorkspaceReferenceArg { |
niri-ipc/src/state.rsmodified+30 -0
| @@ -40,6 +40,9 @@ pub struct EventStreamState { | |||
| 40 | 40 | ||
| 41 | /// State of the keyboard layouts. | 41 | /// State of the keyboard layouts. |
| 42 | pub keyboard_layouts: KeyboardLayoutsState, | 42 | pub keyboard_layouts: KeyboardLayoutsState, |
| 43 | + | ||
| 44 | + /// State of the overview. | ||
| 45 | + pub overview: OverviewState, | ||
| 43 | } | 46 | } |
| 44 | 47 | ||
| 45 | /// The workspaces state communicated over the event stream. | 48 | /// The workspaces state communicated over the event stream. |
| @@ -63,12 +66,20 @@ pub struct KeyboardLayoutsState { | |||
| 63 | pub keyboard_layouts: Option<KeyboardLayouts>, | 66 | pub keyboard_layouts: Option<KeyboardLayouts>, |
| 64 | } | 67 | } |
| 65 | 68 | ||
| 69 | +/// The overview state communicated over the event stream. | ||
| 70 | +#[derive(Debug, Default)] | ||
| 71 | +pub struct OverviewState { | ||
| 72 | + /// Whether the overview is currently open. | ||
| 73 | + pub is_open: bool, | ||
| 74 | +} | ||
| 75 | + | ||
| 66 | impl EventStreamStatePart for EventStreamState { | 76 | impl EventStreamStatePart for EventStreamState { |
| 67 | fn replicate(&self) -> Vec<Event> { | 77 | fn replicate(&self) -> Vec<Event> { |
| 68 | let mut events = Vec::new(); | 78 | let mut events = Vec::new(); |
| 69 | events.extend(self.workspaces.replicate()); | 79 | events.extend(self.workspaces.replicate()); |
| 70 | events.extend(self.windows.replicate()); | 80 | events.extend(self.windows.replicate()); |
| 71 | events.extend(self.keyboard_layouts.replicate()); | 81 | events.extend(self.keyboard_layouts.replicate()); |
| 82 | + events.extend(self.overview.replicate()); | ||
| 72 | events | 83 | events |
| 73 | } | 84 | } |
| 74 | 85 | ||
| @@ -76,6 +87,7 @@ impl EventStreamStatePart for EventStreamState { | |||
| 76 | let event = self.workspaces.apply(event)?; | 87 | let event = self.workspaces.apply(event)?; |
| 77 | let event = self.windows.apply(event)?; | 88 | let event = self.windows.apply(event)?; |
| 78 | let event = self.keyboard_layouts.apply(event)?; | 89 | let event = self.keyboard_layouts.apply(event)?; |
| 90 | + let event = self.overview.apply(event)?; | ||
| 79 | Some(event) | 91 | Some(event) |
| 80 | } | 92 | } |
| 81 | } | 93 | } |
| @@ -192,3 +204,21 @@ impl EventStreamStatePart for KeyboardLayoutsState { | |||
| 192 | None | 204 | None |
| 193 | } | 205 | } |
| 194 | } | 206 | } |
| 207 | + | ||
| 208 | +impl EventStreamStatePart for OverviewState { | ||
| 209 | + fn replicate(&self) -> Vec<Event> { | ||
| 210 | + vec![Event::OverviewOpenedOrClosed { | ||
| 211 | + is_open: self.is_open, | ||
| 212 | + }] | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + fn apply(&mut self, event: Event) -> Option<Event> { | ||
| 216 | + match event { | ||
| 217 | + Event::OverviewOpenedOrClosed { is_open } => { | ||
| 218 | + self.is_open = is_open; | ||
| 219 | + } | ||
| 220 | + event => return Some(event), | ||
| 221 | + } | ||
| 222 | + None | ||
| 223 | + } | ||
| 224 | +} | ||
src/cli.rsmodified+2 -0
| @@ -105,4 +105,6 @@ pub enum Msg { | |||
| 105 | Version, | 105 | Version, |
| 106 | /// Request an error from the running niri instance. | 106 | /// Request an error from the running niri instance. |
| 107 | RequestError, | 107 | RequestError, |
| 108 | + /// Print the overview state. | ||
| 109 | + OverviewState, | ||
| 108 | } | 110 | } |
src/ipc/client.rsmodified+25 -2
| @@ -5,8 +5,8 @@ use anyhow::{anyhow, bail, Context}; | |||
| 5 | use niri_config::OutputName; | 5 | use niri_config::OutputName; |
| 6 | use niri_ipc::socket::Socket; | 6 | use niri_ipc::socket::Socket; |
| 7 | use niri_ipc::{ | 7 | use niri_ipc::{ |
| 8 | - Event, KeyboardLayouts, LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, | 8 | + Event, KeyboardLayouts, LogicalOutput, Mode, Output, OutputConfigChanged, Overview, Request, |
| 9 | - Transform, Window, | 9 | + Response, Transform, Window, |
| 10 | }; | 10 | }; |
| 11 | use serde_json::json; | 11 | use serde_json::json; |
| 12 | 12 | ||
| @@ -32,6 +32,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 32 | Msg::KeyboardLayouts => Request::KeyboardLayouts, | 32 | Msg::KeyboardLayouts => Request::KeyboardLayouts, |
| 33 | Msg::EventStream => Request::EventStream, | 33 | Msg::EventStream => Request::EventStream, |
| 34 | Msg::RequestError => Request::ReturnError, | 34 | Msg::RequestError => Request::ReturnError, |
| 35 | + Msg::OverviewState => Request::OverviewState, | ||
| 35 | }; | 36 | }; |
| 36 | 37 | ||
| 37 | let socket = Socket::connect().context("error connecting to the niri socket")?; | 38 | let socket = Socket::connect().context("error connecting to the niri socket")?; |
| @@ -435,9 +436,31 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 435 | Event::KeyboardLayoutSwitched { idx } => { | 436 | Event::KeyboardLayoutSwitched { idx } => { |
| 436 | println!("Keyboard layout switched: {idx}"); | 437 | println!("Keyboard layout switched: {idx}"); |
| 437 | } | 438 | } |
| 439 | + Event::OverviewOpenedOrClosed { is_open: opened } => { | ||
| 440 | + println!("Overview toggled: {opened}"); | ||
| 441 | + } | ||
| 438 | } | 442 | } |
| 439 | } | 443 | } |
| 440 | } | 444 | } |
| 445 | + Msg::OverviewState => { | ||
| 446 | + let Response::OverviewState(response) = response else { | ||
| 447 | + bail!("unexpected response: expected Overview, got {response:?}"); | ||
| 448 | + }; | ||
| 449 | + | ||
| 450 | + if json { | ||
| 451 | + let response = | ||
| 452 | + serde_json::to_string(&response).context("error formatting response")?; | ||
| 453 | + println!("{response}"); | ||
| 454 | + return Ok(()); | ||
| 455 | + } | ||
| 456 | + | ||
| 457 | + let Overview { is_open } = response; | ||
| 458 | + if is_open { | ||
| 459 | + println!("Overview is open."); | ||
| 460 | + } else { | ||
| 461 | + println!("Overview is closed."); | ||
| 462 | + } | ||
| 463 | + } | ||
| 441 | } | 464 | } |
| 442 | 465 | ||
| 443 | Ok(()) | 466 | Ok(()) |
src/ipc/server.rsmodified+27 -1
| @@ -16,7 +16,9 @@ use futures_util::io::{AsyncReadExt, BufReader}; | |||
| 16 | use futures_util::{select_biased, AsyncBufReadExt, AsyncWrite, AsyncWriteExt, FutureExt as _}; | 16 | use futures_util::{select_biased, AsyncBufReadExt, AsyncWrite, AsyncWriteExt, FutureExt as _}; |
| 17 | use niri_config::OutputName; | 17 | use niri_config::OutputName; |
| 18 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; | 18 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; |
| 19 | -use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace}; | 19 | +use niri_ipc::{ |
| 20 | + Event, KeyboardLayouts, OutputConfigChanged, Overview, Reply, Request, Response, Workspace, | ||
| 21 | +}; | ||
| 20 | use smithay::desktop::layer_map_for_output; | 22 | use smithay::desktop::layer_map_for_output; |
| 21 | use smithay::input::pointer::{ | 23 | use smithay::input::pointer::{ |
| 22 | CursorIcon, CursorImageStatus, Focus, GrabStartData as PointerGrabStartData, | 24 | CursorIcon, CursorImageStatus, Focus, GrabStartData as PointerGrabStartData, |
| @@ -428,6 +430,11 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 428 | Response::FocusedOutput(output) | 430 | Response::FocusedOutput(output) |
| 429 | } | 431 | } |
| 430 | Request::EventStream => Response::Handled, | 432 | Request::EventStream => Response::Handled, |
| 433 | + Request::OverviewState => { | ||
| 434 | + let state = ctx.event_stream_state.borrow(); | ||
| 435 | + let is_open = state.overview.is_open; | ||
| 436 | + Response::OverviewState(Overview { is_open }) | ||
| 437 | + } | ||
| 431 | }; | 438 | }; |
| 432 | 439 | ||
| 433 | Ok(response) | 440 | Ok(response) |
| @@ -524,6 +531,7 @@ impl State { | |||
| 524 | pub fn ipc_refresh_layout(&mut self) { | 531 | pub fn ipc_refresh_layout(&mut self) { |
| 525 | self.ipc_refresh_workspaces(); | 532 | self.ipc_refresh_workspaces(); |
| 526 | self.ipc_refresh_windows(); | 533 | self.ipc_refresh_windows(); |
| 534 | + self.ipc_refresh_overview(); | ||
| 527 | } | 535 | } |
| 528 | 536 | ||
| 529 | fn ipc_refresh_workspaces(&mut self) { | 537 | fn ipc_refresh_workspaces(&mut self) { |
| @@ -690,4 +698,22 @@ impl State { | |||
| 690 | server.send_event(event); | 698 | server.send_event(event); |
| 691 | } | 699 | } |
| 692 | } | 700 | } |
| 701 | + | ||
| 702 | + pub fn ipc_refresh_overview(&mut self) { | ||
| 703 | + let Some(server) = &self.niri.ipc_server else { | ||
| 704 | + return; | ||
| 705 | + }; | ||
| 706 | + | ||
| 707 | + let mut state = server.event_stream_state.borrow_mut(); | ||
| 708 | + let state = &mut state.overview; | ||
| 709 | + let is_open = self.niri.layout.is_overview_open(); | ||
| 710 | + | ||
| 711 | + if state.is_open == is_open { | ||
| 712 | + return; | ||
| 713 | + } | ||
| 714 | + | ||
| 715 | + let event = Event::OverviewOpenedOrClosed { is_open }; | ||
| 716 | + state.apply(event.clone()); | ||
| 717 | + server.send_event(event); | ||
| 718 | + } | ||
| 693 | } | 719 | } |