forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

client.rs202 lines, 7.2 KiBHistoryRawDownload
1use std::env;
2use std::io::{Read, Write};
3use std::net::Shutdown;
4use std::os::unix::net::UnixStream;
5
6use anyhow::{anyhow, bail, Context};
7use niri_ipc::{LogicalOutput, Mode, Output, Reply, Request, Response};
8
9use crate::cli::Msg;
10
11pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
12 let socket_path = env::var_os(niri_ipc::SOCKET_PATH_ENV).with_context(|| {
13 format!(
14 "{} is not set, are you running this within niri?",
15 niri_ipc::SOCKET_PATH_ENV
16 )
17 })?;
18
19 let mut stream =
20 UnixStream::connect(socket_path).context("error connecting to {socket_path}")?;
21
22 let request = match &msg {
23 Msg::Outputs => Request::Outputs,
24 Msg::FocusedWindow => Request::FocusedWindow,
25 Msg::Action { action } => Request::Action(action.clone()),
26 };
27 let mut buf = serde_json::to_vec(&request).unwrap();
28 stream
29 .write_all(&buf)
30 .context("error writing IPC request")?;
31 stream
32 .shutdown(Shutdown::Write)
33 .context("error closing IPC stream for writing")?;
34
35 buf.clear();
36 stream
37 .read_to_end(&mut buf)
38 .context("error reading IPC response")?;
39
40 let reply: Reply = serde_json::from_slice(&buf).context("error parsing IPC reply")?;
41
42 let response = reply
43 .map_err(|msg| anyhow!(msg))
44 .context("niri could not handle the request")?;
45
46 // Default SIGPIPE so that our prints don't panic on stdout closing.
47 unsafe {
48 libc::signal(libc::SIGPIPE, libc::SIG_DFL);
49 }
50
51 match msg {
52 Msg::Outputs => {
53 let Response::Outputs(outputs) = response else {
54 bail!("unexpected response: expected Outputs, got {response:?}");
55 };
56
57 if json {
58 let output =
59 serde_json::to_string(&outputs).context("error formatting response")?;
60 println!("{output}");
61 return Ok(());
62 }
63
64 let mut outputs = outputs.into_iter().collect::<Vec<_>>();
65 outputs.sort_unstable_by(|a, b| a.0.cmp(&b.0));
66
67 for (connector, output) in outputs.into_iter() {
68 let Output {
69 name,
70 make,
71 model,
72 physical_size,
73 modes,
74 current_mode,
75 vrr_supported,
76 vrr_enabled,
77 logical,
78 } = output;
79
80 println!(r#"Output "{connector}" ({make} - {model} - {name})"#);
81
82 if let Some(current) = current_mode {
83 let mode = *modes
84 .get(current)
85 .context("invalid response: current mode does not exist")?;
86 let Mode {
87 width,
88 height,
89 refresh_rate,
90 is_preferred,
91 } = mode;
92 let refresh = refresh_rate as f64 / 1000.;
93 let preferred = if is_preferred { " (preferred)" } else { "" };
94 println!(" Current mode: {width}x{height} @ {refresh:.3} Hz{preferred}");
95 } else {
96 println!(" Disabled");
97 }
98
99 if vrr_supported {
100 let enabled = if vrr_enabled { "enabled" } else { "disabled" };
101 println!(" Variable refresh rate: supported, {enabled}");
102 } else {
103 println!(" Variable refresh rate: not supported");
104 }
105
106 if let Some((width, height)) = physical_size {
107 println!(" Physical size: {width}x{height} mm");
108 } else {
109 println!(" Physical size: unknown");
110 }
111
112 if let Some(logical) = logical {
113 let LogicalOutput {
114 x,
115 y,
116 width,
117 height,
118 scale,
119 transform,
120 } = logical;
121 println!(" Logical position: {x}, {y}");
122 println!(" Logical size: {width}x{height}");
123 println!(" Scale: {scale}");
124
125 let transform = match transform {
126 niri_ipc::Transform::Normal => "normal",
127 niri_ipc::Transform::_90 => "90° counter-clockwise",
128 niri_ipc::Transform::_180 => "180°",
129 niri_ipc::Transform::_270 => "270° counter-clockwise",
130 niri_ipc::Transform::Flipped => "flipped horizontally",
131 niri_ipc::Transform::Flipped90 => {
132 "90° counter-clockwise, flipped horizontally"
133 }
134 niri_ipc::Transform::Flipped180 => "flipped vertically",
135 niri_ipc::Transform::Flipped270 => {
136 "270° counter-clockwise, flipped horizontally"
137 }
138 };
139 println!(" Transform: {transform}");
140 }
141
142 println!(" Available modes:");
143 for (idx, mode) in modes.into_iter().enumerate() {
144 let Mode {
145 width,
146 height,
147 refresh_rate,
148 is_preferred,
149 } = mode;
150 let refresh = refresh_rate as f64 / 1000.;
151
152 let is_current = Some(idx) == current_mode;
153 let qualifier = match (is_current, is_preferred) {
154 (true, true) => " (current, preferred)",
155 (true, false) => " (current)",
156 (false, true) => " (preferred)",
157 (false, false) => "",
158 };
159
160 println!(" {width}x{height}@{refresh:.3}{qualifier}");
161 }
162 println!();
163 }
164 }
165 Msg::FocusedWindow => {
166 let Response::FocusedWindow(window) = response else {
167 bail!("unexpected response: expected FocusedWindow, got {response:?}");
168 };
169
170 if json {
171 let window = serde_json::to_string(&window).context("error formatting response")?;
172 println!("{window}");
173 return Ok(());
174 }
175
176 if let Some(window) = window {
177 println!("Focused window:");
178
179 if let Some(title) = window.title {
180 println!(" Title: \"{title}\"");
181 } else {
182 println!(" Title: (unset)");
183 }
184
185 if let Some(app_id) = window.app_id {
186 println!(" App ID: \"{app_id}\"");
187 } else {
188 println!(" App ID: (unset)");
189 }
190 } else {
191 println!("No window is focused.");
192 }
193 }
194 Msg::Action { .. } => {
195 let Response::Handled = response else {
196 bail!("unexpected response: expected Handled, got {response:?}");
197 };
198 }
199 }
200
201 Ok(())
202}