niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·dde9214·Ivan Molodetskikh·1 year ago·1835 commits
| 1 | use std::iter::Peekable; |
| 2 | use std::slice; |
| 3 | |
| 4 | use anyhow::{anyhow, bail, Context}; |
| 5 | use niri_config::OutputName; |
| 6 | use niri_ipc::socket::Socket; |
| 7 | use niri_ipc::{ |
| 8 | Event, KeyboardLayouts, LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, |
| 9 | Transform, Window, |
| 10 | }; |
| 11 | use serde_json::json; |
| 12 | |
| 13 | use crate::cli::Msg; |
| 14 | use crate::utils::version; |
| 15 | |
| 16 | pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { |
| 17 | let request = match &msg { |
| 18 | Msg::Version => Request::Version, |
| 19 | Msg::Outputs => Request::Outputs, |
| 20 | Msg::FocusedWindow => Request::FocusedWindow, |
| 21 | Msg::FocusedOutput => Request::FocusedOutput, |
| 22 | Msg::Action { action } => Request::Action(action.clone()), |
| 23 | Msg::Output { output, action } => Request::Output { |
| 24 | output: output.clone(), |
| 25 | action: action.clone(), |
| 26 | }, |
| 27 | Msg::Workspaces => Request::Workspaces, |
| 28 | Msg::Windows => Request::Windows, |
| 29 | Msg::Layers => Request::Layers, |
| 30 | Msg::KeyboardLayouts => Request::KeyboardLayouts, |
| 31 | Msg::EventStream => Request::EventStream, |
| 32 | Msg::RequestError => Request::ReturnError, |
| 33 | }; |
| 34 | |
| 35 | let socket = Socket::connect().context("error connecting to the niri socket")?; |
| 36 | |
| 37 | let (reply, mut read_event) = socket |
| 38 | .send(request) |
| 39 | .context("error communicating with niri")?; |
| 40 | |
| 41 | let compositor_version = match reply { |
| 42 | Err(_) if !matches!(msg, Msg::Version) => { |
| 43 | // If we got an error, it might be that the CLI is a different version from the running |
| 44 | // niri instance. Request the running instance version to compare and print a message. |
| 45 | Socket::connect() |
| 46 | .and_then(|socket| socket.send(Request::Version)) |
| 47 | .ok() |
| 48 | .map(|(reply, _read_event)| reply) |
| 49 | } |
| 50 | _ => None, |
| 51 | }; |
| 52 | |
| 53 | // Default SIGPIPE so that our prints don't panic on stdout closing. |
| 54 | unsafe { |
| 55 | libc::signal(libc::SIGPIPE, libc::SIG_DFL); |
| 56 | } |
| 57 | |
| 58 | let response = reply.map_err(|err_msg| { |
| 59 | // Check for CLI-server version mismatch to add helpful context. |
| 60 | match compositor_version { |
| 61 | Some(Ok(Response::Version(compositor_version))) => { |
| 62 | let cli_version = version(); |
| 63 | if cli_version != compositor_version { |
| 64 | eprintln!("Running niri compositor has a different version from the niri CLI:"); |
| 65 | eprintln!("Compositor version: {compositor_version}"); |
| 66 | eprintln!("CLI version: {cli_version}"); |
| 67 | eprintln!("Did you forget to restart niri after an update?"); |
| 68 | eprintln!(); |
| 69 | } |
| 70 | } |
| 71 | Some(_) => { |
| 72 | eprintln!("Unable to get the running niri compositor version."); |
| 73 | eprintln!("Did you forget to restart niri after an update?"); |
| 74 | eprintln!(); |
| 75 | } |
| 76 | None => { |
| 77 | // Communication error, or the original request was already a version request. |
| 78 | // Don't add irrelevant context. |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | anyhow!(err_msg).context("niri returned an error") |
| 83 | })?; |
| 84 | |
| 85 | match msg { |
| 86 | Msg::RequestError => { |
| 87 | bail!("unexpected response: expected an error, got {response:?}"); |
| 88 | } |
| 89 | Msg::Version => { |
| 90 | let Response::Version(compositor_version) = response else { |
| 91 | bail!("unexpected response: expected Version, got {response:?}"); |
| 92 | }; |
| 93 | |
| 94 | let cli_version = version(); |
| 95 | |
| 96 | if json { |
| 97 | println!( |
| 98 | "{}", |
| 99 | json!({ |
| 100 | "compositor": compositor_version, |
| 101 | "cli": cli_version, |
| 102 | }) |
| 103 | ); |
| 104 | return Ok(()); |
| 105 | } |
| 106 | |
| 107 | if cli_version != compositor_version { |
| 108 | eprintln!("Running niri compositor has a different version from the niri CLI."); |
| 109 | eprintln!("Did you forget to restart niri after an update?"); |
| 110 | eprintln!(); |
| 111 | } |
| 112 | |
| 113 | println!("Compositor version: {compositor_version}"); |
| 114 | println!("CLI version: {cli_version}"); |
| 115 | } |
| 116 | Msg::Outputs => { |
| 117 | let Response::Outputs(outputs) = response else { |
| 118 | bail!("unexpected response: expected Outputs, got {response:?}"); |
| 119 | }; |
| 120 | |
| 121 | if json { |
| 122 | let output = |
| 123 | serde_json::to_string(&outputs).context("error formatting response")?; |
| 124 | println!("{output}"); |
| 125 | return Ok(()); |
| 126 | } |
| 127 | |
| 128 | let mut outputs = outputs |
| 129 | .into_values() |
| 130 | .map(|out| (OutputName::from_ipc_output(&out), out)) |
| 131 | .collect::<Vec<_>>(); |
| 132 | outputs.sort_unstable_by(|a, b| a.0.compare(&b.0)); |
| 133 | |
| 134 | for (_name, output) in outputs.into_iter() { |
| 135 | print_output(output)?; |
| 136 | println!(); |
| 137 | } |
| 138 | } |
| 139 | Msg::FocusedWindow => { |
| 140 | let Response::FocusedWindow(window) = response else { |
| 141 | bail!("unexpected response: expected FocusedWindow, got {response:?}"); |
| 142 | }; |
| 143 | |
| 144 | if json { |
| 145 | let window = serde_json::to_string(&window).context("error formatting response")?; |
| 146 | println!("{window}"); |
| 147 | return Ok(()); |
| 148 | } |
| 149 | |
| 150 | if let Some(window) = window { |
| 151 | print_window(&window); |
| 152 | } else { |
| 153 | println!("No window is focused."); |
| 154 | } |
| 155 | } |
| 156 | Msg::Windows => { |
| 157 | let Response::Windows(mut windows) = response else { |
| 158 | bail!("unexpected response: expected Windows, got {response:?}"); |
| 159 | }; |
| 160 | |
| 161 | if json { |
| 162 | let windows = |
| 163 | serde_json::to_string(&windows).context("error formatting response")?; |
| 164 | println!("{windows}"); |
| 165 | return Ok(()); |
| 166 | } |
| 167 | |
| 168 | windows.sort_unstable_by(|a, b| a.id.cmp(&b.id)); |
| 169 | |
| 170 | for window in windows { |
| 171 | print_window(&window); |
| 172 | println!(); |
| 173 | } |
| 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 | } |
| 238 | Msg::FocusedOutput => { |
| 239 | let Response::FocusedOutput(output) = response else { |
| 240 | bail!("unexpected response: expected FocusedOutput, got {response:?}"); |
| 241 | }; |
| 242 | |
| 243 | if json { |
| 244 | let output = serde_json::to_string(&output).context("error formatting response")?; |
| 245 | println!("{output}"); |
| 246 | return Ok(()); |
| 247 | } |
| 248 | |
| 249 | if let Some(output) = output { |
| 250 | print_output(output)?; |
| 251 | } else { |
| 252 | println!("No output is focused."); |
| 253 | } |
| 254 | } |
| 255 | Msg::Action { .. } => { |
| 256 | let Response::Handled = response else { |
| 257 | bail!("unexpected response: expected Handled, got {response:?}"); |
| 258 | }; |
| 259 | } |
| 260 | Msg::Output { output, .. } => { |
| 261 | let Response::OutputConfigChanged(response) = response else { |
| 262 | bail!("unexpected response: expected OutputConfigChanged, got {response:?}"); |
| 263 | }; |
| 264 | |
| 265 | if json { |
| 266 | let response = |
| 267 | serde_json::to_string(&response).context("error formatting response")?; |
| 268 | println!("{response}"); |
| 269 | return Ok(()); |
| 270 | } |
| 271 | |
| 272 | if response == OutputConfigChanged::OutputWasMissing { |
| 273 | println!("Output \"{output}\" is not connected."); |
| 274 | println!("The change will apply when it is connected."); |
| 275 | } |
| 276 | } |
| 277 | Msg::Workspaces => { |
| 278 | let Response::Workspaces(mut response) = response else { |
| 279 | bail!("unexpected response: expected Workspaces, got {response:?}"); |
| 280 | }; |
| 281 | |
| 282 | if json { |
| 283 | let response = |
| 284 | serde_json::to_string(&response).context("error formatting response")?; |
| 285 | println!("{response}"); |
| 286 | return Ok(()); |
| 287 | } |
| 288 | |
| 289 | if response.is_empty() { |
| 290 | println!("No workspaces."); |
| 291 | return Ok(()); |
| 292 | } |
| 293 | |
| 294 | response.sort_by_key(|ws| ws.idx); |
| 295 | response.sort_by(|a, b| a.output.cmp(&b.output)); |
| 296 | |
| 297 | let mut current_output = if let Some(output) = response[0].output.as_deref() { |
| 298 | println!("Output \"{output}\":"); |
| 299 | Some(output) |
| 300 | } else { |
| 301 | println!("No output:"); |
| 302 | None |
| 303 | }; |
| 304 | |
| 305 | for ws in &response { |
| 306 | if ws.output.as_deref() != current_output { |
| 307 | let output = ws.output.as_deref().context( |
| 308 | "invalid response: workspace with no output \ |
| 309 | following a workspace with an output", |
| 310 | )?; |
| 311 | current_output = Some(output); |
| 312 | println!("\nOutput \"{output}\":"); |
| 313 | } |
| 314 | |
| 315 | let is_active = if ws.is_active { " * " } else { " " }; |
| 316 | let idx = ws.idx; |
| 317 | let name = if let Some(name) = ws.name.as_deref() { |
| 318 | format!(" \"{name}\"") |
| 319 | } else { |
| 320 | String::new() |
| 321 | }; |
| 322 | println!("{is_active}{idx}{name}"); |
| 323 | } |
| 324 | } |
| 325 | Msg::KeyboardLayouts => { |
| 326 | let Response::KeyboardLayouts(response) = response else { |
| 327 | bail!("unexpected response: expected KeyboardLayouts, got {response:?}"); |
| 328 | }; |
| 329 | |
| 330 | if json { |
| 331 | let response = |
| 332 | serde_json::to_string(&response).context("error formatting response")?; |
| 333 | println!("{response}"); |
| 334 | return Ok(()); |
| 335 | } |
| 336 | |
| 337 | let KeyboardLayouts { names, current_idx } = response; |
| 338 | let current_idx = usize::from(current_idx); |
| 339 | |
| 340 | println!("Keyboard layouts:"); |
| 341 | for (idx, name) in names.iter().enumerate() { |
| 342 | let is_active = if idx == current_idx { " * " } else { " " }; |
| 343 | println!("{is_active}{idx} {name}"); |
| 344 | } |
| 345 | } |
| 346 | Msg::EventStream => { |
| 347 | let Response::Handled = response else { |
| 348 | bail!("unexpected response: expected Handled, got {response:?}"); |
| 349 | }; |
| 350 | |
| 351 | if !json { |
| 352 | println!("Started reading events."); |
| 353 | } |
| 354 | |
| 355 | loop { |
| 356 | let event = read_event().context("error reading event from niri")?; |
| 357 | |
| 358 | if json { |
| 359 | let event = serde_json::to_string(&event).context("error formatting event")?; |
| 360 | println!("{event}"); |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | match event { |
| 365 | Event::WorkspacesChanged { workspaces } => { |
| 366 | println!("Workspaces changed: {workspaces:?}"); |
| 367 | } |
| 368 | Event::WorkspaceActivated { id, focused } => { |
| 369 | let word = if focused { "focused" } else { "activated" }; |
| 370 | println!("Workspace {word}: {id}"); |
| 371 | } |
| 372 | Event::WorkspaceActiveWindowChanged { |
| 373 | workspace_id, |
| 374 | active_window_id, |
| 375 | } => { |
| 376 | println!( |
| 377 | "Workspace {workspace_id}: \ |
| 378 | active window changed to {active_window_id:?}" |
| 379 | ); |
| 380 | } |
| 381 | Event::WindowsChanged { windows } => { |
| 382 | println!("Windows changed: {windows:?}"); |
| 383 | } |
| 384 | Event::WindowOpenedOrChanged { window } => { |
| 385 | println!("Window opened or changed: {window:?}"); |
| 386 | } |
| 387 | Event::WindowClosed { id } => { |
| 388 | println!("Window closed: {id}"); |
| 389 | } |
| 390 | Event::WindowFocusChanged { id } => { |
| 391 | println!("Window focus changed: {id:?}"); |
| 392 | } |
| 393 | Event::KeyboardLayoutsChanged { keyboard_layouts } => { |
| 394 | println!("Keyboard layouts changed: {keyboard_layouts:?}"); |
| 395 | } |
| 396 | Event::KeyboardLayoutSwitched { idx } => { |
| 397 | println!("Keyboard layout switched: {idx}"); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | Ok(()) |
| 405 | } |
| 406 | |
| 407 | fn print_output(output: Output) -> anyhow::Result<()> { |
| 408 | let Output { |
| 409 | name, |
| 410 | make, |
| 411 | model, |
| 412 | serial, |
| 413 | physical_size, |
| 414 | modes, |
| 415 | current_mode, |
| 416 | vrr_supported, |
| 417 | vrr_enabled, |
| 418 | logical, |
| 419 | } = output; |
| 420 | |
| 421 | let serial = serial.as_deref().unwrap_or("Unknown"); |
| 422 | println!(r#"Output "{make} {model} {serial}" ({name})"#); |
| 423 | |
| 424 | if let Some(current) = current_mode { |
| 425 | let mode = *modes |
| 426 | .get(current) |
| 427 | .context("invalid response: current mode does not exist")?; |
| 428 | let Mode { |
| 429 | width, |
| 430 | height, |
| 431 | refresh_rate, |
| 432 | is_preferred, |
| 433 | } = mode; |
| 434 | let refresh = refresh_rate as f64 / 1000.; |
| 435 | let preferred = if is_preferred { " (preferred)" } else { "" }; |
| 436 | println!(" Current mode: {width}x{height} @ {refresh:.3} Hz{preferred}"); |
| 437 | } else { |
| 438 | println!(" Disabled"); |
| 439 | } |
| 440 | |
| 441 | if vrr_supported { |
| 442 | let enabled = if vrr_enabled { "enabled" } else { "disabled" }; |
| 443 | println!(" Variable refresh rate: supported, {enabled}"); |
| 444 | } else { |
| 445 | println!(" Variable refresh rate: not supported"); |
| 446 | } |
| 447 | |
| 448 | if let Some((width, height)) = physical_size { |
| 449 | println!(" Physical size: {width}x{height} mm"); |
| 450 | } else { |
| 451 | println!(" Physical size: unknown"); |
| 452 | } |
| 453 | |
| 454 | if let Some(logical) = logical { |
| 455 | let LogicalOutput { |
| 456 | x, |
| 457 | y, |
| 458 | width, |
| 459 | height, |
| 460 | scale, |
| 461 | transform, |
| 462 | } = logical; |
| 463 | println!(" Logical position: {x}, {y}"); |
| 464 | println!(" Logical size: {width}x{height}"); |
| 465 | println!(" Scale: {scale}"); |
| 466 | |
| 467 | let transform = match transform { |
| 468 | Transform::Normal => "normal", |
| 469 | Transform::_90 => "90° counter-clockwise", |
| 470 | Transform::_180 => "180°", |
| 471 | Transform::_270 => "270° counter-clockwise", |
| 472 | Transform::Flipped => "flipped horizontally", |
| 473 | Transform::Flipped90 => "90° counter-clockwise, flipped horizontally", |
| 474 | Transform::Flipped180 => "flipped vertically", |
| 475 | Transform::Flipped270 => "270° counter-clockwise, flipped horizontally", |
| 476 | }; |
| 477 | println!(" Transform: {transform}"); |
| 478 | } |
| 479 | |
| 480 | println!(" Available modes:"); |
| 481 | for (idx, mode) in modes.into_iter().enumerate() { |
| 482 | let Mode { |
| 483 | width, |
| 484 | height, |
| 485 | refresh_rate, |
| 486 | is_preferred, |
| 487 | } = mode; |
| 488 | let refresh = refresh_rate as f64 / 1000.; |
| 489 | |
| 490 | let is_current = Some(idx) == current_mode; |
| 491 | let qualifier = match (is_current, is_preferred) { |
| 492 | (true, true) => " (current, preferred)", |
| 493 | (true, false) => " (current)", |
| 494 | (false, true) => " (preferred)", |
| 495 | (false, false) => "", |
| 496 | }; |
| 497 | |
| 498 | println!(" {width}x{height}@{refresh:.3}{qualifier}"); |
| 499 | } |
| 500 | Ok(()) |
| 501 | } |
| 502 | |
| 503 | fn print_window(window: &Window) { |
| 504 | let focused = if window.is_focused { " (focused)" } else { "" }; |
| 505 | println!("Window ID {}:{focused}", window.id); |
| 506 | |
| 507 | if let Some(title) = &window.title { |
| 508 | println!(" Title: \"{title}\""); |
| 509 | } else { |
| 510 | println!(" Title: (unset)"); |
| 511 | } |
| 512 | |
| 513 | if let Some(app_id) = &window.app_id { |
| 514 | println!(" App ID: \"{app_id}\""); |
| 515 | } else { |
| 516 | println!(" App ID: (unset)"); |
| 517 | } |
| 518 | |
| 519 | println!( |
| 520 | " Is floating: {}", |
| 521 | if window.is_floating { "yes" } else { "no" } |
| 522 | ); |
| 523 | |
| 524 | if let Some(pid) = window.pid { |
| 525 | println!(" PID: {pid}"); |
| 526 | } else { |
| 527 | println!(" PID: (unknown)"); |
| 528 | } |
| 529 | |
| 530 | if let Some(workspace_id) = window.workspace_id { |
| 531 | println!(" Workspace ID: {workspace_id}"); |
| 532 | } else { |
| 533 | println!(" Workspace ID: (none)"); |
| 534 | } |
| 535 | } |