niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::io::ErrorKind; |
| 2 | use std::iter::Peekable; |
| 3 | use std::path::Path; |
| 4 | use std::{env, slice}; |
| 5 | |
| 6 | use anyhow::{anyhow, bail, Context}; |
| 7 | use niri_config::OutputName; |
| 8 | use niri_ipc::socket::Socket; |
| 9 | use niri_ipc::{ |
| 10 | Action, Cast, CastKind, CastTarget, Event, KeyboardLayouts, LogicalOutput, Mode, Output, |
| 11 | OutputConfigChanged, Overview, Request, Response, Transform, Window, WindowLayout, |
| 12 | }; |
| 13 | use serde_json::json; |
| 14 | |
| 15 | use crate::cli::Msg; |
| 16 | use crate::utils::version; |
| 17 | |
| 18 | pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> { |
| 19 | // For actions taking paths, prepend the niri CLI's working directory. |
| 20 | if let Msg::Action { |
| 21 | action: |
| 22 | Action::Screenshot { path, .. } |
| 23 | | Action::ScreenshotScreen { path, .. } |
| 24 | | Action::ScreenshotWindow { path, .. }, |
| 25 | } = &mut msg |
| 26 | { |
| 27 | if let Some(path) = path { |
| 28 | ensure_absolute_path(path).context("error making the path absolute")?; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | let request = match &msg { |
| 33 | Msg::Version => Request::Version, |
| 34 | Msg::Outputs => Request::Outputs, |
| 35 | Msg::FocusedWindow => Request::FocusedWindow, |
| 36 | Msg::FocusedOutput => Request::FocusedOutput, |
| 37 | Msg::PickWindow => Request::PickWindow, |
| 38 | Msg::PickColor => Request::PickColor, |
| 39 | Msg::Action { action } => Request::Action(action.clone()), |
| 40 | Msg::Output { output, action } => Request::Output { |
| 41 | output: output.clone(), |
| 42 | action: action.clone(), |
| 43 | }, |
| 44 | Msg::Workspaces => Request::Workspaces, |
| 45 | Msg::Windows => Request::Windows, |
| 46 | Msg::Layers => Request::Layers, |
| 47 | Msg::KeyboardLayouts => Request::KeyboardLayouts, |
| 48 | Msg::EventStream => Request::EventStream, |
| 49 | Msg::RequestError => Request::ReturnError, |
| 50 | Msg::OverviewState => Request::OverviewState, |
| 51 | Msg::Casts => Request::Casts, |
| 52 | }; |
| 53 | |
| 54 | let mut socket = Socket::connect().context("error connecting to the niri socket")?; |
| 55 | |
| 56 | let result = socket.send(request); |
| 57 | |
| 58 | // For errors that can be caused by a version mismatch between the running niri instance and |
| 59 | // the niri msg CLI, we will try to fetch and compare the versions. |
| 60 | let check_compositor_version = match &result { |
| 61 | Err(err) => { |
| 62 | // Response JSON parsing errors. |
| 63 | matches!( |
| 64 | err.kind(), |
| 65 | ErrorKind::InvalidData | ErrorKind::UnexpectedEof |
| 66 | ) |
| 67 | } |
| 68 | // Error returned from niri. |
| 69 | Ok(Err(_)) => true, |
| 70 | _ => false, |
| 71 | }; |
| 72 | |
| 73 | let compositor_version = if check_compositor_version && !matches!(msg, Msg::Version) { |
| 74 | // Reconnect to support older niri versions with one request per connection. |
| 75 | Socket::connect() |
| 76 | .and_then(|mut socket| socket.send(Request::Version)) |
| 77 | .ok() |
| 78 | } else { |
| 79 | None |
| 80 | }; |
| 81 | |
| 82 | // Default SIGPIPE so that our prints don't panic on stdout closing. |
| 83 | unsafe { |
| 84 | libc::signal(libc::SIGPIPE, libc::SIG_DFL); |
| 85 | } |
| 86 | |
| 87 | // Check for CLI-server version mismatch to add helpful context. |
| 88 | match compositor_version { |
| 89 | Some(Ok(Response::Version(compositor_version))) => { |
| 90 | let cli_version = version(); |
| 91 | if cli_version != compositor_version { |
| 92 | eprintln!("Running niri compositor has a different version from the niri CLI:"); |
| 93 | eprintln!("Compositor version: {compositor_version}"); |
| 94 | eprintln!("CLI version: {cli_version}"); |
| 95 | eprintln!("Did you forget to restart niri after an update?"); |
| 96 | eprintln!(); |
| 97 | } |
| 98 | } |
| 99 | Some(_) => { |
| 100 | eprintln!("Unable to get the running niri compositor version."); |
| 101 | eprintln!("Did you forget to restart niri after an update?"); |
| 102 | eprintln!(); |
| 103 | } |
| 104 | None => { |
| 105 | // Communication error, or the original request was already a version request, or the |
| 106 | // original request had succeeded. Don't add irrelevant context. |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | let reply = result.context("error communicating with niri")?; |
| 111 | let response = reply.map_err(|err_msg| anyhow!(err_msg).context("niri returned an error"))?; |
| 112 | |
| 113 | match msg { |
| 114 | Msg::RequestError => { |
| 115 | bail!("unexpected response: expected an error, got {response:?}"); |
| 116 | } |
| 117 | Msg::Version => { |
| 118 | let Response::Version(compositor_version) = response else { |
| 119 | bail!("unexpected response: expected Version, got {response:?}"); |
| 120 | }; |
| 121 | |
| 122 | let cli_version = version(); |
| 123 | |
| 124 | if json { |
| 125 | println!( |
| 126 | "{}", |
| 127 | json!({ |
| 128 | "compositor": compositor_version, |
| 129 | "cli": cli_version, |
| 130 | }) |
| 131 | ); |
| 132 | return Ok(()); |
| 133 | } |
| 134 | |
| 135 | if cli_version != compositor_version { |
| 136 | eprintln!("Running niri compositor has a different version from the niri CLI."); |
| 137 | eprintln!("Did you forget to restart niri after an update?"); |
| 138 | eprintln!(); |
| 139 | } |
| 140 | |
| 141 | println!("Compositor version: {compositor_version}"); |
| 142 | println!("CLI version: {cli_version}"); |
| 143 | } |
| 144 | Msg::Outputs => { |
| 145 | let Response::Outputs(outputs) = response else { |
| 146 | bail!("unexpected response: expected Outputs, got {response:?}"); |
| 147 | }; |
| 148 | |
| 149 | if json { |
| 150 | let output = |
| 151 | serde_json::to_string(&outputs).context("error formatting response")?; |
| 152 | println!("{output}"); |
| 153 | return Ok(()); |
| 154 | } |
| 155 | |
| 156 | let mut outputs = outputs |
| 157 | .into_values() |
| 158 | .map(|out| (OutputName::from_ipc_output(&out), out)) |
| 159 | .collect::<Vec<_>>(); |
| 160 | outputs.sort_unstable_by(|a, b| a.0.compare(&b.0)); |
| 161 | |
| 162 | for (_name, output) in outputs.into_iter() { |
| 163 | print_output(output)?; |
| 164 | println!(); |
| 165 | } |
| 166 | } |
| 167 | Msg::FocusedWindow => { |
| 168 | let Response::FocusedWindow(window) = response else { |
| 169 | bail!("unexpected response: expected FocusedWindow, got {response:?}"); |
| 170 | }; |
| 171 | |
| 172 | if json { |
| 173 | let window = serde_json::to_string(&window).context("error formatting response")?; |
| 174 | println!("{window}"); |
| 175 | return Ok(()); |
| 176 | } |
| 177 | |
| 178 | if let Some(window) = window { |
| 179 | print_window(&window); |
| 180 | } else { |
| 181 | println!("No window is focused."); |
| 182 | } |
| 183 | } |
| 184 | Msg::Windows => { |
| 185 | let Response::Windows(mut windows) = response else { |
| 186 | bail!("unexpected response: expected Windows, got {response:?}"); |
| 187 | }; |
| 188 | |
| 189 | if json { |
| 190 | let windows = |
| 191 | serde_json::to_string(&windows).context("error formatting response")?; |
| 192 | println!("{windows}"); |
| 193 | return Ok(()); |
| 194 | } |
| 195 | |
| 196 | windows.sort_unstable_by_key(|a| a.id); |
| 197 | |
| 198 | for window in windows { |
| 199 | print_window(&window); |
| 200 | println!(); |
| 201 | } |
| 202 | } |
| 203 | Msg::Layers => { |
| 204 | let Response::Layers(mut layers) = response else { |
| 205 | bail!("unexpected response: expected Layers, got {response:?}"); |
| 206 | }; |
| 207 | |
| 208 | if json { |
| 209 | let layers = serde_json::to_string(&layers).context("error formatting response")?; |
| 210 | println!("{layers}"); |
| 211 | return Ok(()); |
| 212 | } |
| 213 | |
| 214 | layers.sort_by(|a, b| { |
| 215 | Ord::cmp(&a.output, &b.output) |
| 216 | .then_with(|| Ord::cmp(&a.layer, &b.layer)) |
| 217 | .then_with(|| Ord::cmp(&a.namespace, &b.namespace)) |
| 218 | }); |
| 219 | let mut iter = layers.iter().peekable(); |
| 220 | |
| 221 | let print = |surface: &niri_ipc::LayerSurface| { |
| 222 | println!(" Surface:"); |
| 223 | println!(" Namespace: \"{}\"", surface.namespace); |
| 224 | |
| 225 | let interactivity = match surface.keyboard_interactivity { |
| 226 | niri_ipc::LayerSurfaceKeyboardInteractivity::None => "none", |
| 227 | niri_ipc::LayerSurfaceKeyboardInteractivity::Exclusive => "exclusive", |
| 228 | niri_ipc::LayerSurfaceKeyboardInteractivity::OnDemand => "on-demand", |
| 229 | }; |
| 230 | println!(" Keyboard interactivity: {interactivity}"); |
| 231 | }; |
| 232 | |
| 233 | let print_layer = |iter: &mut Peekable<slice::Iter<niri_ipc::LayerSurface>>, |
| 234 | output: &str, |
| 235 | layer| { |
| 236 | let mut empty = true; |
| 237 | while let Some(surface) = iter.next_if(|s| s.output == output && s.layer == layer) { |
| 238 | empty = false; |
| 239 | println!(); |
| 240 | print(surface); |
| 241 | } |
| 242 | if empty { |
| 243 | println!(" (empty)\n"); |
| 244 | } else { |
| 245 | println!(); |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | while let Some(surface) = iter.peek() { |
| 250 | let output = &surface.output; |
| 251 | println!("Output \"{output}\":"); |
| 252 | |
| 253 | print!(" Background layer:"); |
| 254 | print_layer(&mut iter, output, niri_ipc::Layer::Background); |
| 255 | |
| 256 | print!(" Bottom layer:"); |
| 257 | print_layer(&mut iter, output, niri_ipc::Layer::Bottom); |
| 258 | |
| 259 | print!(" Top layer:"); |
| 260 | print_layer(&mut iter, output, niri_ipc::Layer::Top); |
| 261 | |
| 262 | print!(" Overlay layer:"); |
| 263 | print_layer(&mut iter, output, niri_ipc::Layer::Overlay); |
| 264 | } |
| 265 | } |
| 266 | Msg::FocusedOutput => { |
| 267 | let Response::FocusedOutput(output) = response else { |
| 268 | bail!("unexpected response: expected FocusedOutput, got {response:?}"); |
| 269 | }; |
| 270 | |
| 271 | if json { |
| 272 | let output = serde_json::to_string(&output).context("error formatting response")?; |
| 273 | println!("{output}"); |
| 274 | return Ok(()); |
| 275 | } |
| 276 | |
| 277 | if let Some(output) = output { |
| 278 | print_output(output)?; |
| 279 | } else { |
| 280 | println!("No output is focused."); |
| 281 | } |
| 282 | } |
| 283 | Msg::PickWindow => { |
| 284 | let Response::PickedWindow(window) = response else { |
| 285 | bail!("unexpected response: expected PickedWindow, got {response:?}"); |
| 286 | }; |
| 287 | |
| 288 | if json { |
| 289 | let window = serde_json::to_string(&window).context("error formatting response")?; |
| 290 | println!("{window}"); |
| 291 | return Ok(()); |
| 292 | } |
| 293 | |
| 294 | if let Some(window) = window { |
| 295 | print_window(&window); |
| 296 | } else { |
| 297 | println!("No window selected."); |
| 298 | } |
| 299 | } |
| 300 | Msg::PickColor => { |
| 301 | let Response::PickedColor(color) = response else { |
| 302 | bail!("unexpected response: expected PickedColor, got {response:?}"); |
| 303 | }; |
| 304 | |
| 305 | if json { |
| 306 | let color = serde_json::to_string(&color).context("error formatting response")?; |
| 307 | println!("{color}"); |
| 308 | return Ok(()); |
| 309 | } |
| 310 | |
| 311 | if let Some(color) = color { |
| 312 | let [r, g, b] = color.rgb.map(|v| (v.clamp(0., 1.) * 255.).round() as u8); |
| 313 | |
| 314 | println!("Picked color: rgb({r}, {g}, {b})",); |
| 315 | println!("Hex: #{r:02x}{g:02x}{b:02x}"); |
| 316 | } else { |
| 317 | println!("No color was picked."); |
| 318 | } |
| 319 | } |
| 320 | Msg::Action { .. } => { |
| 321 | let Response::Handled = response else { |
| 322 | bail!("unexpected response: expected Handled, got {response:?}"); |
| 323 | }; |
| 324 | } |
| 325 | Msg::Output { output, .. } => { |
| 326 | let Response::OutputConfigChanged(response) = response else { |
| 327 | bail!("unexpected response: expected OutputConfigChanged, 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 | if response == OutputConfigChanged::OutputWasMissing { |
| 338 | println!("Output \"{output}\" is not connected."); |
| 339 | println!("The change will apply when it is connected."); |
| 340 | } |
| 341 | } |
| 342 | Msg::Workspaces => { |
| 343 | let Response::Workspaces(mut response) = response else { |
| 344 | bail!("unexpected response: expected Workspaces, got {response:?}"); |
| 345 | }; |
| 346 | |
| 347 | if json { |
| 348 | let response = |
| 349 | serde_json::to_string(&response).context("error formatting response")?; |
| 350 | println!("{response}"); |
| 351 | return Ok(()); |
| 352 | } |
| 353 | |
| 354 | if response.is_empty() { |
| 355 | println!("No workspaces."); |
| 356 | return Ok(()); |
| 357 | } |
| 358 | |
| 359 | response.sort_by_key(|ws| ws.idx); |
| 360 | response.sort_by(|a, b| a.output.cmp(&b.output)); |
| 361 | |
| 362 | let mut current_output = if let Some(output) = response[0].output.as_deref() { |
| 363 | println!("Output \"{output}\":"); |
| 364 | Some(output) |
| 365 | } else { |
| 366 | println!("No output:"); |
| 367 | None |
| 368 | }; |
| 369 | |
| 370 | for ws in &response { |
| 371 | if ws.output.as_deref() != current_output { |
| 372 | let output = ws.output.as_deref().context( |
| 373 | "invalid response: workspace with no output \ |
| 374 | following a workspace with an output", |
| 375 | )?; |
| 376 | current_output = Some(output); |
| 377 | println!("\nOutput \"{output}\":"); |
| 378 | } |
| 379 | |
| 380 | let is_active = if ws.is_active { " * " } else { " " }; |
| 381 | let idx = ws.idx; |
| 382 | let name = if let Some(name) = ws.name.as_deref() { |
| 383 | format!(" \"{name}\"") |
| 384 | } else { |
| 385 | String::new() |
| 386 | }; |
| 387 | println!("{is_active}{idx}{name}"); |
| 388 | } |
| 389 | } |
| 390 | Msg::KeyboardLayouts => { |
| 391 | let Response::KeyboardLayouts(response) = response else { |
| 392 | bail!("unexpected response: expected KeyboardLayouts, got {response:?}"); |
| 393 | }; |
| 394 | |
| 395 | if json { |
| 396 | let response = |
| 397 | serde_json::to_string(&response).context("error formatting response")?; |
| 398 | println!("{response}"); |
| 399 | return Ok(()); |
| 400 | } |
| 401 | |
| 402 | let KeyboardLayouts { names, current_idx } = response; |
| 403 | let current_idx = usize::from(current_idx); |
| 404 | |
| 405 | println!("Keyboard layouts:"); |
| 406 | for (idx, name) in names.iter().enumerate() { |
| 407 | let is_active = if idx == current_idx { " * " } else { " " }; |
| 408 | println!("{is_active}{idx} {name}"); |
| 409 | } |
| 410 | } |
| 411 | Msg::EventStream => { |
| 412 | let Response::Handled = response else { |
| 413 | bail!("unexpected response: expected Handled, got {response:?}"); |
| 414 | }; |
| 415 | |
| 416 | if !json { |
| 417 | println!("Started reading events."); |
| 418 | } |
| 419 | |
| 420 | let mut read_event = socket.read_events(); |
| 421 | loop { |
| 422 | let event = read_event().context("error reading event from niri")?; |
| 423 | |
| 424 | if json { |
| 425 | let event = serde_json::to_string(&event).context("error formatting event")?; |
| 426 | println!("{event}"); |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | match event { |
| 431 | Event::WorkspacesChanged { workspaces } => { |
| 432 | println!("Workspaces changed: {workspaces:?}"); |
| 433 | } |
| 434 | Event::WorkspaceUrgencyChanged { id, urgent } => { |
| 435 | println!("Workspace {id}: urgency changed to {urgent}"); |
| 436 | } |
| 437 | Event::WorkspaceActivated { id, focused } => { |
| 438 | let word = if focused { "focused" } else { "activated" }; |
| 439 | println!("Workspace {word}: {id}"); |
| 440 | } |
| 441 | Event::WorkspaceActiveWindowChanged { |
| 442 | workspace_id, |
| 443 | active_window_id, |
| 444 | } => { |
| 445 | println!( |
| 446 | "Workspace {workspace_id}: \ |
| 447 | active window changed to {active_window_id:?}" |
| 448 | ); |
| 449 | } |
| 450 | Event::WindowsChanged { windows } => { |
| 451 | println!("Windows changed: {windows:?}"); |
| 452 | } |
| 453 | Event::WindowOpenedOrChanged { window } => { |
| 454 | println!("Window opened or changed: {window:?}"); |
| 455 | } |
| 456 | Event::WindowClosed { id } => { |
| 457 | println!("Window closed: {id}"); |
| 458 | } |
| 459 | Event::WindowFocusChanged { id } => { |
| 460 | println!("Window focus changed: {id:?}"); |
| 461 | } |
| 462 | Event::WindowFocusTimestampChanged { |
| 463 | id, |
| 464 | focus_timestamp, |
| 465 | } => { |
| 466 | println!("Window {id}: focus timestamp changed to {focus_timestamp:?}"); |
| 467 | } |
| 468 | Event::WindowUrgencyChanged { id, urgent } => { |
| 469 | println!("Window {id}: urgency changed to {urgent}"); |
| 470 | } |
| 471 | Event::WindowLayoutsChanged { changes } => { |
| 472 | println!("Window layouts changed: {changes:?}"); |
| 473 | } |
| 474 | Event::KeyboardLayoutsChanged { keyboard_layouts } => { |
| 475 | println!("Keyboard layouts changed: {keyboard_layouts:?}"); |
| 476 | } |
| 477 | Event::KeyboardLayoutSwitched { idx } => { |
| 478 | println!("Keyboard layout switched: {idx}"); |
| 479 | } |
| 480 | Event::OverviewOpenedOrClosed { is_open: opened } => { |
| 481 | println!("Overview toggled: {opened}"); |
| 482 | } |
| 483 | Event::ConfigLoaded { failed } => { |
| 484 | let status = if failed { |
| 485 | "with an error" |
| 486 | } else { |
| 487 | "successfully" |
| 488 | }; |
| 489 | println!("Config loaded {status}"); |
| 490 | } |
| 491 | Event::ScreenshotCaptured { path } => { |
| 492 | let mut parts = vec![]; |
| 493 | parts.push("copied to clipboard".to_string()); |
| 494 | if let Some(path) = &path { |
| 495 | parts.push(format!("saved to {path}")); |
| 496 | } |
| 497 | let description = parts.join(" and "); |
| 498 | println!("Screenshot captured: {description}"); |
| 499 | } |
| 500 | Event::CastsChanged { casts } => { |
| 501 | println!("Casts changed: {casts:?}"); |
| 502 | } |
| 503 | Event::CastStartedOrChanged { cast } => { |
| 504 | println!("Cast started or changed: {cast:?}"); |
| 505 | } |
| 506 | Event::CastStopped { stream_id } => { |
| 507 | println!("Cast stopped: stream id {stream_id}"); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | Msg::OverviewState => { |
| 513 | let Response::OverviewState(response) = response else { |
| 514 | bail!("unexpected response: expected Overview, got {response:?}"); |
| 515 | }; |
| 516 | |
| 517 | if json { |
| 518 | let response = |
| 519 | serde_json::to_string(&response).context("error formatting response")?; |
| 520 | println!("{response}"); |
| 521 | return Ok(()); |
| 522 | } |
| 523 | |
| 524 | let Overview { is_open } = response; |
| 525 | if is_open { |
| 526 | println!("Overview is open."); |
| 527 | } else { |
| 528 | println!("Overview is closed."); |
| 529 | } |
| 530 | } |
| 531 | Msg::Casts => { |
| 532 | let Response::Casts(mut casts) = response else { |
| 533 | bail!("unexpected response: expected Casts, got {response:?}"); |
| 534 | }; |
| 535 | |
| 536 | if json { |
| 537 | let casts = serde_json::to_string(&casts).context("error formatting response")?; |
| 538 | println!("{casts}"); |
| 539 | return Ok(()); |
| 540 | } |
| 541 | |
| 542 | if casts.is_empty() { |
| 543 | println!("No screencasts."); |
| 544 | return Ok(()); |
| 545 | } |
| 546 | |
| 547 | casts.sort_by_key(|c| (c.session_id, c.stream_id)); |
| 548 | for cast in casts { |
| 549 | print_cast(&cast); |
| 550 | println!(); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | Ok(()) |
| 556 | } |
| 557 | |
| 558 | fn print_output(output: Output) -> anyhow::Result<()> { |
| 559 | let Output { |
| 560 | name, |
| 561 | make, |
| 562 | model, |
| 563 | serial, |
| 564 | physical_size, |
| 565 | modes, |
| 566 | current_mode, |
| 567 | is_custom_mode, |
| 568 | vrr_supported, |
| 569 | vrr_enabled, |
| 570 | logical, |
| 571 | max_bpc, |
| 572 | } = output; |
| 573 | |
| 574 | let serial = serial.as_deref().unwrap_or("Unknown"); |
| 575 | println!(r#"Output "{make} {model} {serial}" ({name})"#); |
| 576 | |
| 577 | let print_qualifier = |is_preferred: bool, is_current: bool, is_custom_mode: bool| { |
| 578 | let mut qualifier = Vec::new(); |
| 579 | if is_current { |
| 580 | qualifier.push("current"); |
| 581 | if is_custom_mode { |
| 582 | qualifier.push("custom"); |
| 583 | }; |
| 584 | }; |
| 585 | |
| 586 | if is_preferred { |
| 587 | qualifier.push("preferred"); |
| 588 | }; |
| 589 | |
| 590 | if qualifier.is_empty() { |
| 591 | String::new() |
| 592 | } else { |
| 593 | format!(" ({})", qualifier.join(", ")) |
| 594 | } |
| 595 | }; |
| 596 | |
| 597 | if let Some(current) = current_mode { |
| 598 | let mode = *modes |
| 599 | .get(current) |
| 600 | .context("invalid response: current mode does not exist")?; |
| 601 | let Mode { |
| 602 | width, |
| 603 | height, |
| 604 | refresh_rate, |
| 605 | is_preferred, |
| 606 | } = mode; |
| 607 | let refresh = refresh_rate as f64 / 1000.; |
| 608 | |
| 609 | // This is technically the current mode, but the println below already specifies that. |
| 610 | let qualifier = print_qualifier(is_preferred, false, is_custom_mode); |
| 611 | println!(" Current mode: {width}x{height} @ {refresh:.3} Hz{qualifier}"); |
| 612 | } else { |
| 613 | println!(" Disabled"); |
| 614 | } |
| 615 | |
| 616 | if vrr_supported { |
| 617 | let enabled = if vrr_enabled { "enabled" } else { "disabled" }; |
| 618 | println!(" Variable refresh rate: supported, {enabled}"); |
| 619 | } else { |
| 620 | println!(" Variable refresh rate: not supported"); |
| 621 | } |
| 622 | |
| 623 | if let Some((width, height)) = physical_size { |
| 624 | println!(" Physical size: {width}x{height} mm"); |
| 625 | } else { |
| 626 | println!(" Physical size: unknown"); |
| 627 | } |
| 628 | |
| 629 | if let Some(logical) = logical { |
| 630 | let LogicalOutput { |
| 631 | x, |
| 632 | y, |
| 633 | width, |
| 634 | height, |
| 635 | scale, |
| 636 | transform, |
| 637 | } = logical; |
| 638 | println!(" Logical position: {x}, {y}"); |
| 639 | println!(" Logical size: {width}x{height}"); |
| 640 | println!(" Scale: {scale}"); |
| 641 | |
| 642 | let transform = match transform { |
| 643 | Transform::Normal => "normal", |
| 644 | Transform::_90 => "90° counter-clockwise", |
| 645 | Transform::_180 => "180°", |
| 646 | Transform::_270 => "270° counter-clockwise", |
| 647 | Transform::Flipped => "flipped horizontally", |
| 648 | Transform::Flipped90 => "90° counter-clockwise, flipped horizontally", |
| 649 | Transform::Flipped180 => "flipped vertically", |
| 650 | Transform::Flipped270 => "270° counter-clockwise, flipped horizontally", |
| 651 | }; |
| 652 | println!(" Transform: {transform}"); |
| 653 | } |
| 654 | |
| 655 | if let Some(max_bpc) = max_bpc { |
| 656 | println!(" Max bits per channel: {max_bpc}"); |
| 657 | } |
| 658 | |
| 659 | println!(" Available modes:"); |
| 660 | for (idx, mode) in modes.into_iter().enumerate() { |
| 661 | let Mode { |
| 662 | width, |
| 663 | height, |
| 664 | refresh_rate, |
| 665 | is_preferred, |
| 666 | } = mode; |
| 667 | let refresh = refresh_rate as f64 / 1000.; |
| 668 | |
| 669 | let is_current = Some(idx) == current_mode; |
| 670 | let qualifier = print_qualifier(is_preferred, is_current, is_custom_mode); |
| 671 | |
| 672 | println!(" {width}x{height}@{refresh:.3}{qualifier}"); |
| 673 | } |
| 674 | Ok(()) |
| 675 | } |
| 676 | |
| 677 | fn print_window(window: &Window) { |
| 678 | let focused = if window.is_focused { " (focused)" } else { "" }; |
| 679 | let urgent = if window.is_urgent { " (urgent)" } else { "" }; |
| 680 | println!("Window ID {}:{focused}{urgent}", window.id); |
| 681 | |
| 682 | if let Some(title) = &window.title { |
| 683 | println!(" Title: \"{title}\""); |
| 684 | } else { |
| 685 | println!(" Title: (unset)"); |
| 686 | } |
| 687 | |
| 688 | if let Some(app_id) = &window.app_id { |
| 689 | println!(" App ID: \"{app_id}\""); |
| 690 | } else { |
| 691 | println!(" App ID: (unset)"); |
| 692 | } |
| 693 | |
| 694 | println!( |
| 695 | " Is floating: {}", |
| 696 | if window.is_floating { "yes" } else { "no" } |
| 697 | ); |
| 698 | |
| 699 | if let Some(pid) = window.pid { |
| 700 | println!(" PID: {pid}"); |
| 701 | } else { |
| 702 | println!(" PID: (unknown)"); |
| 703 | } |
| 704 | |
| 705 | if let Some(workspace_id) = window.workspace_id { |
| 706 | println!(" Workspace ID: {workspace_id}"); |
| 707 | } else { |
| 708 | println!(" Workspace ID: (none)"); |
| 709 | } |
| 710 | |
| 711 | let WindowLayout { |
| 712 | pos_in_scrolling_layout, |
| 713 | tile_size, |
| 714 | window_size, |
| 715 | tile_pos_in_workspace_view, |
| 716 | window_offset_in_tile, |
| 717 | } = window.layout; |
| 718 | |
| 719 | println!(" Layout:"); |
| 720 | println!( |
| 721 | " Tile size: {} x {}", |
| 722 | fmt_rounded(tile_size.0), |
| 723 | fmt_rounded(tile_size.1) |
| 724 | ); |
| 725 | |
| 726 | if let Some(pos) = pos_in_scrolling_layout { |
| 727 | println!(" Scrolling position: column {}, tile {}", pos.0, pos.1); |
| 728 | } |
| 729 | |
| 730 | if let Some(pos) = tile_pos_in_workspace_view { |
| 731 | println!( |
| 732 | " Workspace-view position: {}, {}", |
| 733 | fmt_rounded(pos.0), |
| 734 | fmt_rounded(pos.1) |
| 735 | ); |
| 736 | } |
| 737 | |
| 738 | println!(" Window size: {} x {}", window_size.0, window_size.1); |
| 739 | println!( |
| 740 | " Window offset in tile: {} x {}", |
| 741 | fmt_rounded(window_offset_in_tile.0), |
| 742 | fmt_rounded(window_offset_in_tile.1) |
| 743 | ); |
| 744 | } |
| 745 | |
| 746 | fn print_cast(cast: &Cast) { |
| 747 | let active = if cast.is_active { "" } else { " (inactive)" }; |
| 748 | println!("Cast stream ID {}:{active}", cast.stream_id); |
| 749 | println!(" Session ID: {}", cast.session_id); |
| 750 | |
| 751 | let kind = match cast.kind { |
| 752 | CastKind::PipeWire => "PipeWire", |
| 753 | CastKind::WlrScreencopy => "wlr-screencopy", |
| 754 | }; |
| 755 | println!(" Kind: {kind}"); |
| 756 | |
| 757 | match &cast.target { |
| 758 | CastTarget::Nothing {} => { |
| 759 | println!(" Target: nothing (cleared)"); |
| 760 | } |
| 761 | CastTarget::Output { name } => { |
| 762 | println!(" Target: output \"{name}\""); |
| 763 | } |
| 764 | CastTarget::Window { id } => { |
| 765 | println!(" Target: window {id}"); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if cast.is_dynamic_target { |
| 770 | println!(" Dynamic cast target"); |
| 771 | } |
| 772 | |
| 773 | if let Some(pid) = cast.pid { |
| 774 | println!(" PID: {pid}"); |
| 775 | } |
| 776 | |
| 777 | if let Some(node_id) = cast.pw_node_id { |
| 778 | println!(" PipeWire node ID: {node_id}"); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | fn fmt_rounded(x: f64) -> String { |
| 783 | let r = x.round(); |
| 784 | if (r - x).abs() <= 0.005 { |
| 785 | format!("{r}") |
| 786 | } else { |
| 787 | format!("{x:.2}") |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | fn ensure_absolute_path(path: &mut String) -> anyhow::Result<()> { |
| 792 | let p = Path::new(path); |
| 793 | if p.is_relative() { |
| 794 | let mut cwd = env::current_dir().context("error getting current working directory")?; |
| 795 | cwd.push(p); |
| 796 | match cwd.into_os_string().into_string() { |
| 797 | Ok(absolute) => *path = absolute, |
| 798 | Err(cwd) => bail!("couldn't convert absolute path to string: {cwd:?}"), |
| 799 | } |
| 800 | } |
| 801 | Ok(()) |
| 802 | } |
| 803 | |
| 804 | #[cfg(test)] |
| 805 | mod tests { |
| 806 | use insta::assert_snapshot; |
| 807 | |
| 808 | use super::*; |
| 809 | |
| 810 | #[test] |
| 811 | fn test_fmt_rounded() { |
| 812 | assert_snapshot!(fmt_rounded(1.9), @"1.90"); |
| 813 | assert_snapshot!(fmt_rounded(1.994), @"1.99"); |
| 814 | assert_snapshot!(fmt_rounded(1.996), @"2"); |
| 815 | assert_snapshot!(fmt_rounded(2.0), @"2"); |
| 816 | assert_snapshot!(fmt_rounded(2.004), @"2"); |
| 817 | assert_snapshot!(fmt_rounded(2.006), @"2.01"); |
| 818 | assert_snapshot!(fmt_rounded(2.1), @"2.10"); |
| 819 | } |
| 820 | } |