forge
Explore
Log in

Don't you dare go Hollow

Implement niri msg workspaces

Browse files at this commit

36d3e70f11bde96eb67d157b22ebcdf4767af0c2rustysecParent: a2f74c9
niri-ipc/src/lib.rsmodified+21 -0
@@ -31,6 +31,8 @@ pub enum Request {
3131 /// Configuration to apply.
3232 action: OutputAction,
3333 },
34+ /// Request information about workspaces.
35+ Workspaces,
3436 /// Respond with an error (for testing error handling).
3537 ReturnError,
3638 }
@@ -60,6 +62,8 @@ pub enum Response {
6062 FocusedWindow(Option<Window>),
6163 /// Output configuration change result.
6264 OutputConfigChanged(OutputConfigChanged),
65+ /// Information about workspaces.
66+ Workspaces(Vec<Workspace>),
6367 }
6468
6569 /// Actions that niri can perform.
@@ -484,6 +488,23 @@ pub enum OutputConfigChanged {
484488 OutputWasMissing,
485489 }
486490
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+
487508 impl FromStr for WorkspaceReferenceArg {
488509 type Err = &'static str;
489510
src/cli.rsmodified+2 -0
@@ -78,6 +78,8 @@ pub enum Msg {
7878 #[command(subcommand)]
7979 action: OutputAction,
8080 },
81+ /// List workspaces.
82+ Workspaces,
8183 /// Request an error from the running niri instance.
8284 RequestError,
8385 }
src/ipc/client.rsmodified+49 -0
@@ -17,6 +17,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
1717 output: output.clone(),
1818 action: action.clone(),
1919 },
20+ Msg::Workspaces => Request::Workspaces,
2021 Msg::RequestError => Request::ReturnError,
2122 };
2223
@@ -260,6 +261,54 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
260261 println!("The change will apply when it is connected.");
261262 }
262263 }
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+ }
263312 }
264313
265314 Ok(())
src/ipc/server.rsmodified+10 -0
@@ -198,6 +198,16 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply {
198198
199199 Response::OutputConfigChanged(response)
200200 }
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+ }
201211 };
202212
203213 Ok(response)
src/layout/mod.rsmodified+35 -0
@@ -2278,6 +2278,41 @@ impl<W: LayoutElement> Layout<W> {
22782278 }
22792279 }
22802280 }
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+ }
22812316 }
22822317
22832318 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
1212 use anyhow::{ensure, Context};
1313 use calloop::futures::Scheduler;
1414 use niri_config::{Config, Key, Modifiers, PreviewRender, TrackLayout, WorkspaceReference};
15+use niri_ipc::Workspace;
1516 use smithay::backend::allocator::Fourcc;
1617 use smithay::backend::renderer::damage::OutputDamageTracker;
1718 use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement;
@@ -3934,6 +3935,10 @@ impl Niri {
39343935 self.queue_redraw_all();
39353936 }
39363937 }
3938+
3939+ pub fn ipc_workspaces(&self) -> Vec<Workspace> {
3940+ self.layout.ipc_workspaces()
3941+ }
39373942 }
39383943
39393944 pub struct ClientState {