Add niri msg layers
Browse files at this commit0e5e764c78421dc61c82ca6df630f33e8221005aParent: db1faec4 changed files +156 -0
- niri-ipc/src/lib.rs +44 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +67 -0
- src/ipc/server.rs +43 -0
niri-ipc/src/lib.rsmodified+44 -0
| @@ -55,6 +55,8 @@ pub enum Request { | |||
| 55 | Workspaces, | 55 | Workspaces, |
| 56 | /// Request information about open windows. | 56 | /// Request information about open windows. |
| 57 | Windows, | 57 | Windows, |
| 58 | + /// Request information about layer-shell surfaces. | ||
| 59 | + Layers, | ||
| 58 | /// Request information about the configured keyboard layouts. | 60 | /// Request information about the configured keyboard layouts. |
| 59 | KeyboardLayouts, | 61 | KeyboardLayouts, |
| 60 | /// Request information about the focused output. | 62 | /// Request information about the focused output. |
| @@ -119,6 +121,8 @@ pub enum Response { | |||
| 119 | Workspaces(Vec<Workspace>), | 121 | Workspaces(Vec<Workspace>), |
| 120 | /// Information about open windows. | 122 | /// Information about open windows. |
| 121 | Windows(Vec<Window>), | 123 | Windows(Vec<Window>), |
| 124 | + /// Information about layer-shell surfaces. | ||
| 125 | + Layers(Vec<LayerSurface>), | ||
| 122 | /// Information about the keyboard layout. | 126 | /// Information about the keyboard layout. |
| 123 | KeyboardLayouts(KeyboardLayouts), | 127 | KeyboardLayouts(KeyboardLayouts), |
| 124 | /// Information about the focused output. | 128 | /// Information about the focused output. |
| @@ -767,6 +771,46 @@ pub struct KeyboardLayouts { | |||
| 767 | pub current_idx: u8, | 771 | pub current_idx: u8, |
| 768 | } | 772 | } |
| 769 | 773 | ||
| 774 | +/// A layer-shell layer. | ||
| 775 | +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
| 776 | +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] | ||
| 777 | +pub enum Layer { | ||
| 778 | + /// The background layer. | ||
| 779 | + Background, | ||
| 780 | + /// The bottom layer. | ||
| 781 | + Bottom, | ||
| 782 | + /// The top layer. | ||
| 783 | + Top, | ||
| 784 | + /// The overlay layer. | ||
| 785 | + Overlay, | ||
| 786 | +} | ||
| 787 | + | ||
| 788 | +/// Keyboard interactivity modes for a layer-shell surface. | ||
| 789 | +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] | ||
| 790 | +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] | ||
| 791 | +pub enum LayerSurfaceKeyboardInteractivity { | ||
| 792 | + /// Surface cannot receive keyboard focus. | ||
| 793 | + None, | ||
| 794 | + /// Surface receives keyboard focus whenever possible. | ||
| 795 | + Exclusive, | ||
| 796 | + /// Surface receives keyboard focus on demand, e.g. when clicked. | ||
| 797 | + OnDemand, | ||
| 798 | +} | ||
| 799 | + | ||
| 800 | +/// A layer-shell surface. | ||
| 801 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
| 802 | +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] | ||
| 803 | +pub struct LayerSurface { | ||
| 804 | + /// Namespace provided by the layer-shell client. | ||
| 805 | + pub namespace: String, | ||
| 806 | + /// Name of the output the surface is on. | ||
| 807 | + pub output: String, | ||
| 808 | + /// Layer that the surface is on. | ||
| 809 | + pub layer: Layer, | ||
| 810 | + /// The surface's keyboard interactivity mode. | ||
| 811 | + pub keyboard_interactivity: LayerSurfaceKeyboardInteractivity, | ||
| 812 | +} | ||
| 813 | + | ||
| 770 | /// A compositor event. | 814 | /// A compositor event. |
| 771 | #[derive(Serialize, Deserialize, Debug, Clone)] | 815 | #[derive(Serialize, Deserialize, Debug, Clone)] |
| 772 | #[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] | 816 | #[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] |
src/cli.rsmodified+2 -0
| @@ -64,6 +64,8 @@ pub enum Msg { | |||
| 64 | Workspaces, | 64 | Workspaces, |
| 65 | /// List open windows. | 65 | /// List open windows. |
| 66 | Windows, | 66 | Windows, |
| 67 | + /// List open layer-shell surfaces. | ||
| 68 | + Layers, | ||
| 67 | /// Get the configured keyboard layouts. | 69 | /// Get the configured keyboard layouts. |
| 68 | KeyboardLayouts, | 70 | KeyboardLayouts, |
| 69 | /// Print information about the focused output. | 71 | /// Print information about the focused output. |
src/ipc/client.rsmodified+67 -0
| @@ -1,3 +1,6 @@ | |||
| 1 | +use std::iter::Peekable; | ||
| 2 | +use std::slice; | ||
| 3 | + | ||
| 1 | use anyhow::{anyhow, bail, Context}; | 4 | use anyhow::{anyhow, bail, Context}; |
| 2 | use niri_config::OutputName; | 5 | use niri_config::OutputName; |
| 3 | use niri_ipc::socket::Socket; | 6 | use niri_ipc::socket::Socket; |
| @@ -23,6 +26,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 23 | }, | 26 | }, |
| 24 | Msg::Workspaces => Request::Workspaces, | 27 | Msg::Workspaces => Request::Workspaces, |
| 25 | Msg::Windows => Request::Windows, | 28 | Msg::Windows => Request::Windows, |
| 29 | + Msg::Layers => Request::Layers, | ||
| 26 | Msg::KeyboardLayouts => Request::KeyboardLayouts, | 30 | Msg::KeyboardLayouts => Request::KeyboardLayouts, |
| 27 | Msg::EventStream => Request::EventStream, | 31 | Msg::EventStream => Request::EventStream, |
| 28 | Msg::RequestError => Request::ReturnError, | 32 | Msg::RequestError => Request::ReturnError, |
| @@ -168,6 +172,69 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 168 | println!(); | 172 | println!(); |
| 169 | } | 173 | } |
| 170 | } | 174 | } |
| 175 | + Msg::Layers => { | ||
| 176 | + let Response::Layers(mut layers) = response else { | ||
| 177 | + bail!("unexpected response: expected Layers, got {response:?}"); | ||
| 178 | + }; | ||
| 179 | + | ||
| 180 | + if json { | ||
| 181 | + let layers = serde_json::to_string(&layers).context("error formatting response")?; | ||
| 182 | + println!("{layers}"); | ||
| 183 | + return Ok(()); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + layers.sort_by(|a, b| { | ||
| 187 | + Ord::cmp(&a.output, &b.output) | ||
| 188 | + .then_with(|| Ord::cmp(&a.layer, &b.layer)) | ||
| 189 | + .then_with(|| Ord::cmp(&a.namespace, &b.namespace)) | ||
| 190 | + }); | ||
| 191 | + let mut iter = layers.iter().peekable(); | ||
| 192 | + | ||
| 193 | + let print = |surface: &niri_ipc::LayerSurface| { | ||
| 194 | + println!(" Surface:"); | ||
| 195 | + println!(" Namespace: \"{}\"", &surface.namespace); | ||
| 196 | + | ||
| 197 | + let interactivity = match surface.keyboard_interactivity { | ||
| 198 | + niri_ipc::LayerSurfaceKeyboardInteractivity::None => "none", | ||
| 199 | + niri_ipc::LayerSurfaceKeyboardInteractivity::Exclusive => "exclusive", | ||
| 200 | + niri_ipc::LayerSurfaceKeyboardInteractivity::OnDemand => "on-demand", | ||
| 201 | + }; | ||
| 202 | + println!(" Keyboard interactivity: {interactivity}"); | ||
| 203 | + }; | ||
| 204 | + | ||
| 205 | + let print_layer = |iter: &mut Peekable<slice::Iter<niri_ipc::LayerSurface>>, | ||
| 206 | + output: &str, | ||
| 207 | + layer| { | ||
| 208 | + let mut empty = true; | ||
| 209 | + while let Some(surface) = iter.next_if(|s| s.output == output && s.layer == layer) { | ||
| 210 | + empty = false; | ||
| 211 | + println!(); | ||
| 212 | + print(surface); | ||
| 213 | + } | ||
| 214 | + if empty { | ||
| 215 | + println!(" (empty)\n"); | ||
| 216 | + } else { | ||
| 217 | + println!(); | ||
| 218 | + } | ||
| 219 | + }; | ||
| 220 | + | ||
| 221 | + while let Some(surface) = iter.peek() { | ||
| 222 | + let output = &surface.output; | ||
| 223 | + println!("Output \"{output}\":"); | ||
| 224 | + | ||
| 225 | + print!(" Background layer:"); | ||
| 226 | + print_layer(&mut iter, output, niri_ipc::Layer::Background); | ||
| 227 | + | ||
| 228 | + print!(" Bottom layer:"); | ||
| 229 | + print_layer(&mut iter, output, niri_ipc::Layer::Bottom); | ||
| 230 | + | ||
| 231 | + print!(" Top layer:"); | ||
| 232 | + print_layer(&mut iter, output, niri_ipc::Layer::Top); | ||
| 233 | + | ||
| 234 | + print!(" Overlay layer:"); | ||
| 235 | + print_layer(&mut iter, output, niri_ipc::Layer::Overlay); | ||
| 236 | + } | ||
| 237 | + } | ||
| 171 | Msg::FocusedOutput => { | 238 | Msg::FocusedOutput => { |
| 172 | let Response::FocusedOutput(output) = response else { | 239 | let Response::FocusedOutput(output) = response else { |
| 173 | bail!("unexpected response: expected FocusedOutput, got {response:?}"); | 240 | bail!("unexpected response: expected FocusedOutput, got {response:?}"); |
src/ipc/server.rsmodified+43 -0
| @@ -16,9 +16,11 @@ use futures_util::{select_biased, AsyncBufReadExt, AsyncWrite, AsyncWriteExt, Fu | |||
| 16 | use niri_config::OutputName; | 16 | use niri_config::OutputName; |
| 17 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; | 17 | use niri_ipc::state::{EventStreamState, EventStreamStatePart as _}; |
| 18 | use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace}; | 18 | use niri_ipc::{Event, KeyboardLayouts, OutputConfigChanged, Reply, Request, Response, Workspace}; |
| 19 | +use smithay::desktop::layer_map_for_output; | ||
| 19 | use smithay::reexports::calloop::generic::Generic; | 20 | use smithay::reexports::calloop::generic::Generic; |
| 20 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; | 21 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 21 | use smithay::reexports::rustix::fs::unlink; | 22 | use smithay::reexports::rustix::fs::unlink; |
| 23 | +use smithay::wayland::shell::wlr_layer::{KeyboardInteractivity, Layer}; | ||
| 22 | 24 | ||
| 23 | use crate::backend::IpcOutputMap; | 25 | use crate::backend::IpcOutputMap; |
| 24 | use crate::layout::workspace::WorkspaceId; | 26 | use crate::layout::workspace::WorkspaceId; |
| @@ -254,6 +256,47 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 254 | let windows = state.windows.windows.values().cloned().collect(); | 256 | let windows = state.windows.windows.values().cloned().collect(); |
| 255 | Response::Windows(windows) | 257 | Response::Windows(windows) |
| 256 | } | 258 | } |
| 259 | + Request::Layers => { | ||
| 260 | + let (tx, rx) = async_channel::bounded(1); | ||
| 261 | + ctx.event_loop.insert_idle(move |state| { | ||
| 262 | + let mut layers = Vec::new(); | ||
| 263 | + for output in state.niri.global_space.outputs() { | ||
| 264 | + let name = output.name(); | ||
| 265 | + for surface in layer_map_for_output(output).layers() { | ||
| 266 | + let layer = match surface.layer() { | ||
| 267 | + Layer::Background => niri_ipc::Layer::Background, | ||
| 268 | + Layer::Bottom => niri_ipc::Layer::Bottom, | ||
| 269 | + Layer::Top => niri_ipc::Layer::Top, | ||
| 270 | + Layer::Overlay => niri_ipc::Layer::Overlay, | ||
| 271 | + }; | ||
| 272 | + let keyboard_interactivity = | ||
| 273 | + match surface.cached_state().keyboard_interactivity { | ||
| 274 | + KeyboardInteractivity::None => { | ||
| 275 | + niri_ipc::LayerSurfaceKeyboardInteractivity::None | ||
| 276 | + } | ||
| 277 | + KeyboardInteractivity::Exclusive => { | ||
| 278 | + niri_ipc::LayerSurfaceKeyboardInteractivity::Exclusive | ||
| 279 | + } | ||
| 280 | + KeyboardInteractivity::OnDemand => { | ||
| 281 | + niri_ipc::LayerSurfaceKeyboardInteractivity::OnDemand | ||
| 282 | + } | ||
| 283 | + }; | ||
| 284 | + | ||
| 285 | + layers.push(niri_ipc::LayerSurface { | ||
| 286 | + namespace: surface.namespace().to_owned(), | ||
| 287 | + output: name.clone(), | ||
| 288 | + layer, | ||
| 289 | + keyboard_interactivity, | ||
| 290 | + }); | ||
| 291 | + } | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + let _ = tx.send_blocking(layers); | ||
| 295 | + }); | ||
| 296 | + let result = rx.recv().await; | ||
| 297 | + let layers = result.map_err(|_| String::from("error getting layers info"))?; | ||
| 298 | + Response::Layers(layers) | ||
| 299 | + } | ||
| 257 | Request::KeyboardLayouts => { | 300 | Request::KeyboardLayouts => { |
| 258 | let state = ctx.event_stream_state.borrow(); | 301 | let state = ctx.event_stream_state.borrow(); |
| 259 | let layout = state.keyboard_layouts.keyboard_layouts.clone(); | 302 | let layout = state.keyboard_layouts.keyboard_layouts.clone(); |