Disable laptop panel when the lid is closed
Browse files at this commitcd90dfc7be11147936c2b637ae822ff0adf5a7b7Parent: a778ab36 changed files +66 -9
- niri-config/src/lib.rs +2 -0
- src/backend/tty.rs +6 -2
- src/input/mod.rs +7 -0
- src/niri.rs +32 -2
- wiki/Configuration:-Debug-Options.md +14 -0
- wiki/Configuration:-Switch-Events.md +5 -5
niri-config/src/lib.rsmodified+2 -0
| @@ -1533,6 +1533,8 @@ pub struct DebugConfig { | |||
| 1533 | pub disable_resize_throttling: bool, | 1533 | pub disable_resize_throttling: bool, |
| 1534 | #[knuffel(child)] | 1534 | #[knuffel(child)] |
| 1535 | pub disable_transactions: bool, | 1535 | pub disable_transactions: bool, |
| 1536 | + #[knuffel(child)] | ||
| 1537 | + pub keep_laptop_panel_on_when_lid_is_closed: bool, | ||
| 1536 | } | 1538 | } |
| 1537 | 1539 | ||
| 1538 | #[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)] | 1540 | #[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)] |
src/backend/tty.rsmodified+6 -2
| @@ -783,6 +783,10 @@ impl Tty { | |||
| 783 | debug!("output is disabled in the config"); | 783 | debug!("output is disabled in the config"); |
| 784 | return Ok(()); | 784 | return Ok(()); |
| 785 | } | 785 | } |
| 786 | + if !niri.should_enable_laptop_panel(&output_name.connector) { | ||
| 787 | + debug!("output is disabled because it is a laptop panel and the lid is closed"); | ||
| 788 | + return Ok(()); | ||
| 789 | + } | ||
| 786 | 790 | ||
| 787 | for m in connector.modes() { | 791 | for m in connector.modes() { |
| 788 | trace!("{m:?}"); | 792 | trace!("{m:?}"); |
| @@ -1723,7 +1727,7 @@ impl Tty { | |||
| 1723 | .find(&surface.name) | 1727 | .find(&surface.name) |
| 1724 | .cloned() | 1728 | .cloned() |
| 1725 | .unwrap_or_default(); | 1729 | .unwrap_or_default(); |
| 1726 | - if config.off { | 1730 | + if config.off || !niri.should_enable_laptop_panel(&surface.name.connector) { |
| 1727 | to_disconnect.push((node, crtc)); | 1731 | to_disconnect.push((node, crtc)); |
| 1728 | continue; | 1732 | continue; |
| 1729 | } | 1733 | } |
| @@ -1833,7 +1837,7 @@ impl Tty { | |||
| 1833 | .cloned() | 1837 | .cloned() |
| 1834 | .unwrap_or_default(); | 1838 | .unwrap_or_default(); |
| 1835 | 1839 | ||
| 1836 | - if !config.off { | 1840 | + if !config.off && niri.should_enable_laptop_panel(&output_name.connector) { |
| 1837 | to_connect.push((node, connector.clone(), crtc)); | 1841 | to_connect.push((node, connector.clone(), crtc)); |
| 1838 | } | 1842 | } |
| 1839 | } | 1843 | } |
src/input/mod.rsmodified+7 -0
| @@ -2434,6 +2434,13 @@ impl State { | |||
| 2434 | return; | 2434 | return; |
| 2435 | }; | 2435 | }; |
| 2436 | 2436 | ||
| 2437 | + if switch == Switch::Lid { | ||
| 2438 | + let is_closed = evt.state() == SwitchState::On; | ||
| 2439 | + debug!("lid switch {}", if is_closed { "closed" } else { "opened" }); | ||
| 2440 | + self.niri.is_lid_closed = is_closed; | ||
| 2441 | + self.reload_output_config(); | ||
| 2442 | + } | ||
| 2443 | + | ||
| 2437 | let action = { | 2444 | let action = { |
| 2438 | let bindings = &self.niri.config.borrow().switch_events; | 2445 | let bindings = &self.niri.config.borrow().switch_events; |
| 2439 | find_configured_switch_action(bindings, switch, evt.state()) | 2446 | find_configured_switch_action(bindings, switch, evt.state()) |
src/niri.rsmodified+32 -2
| @@ -143,8 +143,9 @@ 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, logical_output, | 146 | + center, center_f64, get_monotonic_time, ipc_transform_to_smithay, is_laptop_panel, |
| 147 | - make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8, | 147 | + logical_output, make_screenshot_path, output_matches_name, output_size, send_scale_transform, |
| 148 | + write_png_rgba8, | ||
| 148 | }; | 149 | }; |
| 149 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; | 150 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| 150 | use crate::{animation, niri_render_elements}; | 151 | use crate::{animation, niri_render_elements}; |
| @@ -207,6 +208,12 @@ pub struct Niri { | |||
| 207 | // When false, we're idling with monitors powered off. | 208 | // When false, we're idling with monitors powered off. |
| 208 | pub monitors_active: bool, | 209 | pub monitors_active: bool, |
| 209 | 210 | ||
| 211 | + /// Whether the laptop lid is closed. | ||
| 212 | + /// | ||
| 213 | + /// Libinput guarantees that the lid switch starts in open state, and if it was closed during | ||
| 214 | + /// startup, libinput will immediately send a closed event. | ||
| 215 | + pub is_lid_closed: bool, | ||
| 216 | + | ||
| 210 | pub devices: HashSet<input::Device>, | 217 | pub devices: HashSet<input::Device>, |
| 211 | pub tablets: HashMap<input::Device, TabletData>, | 218 | pub tablets: HashMap<input::Device, TabletData>, |
| 212 | pub touch: HashSet<input::Device>, | 219 | pub touch: HashSet<input::Device>, |
| @@ -1101,6 +1108,12 @@ impl State { | |||
| 1101 | 1108 | ||
| 1102 | if config.debug != old_config.debug { | 1109 | if config.debug != old_config.debug { |
| 1103 | debug_config_changed = true; | 1110 | debug_config_changed = true; |
| 1111 | + | ||
| 1112 | + if config.debug.keep_laptop_panel_on_when_lid_is_closed | ||
| 1113 | + != old_config.debug.keep_laptop_panel_on_when_lid_is_closed | ||
| 1114 | + { | ||
| 1115 | + output_config_changed = true; | ||
| 1116 | + } | ||
| 1104 | } | 1117 | } |
| 1105 | 1118 | ||
| 1106 | *old_config = config; | 1119 | *old_config = config; |
| @@ -1831,6 +1844,7 @@ impl Niri { | |||
| 1831 | blocker_cleared_tx, | 1844 | blocker_cleared_tx, |
| 1832 | blocker_cleared_rx, | 1845 | blocker_cleared_rx, |
| 1833 | monitors_active: true, | 1846 | monitors_active: true, |
| 1847 | + is_lid_closed: false, | ||
| 1834 | 1848 | ||
| 1835 | devices: HashSet::new(), | 1849 | devices: HashSet::new(), |
| 1836 | tablets: HashMap::new(), | 1850 | tablets: HashMap::new(), |
| @@ -4797,6 +4811,22 @@ impl Niri { | |||
| 4797 | .unwrap(); | 4811 | .unwrap(); |
| 4798 | self.pointer_inactivity_timer = Some(token); | 4812 | self.pointer_inactivity_timer = Some(token); |
| 4799 | } | 4813 | } |
| 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 | + } | ||
| 4800 | } | 4830 | } |
| 4801 | 4831 | ||
| 4802 | pub struct ClientState { | 4832 | pub struct ClientState { |
wiki/Configuration:-Debug-Options.mdmodified+14 -0
| @@ -22,6 +22,7 @@ debug { | |||
| 22 | emulate-zero-presentation-time | 22 | emulate-zero-presentation-time |
| 23 | disable-resize-throttling | 23 | disable-resize-throttling |
| 24 | disable-transactions | 24 | disable-transactions |
| 25 | + keep-laptop-panel-on-when-lid-is-closed | ||
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | binds { | 28 | binds { |
| @@ -165,6 +166,19 @@ debug { | |||
| 165 | } | 166 | } |
| 166 | ``` | 167 | ``` |
| 167 | 168 | ||
| 169 | +### `keep-laptop-panel-on-when-lid-is-closed` | ||
| 170 | + | ||
| 171 | +<sup>Since: 0.1.10</sup> | ||
| 172 | + | ||
| 173 | +By default, niri will disable the internal laptop monitor when the laptop lid is closed. | ||
| 174 | +This flag turns off this behavior and will leave the internal laptop monitor on. | ||
| 175 | + | ||
| 176 | +```kdl | ||
| 177 | +debug { | ||
| 178 | + keep-laptop-panel-on-when-lid-is-closed | ||
| 179 | +} | ||
| 180 | +``` | ||
| 181 | + | ||
| 168 | ### Key Bindings | 182 | ### Key Bindings |
| 169 | 183 | ||
| 170 | These are not debug options, but rather key bindings. | 184 | These are not debug options, but rather key bindings. |
wiki/Configuration:-Switch-Events.mdmodified+5 -5
| @@ -8,8 +8,8 @@ Here are all the events that you can bind at a glance: | |||
| 8 | 8 | ||
| 9 | ```kdl | 9 | ```kdl |
| 10 | switch-events { | 10 | switch-events { |
| 11 | - lid-close { spawn "bash" "-c" "niri msg output \"eDP-1\" off"; } | 11 | + lid-close { spawn "notify-send" "The laptop lid is closed!"; } |
| 12 | - lid-open { spawn "bash" "-c" "niri msg output \"eDP-1\" on"; } | 12 | + lid-open { spawn "notify-send" "The laptop lid is open!"; } |
| 13 | tablet-mode-on { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true"; } | 13 | tablet-mode-on { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true"; } |
| 14 | tablet-mode-off { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false"; } | 14 | tablet-mode-off { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false"; } |
| 15 | } | 15 | } |
| @@ -25,12 +25,12 @@ Currently, only the `spawn` action are supported. | |||
| 25 | 25 | ||
| 26 | These events correspond to closing and opening of the laptop lid. | 26 | These events correspond to closing and opening of the laptop lid. |
| 27 | 27 | ||
| 28 | -You could use them to turn the laptop internal monitor off and on (until niri gets this functionality built-in). | 28 | +Note that niri will already automatically turn the internal laptop monitor on and off in accordance with the laptop lid. |
| 29 | 29 | ||
| 30 | ```kdl | 30 | ```kdl |
| 31 | switch-events { | 31 | switch-events { |
| 32 | - lid-close { spawn "bash" "-c" "niri msg output \"eDP-1\" off"; } | 32 | + lid-close { spawn "notify-send" "The laptop lid is closed!"; } |
| 33 | - lid-open { spawn "bash" "-c" "niri msg output \"eDP-1\" on"; } | 33 | + lid-open { spawn "notify-send" "The laptop lid is open!"; } |
| 34 | } | 34 | } |
| 35 | ``` | 35 | ``` |
| 36 | 36 | ||