Add variable-refresh-rate "fullscreen-only" window rule
Browse files at this commit83193223171fb55f5bfceaa2fe9aaf6cf6159684Parent: 964275e8 changed files +112 -29
- docs/wiki/Configuration:-Window-Rules.md +12 -0
- niri-config/src/lib.rs +4 -1
- niri-config/src/window_rule.rs +46 -1
- src/layout/monitor.rs +7 -3
- src/layout/scrolling.rs +9 -6
- src/layout/workspace.rs +3 -3
- src/niri.rs +29 -13
- src/window/mod.rs +2 -2
docs/wiki/Configuration:-Window-Rules.mdmodified+12 -0
| @@ -668,6 +668,18 @@ window-rule { | |||
| 668 | } | 668 | } |
| 669 | ``` | 669 | ``` |
| 670 | 670 | ||
| 671 | +<sup>Since: next release</sup> You can set `variable-refresh-rate` to `"fullscreen-only"` to activate on-demand VRR only when this window is fullscreen on this output. | ||
| 672 | +The window must cover the entire screen (when niri hides the top layer-shell layer). | ||
| 673 | + | ||
| 674 | +This mode will work even when there are overlays over the fullscreen window. | ||
| 675 | + | ||
| 676 | +```kdl | ||
| 677 | +// Enable on-demand VRR when any window is fullscreen. | ||
| 678 | +window-rule { | ||
| 679 | + variable-refresh-rate "fullscreen-only" | ||
| 680 | +} | ||
| 681 | +``` | ||
| 682 | + | ||
| 671 | #### `default-column-display` | 683 | #### `default-column-display` |
| 672 | 684 | ||
| 673 | <sup>Since: 25.02</sup> | 685 | <sup>Since: 25.02</sup> |
niri-config/src/lib.rsmodified+4 -1
| @@ -909,6 +909,7 @@ mod tests { | |||
| 909 | default-column-display "tabbed" | 909 | default-column-display "tabbed" |
| 910 | default-floating-position x=100 y=-200 relative-to="bottom-left" | 910 | default-floating-position x=100 y=-200 relative-to="bottom-left" |
| 911 | on-xdg-activate "ignore" | 911 | on-xdg-activate "ignore" |
| 912 | + variable-refresh-rate "fullscreen-only" | ||
| 912 | 913 | ||
| 913 | focus-ring { | 914 | focus-ring { |
| 914 | off | 915 | off |
| @@ -1900,7 +1901,9 @@ mod tests { | |||
| 1900 | clip_to_geometry: None, | 1901 | clip_to_geometry: None, |
| 1901 | baba_is_float: None, | 1902 | baba_is_float: None, |
| 1902 | block_out_from: None, | 1903 | block_out_from: None, |
| 1903 | - variable_refresh_rate: None, | 1904 | + variable_refresh_rate: Some( |
| 1905 | + FullscreenOnly, | ||
| 1906 | + ), | ||
| 1904 | default_column_display: Some( | 1907 | default_column_display: Some( |
| 1905 | Tabbed, | 1908 | Tabbed, |
| 1906 | ), | 1909 | ), |
niri-config/src/window_rule.rsmodified+46 -1
| @@ -1,3 +1,4 @@ | |||
| 1 | +use knuffel::errors::DecodeError; | ||
| 1 | use niri_ipc::ColumnDisplay; | 2 | use niri_ipc::ColumnDisplay; |
| 2 | 3 | ||
| 3 | use crate::appearance::{ | 4 | use crate::appearance::{ |
| @@ -68,7 +69,7 @@ pub struct WindowRule { | |||
| 68 | #[knuffel(child, unwrap(argument))] | 69 | #[knuffel(child, unwrap(argument))] |
| 69 | pub block_out_from: Option<BlockOutFrom>, | 70 | pub block_out_from: Option<BlockOutFrom>, |
| 70 | #[knuffel(child, unwrap(argument))] | 71 | #[knuffel(child, unwrap(argument))] |
| 71 | - pub variable_refresh_rate: Option<bool>, | 72 | + pub variable_refresh_rate: Option<VrrRule>, |
| 72 | #[knuffel(child, unwrap(argument, str))] | 73 | #[knuffel(child, unwrap(argument, str))] |
| 73 | pub default_column_display: Option<ColumnDisplay>, | 74 | pub default_column_display: Option<ColumnDisplay>, |
| 74 | #[knuffel(child)] | 75 | #[knuffel(child)] |
| @@ -85,6 +86,16 @@ pub struct WindowRule { | |||
| 85 | pub popups: PopupsRule, | 86 | pub popups: PopupsRule, |
| 86 | } | 87 | } |
| 87 | 88 | ||
| 89 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 90 | +pub enum VrrRule { | ||
| 91 | + /// No effect on VRR. | ||
| 92 | + Off, | ||
| 93 | + /// Enable VRR when the window is visible. | ||
| 94 | + On, | ||
| 95 | + /// Enable VRR when the window is the current fullscreen window. | ||
| 96 | + FullscreenOnly, | ||
| 97 | +} | ||
| 98 | + | ||
| 88 | /// Rules for popup surfaces. | 99 | /// Rules for popup surfaces. |
| 89 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | 100 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| 90 | pub struct PopupsRule { | 101 | pub struct PopupsRule { |
| @@ -172,3 +183,37 @@ pub enum RelativeTo { | |||
| 172 | Left, | 183 | Left, |
| 173 | Right, | 184 | Right, |
| 174 | } | 185 | } |
| 186 | + | ||
| 187 | +impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for VrrRule { | ||
| 188 | + fn raw_decode( | ||
| 189 | + val: &knuffel::span::Spanned<knuffel::ast::Literal, S>, | ||
| 190 | + _ctx: &mut knuffel::decode::Context<S>, | ||
| 191 | + ) -> Result<VrrRule, DecodeError<S>> { | ||
| 192 | + const ERROR: &str = "expected `true`, `\"fullscreen-only\"`, or `false`"; | ||
| 193 | + match &**val { | ||
| 194 | + knuffel::ast::Literal::Bool(b) => Ok(if *b { VrrRule::On } else { VrrRule::Off }), | ||
| 195 | + knuffel::ast::Literal::String(s) => match &s[..] { | ||
| 196 | + // Also accept bool strings to reduce confusion. | ||
| 197 | + "true" => Ok(VrrRule::On), | ||
| 198 | + "false" => Ok(VrrRule::Off), | ||
| 199 | + "fullscreen-only" => Ok(VrrRule::FullscreenOnly), | ||
| 200 | + _ => Err(DecodeError::conversion(val, ERROR)), | ||
| 201 | + }, | ||
| 202 | + _ => Err(DecodeError::conversion(val, ERROR)), | ||
| 203 | + } | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + fn type_check( | ||
| 207 | + type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>, | ||
| 208 | + ctx: &mut knuffel::decode::Context<S>, | ||
| 209 | + ) { | ||
| 210 | + if let Some(typ) = type_name { | ||
| 211 | + ctx.emit_error(DecodeError::TypeName { | ||
| 212 | + span: typ.span().clone(), | ||
| 213 | + found: Some((**typ).clone()), | ||
| 214 | + expected: knuffel::errors::ExpectedType::no_type(), | ||
| 215 | + rust_type: stringify!(VrrRule), | ||
| 216 | + }); | ||
| 217 | + } | ||
| 218 | + } | ||
| 219 | +} | ||
src/layout/monitor.rsmodified+7 -3
| @@ -1641,14 +1641,18 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 1641 | (InsertWorkspace::NewAt(last_idx + 1), dummy) | 1641 | (InsertWorkspace::NewAt(last_idx + 1), dummy) |
| 1642 | } | 1642 | } |
| 1643 | 1643 | ||
| 1644 | - pub fn render_above_top_layer(&self) -> bool { | 1644 | + pub fn fullscreen_window_above_top_layer(&self) -> Option<&W> { |
| 1645 | // Render above the top layer only if the view is stationary. | 1645 | // Render above the top layer only if the view is stationary. |
| 1646 | if self.workspace_switch.is_some() || self.overview_progress.is_some() { | 1646 | if self.workspace_switch.is_some() || self.overview_progress.is_some() { |
| 1647 | - return false; | 1647 | + return None; |
| 1648 | } | 1648 | } |
| 1649 | 1649 | ||
| 1650 | let ws = &self.workspaces[self.active_workspace_idx]; | 1650 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 1651 | - ws.render_above_top_layer() | 1651 | + ws.fullscreen_window_above_top_layer() |
| 1652 | + } | ||
| 1653 | + | ||
| 1654 | + pub fn render_above_top_layer(&self) -> bool { | ||
| 1655 | + self.fullscreen_window_above_top_layer().is_some() | ||
| 1652 | } | 1656 | } |
| 1653 | 1657 | ||
| 1654 | pub fn render_insert_hint_between_workspaces<R: NiriRenderer>( | 1658 | pub fn render_insert_hint_between_workspaces<R: NiriRenderer>( |
src/layout/scrolling.rsmodified+9 -6
| @@ -2943,19 +2943,22 @@ impl<W: LayoutElement> ScrollingSpace<W> { | |||
| 2943 | true | 2943 | true |
| 2944 | } | 2944 | } |
| 2945 | 2945 | ||
| 2946 | - pub fn render_above_top_layer(&self) -> bool { | 2946 | + pub fn fullscreen_window_above_top_layer(&self) -> Option<&W> { |
| 2947 | // Render above the top layer if we're on a fullscreen window and the view is stationary. | 2947 | // Render above the top layer if we're on a fullscreen window and the view is stationary. |
| 2948 | if self.columns.is_empty() { | 2948 | if self.columns.is_empty() { |
| 2949 | - return false; | 2949 | + return None; |
| 2950 | } | 2950 | } |
| 2951 | 2951 | ||
| 2952 | if !self.view_offset.is_static() { | 2952 | if !self.view_offset.is_static() { |
| 2953 | - return false; | 2953 | + return None; |
| 2954 | } | 2954 | } |
| 2955 | 2955 | ||
| 2956 | - self.columns[self.active_column_idx] | 2956 | + let col = &self.columns[self.active_column_idx]; |
| 2957 | - .sizing_mode() | 2957 | + if !col.sizing_mode().is_fullscreen() { |
| 2958 | - .is_fullscreen() | 2958 | + return None; |
| 2959 | + } | ||
| 2960 | + | ||
| 2961 | + Some(col.tiles[col.active_tile_idx].window()) | ||
| 2959 | } | 2962 | } |
| 2960 | 2963 | ||
| 2961 | pub fn render<R: NiriRenderer>( | 2964 | pub fn render<R: NiriRenderer>( |
src/layout/workspace.rsmodified+3 -3
| @@ -1715,8 +1715,8 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1715 | ) | 1715 | ) |
| 1716 | } | 1716 | } |
| 1717 | 1717 | ||
| 1718 | - pub fn render_above_top_layer(&self) -> bool { | 1718 | + pub fn fullscreen_window_above_top_layer(&self) -> Option<&W> { |
| 1719 | - self.scrolling.render_above_top_layer() | 1719 | + self.scrolling.fullscreen_window_above_top_layer() |
| 1720 | } | 1720 | } |
| 1721 | 1721 | ||
| 1722 | pub fn is_floating_visible(&self) -> bool { | 1722 | pub fn is_floating_visible(&self) -> bool { |
| @@ -1724,7 +1724,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1724 | matches!( | 1724 | matches!( |
| 1725 | self.floating_is_active, | 1725 | self.floating_is_active, |
| 1726 | FloatingActive::Yes | FloatingActive::NoButRaised | 1726 | FloatingActive::Yes | FloatingActive::NoButRaised |
| 1727 | - ) || !self.render_above_top_layer() | 1727 | + ) || self.fullscreen_window_above_top_layer().is_none() |
| 1728 | } | 1728 | } |
| 1729 | 1729 | ||
| 1730 | pub fn store_unmap_snapshot_if_empty( | 1730 | pub fn store_unmap_snapshot_if_empty( |
src/niri.rsmodified+29 -13
| @@ -15,6 +15,7 @@ use anyhow::{bail, ensure, Context}; | |||
| 15 | use calloop::futures::Scheduler; | 15 | use calloop::futures::Scheduler; |
| 16 | use niri_config::debug::PreviewRender; | 16 | use niri_config::debug::PreviewRender; |
| 17 | use niri_config::output::MaxBpc; | 17 | use niri_config::output::MaxBpc; |
| 18 | +use niri_config::window_rule::VrrRule; | ||
| 18 | use niri_config::{ | 19 | use niri_config::{ |
| 19 | Config, FloatOrInt, Key, Modifiers, OutputName, TrackLayout, WarpMouseToFocusMode, | 20 | Config, FloatOrInt, Key, Modifiers, OutputName, TrackLayout, WarpMouseToFocusMode, |
| 20 | WorkspaceReference, Xkb, | 21 | WorkspaceReference, Xkb, |
| @@ -5016,19 +5017,34 @@ impl Niri { | |||
| 5016 | return; | 5017 | return; |
| 5017 | } | 5018 | } |
| 5018 | 5019 | ||
| 5019 | - let current = self.layout.windows_for_output(output).any(|mapped| { | 5020 | + // Check for fullscreen-only VRR first as that's cheaper. |
| 5020 | - mapped.rules().variable_refresh_rate == Some(true) && { | 5021 | + let mon = self.layout.monitor_for_output(output).unwrap(); |
| 5021 | - let mut visible = false; | 5022 | + // We allow the overlay layer and floating windows for this check |
| 5022 | - mapped.window.with_surfaces(|surface, states| { | 5023 | + // so VRR keeps working with overlays. |
| 5023 | - if !visible | 5024 | + let current = mon |
| 5024 | - && surface_primary_scanout_output(surface, states).as_ref() == Some(output) | 5025 | + .fullscreen_window_above_top_layer() |
| 5025 | - { | 5026 | + .is_some_and(|mapped| { |
| 5026 | - visible = true; | 5027 | + mapped.rules().variable_refresh_rate == Some(VrrRule::FullscreenOnly) |
| 5027 | - } | 5028 | + }); |
| 5028 | - }); | 5029 | + |
| 5029 | - visible | 5030 | + let current = current |
| 5030 | - } | 5031 | + || self.layout.windows_for_output(output).any(|mapped| { |
| 5031 | - }); | 5032 | + mapped.rules().variable_refresh_rate == Some(VrrRule::On) && { |
| 5033 | + let mut visible = false; | ||
| 5034 | + mapped.window.with_surfaces(|surface, states| { | ||
| 5035 | + if visible { | ||
| 5036 | + // Skip checking. | ||
| 5037 | + return; | ||
| 5038 | + } | ||
| 5039 | + | ||
| 5040 | + if surface_primary_scanout_output(surface, states).as_ref() == Some(output) | ||
| 5041 | + { | ||
| 5042 | + visible = true; | ||
| 5043 | + } | ||
| 5044 | + }); | ||
| 5045 | + visible | ||
| 5046 | + } | ||
| 5047 | + }); | ||
| 5032 | 5048 | ||
| 5033 | backend.set_output_on_demand_vrr(self, output, current); | 5049 | backend.set_output_on_demand_vrr(self, output, current); |
| 5034 | } | 5050 | } |
src/window/mod.rsmodified+2 -2
| @@ -1,7 +1,7 @@ | |||
| 1 | use std::cmp::{max, min}; | 1 | use std::cmp::{max, min}; |
| 2 | 2 | ||
| 3 | use niri_config::utils::MergeWith as _; | 3 | use niri_config::utils::MergeWith as _; |
| 4 | -use niri_config::window_rule::{Match, OnXdgActivate, WindowRule}; | 4 | +use niri_config::window_rule::{Match, OnXdgActivate, VrrRule, WindowRule}; |
| 5 | use niri_config::{ | 5 | use niri_config::{ |
| 6 | BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, | 6 | BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, |
| 7 | ResolvedPopupsRules, ShadowRule, TabIndicatorRule, | 7 | ResolvedPopupsRules, ShadowRule, TabIndicatorRule, |
| @@ -115,7 +115,7 @@ pub struct ResolvedWindowRules { | |||
| 115 | pub block_out_from: Option<BlockOutFrom>, | 115 | pub block_out_from: Option<BlockOutFrom>, |
| 116 | 116 | ||
| 117 | /// Whether to enable VRR on this window's primary output if it is on-demand. | 117 | /// Whether to enable VRR on this window's primary output if it is on-demand. |
| 118 | - pub variable_refresh_rate: Option<bool>, | 118 | + pub variable_refresh_rate: Option<VrrRule>, |
| 119 | 119 | ||
| 120 | /// Multiplier for all scroll events sent to this window. | 120 | /// Multiplier for all scroll events sent to this window. |
| 121 | pub scroll_factor: Option<f64>, | 121 | pub scroll_factor: Option<f64>, |