Rework output connection to always go through on_output_config_changed()
Browse files at this commit5ff8b89aafca0143866f93dd7e2f27aee6c2b0f5Parent: 927abad2 changed files +60 -46
- src/backend/tty.rs +58 -27
- src/niri.rs +2 -19
src/backend/tty.rsmodified+58 -27
| @@ -61,7 +61,7 @@ use crate::niri::{Niri, RedrawState, State}; | |||
| 61 | use crate::render_helpers::debug::draw_damage; | 61 | use crate::render_helpers::debug::draw_damage; |
| 62 | use crate::render_helpers::renderer::AsGlesRenderer; | 62 | use crate::render_helpers::renderer::AsGlesRenderer; |
| 63 | use crate::render_helpers::{resources, shaders, RenderTarget}; | 63 | 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}; |
| 65 | 65 | ||
| 66 | const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888]; | 66 | const SUPPORTED_COLOR_FORMATS: &[Fourcc] = &[Fourcc::Argb8888, Fourcc::Abgr8888]; |
| 67 | 67 | ||
| @@ -636,22 +636,31 @@ impl Tty { | |||
| 636 | connector, | 636 | connector, |
| 637 | crtc: Some(crtc), | 637 | crtc: Some(crtc), |
| 638 | } => { | 638 | } => { |
| 639 | - if let Err(err) = self.connector_connected(niri, node, connector, crtc) { | 639 | + let connector_name = format_connector_name(&connector); |
| 640 | - warn!("error connecting connector: {err:?}"); | 640 | + let output_name = |
| 641 | - } | 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()); | ||
| 642 | } | 650 | } |
| 643 | DrmScanEvent::Disconnected { | 651 | DrmScanEvent::Disconnected { |
| 644 | crtc: Some(crtc), .. | 652 | crtc: Some(crtc), .. |
| 645 | } => { | 653 | } => { |
| 646 | - self.connector_disconnected(niri, node, crtc); | ||
| 647 | removed.push(crtc); | 654 | removed.push(crtc); |
| 648 | } | 655 | } |
| 649 | _ => (), | 656 | _ => (), |
| 650 | } | 657 | } |
| 651 | } | 658 | } |
| 652 | 659 | ||
| 653 | - // FIXME: this is better done in connector_disconnected(), but currently we call that to | 660 | + for crtc in &removed { |
| 654 | - // turn off outputs temporarily, too. So we can't do this there. | 661 | + self.connector_disconnected(niri, node, *crtc); |
| 662 | + } | ||
| 663 | + | ||
| 655 | let Some(device) = self.devices.get_mut(&node) else { | 664 | let Some(device) = self.devices.get_mut(&node) else { |
| 656 | error!("device disappeared"); | 665 | error!("device disappeared"); |
| 657 | return; | 666 | return; |
| @@ -663,7 +672,12 @@ impl Tty { | |||
| 663 | } | 672 | } |
| 664 | } | 673 | } |
| 665 | 674 | ||
| 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); | ||
| 667 | } | 681 | } |
| 668 | 682 | ||
| 669 | fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) { | 683 | fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) { |
| @@ -772,10 +786,6 @@ impl Tty { | |||
| 772 | return Ok(()); | 786 | return Ok(()); |
| 773 | } | 787 | } |
| 774 | 788 | ||
| 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 | - | ||
| 779 | let config = self | 789 | let config = self |
| 780 | .config | 790 | .config |
| 781 | .borrow() | 791 | .borrow() |
| @@ -784,15 +794,6 @@ impl Tty { | |||
| 784 | .cloned() | 794 | .cloned() |
| 785 | .unwrap_or_default(); | 795 | .unwrap_or_default(); |
| 786 | 796 | ||
| 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 | - | ||
| 796 | for m in connector.modes() { | 797 | for m in connector.modes() { |
| 797 | trace!("{m:?}"); | 798 | trace!("{m:?}"); |
| 798 | } | 799 | } |
| @@ -1724,6 +1725,24 @@ impl Tty { | |||
| 1724 | } | 1725 | } |
| 1725 | self.update_output_config_on_resume = false; | 1726 | self.update_output_config_on_resume = false; |
| 1726 | 1727 | ||
| 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 | + | ||
| 1727 | let mut to_disconnect = vec![]; | 1746 | let mut to_disconnect = vec![]; |
| 1728 | let mut to_connect = vec![]; | 1747 | let mut to_connect = vec![]; |
| 1729 | 1748 | ||
| @@ -1736,7 +1755,7 @@ impl Tty { | |||
| 1736 | .find(&surface.name) | 1755 | .find(&surface.name) |
| 1737 | .cloned() | 1756 | .cloned() |
| 1738 | .unwrap_or_default(); | 1757 | .unwrap_or_default(); |
| 1739 | - if config.off || !niri.should_enable_laptop_panel(&surface.name.connector) { | 1758 | + if config.off || should_disable(&surface.name.connector) { |
| 1740 | to_disconnect.push((node, crtc)); | 1759 | to_disconnect.push((node, crtc)); |
| 1741 | continue; | 1760 | continue; |
| 1742 | } | 1761 | } |
| @@ -1832,7 +1851,11 @@ impl Tty { | |||
| 1832 | } | 1851 | } |
| 1833 | 1852 | ||
| 1834 | // Check if already enabled. | 1853 | // 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 | + { | ||
| 1836 | continue; | 1859 | continue; |
| 1837 | } | 1860 | } |
| 1838 | 1861 | ||
| @@ -1851,8 +1874,8 @@ impl Tty { | |||
| 1851 | .cloned() | 1874 | .cloned() |
| 1852 | .unwrap_or_default(); | 1875 | .unwrap_or_default(); |
| 1853 | 1876 | ||
| 1854 | - if !config.off && niri.should_enable_laptop_panel(&output_name.connector) { | 1877 | + if !(config.off || should_disable(&output_name.connector)) { |
| 1855 | - to_connect.push((node, connector.clone(), crtc)); | 1878 | + to_connect.push((node, connector.clone(), crtc, output_name)); |
| 1856 | } | 1879 | } |
| 1857 | } | 1880 | } |
| 1858 | } | 1881 | } |
| @@ -1861,7 +1884,11 @@ impl Tty { | |||
| 1861 | self.connector_disconnected(niri, node, crtc); | 1884 | self.connector_disconnected(niri, node, crtc); |
| 1862 | } | 1885 | } |
| 1863 | 1886 | ||
| 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 { | ||
| 1865 | if let Err(err) = self.connector_connected(niri, node, connector, crtc) { | 1892 | if let Err(err) = self.connector_connected(niri, node, connector, crtc) { |
| 1866 | warn!("error connecting connector: {err:?}"); | 1893 | warn!("error connecting connector: {err:?}"); |
| 1867 | } | 1894 | } |
| @@ -1896,7 +1923,11 @@ impl Tty { | |||
| 1896 | } | 1923 | } |
| 1897 | 1924 | ||
| 1898 | // Check if already enabled. | 1925 | // 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 | + { | ||
| 1900 | continue; | 1931 | continue; |
| 1901 | } | 1932 | } |
| 1902 | 1933 | ||
src/niri.rsmodified+2 -19
| @@ -143,9 +143,8 @@ use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRende | |||
| 143 | use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; | 143 | use crate::utils::scale::{closest_representable_scale, guess_monitor_scale}; |
| 144 | use crate::utils::spawning::CHILD_ENV; | 144 | use crate::utils::spawning::CHILD_ENV; |
| 145 | use crate::utils::{ | 145 | use crate::utils::{ |
| 146 | - center, center_f64, get_monotonic_time, ipc_transform_to_smithay, is_laptop_panel, | 146 | + center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output, |
| 147 | - logical_output, make_screenshot_path, output_matches_name, output_size, send_scale_transform, | 147 | + make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8, |
| 148 | - write_png_rgba8, | ||
| 149 | }; | 148 | }; |
| 150 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; | 149 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| 151 | use crate::{animation, niri_render_elements}; | 150 | use crate::{animation, niri_render_elements}; |
| @@ -4811,22 +4810,6 @@ impl Niri { | |||
| 4811 | .unwrap(); | 4810 | .unwrap(); |
| 4812 | self.pointer_inactivity_timer = Some(token); | 4811 | self.pointer_inactivity_timer = Some(token); |
| 4813 | } | 4812 | } |
| 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 | - } | ||
| 4830 | } | 4813 | } |
| 4831 | 4814 | ||
| 4832 | pub struct ClientState { | 4815 | pub struct ClientState { |