Add niri msg focused-output
Browse files at this commitae7fb4c4f405aa0ff49930040d414581a812d938Parent: 4746a0d4 changed files +144 -90
- niri-ipc/src/lib.rs +4 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +113 -90
- src/ipc/server.rs +25 -0
niri-ipc/src/lib.rsmodified+4 -0
| @@ -33,6 +33,8 @@ pub enum Request { | |||
| 33 | }, | 33 | }, |
| 34 | /// Request information about workspaces. | 34 | /// Request information about workspaces. |
| 35 | Workspaces, | 35 | Workspaces, |
| 36 | + /// Request information about the focused output. | ||
| 37 | + FocusedOutput, | ||
| 36 | /// Respond with an error (for testing error handling). | 38 | /// Respond with an error (for testing error handling). |
| 37 | ReturnError, | 39 | ReturnError, |
| 38 | } | 40 | } |
| @@ -64,6 +66,8 @@ pub enum Response { | |||
| 64 | OutputConfigChanged(OutputConfigChanged), | 66 | OutputConfigChanged(OutputConfigChanged), |
| 65 | /// Information about workspaces. | 67 | /// Information about workspaces. |
| 66 | Workspaces(Vec<Workspace>), | 68 | Workspaces(Vec<Workspace>), |
| 69 | + /// Information about the focused output. | ||
| 70 | + FocusedOutput(Option<Output>), | ||
| 67 | } | 71 | } |
| 68 | 72 | ||
| 69 | /// Actions that niri can perform. | 73 | /// Actions that niri can perform. |
src/cli.rsmodified+2 -0
| @@ -58,6 +58,8 @@ pub enum Msg { | |||
| 58 | Workspaces, | 58 | Workspaces, |
| 59 | /// Print information about the focused window. | 59 | /// Print information about the focused window. |
| 60 | FocusedWindow, | 60 | FocusedWindow, |
| 61 | + /// Print information about the focused output. | ||
| 62 | + FocusedOutput, | ||
| 61 | /// Perform an action. | 63 | /// Perform an action. |
| 62 | Action { | 64 | Action { |
| 63 | #[command(subcommand)] | 65 | #[command(subcommand)] |
src/ipc/client.rsmodified+113 -90
| @@ -12,6 +12,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 12 | Msg::Version => Request::Version, | 12 | Msg::Version => Request::Version, |
| 13 | Msg::Outputs => Request::Outputs, | 13 | Msg::Outputs => Request::Outputs, |
| 14 | Msg::FocusedWindow => Request::FocusedWindow, | 14 | Msg::FocusedWindow => Request::FocusedWindow, |
| 15 | + Msg::FocusedOutput => Request::FocusedOutput, | ||
| 15 | Msg::Action { action } => Request::Action(action.clone()), | 16 | Msg::Action { action } => Request::Action(action.clone()), |
| 16 | Msg::Output { output, action } => Request::Output { | 17 | Msg::Output { output, action } => Request::Output { |
| 17 | output: output.clone(), | 18 | output: output.clone(), |
| @@ -117,96 +118,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 117 | outputs.sort_unstable_by(|a, b| a.0.cmp(&b.0)); | 118 | outputs.sort_unstable_by(|a, b| a.0.cmp(&b.0)); |
| 118 | 119 | ||
| 119 | for (connector, output) in outputs.into_iter() { | 120 | for (connector, output) in outputs.into_iter() { |
| 120 | - let Output { | 121 | + print_output(connector, output)?; |
| 121 | - name, | ||
| 122 | - make, | ||
| 123 | - model, | ||
| 124 | - physical_size, | ||
| 125 | - modes, | ||
| 126 | - current_mode, | ||
| 127 | - vrr_supported, | ||
| 128 | - vrr_enabled, | ||
| 129 | - logical, | ||
| 130 | - } = output; | ||
| 131 | - | ||
| 132 | - println!(r#"Output "{connector}" ({make} - {model} - {name})"#); | ||
| 133 | - | ||
| 134 | - if let Some(current) = current_mode { | ||
| 135 | - let mode = *modes | ||
| 136 | - .get(current) | ||
| 137 | - .context("invalid response: current mode does not exist")?; | ||
| 138 | - let Mode { | ||
| 139 | - width, | ||
| 140 | - height, | ||
| 141 | - refresh_rate, | ||
| 142 | - is_preferred, | ||
| 143 | - } = mode; | ||
| 144 | - let refresh = refresh_rate as f64 / 1000.; | ||
| 145 | - let preferred = if is_preferred { " (preferred)" } else { "" }; | ||
| 146 | - println!(" Current mode: {width}x{height} @ {refresh:.3} Hz{preferred}"); | ||
| 147 | - } else { | ||
| 148 | - println!(" Disabled"); | ||
| 149 | - } | ||
| 150 | - | ||
| 151 | - if vrr_supported { | ||
| 152 | - let enabled = if vrr_enabled { "enabled" } else { "disabled" }; | ||
| 153 | - println!(" Variable refresh rate: supported, {enabled}"); | ||
| 154 | - } else { | ||
| 155 | - println!(" Variable refresh rate: not supported"); | ||
| 156 | - } | ||
| 157 | - | ||
| 158 | - if let Some((width, height)) = physical_size { | ||
| 159 | - println!(" Physical size: {width}x{height} mm"); | ||
| 160 | - } else { | ||
| 161 | - println!(" Physical size: unknown"); | ||
| 162 | - } | ||
| 163 | - | ||
| 164 | - if let Some(logical) = logical { | ||
| 165 | - let LogicalOutput { | ||
| 166 | - x, | ||
| 167 | - y, | ||
| 168 | - width, | ||
| 169 | - height, | ||
| 170 | - scale, | ||
| 171 | - transform, | ||
| 172 | - } = logical; | ||
| 173 | - println!(" Logical position: {x}, {y}"); | ||
| 174 | - println!(" Logical size: {width}x{height}"); | ||
| 175 | - println!(" Scale: {scale}"); | ||
| 176 | - | ||
| 177 | - let transform = match transform { | ||
| 178 | - Transform::Normal => "normal", | ||
| 179 | - Transform::_90 => "90° counter-clockwise", | ||
| 180 | - Transform::_180 => "180°", | ||
| 181 | - Transform::_270 => "270° counter-clockwise", | ||
| 182 | - Transform::Flipped => "flipped horizontally", | ||
| 183 | - Transform::Flipped90 => "90° counter-clockwise, flipped horizontally", | ||
| 184 | - Transform::Flipped180 => "flipped vertically", | ||
| 185 | - Transform::Flipped270 => "270° counter-clockwise, flipped horizontally", | ||
| 186 | - }; | ||
| 187 | - println!(" Transform: {transform}"); | ||
| 188 | - } | ||
| 189 | - | ||
| 190 | - println!(" Available modes:"); | ||
| 191 | - for (idx, mode) in modes.into_iter().enumerate() { | ||
| 192 | - let Mode { | ||
| 193 | - width, | ||
| 194 | - height, | ||
| 195 | - refresh_rate, | ||
| 196 | - is_preferred, | ||
| 197 | - } = mode; | ||
| 198 | - let refresh = refresh_rate as f64 / 1000.; | ||
| 199 | - | ||
| 200 | - let is_current = Some(idx) == current_mode; | ||
| 201 | - let qualifier = match (is_current, is_preferred) { | ||
| 202 | - (true, true) => " (current, preferred)", | ||
| 203 | - (true, false) => " (current)", | ||
| 204 | - (false, true) => " (preferred)", | ||
| 205 | - (false, false) => "", | ||
| 206 | - }; | ||
| 207 | - | ||
| 208 | - println!(" {width}x{height}@{refresh:.3}{qualifier}"); | ||
| 209 | - } | ||
| 210 | println!(); | 122 | println!(); |
| 211 | } | 123 | } |
| 212 | } | 124 | } |
| @@ -239,6 +151,23 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 239 | println!("No window is focused."); | 151 | println!("No window is focused."); |
| 240 | } | 152 | } |
| 241 | } | 153 | } |
| 154 | + Msg::FocusedOutput => { | ||
| 155 | + let Response::FocusedOutput(output) = response else { | ||
| 156 | + bail!("unexpected response: expected FocusedOutput, got {response:?}"); | ||
| 157 | + }; | ||
| 158 | + | ||
| 159 | + if json { | ||
| 160 | + let output = serde_json::to_string(&output).context("error formatting response")?; | ||
| 161 | + println!("{output}"); | ||
| 162 | + return Ok(()); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + if let Some(output) = output { | ||
| 166 | + print_output(output.name.clone(), output)?; | ||
| 167 | + } else { | ||
| 168 | + println!("No output is focused."); | ||
| 169 | + } | ||
| 170 | + } | ||
| 242 | Msg::Action { .. } => { | 171 | Msg::Action { .. } => { |
| 243 | let Response::Handled = response else { | 172 | let Response::Handled = response else { |
| 244 | bail!("unexpected response: expected Handled, got {response:?}"); | 173 | bail!("unexpected response: expected Handled, got {response:?}"); |
| @@ -313,3 +242,97 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | |||
| 313 | 242 | ||
| 314 | Ok(()) | 243 | Ok(()) |
| 315 | } | 244 | } |
| 245 | + | ||
| 246 | +fn print_output(connector: String, output: Output) -> anyhow::Result<()> { | ||
| 247 | + let Output { | ||
| 248 | + name, | ||
| 249 | + make, | ||
| 250 | + model, | ||
| 251 | + physical_size, | ||
| 252 | + modes, | ||
| 253 | + current_mode, | ||
| 254 | + vrr_supported, | ||
| 255 | + vrr_enabled, | ||
| 256 | + logical, | ||
| 257 | + } = output; | ||
| 258 | + | ||
| 259 | + println!(r#"Output "{connector}" ({make} - {model} - {name})"#); | ||
| 260 | + | ||
| 261 | + if let Some(current) = current_mode { | ||
| 262 | + let mode = *modes | ||
| 263 | + .get(current) | ||
| 264 | + .context("invalid response: current mode does not exist")?; | ||
| 265 | + let Mode { | ||
| 266 | + width, | ||
| 267 | + height, | ||
| 268 | + refresh_rate, | ||
| 269 | + is_preferred, | ||
| 270 | + } = mode; | ||
| 271 | + let refresh = refresh_rate as f64 / 1000.; | ||
| 272 | + let preferred = if is_preferred { " (preferred)" } else { "" }; | ||
| 273 | + println!(" Current mode: {width}x{height} @ {refresh:.3} Hz{preferred}"); | ||
| 274 | + } else { | ||
| 275 | + println!(" Disabled"); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + if vrr_supported { | ||
| 279 | + let enabled = if vrr_enabled { "enabled" } else { "disabled" }; | ||
| 280 | + println!(" Variable refresh rate: supported, {enabled}"); | ||
| 281 | + } else { | ||
| 282 | + println!(" Variable refresh rate: not supported"); | ||
| 283 | + } | ||
| 284 | + | ||
| 285 | + if let Some((width, height)) = physical_size { | ||
| 286 | + println!(" Physical size: {width}x{height} mm"); | ||
| 287 | + } else { | ||
| 288 | + println!(" Physical size: unknown"); | ||
| 289 | + } | ||
| 290 | + | ||
| 291 | + if let Some(logical) = logical { | ||
| 292 | + let LogicalOutput { | ||
| 293 | + x, | ||
| 294 | + y, | ||
| 295 | + width, | ||
| 296 | + height, | ||
| 297 | + scale, | ||
| 298 | + transform, | ||
| 299 | + } = logical; | ||
| 300 | + println!(" Logical position: {x}, {y}"); | ||
| 301 | + println!(" Logical size: {width}x{height}"); | ||
| 302 | + println!(" Scale: {scale}"); | ||
| 303 | + | ||
| 304 | + let transform = match transform { | ||
| 305 | + Transform::Normal => "normal", | ||
| 306 | + Transform::_90 => "90° counter-clockwise", | ||
| 307 | + Transform::_180 => "180°", | ||
| 308 | + Transform::_270 => "270° counter-clockwise", | ||
| 309 | + Transform::Flipped => "flipped horizontally", | ||
| 310 | + Transform::Flipped90 => "90° counter-clockwise, flipped horizontally", | ||
| 311 | + Transform::Flipped180 => "flipped vertically", | ||
| 312 | + Transform::Flipped270 => "270° counter-clockwise, flipped horizontally", | ||
| 313 | + }; | ||
| 314 | + println!(" Transform: {transform}"); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + println!(" Available modes:"); | ||
| 318 | + for (idx, mode) in modes.into_iter().enumerate() { | ||
| 319 | + let Mode { | ||
| 320 | + width, | ||
| 321 | + height, | ||
| 322 | + refresh_rate, | ||
| 323 | + is_preferred, | ||
| 324 | + } = mode; | ||
| 325 | + let refresh = refresh_rate as f64 / 1000.; | ||
| 326 | + | ||
| 327 | + let is_current = Some(idx) == current_mode; | ||
| 328 | + let qualifier = match (is_current, is_preferred) { | ||
| 329 | + (true, true) => " (current, preferred)", | ||
| 330 | + (true, false) => " (current)", | ||
| 331 | + (false, true) => " (preferred)", | ||
| 332 | + (false, false) => "", | ||
| 333 | + }; | ||
| 334 | + | ||
| 335 | + println!(" {width}x{height}@{refresh:.3}{qualifier}"); | ||
| 336 | + } | ||
| 337 | + Ok(()) | ||
| 338 | +} | ||
src/ipc/server.rsmodified+25 -0
| @@ -208,6 +208,31 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 208 | let workspaces = result.map_err(|_| String::from("error getting workspace info"))?; | 208 | let workspaces = result.map_err(|_| String::from("error getting workspace info"))?; |
| 209 | Response::Workspaces(workspaces) | 209 | Response::Workspaces(workspaces) |
| 210 | } | 210 | } |
| 211 | + Request::FocusedOutput => { | ||
| 212 | + let (tx, rx) = async_channel::bounded(1); | ||
| 213 | + ctx.event_loop.insert_idle(move |state| { | ||
| 214 | + let active_output = state | ||
| 215 | + .niri | ||
| 216 | + .layout | ||
| 217 | + .active_output() | ||
| 218 | + .map(|output| output.name()); | ||
| 219 | + | ||
| 220 | + let output = active_output.and_then(|active_output| { | ||
| 221 | + state | ||
| 222 | + .backend | ||
| 223 | + .ipc_outputs() | ||
| 224 | + .lock() | ||
| 225 | + .unwrap() | ||
| 226 | + .get(&active_output) | ||
| 227 | + .cloned() | ||
| 228 | + }); | ||
| 229 | + | ||
| 230 | + let _ = tx.send_blocking(output); | ||
| 231 | + }); | ||
| 232 | + let result = rx.recv().await; | ||
| 233 | + let output = result.map_err(|_| String::from("error getting active output info"))?; | ||
| 234 | + Response::FocusedOutput(output) | ||
| 235 | + } | ||
| 211 | }; | 236 | }; |
| 212 | 237 | ||
| 213 | Ok(response) | 238 | Ok(response) |