forge
Explore
Log in

Don't you dare go Hollow

Rework output connection to always go through on_output_config_changed()

Browse files at this commit

5ff8b89aafca0143866f93dd7e2f27aee6c2b0f5Ivan MolodetskikhParent: 927abad
This has the following benefits:
1. connector_connected() is now more closely mirroring
   connector_disconnected() in that it merely lights up the connector,
   and doesn't check if the connector should be off from the config.
2. We can use more complex on/off logic that depends on multiple
   connectors. For example, this commit adds logic to only disable the
   laptop panel on lid close if there are other connected outputs.

We don't want to disable the laptop panel on lid close if it's the only
connected output because it causes screen lockers to create their
surface from scratch on normal laptop unsuspend, which is undesirable
and also confuses some screen lockers.
src/backend/tty.rsmodified+58 -27
@@ -61,7 +61,7 @@ use crate::niri::{Niri, RedrawState, State};
6161 use crate::render_helpers::debug::draw_damage;
6262 use crate::render_helpers::renderer::AsGlesRenderer;
6363 use crate::render_helpers::{resources, shaders, RenderTarget};
64-use crate::utils::{get_monotonic_time, logical_output};
64+use crate::utils::{get_monotonic_time, is_laptop_panel, logical_output};
6565
6666 const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888];
6767
@@ -636,22 +636,31 @@ impl Tty {
636636 connector,
637637 crtc: Some(crtc),
638638 } => {
639- if let Err(err) = self.connector_connected(niri, node, connector, crtc) {
640- warn!("error connecting connector: {err:?}");
641- }
639+ let connector_name = format_connector_name(&connector);
640+ let output_name =
641+ make_output_name(&device.drm, connector.handle(), connector_name, false);
642+ debug!(
643+ "new connector: {} \"{}\"",
644+ &output_name.connector,
645+ output_name.format_make_model_serial(),
646+ );
647+
648+ // Assign an id to this crtc.
649+ device.output_ids.insert(crtc, OutputId::next());
642650 }
643651 DrmScanEvent::Disconnected {
644652 crtc: Some(crtc), ..
645653 } => {
646- self.connector_disconnected(niri, node, crtc);
647654 removed.push(crtc);
648655 }
649656 _ => (),
650657 }
651658 }
652659
653- // FIXME: this is better done in connector_disconnected(), but currently we call that to
654- // turn off outputs temporarily, too. So we can't do this there.
660+ for crtc in &removed {
661+ self.connector_disconnected(niri, node, *crtc);
662+ }
663+
655664 let Some(device) = self.devices.get_mut(&node) else {
656665 error!("device disappeared");
657666 return;
@@ -663,7 +672,12 @@ impl Tty {
663672 }
664673 }
665674
666- self.refresh_ipc_outputs(niri);
675+ // This will connect any new connectors if needed, and apply other changes, such as
676+ // connecting back the internal laptop monitor once it becomes the only monitor left.
677+ //
678+ // It will also call refresh_ipc_outputs(), which will catch the disconnected connectors
679+ // above.
680+ self.on_output_config_changed(niri);
667681 }
668682
669683 fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) {
@@ -772,10 +786,6 @@ impl Tty {
772786 return Ok(());
773787 }
774788
775- // This should be unique per CRTC connection, however currently we can call
776- // connector_connected() multiple times for turning the output off and on.
777- device.output_ids.entry(crtc).or_insert_with(OutputId::next);
778-
779789 let config = self
780790 .config
781791 .borrow()
@@ -784,15 +794,6 @@ impl Tty {
784794 .cloned()
785795 .unwrap_or_default();
786796
787- if config.off {
788- debug!("output is disabled in the config");
789- return Ok(());
790- }
791- if !niri.should_enable_laptop_panel(&output_name.connector) {
792- debug!("output is disabled because it is a laptop panel and the lid is closed");
793- return Ok(());
794- }
795-
796797 for m in connector.modes() {
797798 trace!("{m:?}");
798799 }
@@ -1724,6 +1725,24 @@ impl Tty {
17241725 }
17251726 self.update_output_config_on_resume = false;
17261727
1728+ // Figure out if we should disable laptop panels.
1729+ let mut disable_laptop_panels = false;
1730+ if niri.is_lid_closed {
1731+ let config = self.config.borrow();
1732+ if !config.debug.keep_laptop_panel_on_when_lid_is_closed {
1733+ // Check if any external monitor is connected.
1734+ 'outer: for device in self.devices.values() {
1735+ for (connector, _crtc) in device.drm_scanner.crtcs() {
1736+ if !is_laptop_panel(&format_connector_name(connector)) {
1737+ disable_laptop_panels = true;
1738+ break 'outer;
1739+ }
1740+ }
1741+ }
1742+ }
1743+ }
1744+ let should_disable = |connector: &str| disable_laptop_panels && is_laptop_panel(connector);
1745+
17271746 let mut to_disconnect = vec![];
17281747 let mut to_connect = vec![];
17291748
@@ -1736,7 +1755,7 @@ impl Tty {
17361755 .find(&surface.name)
17371756 .cloned()
17381757 .unwrap_or_default();
1739- if config.off || !niri.should_enable_laptop_panel(&surface.name.connector) {
1758+ if config.off || should_disable(&surface.name.connector) {
17401759 to_disconnect.push((node, crtc));
17411760 continue;
17421761 }
@@ -1832,7 +1851,11 @@ impl Tty {
18321851 }
18331852
18341853 // Check if already enabled.
1835- if device.surfaces.contains_key(&crtc) {
1854+ if device.surfaces.contains_key(&crtc)
1855+ || device
1856+ .non_desktop_connectors
1857+ .contains(&(connector.handle(), crtc))
1858+ {
18361859 continue;
18371860 }
18381861
@@ -1851,8 +1874,8 @@ impl Tty {
18511874 .cloned()
18521875 .unwrap_or_default();
18531876
1854- if !config.off && niri.should_enable_laptop_panel(&output_name.connector) {
1855- to_connect.push((node, connector.clone(), crtc));
1877+ if !(config.off || should_disable(&output_name.connector)) {
1878+ to_connect.push((node, connector.clone(), crtc, output_name));
18561879 }
18571880 }
18581881 }
@@ -1861,7 +1884,11 @@ impl Tty {
18611884 self.connector_disconnected(niri, node, crtc);
18621885 }
18631886
1864- for (node, connector, crtc) in to_connect {
1887+ // Sort by output name to get more predictable first focused output at initial compositor
1888+ // startup, when multiple connectors appear at once.
1889+ to_connect.sort_unstable_by(|a, b| a.3.compare(&b.3));
1890+
1891+ for (node, connector, crtc, _name) in to_connect {
18651892 if let Err(err) = self.connector_connected(niri, node, connector, crtc) {
18661893 warn!("error connecting connector: {err:?}");
18671894 }
@@ -1896,7 +1923,11 @@ impl Tty {
18961923 }
18971924
18981925 // Check if already enabled.
1899- if device.surfaces.contains_key(&crtc) {
1926+ if device.surfaces.contains_key(&crtc)
1927+ || device
1928+ .non_desktop_connectors
1929+ .contains(&(connector.handle(), crtc))
1930+ {
19001931 continue;
19011932 }
19021933
src/niri.rsmodified+2 -19
@@ -143,9 +143,8 @@ use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRende
143143 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};
144144 use crate::utils::spawning::CHILD_ENV;
145145 use crate::utils::{
146- center, center_f64, get_monotonic_time, ipc_transform_to_smithay, is_laptop_panel,
147- logical_output, make_screenshot_path, output_matches_name, output_size, send_scale_transform,
148- write_png_rgba8,
146+ center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output,
147+ make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8,
149148 };
150149 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};
151150 use crate::{animation, niri_render_elements};
@@ -4811,22 +4810,6 @@ impl Niri {
48114810 .unwrap();
48124811 self.pointer_inactivity_timer = Some(token);
48134812 }
4814-
4815- pub fn should_enable_laptop_panel(&self, connector: &str) -> bool {
4816- // Make sure the output config is reloaded when any of the conditions in this function
4817- // change.
4818-
4819- let config = self.config.borrow();
4820-
4821- // We reload the output config when this flag changes.
4822- if config.debug.keep_laptop_panel_on_when_lid_is_closed {
4823- return true;
4824- }
4825-
4826- // We reload the output config when the lid switch is toggled, and the connector name is
4827- // static.
4828- !(self.is_lid_closed && is_laptop_panel(connector))
4829- }
48304813 }
48314814
48324815 pub struct ClientState {