Add `Mouse{Left,Right,Middle,Back,Forward}` binds
Browse files at this commit4e33f4552226fbadb156ca16b483cda10d04b433Parent: b16d7ab5 changed files +106 -14
- niri-config/src/lib.rs +16 -1
- src/input/mod.rs +55 -12
- src/niri.rs +10 -1
- src/ui/hotkey_overlay.rs +5 -0
- wiki/Configuration:-Key-Bindings.md +20 -0
niri-config/src/lib.rsmodified+16 -1
| @@ -1124,6 +1124,11 @@ pub struct Key { | |||
| 1124 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | 1124 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
| 1125 | pub enum Trigger { | 1125 | pub enum Trigger { |
| 1126 | Keysym(Keysym), | 1126 | Keysym(Keysym), |
| 1127 | + MouseLeft, | ||
| 1128 | + MouseRight, | ||
| 1129 | + MouseMiddle, | ||
| 1130 | + MouseBack, | ||
| 1131 | + MouseForward, | ||
| 1127 | WheelScrollDown, | 1132 | WheelScrollDown, |
| 1128 | WheelScrollUp, | 1133 | WheelScrollUp, |
| 1129 | WheelScrollLeft, | 1134 | WheelScrollLeft, |
| @@ -2924,7 +2929,17 @@ impl FromStr for Key { | |||
| 2924 | } | 2929 | } |
| 2925 | } | 2930 | } |
| 2926 | 2931 | ||
| 2927 | - let trigger = if key.eq_ignore_ascii_case("WheelScrollDown") { | 2932 | + let trigger = if key.eq_ignore_ascii_case("MouseLeft") { |
| 2933 | + Trigger::MouseLeft | ||
| 2934 | + } else if key.eq_ignore_ascii_case("MouseRight") { | ||
| 2935 | + Trigger::MouseRight | ||
| 2936 | + } else if key.eq_ignore_ascii_case("MouseMiddle") { | ||
| 2937 | + Trigger::MouseMiddle | ||
| 2938 | + } else if key.eq_ignore_ascii_case("MouseBack") { | ||
| 2939 | + Trigger::MouseBack | ||
| 2940 | + } else if key.eq_ignore_ascii_case("MouseForward") { | ||
| 2941 | + Trigger::MouseForward | ||
| 2942 | + } else if key.eq_ignore_ascii_case("WheelScrollDown") { | ||
| 2928 | Trigger::WheelScrollDown | 2943 | Trigger::WheelScrollDown |
| 2929 | } else if key.eq_ignore_ascii_case("WheelScrollUp") { | 2944 | } else if key.eq_ignore_ascii_case("WheelScrollUp") { |
| 2930 | Trigger::WheelScrollUp | 2945 | Trigger::WheelScrollUp |
src/input/mod.rsmodified+55 -12
| @@ -1656,11 +1656,43 @@ impl State { | |||
| 1656 | 1656 | ||
| 1657 | let serial = SERIAL_COUNTER.next_serial(); | 1657 | let serial = SERIAL_COUNTER.next_serial(); |
| 1658 | 1658 | ||
| 1659 | - let button = event.button_code(); | 1659 | + let button = event.button(); |
| 1660 | + | ||
| 1661 | + let button_code = event.button_code(); | ||
| 1660 | 1662 | ||
| 1661 | let button_state = event.state(); | 1663 | let button_state = event.state(); |
| 1662 | 1664 | ||
| 1665 | + // Ignore release events for mouse clicks that triggered a bind. | ||
| 1666 | + if self.niri.suppressed_buttons.remove(&button_code) { | ||
| 1667 | + return; | ||
| 1668 | + } | ||
| 1669 | + | ||
| 1663 | if ButtonState::Pressed == button_state { | 1670 | if ButtonState::Pressed == button_state { |
| 1671 | + let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 1672 | + let modifiers = modifiers_from_state(mods); | ||
| 1673 | + | ||
| 1674 | + if self.niri.mods_with_mouse_binds.contains(&modifiers) { | ||
| 1675 | + let comp_mod = self.backend.mod_key(); | ||
| 1676 | + | ||
| 1677 | + if let Some(bind) = match button { | ||
| 1678 | + Some(MouseButton::Left) => Some(Trigger::MouseLeft), | ||
| 1679 | + Some(MouseButton::Right) => Some(Trigger::MouseRight), | ||
| 1680 | + Some(MouseButton::Middle) => Some(Trigger::MouseMiddle), | ||
| 1681 | + Some(MouseButton::Back) => Some(Trigger::MouseBack), | ||
| 1682 | + Some(MouseButton::Forward) => Some(Trigger::MouseForward), | ||
| 1683 | + _ => None, | ||
| 1684 | + } | ||
| 1685 | + .and_then(|trigger| { | ||
| 1686 | + let config = self.niri.config.borrow(); | ||
| 1687 | + let bindings = &config.binds; | ||
| 1688 | + find_configured_bind(bindings, comp_mod, trigger, mods) | ||
| 1689 | + }) { | ||
| 1690 | + self.niri.suppressed_buttons.insert(button_code); | ||
| 1691 | + self.handle_bind(bind.clone()); | ||
| 1692 | + return; | ||
| 1693 | + }; | ||
| 1694 | + } | ||
| 1695 | + | ||
| 1664 | // We received an event for the regular pointer, so show it now. | 1696 | // We received an event for the regular pointer, so show it now. |
| 1665 | self.niri.pointer_hidden = false; | 1697 | self.niri.pointer_hidden = false; |
| 1666 | self.niri.tablet_cursor_location = None; | 1698 | self.niri.tablet_cursor_location = None; |
| @@ -1669,8 +1701,7 @@ impl State { | |||
| 1669 | let window = mapped.window.clone(); | 1701 | let window = mapped.window.clone(); |
| 1670 | 1702 | ||
| 1671 | // Check if we need to start an interactive move. | 1703 | // Check if we need to start an interactive move. |
| 1672 | - if event.button() == Some(MouseButton::Left) && !pointer.is_grabbed() { | 1704 | + if button == Some(MouseButton::Left) && !pointer.is_grabbed() { |
| 1673 | - let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 1674 | let mod_down = match self.backend.mod_key() { | 1705 | let mod_down = match self.backend.mod_key() { |
| 1675 | CompositorMod::Super => mods.logo, | 1706 | CompositorMod::Super => mods.logo, |
| 1676 | CompositorMod::Alt => mods.alt, | 1707 | CompositorMod::Alt => mods.alt, |
| @@ -1689,7 +1720,7 @@ impl State { | |||
| 1689 | ) { | 1720 | ) { |
| 1690 | let start_data = PointerGrabStartData { | 1721 | let start_data = PointerGrabStartData { |
| 1691 | focus: None, | 1722 | focus: None, |
| 1692 | - button: event.button_code(), | 1723 | + button: button_code, |
| 1693 | location, | 1724 | location, |
| 1694 | }; | 1725 | }; |
| 1695 | let grab = MoveGrab::new(start_data, window.clone()); | 1726 | let grab = MoveGrab::new(start_data, window.clone()); |
| @@ -1701,8 +1732,7 @@ impl State { | |||
| 1701 | } | 1732 | } |
| 1702 | } | 1733 | } |
| 1703 | // Check if we need to start an interactive resize. | 1734 | // Check if we need to start an interactive resize. |
| 1704 | - else if event.button() == Some(MouseButton::Right) && !pointer.is_grabbed() { | 1735 | + else if button == Some(MouseButton::Right) && !pointer.is_grabbed() { |
| 1705 | - let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 1706 | let mod_down = match self.backend.mod_key() { | 1736 | let mod_down = match self.backend.mod_key() { |
| 1707 | CompositorMod::Super => mods.logo, | 1737 | CompositorMod::Super => mods.logo, |
| 1708 | CompositorMod::Alt => mods.alt, | 1738 | CompositorMod::Alt => mods.alt, |
| @@ -1762,7 +1792,7 @@ impl State { | |||
| 1762 | { | 1792 | { |
| 1763 | let start_data = PointerGrabStartData { | 1793 | let start_data = PointerGrabStartData { |
| 1764 | focus: None, | 1794 | focus: None, |
| 1765 | - button: event.button_code(), | 1795 | + button: button_code, |
| 1766 | location, | 1796 | location, |
| 1767 | }; | 1797 | }; |
| 1768 | let grab = ResizeGrab::new(start_data, window.clone()); | 1798 | let grab = ResizeGrab::new(start_data, window.clone()); |
| @@ -1786,8 +1816,7 @@ impl State { | |||
| 1786 | self.niri.queue_redraw_all(); | 1816 | self.niri.queue_redraw_all(); |
| 1787 | } | 1817 | } |
| 1788 | 1818 | ||
| 1789 | - if event.button() == Some(MouseButton::Middle) && !pointer.is_grabbed() { | 1819 | + if button == Some(MouseButton::Middle) && !pointer.is_grabbed() { |
| 1790 | - let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 1791 | let mod_down = match self.backend.mod_key() { | 1820 | let mod_down = match self.backend.mod_key() { |
| 1792 | CompositorMod::Super => mods.logo, | 1821 | CompositorMod::Super => mods.logo, |
| 1793 | CompositorMod::Alt => mods.alt, | 1822 | CompositorMod::Alt => mods.alt, |
| @@ -1797,7 +1826,7 @@ impl State { | |||
| 1797 | let location = pointer.current_location(); | 1826 | let location = pointer.current_location(); |
| 1798 | let start_data = PointerGrabStartData { | 1827 | let start_data = PointerGrabStartData { |
| 1799 | focus: None, | 1828 | focus: None, |
| 1800 | - button: event.button_code(), | 1829 | + button: button_code, |
| 1801 | location, | 1830 | location, |
| 1802 | }; | 1831 | }; |
| 1803 | let grab = SpatialMovementGrab::new(start_data, output); | 1832 | let grab = SpatialMovementGrab::new(start_data, output); |
| @@ -1817,7 +1846,7 @@ impl State { | |||
| 1817 | self.niri.focus_layer_surface_if_on_demand(layer_under); | 1846 | self.niri.focus_layer_surface_if_on_demand(layer_under); |
| 1818 | } | 1847 | } |
| 1819 | 1848 | ||
| 1820 | - if let Some(button) = event.button() { | 1849 | + if let Some(button) = button { |
| 1821 | let pos = pointer.current_location(); | 1850 | let pos = pointer.current_location(); |
| 1822 | if let Some((output, _)) = self.niri.output_under(pos) { | 1851 | if let Some((output, _)) = self.niri.output_under(pos) { |
| 1823 | let output = output.clone(); | 1852 | let output = output.clone(); |
| @@ -1845,7 +1874,7 @@ impl State { | |||
| 1845 | pointer.button( | 1874 | pointer.button( |
| 1846 | self, | 1875 | self, |
| 1847 | &ButtonEvent { | 1876 | &ButtonEvent { |
| 1848 | - button, | 1877 | + button: button_code, |
| 1849 | state: button_state, | 1878 | state: button_state, |
| 1850 | serial, | 1879 | serial, |
| 1851 | time: event.time_msec(), | 1880 | time: event.time_msec(), |
| @@ -3110,6 +3139,20 @@ pub fn mods_with_binds( | |||
| 3110 | rv | 3139 | rv |
| 3111 | } | 3140 | } |
| 3112 | 3141 | ||
| 3142 | +pub fn mods_with_mouse_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { | ||
| 3143 | + mods_with_binds( | ||
| 3144 | + comp_mod, | ||
| 3145 | + binds, | ||
| 3146 | + &[ | ||
| 3147 | + Trigger::MouseLeft, | ||
| 3148 | + Trigger::MouseRight, | ||
| 3149 | + Trigger::MouseMiddle, | ||
| 3150 | + Trigger::MouseBack, | ||
| 3151 | + Trigger::MouseForward, | ||
| 3152 | + ], | ||
| 3153 | + ) | ||
| 3154 | +} | ||
| 3155 | + | ||
| 3113 | pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { | 3156 | pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { |
| 3114 | mods_with_binds( | 3157 | mods_with_binds( |
| 3115 | comp_mod, | 3158 | comp_mod, |
src/niri.rsmodified+10 -1
| @@ -116,7 +116,8 @@ use crate::frame_clock::FrameClock; | |||
| 116 | use crate::handlers::{configure_lock_surface, XDG_ACTIVATION_TOKEN_TIMEOUT}; | 116 | use crate::handlers::{configure_lock_surface, XDG_ACTIVATION_TOKEN_TIMEOUT}; |
| 117 | use crate::input::scroll_tracker::ScrollTracker; | 117 | use crate::input::scroll_tracker::ScrollTracker; |
| 118 | use crate::input::{ | 118 | use crate::input::{ |
| 119 | - apply_libinput_settings, mods_with_finger_scroll_binds, mods_with_wheel_binds, TabletData, | 119 | + apply_libinput_settings, mods_with_finger_scroll_binds, mods_with_mouse_binds, |
| 120 | + mods_with_wheel_binds, TabletData, | ||
| 120 | }; | 121 | }; |
| 121 | use crate::ipc::server::IpcServer; | 122 | use crate::ipc::server::IpcServer; |
| 122 | use crate::layer::mapped::LayerSurfaceRenderElement; | 123 | use crate::layer::mapped::LayerSurfaceRenderElement; |
| @@ -277,6 +278,8 @@ pub struct Niri { | |||
| 277 | pub seat: Seat<State>, | 278 | pub seat: Seat<State>, |
| 278 | /// Scancodes of the keys to suppress. | 279 | /// Scancodes of the keys to suppress. |
| 279 | pub suppressed_keys: HashSet<Keycode>, | 280 | pub suppressed_keys: HashSet<Keycode>, |
| 281 | + /// Button codes of the mouse buttons to suppress. | ||
| 282 | + pub suppressed_buttons: HashSet<u32>, | ||
| 280 | pub bind_cooldown_timers: HashMap<Key, RegistrationToken>, | 283 | pub bind_cooldown_timers: HashMap<Key, RegistrationToken>, |
| 281 | pub bind_repeat_timer: Option<RegistrationToken>, | 284 | pub bind_repeat_timer: Option<RegistrationToken>, |
| 282 | pub keyboard_focus: KeyboardFocus, | 285 | pub keyboard_focus: KeyboardFocus, |
| @@ -313,6 +316,7 @@ pub struct Niri { | |||
| 313 | pub gesture_swipe_3f_cumulative: Option<(f64, f64)>, | 316 | pub gesture_swipe_3f_cumulative: Option<(f64, f64)>, |
| 314 | pub vertical_wheel_tracker: ScrollTracker, | 317 | pub vertical_wheel_tracker: ScrollTracker, |
| 315 | pub horizontal_wheel_tracker: ScrollTracker, | 318 | pub horizontal_wheel_tracker: ScrollTracker, |
| 319 | + pub mods_with_mouse_binds: HashSet<Modifiers>, | ||
| 316 | pub mods_with_wheel_binds: HashSet<Modifiers>, | 320 | pub mods_with_wheel_binds: HashSet<Modifiers>, |
| 317 | pub vertical_finger_scroll_tracker: ScrollTracker, | 321 | pub vertical_finger_scroll_tracker: ScrollTracker, |
| 318 | pub horizontal_finger_scroll_tracker: ScrollTracker, | 322 | pub horizontal_finger_scroll_tracker: ScrollTracker, |
| @@ -1142,6 +1146,8 @@ impl State { | |||
| 1142 | 1146 | ||
| 1143 | if config.binds != old_config.binds { | 1147 | if config.binds != old_config.binds { |
| 1144 | self.niri.hotkey_overlay.on_hotkey_config_updated(); | 1148 | self.niri.hotkey_overlay.on_hotkey_config_updated(); |
| 1149 | + self.niri.mods_with_mouse_binds = | ||
| 1150 | + mods_with_mouse_binds(self.backend.mod_key(), &config.binds); | ||
| 1145 | self.niri.mods_with_wheel_binds = | 1151 | self.niri.mods_with_wheel_binds = |
| 1146 | mods_with_wheel_binds(self.backend.mod_key(), &config.binds); | 1152 | mods_with_wheel_binds(self.backend.mod_key(), &config.binds); |
| 1147 | self.niri.mods_with_finger_scroll_binds = | 1153 | self.niri.mods_with_finger_scroll_binds = |
| @@ -1846,6 +1852,7 @@ impl Niri { | |||
| 1846 | let cursor_manager = | 1852 | let cursor_manager = |
| 1847 | CursorManager::new(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size); | 1853 | CursorManager::new(&config_.cursor.xcursor_theme, config_.cursor.xcursor_size); |
| 1848 | 1854 | ||
| 1855 | + let mods_with_mouse_binds = mods_with_mouse_binds(backend.mod_key(), &config_.binds); | ||
| 1849 | let mods_with_wheel_binds = mods_with_wheel_binds(backend.mod_key(), &config_.binds); | 1856 | let mods_with_wheel_binds = mods_with_wheel_binds(backend.mod_key(), &config_.binds); |
| 1850 | let mods_with_finger_scroll_binds = | 1857 | let mods_with_finger_scroll_binds = |
| 1851 | mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds); | 1858 | mods_with_finger_scroll_binds(backend.mod_key(), &config_.binds); |
| @@ -1998,6 +2005,7 @@ impl Niri { | |||
| 1998 | popups: PopupManager::default(), | 2005 | popups: PopupManager::default(), |
| 1999 | popup_grab: None, | 2006 | popup_grab: None, |
| 2000 | suppressed_keys: HashSet::new(), | 2007 | suppressed_keys: HashSet::new(), |
| 2008 | + suppressed_buttons: HashSet::new(), | ||
| 2001 | bind_cooldown_timers: HashMap::new(), | 2009 | bind_cooldown_timers: HashMap::new(), |
| 2002 | bind_repeat_timer: Option::default(), | 2010 | bind_repeat_timer: Option::default(), |
| 2003 | presentation_state, | 2011 | presentation_state, |
| @@ -2025,6 +2033,7 @@ impl Niri { | |||
| 2025 | gesture_swipe_3f_cumulative: None, | 2033 | gesture_swipe_3f_cumulative: None, |
| 2026 | vertical_wheel_tracker: ScrollTracker::new(120), | 2034 | vertical_wheel_tracker: ScrollTracker::new(120), |
| 2027 | horizontal_wheel_tracker: ScrollTracker::new(120), | 2035 | horizontal_wheel_tracker: ScrollTracker::new(120), |
| 2036 | + mods_with_mouse_binds, | ||
| 2028 | mods_with_wheel_binds, | 2037 | mods_with_wheel_binds, |
| 2029 | 2038 | ||
| 2030 | // 10 is copied from Clutter: DISCRETE_SCROLL_STEP. | 2039 | // 10 is copied from Clutter: DISCRETE_SCROLL_STEP. |
src/ui/hotkey_overlay.rsmodified+5 -0
| @@ -415,6 +415,11 @@ fn key_name(comp_mod: CompositorMod, key: &Key) -> String { | |||
| 415 | 415 | ||
| 416 | let pretty = match key.trigger { | 416 | let pretty = match key.trigger { |
| 417 | Trigger::Keysym(keysym) => prettify_keysym_name(&keysym_get_name(keysym)), | 417 | Trigger::Keysym(keysym) => prettify_keysym_name(&keysym_get_name(keysym)), |
| 418 | + Trigger::MouseLeft => String::from("Mouse Left"), | ||
| 419 | + Trigger::MouseRight => String::from("Mouse Right"), | ||
| 420 | + Trigger::MouseMiddle => String::from("Mouse Middle"), | ||
| 421 | + Trigger::MouseBack => String::from("Mouse Back"), | ||
| 422 | + Trigger::MouseForward => String::from("Mouse Forward"), | ||
| 418 | Trigger::WheelScrollDown => String::from("Wheel Scroll Down"), | 423 | Trigger::WheelScrollDown => String::from("Wheel Scroll Down"), |
| 419 | Trigger::WheelScrollUp => String::from("Wheel Scroll Up"), | 424 | Trigger::WheelScrollUp => String::from("Wheel Scroll Up"), |
| 420 | Trigger::WheelScrollLeft => String::from("Wheel Scroll Left"), | 425 | Trigger::WheelScrollLeft => String::from("Wheel Scroll Left"), |
wiki/Configuration:-Key-Bindings.mdmodified+20 -0
| @@ -99,6 +99,26 @@ binds { | |||
| 99 | Both mouse wheel and touchpad scroll binds will prevent applications from receiving any scroll events when their modifiers are held down. | 99 | Both mouse wheel and touchpad scroll binds will prevent applications from receiving any scroll events when their modifiers are held down. |
| 100 | For example, if you have a `Mod+WheelScrollDown` bind, then while holding `Mod`, all mouse wheel scrolling will be consumed by niri. | 100 | For example, if you have a `Mod+WheelScrollDown` bind, then while holding `Mod`, all mouse wheel scrolling will be consumed by niri. |
| 101 | 101 | ||
| 102 | +### Mouse Click Bindings | ||
| 103 | + | ||
| 104 | +<sup>Since: next release</sup> | ||
| 105 | + | ||
| 106 | +You can bind mouse clicks using the following syntax. | ||
| 107 | + | ||
| 108 | +```kdl | ||
| 109 | +binds { | ||
| 110 | + Mod+MouseLeft { close-window; } | ||
| 111 | + Mod+MouseRight { close-window; } | ||
| 112 | + Mod+MouseMiddle { close-window; } | ||
| 113 | + Mod+MouseForward { close-window; } | ||
| 114 | + Mod+MouseBack { close-window; } | ||
| 115 | +} | ||
| 116 | +``` | ||
| 117 | + | ||
| 118 | +Mouse clicks operate on the window that was focused at the time of the click, not the window you're clicking. | ||
| 119 | + | ||
| 120 | +Note that binding `Mod+MouseLeft` or `Mod+MouseRight` will override the corresponding gesture (moving or resizing the window). | ||
| 121 | + | ||
| 102 | ### Actions | 122 | ### Actions |
| 103 | 123 | ||
| 104 | Every action that you can bind is also available for programmatic invocation via `niri msg action`. | 124 | Every action that you can bind is also available for programmatic invocation via `niri msg action`. |