Implement niri msg workspaces
Browse files at this commit36d3e70f11bde96eb67d157b22ebcdf4767af0c2Parent: a2f74c96 changed files +122 -0
- niri-ipc/src/lib.rs +21 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +49 -0
- src/ipc/server.rs +10 -0
- src/layout/mod.rs +35 -0
- src/niri.rs +5 -0
niri-ipc/src/lib.rsmodified+21 -0
| @@ -31,6 +31,8 @@ pub enum Request { | |||
| 31 | /// Configuration to apply. | 31 | /// Configuration to apply. |
| 32 | action: OutputAction, | 32 | action: OutputAction, |
| 33 | }, | 33 | }, |
| 34 | + /// Request information about workspaces. | ||
| 35 | + Workspaces, | ||
| 34 | /// Respond with an error (for testing error handling). | 36 | /// Respond with an error (for testing error handling). |
| 35 | ReturnError, | 37 | ReturnError, |
| 36 | } | 38 | } |
| @@ -60,6 +62,8 @@ pub enum Response { | |||
| 60 | FocusedWindow(Option<Window>), | 62 | FocusedWindow(Option<Window>), |
| 61 | /// Output configuration change result. | 63 | /// Output configuration change result. |
| 62 | OutputConfigChanged(OutputConfigChanged), | 64 | OutputConfigChanged(OutputConfigChanged), |
| 65 | + /// Information about workspaces. | ||
| 66 | + Workspaces(Vec<Workspace>), | ||
| 63 | } | 67 | } |
| 64 | 68 | ||
| 65 | /// Actions that niri can perform. | 69 | /// Actions that niri can perform. |
| @@ -484,6 +488,23 @@ pub enum OutputConfigChanged { | |||
| 484 | OutputWasMissing, | 488 | OutputWasMissing, |
| 485 | } | 489 | } |
| 486 | 490 | ||
| 491 | +/// A workspace. | ||
| 492 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
| 493 | +pub struct Workspace { | ||
| 494 | + /// Index of the workspace on its monitor. | ||
| 495 | + /// | ||
| 496 | + /// This is the same index you can use for requests like `niri msg action focus-workspace`. | ||
| 497 | + pub idx: u8, | ||
| 498 | + /// Optional name of the workspace. | ||
| 499 | + pub name: Option<String>, | ||
| 500 | + /// Name of the output that the workspace is on. | ||
| 501 | + /// | ||
| 502 | + /// Can be `None` if no outputs are currently connected. | ||
| 503 | + pub output: Option<String>, | ||
| 504 | + /// Whether the workspace is currently active on its output. | ||
| 505 | + pub is_active: bool, | ||
| 506 | +} | ||
| 507 | + | ||
| 487 | impl FromStr for WorkspaceReferenceArg { | 508 | impl FromStr for WorkspaceReferenceArg { |
| 488 | type Err = &'static str; | 509 | type Err = &'static str; |
| 489 | 510 | ||
src/cli.rsmodified+2 -0
| @@ -78,6 +78,8 @@ pub enum Msg { | |||
| 78 | #[command(subcommand)] | 78 | #[command(subcommand)] |
| 79 | action: OutputAction, | 79 | action: OutputAction, |
| 80 | }, | 80 | }, |
| 81 | + /// List workspaces. | ||
| 82 | + Workspaces, | ||
| 81 | /// Request an error from the running niri instance. | 83 | /// Request an error from the running niri instance. |
| 82 | RequestError, | 84 | RequestError, |
| 83 | } | 85 | } |
src/ipc/client.rsmodified+49 -0
| @@ -17,6 +17,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 17 | output: output.clone(), | 17 | output: output.clone(), |
| 18 | action: action.clone(), | 18 | action: action.clone(), |
| 19 | }, | 19 | }, |
| 20 | + Msg::Workspaces => Request::Workspaces, | ||
| 20 | Msg::RequestError => Request::ReturnError, | 21 | Msg::RequestError => Request::ReturnError, |
| 21 | }; | 22 | }; |
| 22 | 23 | ||
| @@ -260,6 +261,54 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 260 | println!("The change will apply when it is connected."); | 261 | println!("The change will apply when it is connected."); |
| 261 | } | 262 | } |
| 262 | } | 263 | } |
| 264 | + Msg::Workspaces => { | ||
| 265 | + let Response::Workspaces(mut response) = response else { | ||
| 266 | + bail!("unexpected response: expected Workspaces, got {response:?}"); | ||
| 267 | + }; | ||
| 268 | + | ||
| 269 | + if json { | ||
| 270 | + let response = | ||
| 271 | + serde_json::to_string(&response).context("error formatting response")?; | ||
| 272 | + println!("{response}"); | ||
| 273 | + return Ok(()); | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + if response.is_empty() { | ||
| 277 | + println!("No workspaces."); | ||
| 278 | + return Ok(()); | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + response.sort_by_key(|ws| ws.idx); | ||
| 282 | + response.sort_by(|a, b| a.output.cmp(&b.output)); | ||
| 283 | + | ||
| 284 | + let mut current_output = if let Some(output) = response[0].output.as_deref() { | ||
| 285 | + println!("Output \"{output}\":"); | ||
| 286 | + Some(output) | ||
| 287 | + } else { | ||
| 288 | + println!("No output:"); | ||
| 289 | + None | ||
| 290 | + }; | ||
| 291 | + | ||
| 292 | + for ws in &response { | ||
| 293 | + if ws.output.as_deref() != current_output { | ||
| 294 | + let output = ws.output.as_deref().context( | ||
| 295 | + "invalid response: workspace with no output \ | ||
| 296 | + following a workspace with an output", | ||
| 297 | + )?; | ||
| 298 | + current_output = Some(output); | ||
| 299 | + println!("\nOutput \"{output}\":"); | ||
| 300 | + } | ||
| 301 | + | ||
| 302 | + let is_active = if ws.is_active { " * " } else { " " }; | ||
| 303 | + let idx = ws.idx; | ||
| 304 | + let name = if let Some(name) = ws.name.as_deref() { | ||
| 305 | + format!(" \"{name}\"") | ||
| 306 | + } else { | ||
| 307 | + String::new() | ||
| 308 | + }; | ||
| 309 | + println!("{is_active}{idx}{name}"); | ||
| 310 | + } | ||
| 311 | + } | ||
| 263 | } | 312 | } |
| 264 | 313 | ||
| 265 | Ok(()) | 314 | Ok(()) |
src/ipc/server.rsmodified+10 -0
| @@ -198,6 +198,16 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 198 | 198 | ||
| 199 | Response::OutputConfigChanged(response) | 199 | Response::OutputConfigChanged(response) |
| 200 | } | 200 | } |
| 201 | + Request::Workspaces => { | ||
| 202 | + let (tx, rx) = async_channel::bounded(1); | ||
| 203 | + ctx.event_loop.insert_idle(move |state| { | ||
| 204 | + let workspaces = state.niri.layout.ipc_workspaces(); | ||
| 205 | + let _ = tx.send_blocking(workspaces); | ||
| 206 | + }); | ||
| 207 | + let result = rx.recv().await; | ||
| 208 | + let workspaces = result.map_err(|_| String::from("error getting workspace info"))?; | ||
| 209 | + Response::Workspaces(workspaces) | ||
| 210 | + } | ||
| 201 | }; | 211 | }; |
| 202 | 212 | ||
| 203 | Ok(response) | 213 | Ok(response) |
src/layout/mod.rsmodified+35 -0
| @@ -2278,6 +2278,41 @@ impl<W: LayoutElement> Layout<W> { | |||
| 2278 | } | 2278 | } |
| 2279 | } | 2279 | } |
| 2280 | } | 2280 | } |
| 2281 | + | ||
| 2282 | + pub fn ipc_workspaces(&self) -> Vec<niri_ipc::Workspace> { | ||
| 2283 | + match &self.monitor_set { | ||
| 2284 | + MonitorSet::Normal { | ||
| 2285 | + monitors, | ||
| 2286 | + primary_idx: _, | ||
| 2287 | + active_monitor_idx: _, | ||
| 2288 | + } => { | ||
| 2289 | + let mut workspaces = Vec::new(); | ||
| 2290 | + | ||
| 2291 | + for monitor in monitors { | ||
| 2292 | + for (idx, workspace) in monitor.workspaces.iter().enumerate() { | ||
| 2293 | + workspaces.push(niri_ipc::Workspace { | ||
| 2294 | + idx: u8::try_from(idx + 1).unwrap_or(u8::MAX), | ||
| 2295 | + name: workspace.name.clone(), | ||
| 2296 | + output: Some(monitor.output.name()), | ||
| 2297 | + is_active: monitor.active_workspace_idx == idx, | ||
| 2298 | + }) | ||
| 2299 | + } | ||
| 2300 | + } | ||
| 2301 | + | ||
| 2302 | + workspaces | ||
| 2303 | + } | ||
| 2304 | + MonitorSet::NoOutputs { workspaces } => workspaces | ||
| 2305 | + .iter() | ||
| 2306 | + .enumerate() | ||
| 2307 | + .map(|(idx, ws)| niri_ipc::Workspace { | ||
| 2308 | + idx: u8::try_from(idx + 1).unwrap_or(u8::MAX), | ||
| 2309 | + name: ws.name.clone(), | ||
| 2310 | + output: None, | ||
| 2311 | + is_active: false, | ||
| 2312 | + }) | ||
| 2313 | + .collect(), | ||
| 2314 | + } | ||
| 2315 | + } | ||
| 2281 | } | 2316 | } |
| 2282 | 2317 | ||
| 2283 | impl<W: LayoutElement> Default for MonitorSet<W> { | 2318 | impl<W: LayoutElement> Default for MonitorSet<W> { |
src/niri.rsmodified+5 -0
| @@ -12,6 +12,7 @@ use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as | |||
| 12 | use anyhow::{ensure, Context}; | 12 | use anyhow::{ensure, Context}; |
| 13 | use calloop::futures::Scheduler; | 13 | use calloop::futures::Scheduler; |
| 14 | use niri_config::{Config, Key, Modifiers, PreviewRender, TrackLayout, WorkspaceReference}; | 14 | use niri_config::{Config, Key, Modifiers, PreviewRender, TrackLayout, WorkspaceReference}; |
| 15 | +use niri_ipc::Workspace; | ||
| 15 | use smithay::backend::allocator::Fourcc; | 16 | use smithay::backend::allocator::Fourcc; |
| 16 | use smithay::backend::renderer::damage::OutputDamageTracker; | 17 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 17 | use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement; | 18 | use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement; |
| @@ -3934,6 +3935,10 @@ impl Niri { | |||
| 3934 | self.queue_redraw_all(); | 3935 | self.queue_redraw_all(); |
| 3935 | } | 3936 | } |
| 3936 | } | 3937 | } |
| 3938 | + | ||
| 3939 | + pub fn ipc_workspaces(&self) -> Vec<Workspace> { | ||
| 3940 | + self.layout.ipc_workspaces() | ||
| 3941 | + } | ||
| 3937 | } | 3942 | } |
| 3938 | 3943 | ||
| 3939 | pub struct ClientState { | 3944 | pub struct ClientState { |