Add output ID tracking
Browse files at this commitd2087a2cd9f30e40778861666370df56b532af63Parent: c6811987 changed files +69 -18
- src/backend/mod.rs +17 -1
- src/backend/tty.rs +32 -4
- src/backend/winit.rs +3 -3
- src/dbus/mutter_display_config.rs +4 -3
- src/dbus/mutter_screen_cast.rs +5 -1
- src/ipc/server.rs +6 -4
- src/niri.rs +2 -2
src/backend/mod.rsmodified+17 -1
| @@ -9,6 +9,7 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | |||
| 9 | 9 | ||
| 10 | use crate::input::CompositorMod; | 10 | use crate::input::CompositorMod; |
| 11 | use crate::niri::Niri; | 11 | use crate::niri::Niri; |
| 12 | +use crate::utils::id::IdCounter; | ||
| 12 | 13 | ||
| 13 | pub mod tty; | 14 | pub mod tty; |
| 14 | pub use tty::Tty; | 15 | pub use tty::Tty; |
| @@ -31,7 +32,22 @@ pub enum RenderResult { | |||
| 31 | Skipped, | 32 | Skipped, |
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | -pub type IpcOutputMap = HashMap<String, niri_ipc::Output>; | 35 | +pub type IpcOutputMap = HashMap<OutputId, niri_ipc::Output>; |
| 36 | + | ||
| 37 | +static OUTPUT_ID_COUNTER: IdCounter = IdCounter::new(); | ||
| 38 | + | ||
| 39 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| 40 | +pub struct OutputId(u32); | ||
| 41 | + | ||
| 42 | +impl OutputId { | ||
| 43 | + fn next() -> OutputId { | ||
| 44 | + OutputId(OUTPUT_ID_COUNTER.next()) | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + pub fn get(self) -> u32 { | ||
| 48 | + self.0 | ||
| 49 | + } | ||
| 50 | +} | ||
| 35 | 51 | ||
| 36 | impl Backend { | 52 | impl Backend { |
| 37 | pub fn init(&mut self, niri: &mut Niri) { | 53 | pub fn init(&mut self, niri: &mut Niri) { |
src/backend/tty.rsmodified+32 -4
| @@ -57,6 +57,7 @@ use wayland_protocols::wp::linux_dmabuf::zv1::server::zwp_linux_dmabuf_feedback_ | |||
| 57 | use wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; | 57 | use wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 58 | 58 | ||
| 59 | use super::{IpcOutputMap, RenderResult}; | 59 | use super::{IpcOutputMap, RenderResult}; |
| 60 | +use crate::backend::OutputId; | ||
| 60 | use crate::frame_clock::FrameClock; | 61 | use crate::frame_clock::FrameClock; |
| 61 | use crate::niri::{Niri, RedrawState, State}; | 62 | use crate::niri::{Niri, RedrawState, State}; |
| 62 | use crate::render_helpers::debug::draw_damage; | 63 | use crate::render_helpers::debug::draw_damage; |
| @@ -118,6 +119,7 @@ pub struct OutputDevice { | |||
| 118 | render_node: DrmNode, | 119 | render_node: DrmNode, |
| 119 | drm_scanner: DrmScanner, | 120 | drm_scanner: DrmScanner, |
| 120 | surfaces: HashMap<crtc::Handle, Surface>, | 121 | surfaces: HashMap<crtc::Handle, Surface>, |
| 122 | + output_ids: HashMap<crtc::Handle, OutputId>, | ||
| 121 | // SAFETY: drop after all the objects used with them are dropped. | 123 | // SAFETY: drop after all the objects used with them are dropped. |
| 122 | // See https://github.com/Smithay/smithay/issues/1102. | 124 | // See https://github.com/Smithay/smithay/issues/1102. |
| 123 | drm: DrmDevice, | 125 | drm: DrmDevice, |
| @@ -575,6 +577,7 @@ impl Tty { | |||
| 575 | gbm, | 577 | gbm, |
| 576 | drm_scanner: DrmScanner::new(), | 578 | drm_scanner: DrmScanner::new(), |
| 577 | surfaces: HashMap::new(), | 579 | surfaces: HashMap::new(), |
| 580 | + output_ids: HashMap::new(), | ||
| 578 | drm_lease_state, | 581 | drm_lease_state, |
| 579 | active_leases: Vec::new(), | 582 | active_leases: Vec::new(), |
| 580 | non_desktop_connectors: HashSet::new(), | 583 | non_desktop_connectors: HashSet::new(), |
| @@ -599,6 +602,7 @@ impl Tty { | |||
| 599 | return; | 602 | return; |
| 600 | }; | 603 | }; |
| 601 | 604 | ||
| 605 | + let mut removed = Vec::new(); | ||
| 602 | for event in device.drm_scanner.scan_connectors(&device.drm) { | 606 | for event in device.drm_scanner.scan_connectors(&device.drm) { |
| 603 | match event { | 607 | match event { |
| 604 | DrmScanEvent::Connected { | 608 | DrmScanEvent::Connected { |
| @@ -611,11 +615,27 @@ impl Tty { | |||
| 611 | } | 615 | } |
| 612 | DrmScanEvent::Disconnected { | 616 | DrmScanEvent::Disconnected { |
| 613 | crtc: Some(crtc), .. | 617 | crtc: Some(crtc), .. |
| 614 | - } => self.connector_disconnected(niri, node, crtc), | 618 | + } => { |
| 619 | + self.connector_disconnected(niri, node, crtc); | ||
| 620 | + removed.push(crtc); | ||
| 621 | + } | ||
| 615 | _ => (), | 622 | _ => (), |
| 616 | } | 623 | } |
| 617 | } | 624 | } |
| 618 | 625 | ||
| 626 | + // FIXME: this is better done in connector_disconnected(), but currently we call that to | ||
| 627 | + // turn off outputs temporarily, too. So we can't do this there. | ||
| 628 | + let Some(device) = self.devices.get_mut(&node) else { | ||
| 629 | + error!("device disappeared"); | ||
| 630 | + return; | ||
| 631 | + }; | ||
| 632 | + | ||
| 633 | + for crtc in removed { | ||
| 634 | + if device.output_ids.remove(&crtc).is_none() { | ||
| 635 | + error!("output ID missing for disconnected crtc: {crtc:?}"); | ||
| 636 | + } | ||
| 637 | + } | ||
| 638 | + | ||
| 619 | self.refresh_ipc_outputs(niri); | 639 | self.refresh_ipc_outputs(niri); |
| 620 | } | 640 | } |
| 621 | 641 | ||
| @@ -724,6 +744,10 @@ impl Tty { | |||
| 724 | return Ok(()); | 744 | return Ok(()); |
| 725 | } | 745 | } |
| 726 | 746 | ||
| 747 | + // This should be unique per CRTC connection, however currently we can call | ||
| 748 | + // connector_connected() multiple times for turning the output off and on. | ||
| 749 | + device.output_ids.entry(crtc).or_insert_with(OutputId::next); | ||
| 750 | + | ||
| 727 | let config = self | 751 | let config = self |
| 728 | .config | 752 | .config |
| 729 | .borrow() | 753 | .borrow() |
| @@ -1464,7 +1488,7 @@ impl Tty { | |||
| 1464 | 1488 | ||
| 1465 | for (node, device) in &self.devices { | 1489 | for (node, device) in &self.devices { |
| 1466 | for (connector, crtc) in device.drm_scanner.crtcs() { | 1490 | for (connector, crtc) in device.drm_scanner.crtcs() { |
| 1467 | - let connector_name = format!( | 1491 | + let name = format!( |
| 1468 | "{}-{}", | 1492 | "{}-{}", |
| 1469 | connector.interface().as_str(), | 1493 | connector.interface().as_str(), |
| 1470 | connector.interface_id(), | 1494 | connector.interface_id(), |
| @@ -1527,7 +1551,7 @@ impl Tty { | |||
| 1527 | .map(logical_output); | 1551 | .map(logical_output); |
| 1528 | 1552 | ||
| 1529 | let ipc_output = niri_ipc::Output { | 1553 | let ipc_output = niri_ipc::Output { |
| 1530 | - name: connector_name.clone(), | 1554 | + name, |
| 1531 | make, | 1555 | make, |
| 1532 | model, | 1556 | model, |
| 1533 | physical_size, | 1557 | physical_size, |
| @@ -1538,7 +1562,11 @@ impl Tty { | |||
| 1538 | logical, | 1562 | logical, |
| 1539 | }; | 1563 | }; |
| 1540 | 1564 | ||
| 1541 | - ipc_outputs.insert(connector_name, ipc_output); | 1565 | + let id = device.output_ids.get(&crtc).copied().unwrap_or_else(|| { |
| 1566 | + error!("output ID missing for crtc: {crtc:?}"); | ||
| 1567 | + OutputId::next() | ||
| 1568 | + }); | ||
| 1569 | + ipc_outputs.insert(id, ipc_output); | ||
| 1542 | } | 1570 | } |
| 1543 | } | 1571 | } |
| 1544 | 1572 | ||
src/backend/winit.rsmodified+3 -3
| @@ -17,7 +17,7 @@ use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_pre | |||
| 17 | use smithay::reexports::winit::dpi::LogicalSize; | 17 | use smithay::reexports::winit::dpi::LogicalSize; |
| 18 | use smithay::reexports::winit::window::Window; | 18 | use smithay::reexports::winit::window::Window; |
| 19 | 19 | ||
| 20 | -use super::{IpcOutputMap, RenderResult}; | 20 | +use super::{IpcOutputMap, OutputId, RenderResult}; |
| 21 | use crate::niri::{Niri, RedrawState, State}; | 21 | use crate::niri::{Niri, RedrawState, State}; |
| 22 | use crate::render_helpers::debug::draw_damage; | 22 | use crate::render_helpers::debug::draw_damage; |
| 23 | use crate::render_helpers::{resources, shaders, RenderTarget}; | 23 | use crate::render_helpers::{resources, shaders, RenderTarget}; |
| @@ -61,7 +61,7 @@ impl Winit { | |||
| 61 | 61 | ||
| 62 | let physical_properties = output.physical_properties(); | 62 | let physical_properties = output.physical_properties(); |
| 63 | let ipc_outputs = Arc::new(Mutex::new(HashMap::from([( | 63 | let ipc_outputs = Arc::new(Mutex::new(HashMap::from([( |
| 64 | - "winit".to_owned(), | 64 | + OutputId::next(), |
| 65 | niri_ipc::Output { | 65 | niri_ipc::Output { |
| 66 | name: output.name(), | 66 | name: output.name(), |
| 67 | make: physical_properties.make, | 67 | make: physical_properties.make, |
| @@ -98,7 +98,7 @@ impl Winit { | |||
| 98 | 98 | ||
| 99 | { | 99 | { |
| 100 | let mut ipc_outputs = winit.ipc_outputs.lock().unwrap(); | 100 | let mut ipc_outputs = winit.ipc_outputs.lock().unwrap(); |
| 101 | - let output = ipc_outputs.get_mut("winit").unwrap(); | 101 | + let output = ipc_outputs.values_mut().next().unwrap(); |
| 102 | let mode = &mut output.modes[0]; | 102 | let mode = &mut output.modes[0]; |
| 103 | mode.width = size.w.clamp(0, u16::MAX as i32) as u16; | 103 | mode.width = size.w.clamp(0, u16::MAX as i32) as u16; |
| 104 | mode.height = size.h.clamp(0, u16::MAX as i32) as u16; | 104 | mode.height = size.h.clamp(0, u16::MAX as i32) as u16; |
src/dbus/mutter_display_config.rsmodified+4 -3
| @@ -57,11 +57,12 @@ impl DisplayConfig { | |||
| 57 | .ipc_outputs | 57 | .ipc_outputs |
| 58 | .lock() | 58 | .lock() |
| 59 | .unwrap() | 59 | .unwrap() |
| 60 | - .iter() | 60 | + .values() |
| 61 | // Take only enabled outputs. | 61 | // Take only enabled outputs. |
| 62 | - .filter(|(_, output)| output.current_mode.is_some() && output.logical.is_some()) | 62 | + .filter(|output| output.current_mode.is_some() && output.logical.is_some()) |
| 63 | - .map(|(c, output)| { | 63 | + .map(|output| { |
| 64 | // Loosely matches the check in Mutter. | 64 | // Loosely matches the check in Mutter. |
| 65 | + let c = &output.name; | ||
| 65 | let is_laptop_panel = matches!(c.get(..4), Some("eDP-" | "LVDS" | "DSI-")); | 66 | let is_laptop_panel = matches!(c.get(..4), Some("eDP-" | "LVDS" | "DSI-")); |
| 66 | 67 | ||
| 67 | // FIXME: use proper serial when we have libdisplay-info. | 68 | // FIXME: use proper serial when we have libdisplay-info. |
src/dbus/mutter_screen_cast.rsmodified+5 -1
| @@ -191,7 +191,11 @@ impl Session { | |||
| 191 | ) -> fdo::Result<OwnedObjectPath> { | 191 | ) -> fdo::Result<OwnedObjectPath> { |
| 192 | debug!(connector, ?properties, "record_monitor"); | 192 | debug!(connector, ?properties, "record_monitor"); |
| 193 | 193 | ||
| 194 | - let Some(output) = self.ipc_outputs.lock().unwrap().get(connector).cloned() else { | 194 | + let output = { |
| 195 | + let ipc_outputs = self.ipc_outputs.lock().unwrap(); | ||
| 196 | + ipc_outputs.values().find(|o| o.name == connector).cloned() | ||
| 197 | + }; | ||
| 198 | + let Some(output) = output else { | ||
| 195 | return Err(fdo::Error::Failed("no such monitor".to_owned())); | 199 | return Err(fdo::Error::Failed("no such monitor".to_owned())); |
| 196 | }; | 200 | }; |
| 197 | 201 | ||
src/ipc/server.rsmodified+6 -4
| @@ -143,7 +143,8 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 143 | Request::Version => Response::Version(version()), | 143 | Request::Version => Response::Version(version()), |
| 144 | Request::Outputs => { | 144 | Request::Outputs => { |
| 145 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap().clone(); | 145 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap().clone(); |
| 146 | - Response::Outputs(ipc_outputs) | 146 | + let outputs = ipc_outputs.values().cloned().map(|o| (o.name.clone(), o)); |
| 147 | + Response::Outputs(outputs.collect()) | ||
| 147 | } | 148 | } |
| 148 | Request::FocusedWindow => { | 149 | Request::FocusedWindow => { |
| 149 | let window = ctx.ipc_focused_window.lock().unwrap().clone(); | 150 | let window = ctx.ipc_focused_window.lock().unwrap().clone(); |
| @@ -183,8 +184,8 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 183 | Request::Output { output, action } => { | 184 | Request::Output { output, action } => { |
| 184 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap(); | 185 | let ipc_outputs = ctx.ipc_outputs.lock().unwrap(); |
| 185 | let found = ipc_outputs | 186 | let found = ipc_outputs |
| 186 | - .keys() | 187 | + .values() |
| 187 | - .any(|name| name.eq_ignore_ascii_case(&output)); | 188 | + .any(|o| o.name.eq_ignore_ascii_case(&output)); |
| 188 | let response = if found { | 189 | let response = if found { |
| 189 | OutputConfigChanged::Applied | 190 | OutputConfigChanged::Applied |
| 190 | } else { | 191 | } else { |
| @@ -223,7 +224,8 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | |||
| 223 | .ipc_outputs() | 224 | .ipc_outputs() |
| 224 | .lock() | 225 | .lock() |
| 225 | .unwrap() | 226 | .unwrap() |
| 226 | - .get(&active_output) | 227 | + .values() |
| 228 | + .find(|o| o.name == active_output) | ||
| 227 | .cloned() | 229 | .cloned() |
| 228 | }); | 230 | }); |
| 229 | 231 | ||
src/niri.rsmodified+2 -2
| @@ -1166,12 +1166,12 @@ impl State { | |||
| 1166 | 1166 | ||
| 1167 | let _span = tracy_client::span!("State::refresh_ipc_outputs"); | 1167 | let _span = tracy_client::span!("State::refresh_ipc_outputs"); |
| 1168 | 1168 | ||
| 1169 | - for (name, ipc_output) in self.backend.ipc_outputs().lock().unwrap().iter_mut() { | 1169 | + for ipc_output in self.backend.ipc_outputs().lock().unwrap().values_mut() { |
| 1170 | let logical = self | 1170 | let logical = self |
| 1171 | .niri | 1171 | .niri |
| 1172 | .global_space | 1172 | .global_space |
| 1173 | .outputs() | 1173 | .outputs() |
| 1174 | - .find(|output| output.name() == *name) | 1174 | + .find(|output| output.name() == ipc_output.name) |
| 1175 | .map(logical_output); | 1175 | .map(logical_output); |
| 1176 | ipc_output.logical = logical; | 1176 | ipc_output.logical = logical; |
| 1177 | } | 1177 | } |