Implement TouchpadScroll binds
Browse files at this commita16a0f0e52d6df8abd602baa15cee44e0656d790Parent: 6ba19525 changed files +130 -3
- niri-config/src/lib.rs +12 -0
- resources/default-config.kdl +9 -0
- src/input.rs +88 -2
- src/niri.rs +15 -1
- src/ui/hotkey_overlay.rs +6 -0
niri-config/src/lib.rsmodified+12 -0
| @@ -744,6 +744,10 @@ pub enum Trigger { | |||
| 744 | WheelScrollUp, | 744 | WheelScrollUp, |
| 745 | WheelScrollLeft, | 745 | WheelScrollLeft, |
| 746 | WheelScrollRight, | 746 | WheelScrollRight, |
| 747 | + TouchpadScrollDown, | ||
| 748 | + TouchpadScrollUp, | ||
| 749 | + TouchpadScrollLeft, | ||
| 750 | + TouchpadScrollRight, | ||
| 747 | } | 751 | } |
| 748 | 752 | ||
| 749 | bitflags! { | 753 | bitflags! { |
| @@ -1567,6 +1571,14 @@ impl FromStr for Key { | |||
| 1567 | Trigger::WheelScrollLeft | 1571 | Trigger::WheelScrollLeft |
| 1568 | } else if key.eq_ignore_ascii_case("WheelScrollRight") { | 1572 | } else if key.eq_ignore_ascii_case("WheelScrollRight") { |
| 1569 | Trigger::WheelScrollRight | 1573 | Trigger::WheelScrollRight |
| 1574 | + } else if key.eq_ignore_ascii_case("TouchpadScrollDown") { | ||
| 1575 | + Trigger::TouchpadScrollDown | ||
| 1576 | + } else if key.eq_ignore_ascii_case("TouchpadScrollUp") { | ||
| 1577 | + Trigger::TouchpadScrollUp | ||
| 1578 | + } else if key.eq_ignore_ascii_case("TouchpadScrollLeft") { | ||
| 1579 | + Trigger::TouchpadScrollLeft | ||
| 1580 | + } else if key.eq_ignore_ascii_case("TouchpadScrollRight") { | ||
| 1581 | + Trigger::TouchpadScrollRight | ||
| 1570 | } else { | 1582 | } else { |
| 1571 | let keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); | 1583 | let keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); |
| 1572 | if keysym.raw() == KEY_NoSymbol { | 1584 | if keysym.raw() == KEY_NoSymbol { |
resources/default-config.kdlmodified+9 -0
| @@ -553,6 +553,15 @@ binds { | |||
| 553 | Mod+Ctrl+Shift+WheelScrollDown { move-column-right; } | 553 | Mod+Ctrl+Shift+WheelScrollDown { move-column-right; } |
| 554 | Mod+Ctrl+Shift+WheelScrollUp { move-column-left; } | 554 | Mod+Ctrl+Shift+WheelScrollUp { move-column-left; } |
| 555 | 555 | ||
| 556 | + // Similarly, you can bind touchpad scroll "ticks". | ||
| 557 | + // Touchpad scrolling is continuous, so for these binds it is split into | ||
| 558 | + // discrete intervals. | ||
| 559 | + // These binds are also affected by touchpad's natural-scroll, so these | ||
| 560 | + // example binds are "inverted", since we have natural-scroll enabled for | ||
| 561 | + // touchpads by default. | ||
| 562 | + // Mod+TouchpadScrollDown { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.02+"; } | ||
| 563 | + // Mod+TouchpadScrollUp { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.02-"; } | ||
| 564 | + | ||
| 556 | // You can refer to workspaces by index. However, keep in mind that | 565 | // You can refer to workspaces by index. However, keep in mind that |
| 557 | // niri is a dynamic workspace system, so these commands are kind of | 566 | // niri is a dynamic workspace system, so these commands are kind of |
| 558 | // "best effort". Trying to refer to a workspace index bigger than | 567 | // "best effort". Trying to refer to a workspace index bigger than |
src/input.rsmodified+88 -2
| @@ -1178,11 +1178,84 @@ impl State { | |||
| 1178 | } | 1178 | } |
| 1179 | } | 1179 | } |
| 1180 | 1180 | ||
| 1181 | - let horizontal_amount = event.amount(Axis::Horizontal).unwrap_or_else(|| { | 1181 | + let horizontal_amount = event.amount(Axis::Horizontal); |
| 1182 | + let vertical_amount = event.amount(Axis::Vertical); | ||
| 1183 | + | ||
| 1184 | + // Handle touchpad scroll bindings. | ||
| 1185 | + if source == AxisSource::Finger { | ||
| 1186 | + let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 1187 | + let modifiers = modifiers_from_state(mods); | ||
| 1188 | + if self.niri.mods_with_finger_scroll_binds.contains(&modifiers) { | ||
| 1189 | + let comp_mod = self.backend.mod_key(); | ||
| 1190 | + | ||
| 1191 | + let horizontal = horizontal_amount.unwrap_or(0.); | ||
| 1192 | + let ticks = self | ||
| 1193 | + .niri | ||
| 1194 | + .horizontal_finger_scroll_tracker | ||
| 1195 | + .accumulate(horizontal); | ||
| 1196 | + if ticks != 0 { | ||
| 1197 | + let config = self.niri.config.borrow(); | ||
| 1198 | + let bindings = &config.binds; | ||
| 1199 | + let bind_left = | ||
| 1200 | + find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollLeft, mods); | ||
| 1201 | + let bind_right = find_configured_bind( | ||
| 1202 | + bindings, | ||
| 1203 | + comp_mod, | ||
| 1204 | + Trigger::TouchpadScrollRight, | ||
| 1205 | + mods, | ||
| 1206 | + ); | ||
| 1207 | + drop(config); | ||
| 1208 | + | ||
| 1209 | + if let Some(right) = bind_right { | ||
| 1210 | + for _ in 0..ticks { | ||
| 1211 | + self.handle_bind(right.clone()); | ||
| 1212 | + } | ||
| 1213 | + } | ||
| 1214 | + if let Some(left) = bind_left { | ||
| 1215 | + for _ in ticks..0 { | ||
| 1216 | + self.handle_bind(left.clone()); | ||
| 1217 | + } | ||
| 1218 | + } | ||
| 1219 | + } | ||
| 1220 | + | ||
| 1221 | + let vertical = vertical_amount.unwrap_or(0.); | ||
| 1222 | + let ticks = self | ||
| 1223 | + .niri | ||
| 1224 | + .vertical_finger_scroll_tracker | ||
| 1225 | + .accumulate(vertical); | ||
| 1226 | + if ticks != 0 { | ||
| 1227 | + let config = self.niri.config.borrow(); | ||
| 1228 | + let bindings = &config.binds; | ||
| 1229 | + let bind_up = | ||
| 1230 | + find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollUp, mods); | ||
| 1231 | + let bind_down = | ||
| 1232 | + find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollDown, mods); | ||
| 1233 | + drop(config); | ||
| 1234 | + | ||
| 1235 | + if let Some(down) = bind_down { | ||
| 1236 | + for _ in 0..ticks { | ||
| 1237 | + self.handle_bind(down.clone()); | ||
| 1238 | + } | ||
| 1239 | + } | ||
| 1240 | + if let Some(up) = bind_up { | ||
| 1241 | + for _ in ticks..0 { | ||
| 1242 | + self.handle_bind(up.clone()); | ||
| 1243 | + } | ||
| 1244 | + } | ||
| 1245 | + } | ||
| 1246 | + | ||
| 1247 | + return; | ||
| 1248 | + } else { | ||
| 1249 | + self.niri.horizontal_finger_scroll_tracker.reset(); | ||
| 1250 | + self.niri.vertical_finger_scroll_tracker.reset(); | ||
| 1251 | + } | ||
| 1252 | + } | ||
| 1253 | + | ||
| 1254 | + let horizontal_amount = horizontal_amount.unwrap_or_else(|| { | ||
| 1182 | // Winit backend, discrete scrolling. | 1255 | // Winit backend, discrete scrolling. |
| 1183 | horizontal_amount_v120.unwrap_or(0.0) / 120. * 15. | 1256 | horizontal_amount_v120.unwrap_or(0.0) / 120. * 15. |
| 1184 | }); | 1257 | }); |
| 1185 | - let vertical_amount = event.amount(Axis::Vertical).unwrap_or_else(|| { | 1258 | + let vertical_amount = vertical_amount.unwrap_or_else(|| { |
| 1186 | // Winit backend, discrete scrolling. | 1259 | // Winit backend, discrete scrolling. |
| 1187 | vertical_amount_v120.unwrap_or(0.0) / 120. * 15. | 1260 | vertical_amount_v120.unwrap_or(0.0) / 120. * 15. |
| 1188 | }); | 1261 | }); |
| @@ -2067,6 +2140,19 @@ pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet< | |||
| 2067 | ) | 2140 | ) |
| 2068 | } | 2141 | } |
| 2069 | 2142 | ||
| 2143 | +pub fn mods_with_finger_scroll_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { | ||
| 2144 | + mods_with_binds( | ||
| 2145 | + comp_mod, | ||
| 2146 | + binds, | ||
| 2147 | + &[ | ||
| 2148 | + Trigger::TouchpadScrollUp, | ||
| 2149 | + Trigger::TouchpadScrollDown, | ||
| 2150 | + Trigger::TouchpadScrollLeft, | ||
| 2151 | + Trigger::TouchpadScrollRight, | ||
| 2152 | + ], | ||
| 2153 | + ) | ||
| 2154 | +} | ||
| 2155 | + | ||
| 2070 | #[cfg(test)] | 2156 | #[cfg(test)] |
| 2071 | mod tests { | 2157 | mod tests { |
| 2072 | use super::*; | 2158 | use super::*; |
src/niri.rsmodified+15 -1
| @@ -97,7 +97,9 @@ use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | |||
| 97 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; | 97 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; |
| 98 | use crate::frame_clock::FrameClock; | 98 | use crate::frame_clock::FrameClock; |
| 99 | use crate::handlers::configure_lock_surface; | 99 | use crate::handlers::configure_lock_surface; |
| 100 | -use crate::input::{apply_libinput_settings, mods_with_wheel_binds, TabletData}; | 100 | +use crate::input::{ |
| 101 | + apply_libinput_settings, mods_with_finger_scroll_binds, mods_with_wheel_binds, TabletData, | ||
| 102 | +}; | ||
| 101 | use crate::ipc::server::IpcServer; | 103 | use crate::ipc::server::IpcServer; |
| 102 | use crate::layout::{Layout, MonitorRenderElement}; | 104 | use crate::layout::{Layout, MonitorRenderElement}; |
| 103 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; | 105 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; |
| @@ -209,6 +211,9 @@ pub struct Niri { | |||
| 209 | pub vertical_wheel_tracker: ScrollTracker, | 211 | pub vertical_wheel_tracker: ScrollTracker, |
| 210 | pub horizontal_wheel_tracker: ScrollTracker, | 212 | pub horizontal_wheel_tracker: ScrollTracker, |
| 211 | pub mods_with_wheel_binds: HashSet<Modifiers>, | 213 | pub mods_with_wheel_binds: HashSet<Modifiers>, |
| 214 | + pub vertical_finger_scroll_tracker: ScrollTracker, | ||
| 215 | + pub horizontal_finger_scroll_tracker: ScrollTracker, | ||
| 216 | + pub mods_with_finger_scroll_binds: HashSet<Modifiers>, | ||
| 212 | 217 | ||
| 213 | pub lock_state: LockState, | 218 | pub lock_state: LockState, |
| 214 | 219 | ||
| @@ -856,6 +861,8 @@ impl State { | |||
| 856 | self.niri.hotkey_overlay.on_hotkey_config_updated(); | 861 | self.niri.hotkey_overlay.on_hotkey_config_updated(); |
| 857 | self.niri.mods_with_wheel_binds = | 862 | self.niri.mods_with_wheel_binds = |
| 858 | mods_with_wheel_binds(self.backend.mod_key(), &config.binds); | 863 | mods_with_wheel_binds(self.backend.mod_key(), &config.binds); |
| 864 | + self.niri.mods_with_finger_scroll_binds = | ||
| 865 | + mods_with_finger_scroll_binds(self.backend.mod_key(), &config.binds); | ||
| 859 | } | 866 | } |
| 860 | 867 | ||
| 861 | if config.window_rules != old_config.window_rules { | 868 | if config.window_rules != old_config.window_rules { |
| @@ -1166,6 +1173,8 @@ impl Niri { | |||
| 1166 | CursorManager::new(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size); | 1173 | CursorManager::new(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size); |
| 1167 | 1174 | ||
| 1168 | let mods_with_wheel_binds = mods_with_wheel_binds(backend.mod_key(), &config_.binds); | 1175 | let mods_with_wheel_binds = mods_with_wheel_binds(backend.mod_key(), &config_.binds); |
| 1176 | + let mods_with_finger_scroll_binds = | ||
| 1177 | + mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds); | ||
| 1169 | 1178 | ||
| 1170 | let (tx, rx) = calloop::channel::channel(); | 1179 | let (tx, rx) = calloop::channel::channel(); |
| 1171 | event_loop | 1180 | event_loop |
| @@ -1325,6 +1334,11 @@ impl Niri { | |||
| 1325 | horizontal_wheel_tracker: ScrollTracker::new(120), | 1334 | horizontal_wheel_tracker: ScrollTracker::new(120), |
| 1326 | mods_with_wheel_binds, | 1335 | mods_with_wheel_binds, |
| 1327 | 1336 | ||
| 1337 | + // 10 is copied from Clutter: DISCRETE_SCROLL_STEP. | ||
| 1338 | + vertical_finger_scroll_tracker: ScrollTracker::new(10), | ||
| 1339 | + horizontal_finger_scroll_tracker: ScrollTracker::new(10), | ||
| 1340 | + mods_with_finger_scroll_binds, | ||
| 1341 | + | ||
| 1328 | lock_state: LockState::Unlocked, | 1342 | lock_state: LockState::Unlocked, |
| 1329 | 1343 | ||
| 1330 | screenshot_ui, | 1344 | screenshot_ui, |
src/ui/hotkey_overlay.rsmodified+6 -0
| @@ -226,6 +226,8 @@ fn render(config: &Config, comp_mod: CompositorMod, scale: i32) -> anyhow::Resul | |||
| 226 | // Only show binds with Mod or Super to filter out stuff like volume up/down. | 226 | // Only show binds with Mod or Super to filter out stuff like volume up/down. |
| 227 | && (bind.key.modifiers.contains(Modifiers::COMPOSITOR) | 227 | && (bind.key.modifiers.contains(Modifiers::COMPOSITOR) |
| 228 | || bind.key.modifiers.contains(Modifiers::SUPER)) | 228 | || bind.key.modifiers.contains(Modifiers::SUPER)) |
| 229 | + // Also filter out wheel and touchpad scroll binds. | ||
| 230 | + && matches!(bind.key.trigger, Trigger::Keysym(_)) | ||
| 229 | }) { | 231 | }) { |
| 230 | let action = &bind.action; | 232 | let action = &bind.action; |
| 231 | 233 | ||
| @@ -421,6 +423,10 @@ fn key_name(comp_mod: CompositorMod, key: &Key) -> String { | |||
| 421 | Trigger::WheelScrollUp => String::from("Wheel Scroll Up"), | 423 | Trigger::WheelScrollUp => String::from("Wheel Scroll Up"), |
| 422 | Trigger::WheelScrollLeft => String::from("Wheel Scroll Left"), | 424 | Trigger::WheelScrollLeft => String::from("Wheel Scroll Left"), |
| 423 | Trigger::WheelScrollRight => String::from("Wheel Scroll Right"), | 425 | Trigger::WheelScrollRight => String::from("Wheel Scroll Right"), |
| 426 | + Trigger::TouchpadScrollDown => String::from("Touchpad Scroll Down"), | ||
| 427 | + Trigger::TouchpadScrollUp => String::from("Touchpad Scroll Up"), | ||
| 428 | + Trigger::TouchpadScrollLeft => String::from("Touchpad Scroll Left"), | ||
| 429 | + Trigger::TouchpadScrollRight => String::from("Touchpad Scroll Right"), | ||
| 424 | }; | 430 | }; |
| 425 | name.push_str(&pretty); | 431 | name.push_str(&pretty); |
| 426 | 432 | ||