Avoid scroll bind lookup until it is triggered
Browse files at this commitafaaf36f27caf73b6c6b394e8f96ae362c454e61Parent: f1b36b02 changed files +100 -58
- src/input.rs +92 -56
- src/niri.rs +8 -2
src/input.rsmodified+92 -56
| @@ -1118,26 +1118,24 @@ impl State { | ||
| 1118 | 1118 | |
| 1119 | 1119 | // Handle wheel scroll bindings. |
| 1120 | 1120 | if source == AxisSource::Wheel { |
| 1121 | - let comp_mod = self.backend.mod_key(); | |
| 1121 | + // If we have a scroll bind with current modifiers, then accumulate and don't pass to | |
| 1122 | + // Wayland. If there's no bind, reset the accumulator. | |
| 1122 | 1123 | let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 1124 | + let modifiers = modifiers_from_state(mods); | |
| 1125 | + if self.niri.mods_with_wheel_binds.contains(&modifiers) { | |
| 1126 | + let comp_mod = self.backend.mod_key(); | |
| 1127 | + | |
| 1128 | + let horizontal = horizontal_amount_v120.unwrap_or(0.); | |
| 1129 | + let ticks = self.niri.horizontal_wheel_tracker.accumulate(horizontal); | |
| 1130 | + if ticks != 0 { | |
| 1131 | + let config = self.niri.config.borrow(); | |
| 1132 | + let bindings = &config.binds; | |
| 1133 | + let bind_left = | |
| 1134 | + find_configured_bind(bindings, comp_mod, Trigger::WheelScrollLeft, mods); | |
| 1135 | + let bind_right = | |
| 1136 | + find_configured_bind(bindings, comp_mod, Trigger::WheelScrollRight, mods); | |
| 1137 | + drop(config); | |
| 1123 | 1138 | |
| 1124 | - // Winit sends scroll events where both directions are set at once, so we can't early | |
| 1125 | - // return after handling just one. | |
| 1126 | - let mut handled = false; | |
| 1127 | - | |
| 1128 | - if let Some(v120) = horizontal_amount_v120 { | |
| 1129 | - let config = self.niri.config.borrow(); | |
| 1130 | - let bindings = &config.binds; | |
| 1131 | - let bind_left = | |
| 1132 | - find_configured_bind(bindings, comp_mod, Trigger::WheelScrollLeft, mods); | |
| 1133 | - let bind_right = | |
| 1134 | - find_configured_bind(bindings, comp_mod, Trigger::WheelScrollRight, mods); | |
| 1135 | - drop(config); | |
| 1136 | - | |
| 1137 | - // If we have a bind with current modifiers along the scroll direction, then | |
| 1138 | - // accumulate and don't pass to Wayland. If there's no bind, reset the accumulator. | |
| 1139 | - if bind_left.is_some() || bind_right.is_some() { | |
| 1140 | - let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120); | |
| 1141 | 1139 | if let Some(right) = bind_right { |
| 1142 | 1140 | for _ in 0..ticks { |
| 1143 | 1141 | self.handle_bind(right.clone()); |
| @@ -1148,24 +1146,19 @@ impl State { | ||
| 1148 | 1146 | self.handle_bind(left.clone()); |
| 1149 | 1147 | } |
| 1150 | 1148 | } |
| 1151 | - | |
| 1152 | - handled = true; | |
| 1153 | - } else { | |
| 1154 | - self.niri.horizontal_wheel_tracker.reset(); | |
| 1155 | 1149 | } |
| 1156 | - } | |
| 1157 | 1150 | |
| 1158 | - if let Some(v120) = vertical_amount_v120 { | |
| 1159 | - let config = self.niri.config.borrow(); | |
| 1160 | - let bindings = &config.binds; | |
| 1161 | - let bind_up = | |
| 1162 | - find_configured_bind(bindings, comp_mod, Trigger::WheelScrollUp, mods); | |
| 1163 | - let bind_down = | |
| 1164 | - find_configured_bind(bindings, comp_mod, Trigger::WheelScrollDown, mods); | |
| 1165 | - drop(config); | |
| 1166 | - | |
| 1167 | - if bind_up.is_some() || bind_down.is_some() { | |
| 1168 | - let ticks = self.niri.vertical_wheel_tracker.accumulate(v120); | |
| 1151 | + let vertical = vertical_amount_v120.unwrap_or(0.); | |
| 1152 | + let ticks = self.niri.vertical_wheel_tracker.accumulate(vertical); | |
| 1153 | + if ticks != 0 { | |
| 1154 | + let config = self.niri.config.borrow(); | |
| 1155 | + let bindings = &config.binds; | |
| 1156 | + let bind_up = | |
| 1157 | + find_configured_bind(bindings, comp_mod, Trigger::WheelScrollUp, mods); | |
| 1158 | + let bind_down = | |
| 1159 | + find_configured_bind(bindings, comp_mod, Trigger::WheelScrollDown, mods); | |
| 1160 | + drop(config); | |
| 1161 | + | |
| 1169 | 1162 | if let Some(down) = bind_down { |
| 1170 | 1163 | for _ in 0..ticks { |
| 1171 | 1164 | self.handle_bind(down.clone()); |
| @@ -1176,15 +1169,12 @@ impl State { | ||
| 1176 | 1169 | self.handle_bind(up.clone()); |
| 1177 | 1170 | } |
| 1178 | 1171 | } |
| 1179 | - | |
| 1180 | - handled = true; | |
| 1181 | - } else { | |
| 1182 | - self.niri.vertical_wheel_tracker.reset(); | |
| 1183 | 1172 | } |
| 1184 | - } | |
| 1185 | 1173 | |
| 1186 | - if handled { | |
| 1187 | 1174 | return; |
| 1175 | + } else { | |
| 1176 | + self.niri.horizontal_wheel_tracker.reset(); | |
| 1177 | + self.niri.vertical_wheel_tracker.reset(); | |
| 1188 | 1178 | } |
| 1189 | 1179 | } |
| 1190 | 1180 | |
| @@ -1836,22 +1826,7 @@ fn find_configured_bind( | ||
| 1836 | 1826 | mods: ModifiersState, |
| 1837 | 1827 | ) -> Option<Bind> { |
| 1838 | 1828 | // Handle configured binds. |
| 1839 | - let mut modifiers = Modifiers::empty(); | |
| 1840 | - if mods.ctrl { | |
| 1841 | - modifiers |= Modifiers::CTRL; | |
| 1842 | - } | |
| 1843 | - if mods.shift { | |
| 1844 | - modifiers |= Modifiers::SHIFT; | |
| 1845 | - } | |
| 1846 | - if mods.alt { | |
| 1847 | - modifiers |= Modifiers::ALT; | |
| 1848 | - } | |
| 1849 | - if mods.logo { | |
| 1850 | - modifiers |= Modifiers::SUPER; | |
| 1851 | - } | |
| 1852 | - if mods.iso_level3_shift { | |
| 1853 | - modifiers |= Modifiers::ISO_LEVEL3_SHIFT; | |
| 1854 | - } | |
| 1829 | + let mut modifiers = modifiers_from_state(mods); | |
| 1855 | 1830 | |
| 1856 | 1831 | let (mod_down, comp_mod) = match comp_mod { |
| 1857 | 1832 | CompositorMod::Super => (mods.logo, Modifiers::SUPER), |
| @@ -1881,6 +1856,26 @@ fn find_configured_bind( | ||
| 1881 | 1856 | None |
| 1882 | 1857 | } |
| 1883 | 1858 | |
| 1859 | +fn modifiers_from_state(mods: ModifiersState) -> Modifiers { | |
| 1860 | + let mut modifiers = Modifiers::empty(); | |
| 1861 | + if mods.ctrl { | |
| 1862 | + modifiers |= Modifiers::CTRL; | |
| 1863 | + } | |
| 1864 | + if mods.shift { | |
| 1865 | + modifiers |= Modifiers::SHIFT; | |
| 1866 | + } | |
| 1867 | + if mods.alt { | |
| 1868 | + modifiers |= Modifiers::ALT; | |
| 1869 | + } | |
| 1870 | + if mods.logo { | |
| 1871 | + modifiers |= Modifiers::SUPER; | |
| 1872 | + } | |
| 1873 | + if mods.iso_level3_shift { | |
| 1874 | + modifiers |= Modifiers::ISO_LEVEL3_SHIFT; | |
| 1875 | + } | |
| 1876 | + modifiers | |
| 1877 | +} | |
| 1878 | + | |
| 1884 | 1879 | fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 1885 | 1880 | match event { |
| 1886 | 1881 | InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, |
| @@ -2031,6 +2026,47 @@ pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input:: | ||
| 2031 | 2026 | } |
| 2032 | 2027 | } |
| 2033 | 2028 | |
| 2029 | +pub fn mods_with_binds( | |
| 2030 | + comp_mod: CompositorMod, | |
| 2031 | + binds: &Binds, | |
| 2032 | + triggers: &[Trigger], | |
| 2033 | +) -> HashSet<Modifiers> { | |
| 2034 | + let comp_mod = match comp_mod { | |
| 2035 | + CompositorMod::Super => Modifiers::SUPER, | |
| 2036 | + CompositorMod::Alt => Modifiers::ALT, | |
| 2037 | + }; | |
| 2038 | + | |
| 2039 | + let mut rv = HashSet::new(); | |
| 2040 | + for bind in &binds.0 { | |
| 2041 | + if !triggers.iter().any(|trigger| bind.key.trigger == *trigger) { | |
| 2042 | + continue; | |
| 2043 | + } | |
| 2044 | + | |
| 2045 | + let mut mods = bind.key.modifiers; | |
| 2046 | + if mods.contains(Modifiers::COMPOSITOR) { | |
| 2047 | + mods.remove(Modifiers::COMPOSITOR); | |
| 2048 | + mods.insert(comp_mod); | |
| 2049 | + } | |
| 2050 | + | |
| 2051 | + rv.insert(mods); | |
| 2052 | + } | |
| 2053 | + | |
| 2054 | + rv | |
| 2055 | +} | |
| 2056 | + | |
| 2057 | +pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { | |
| 2058 | + mods_with_binds( | |
| 2059 | + comp_mod, | |
| 2060 | + binds, | |
| 2061 | + &[ | |
| 2062 | + Trigger::WheelScrollUp, | |
| 2063 | + Trigger::WheelScrollDown, | |
| 2064 | + Trigger::WheelScrollLeft, | |
| 2065 | + Trigger::WheelScrollRight, | |
| 2066 | + ], | |
| 2067 | + ) | |
| 2068 | +} | |
| 2069 | + | |
| 2034 | 2070 | #[cfg(test)] |
| 2035 | 2071 | mod tests { |
| 2036 | 2072 | use super::*; |
src/niri.rsmodified+8 -2
| @@ -11,7 +11,7 @@ use std::{env, mem, thread}; | ||
| 11 | 11 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; |
| 12 | 12 | use anyhow::{ensure, Context}; |
| 13 | 13 | use calloop::futures::Scheduler; |
| 14 | -use niri_config::{Config, Key, TrackLayout}; | |
| 14 | +use niri_config::{Config, Key, Modifiers, TrackLayout}; | |
| 15 | 15 | use smithay::backend::allocator::Fourcc; |
| 16 | 16 | use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement; |
| 17 | 17 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| @@ -97,7 +97,7 @@ use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | ||
| 97 | 97 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; |
| 98 | 98 | use crate::frame_clock::FrameClock; |
| 99 | 99 | use crate::handlers::configure_lock_surface; |
| 100 | -use crate::input::{apply_libinput_settings, TabletData}; | |
| 100 | +use crate::input::{apply_libinput_settings, mods_with_wheel_binds, TabletData}; | |
| 101 | 101 | use crate::ipc::server::IpcServer; |
| 102 | 102 | use crate::layout::{Layout, MonitorRenderElement}; |
| 103 | 103 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; |
| @@ -208,6 +208,7 @@ pub struct Niri { | ||
| 208 | 208 | pub gesture_swipe_3f_cumulative: Option<(f64, f64)>, |
| 209 | 209 | pub vertical_wheel_tracker: WheelTracker, |
| 210 | 210 | pub horizontal_wheel_tracker: WheelTracker, |
| 211 | + pub mods_with_wheel_binds: HashSet<Modifiers>, | |
| 211 | 212 | |
| 212 | 213 | pub lock_state: LockState, |
| 213 | 214 | |
| @@ -853,6 +854,8 @@ impl State { | ||
| 853 | 854 | |
| 854 | 855 | if config.binds != old_config.binds { |
| 855 | 856 | self.niri.hotkey_overlay.on_hotkey_config_updated(); |
| 857 | + self.niri.mods_with_wheel_binds = | |
| 858 | + mods_with_wheel_binds(self.backend.mod_key(), &config.binds); | |
| 856 | 859 | } |
| 857 | 860 | |
| 858 | 861 | if config.window_rules != old_config.window_rules { |
| @@ -1162,6 +1165,8 @@ impl Niri { | ||
| 1162 | 1165 | let cursor_manager = |
| 1163 | 1166 | CursorManager::new(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size); |
| 1164 | 1167 | |
| 1168 | + let mods_with_wheel_binds = mods_with_wheel_binds(backend.mod_key(), &config_.binds); | |
| 1169 | + | |
| 1165 | 1170 | let (tx, rx) = calloop::channel::channel(); |
| 1166 | 1171 | event_loop |
| 1167 | 1172 | .insert_source(rx, move |event, _, state| { |
| @@ -1318,6 +1323,7 @@ impl Niri { | ||
| 1318 | 1323 | gesture_swipe_3f_cumulative: None, |
| 1319 | 1324 | vertical_wheel_tracker: WheelTracker::new(), |
| 1320 | 1325 | horizontal_wheel_tracker: WheelTracker::new(), |
| 1326 | + mods_with_wheel_binds, | |
| 1321 | 1327 | |
| 1322 | 1328 | lock_state: LockState::Unlocked, |
| 1323 | 1329 |