support binding actions to switches (#747)
Browse files at this commit5 changed files +127 -4
- niri-config/src/lib.rs +43 -0
- src/input/mod.rs +37 -4
- wiki/Configuration:-Overview.md +1 -0
- wiki/Configuration:-Switch-Events.md +45 -0
- wiki/_Sidebar.md +1 -0
niri-config/src/lib.rsmodified+43 -0
| @@ -54,6 +54,8 @@ pub struct Config { | |||
| 54 | #[knuffel(child, default)] | 54 | #[knuffel(child, default)] |
| 55 | pub binds: Binds, | 55 | pub binds: Binds, |
| 56 | #[knuffel(child, default)] | 56 | #[knuffel(child, default)] |
| 57 | + pub switch_events: SwitchBinds, | ||
| 58 | + #[knuffel(child, default)] | ||
| 57 | pub debug: DebugConfig, | 59 | pub debug: DebugConfig, |
| 58 | #[knuffel(children(name = "workspace"))] | 60 | #[knuffel(children(name = "workspace"))] |
| 59 | pub workspaces: Vec<Workspace>, | 61 | pub workspaces: Vec<Workspace>, |
| @@ -1088,6 +1090,24 @@ bitflags! { | |||
| 1088 | } | 1090 | } |
| 1089 | } | 1091 | } |
| 1090 | 1092 | ||
| 1093 | +#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | ||
| 1094 | +pub struct SwitchBinds { | ||
| 1095 | + #[knuffel(child)] | ||
| 1096 | + pub lid_open: Option<SwitchAction>, | ||
| 1097 | + #[knuffel(child)] | ||
| 1098 | + pub lid_close: Option<SwitchAction>, | ||
| 1099 | + #[knuffel(child)] | ||
| 1100 | + pub tablet_mode_on: Option<SwitchAction>, | ||
| 1101 | + #[knuffel(child)] | ||
| 1102 | + pub tablet_mode_off: Option<SwitchAction>, | ||
| 1103 | +} | ||
| 1104 | + | ||
| 1105 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | ||
| 1106 | +pub struct SwitchAction { | ||
| 1107 | + #[knuffel(child, unwrap(arguments))] | ||
| 1108 | + pub spawn: Vec<String>, | ||
| 1109 | +} | ||
| 1110 | + | ||
| 1091 | // Remember to add new actions to the CLI enum too. | 1111 | // Remember to add new actions to the CLI enum too. |
| 1092 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] | 1112 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] |
| 1093 | pub enum Action { | 1113 | pub enum Action { |
| @@ -3079,6 +3099,11 @@ mod tests { | |||
| 3079 | Mod+WheelScrollDown cooldown-ms=150 { focus-workspace-down; } | 3099 | Mod+WheelScrollDown cooldown-ms=150 { focus-workspace-down; } |
| 3080 | } | 3100 | } |
| 3081 | 3101 | ||
| 3102 | + switch-events { | ||
| 3103 | + tablet-mode-on { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true"; } | ||
| 3104 | + tablet-mode-off { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false"; } | ||
| 3105 | + } | ||
| 3106 | + | ||
| 3082 | debug { | 3107 | debug { |
| 3083 | render-drm-device "/dev/dri/renderD129" | 3108 | render-drm-device "/dev/dri/renderD129" |
| 3084 | } | 3109 | } |
| @@ -3426,6 +3451,24 @@ mod tests { | |||
| 3426 | allow_when_locked: false, | 3451 | allow_when_locked: false, |
| 3427 | }, | 3452 | }, |
| 3428 | ]), | 3453 | ]), |
| 3454 | + switch_events: SwitchBinds { | ||
| 3455 | + lid_open: None, | ||
| 3456 | + lid_close: None, | ||
| 3457 | + tablet_mode_on: Some(SwitchAction { | ||
| 3458 | + spawn: vec![ | ||
| 3459 | + "bash".to_owned(), | ||
| 3460 | + "-c".to_owned(), | ||
| 3461 | + "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true".to_owned(), | ||
| 3462 | + ], | ||
| 3463 | + }), | ||
| 3464 | + tablet_mode_off: Some(SwitchAction { | ||
| 3465 | + spawn: vec![ | ||
| 3466 | + "bash".to_owned(), | ||
| 3467 | + "-c".to_owned(), | ||
| 3468 | + "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false".to_owned(), | ||
| 3469 | + ], | ||
| 3470 | + }), | ||
| 3471 | + }, | ||
| 3429 | debug: DebugConfig { | 3472 | debug: DebugConfig { |
| 3430 | render_drm_device: Some(PathBuf::from("/dev/dri/renderD129")), | 3473 | render_drm_device: Some(PathBuf::from("/dev/dri/renderD129")), |
| 3431 | ..Default::default() | 3474 | ..Default::default() |
src/input/mod.rsmodified+37 -4
| @@ -6,14 +6,15 @@ use std::time::Duration; | |||
| 6 | 6 | ||
| 7 | use calloop::timer::{TimeoutAction, Timer}; | 7 | use calloop::timer::{TimeoutAction, Timer}; |
| 8 | use input::event::gesture::GestureEventCoordinates as _; | 8 | use input::event::gesture::GestureEventCoordinates as _; |
| 9 | -use niri_config::{Action, Bind, Binds, Key, Modifiers, Trigger}; | 9 | +use niri_config::{Action, Bind, Binds, Key, Modifiers, SwitchBinds, Trigger}; |
| 10 | use niri_ipc::LayoutSwitchTarget; | 10 | use niri_ipc::LayoutSwitchTarget; |
| 11 | use smithay::backend::input::{ | 11 | use smithay::backend::input::{ |
| 12 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, | 12 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, |
| 13 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, | 13 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, |
| 14 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, Keycode, MouseButton, PointerAxisEvent, | 14 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, Keycode, MouseButton, PointerAxisEvent, |
| 15 | - PointerButtonEvent, PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent, | 15 | + PointerButtonEvent, PointerMotionEvent, ProximityState, Switch, SwitchState, SwitchToggleEvent, |
| 16 | - TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, TouchEvent, | 16 | + TabletToolButtonEvent, TabletToolEvent, TabletToolProximityEvent, TabletToolTipEvent, |
| 17 | + TabletToolTipState, TouchEvent, | ||
| 17 | }; | 18 | }; |
| 18 | use smithay::backend::libinput::LibinputInputBackend; | 19 | use smithay::backend::libinput::LibinputInputBackend; |
| 19 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; | 20 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; |
| @@ -127,7 +128,7 @@ impl State { | |||
| 127 | TouchUp { event } => self.on_touch_up::<I>(event), | 128 | TouchUp { event } => self.on_touch_up::<I>(event), |
| 128 | TouchCancel { event } => self.on_touch_cancel::<I>(event), | 129 | TouchCancel { event } => self.on_touch_cancel::<I>(event), |
| 129 | TouchFrame { event } => self.on_touch_frame::<I>(event), | 130 | TouchFrame { event } => self.on_touch_frame::<I>(event), |
| 130 | - SwitchToggle { .. } => (), | 131 | + SwitchToggle { event } => self.on_switch_toggle::<I>(event), |
| 131 | Special(_) => (), | 132 | Special(_) => (), |
| 132 | } | 133 | } |
| 133 | 134 | ||
| @@ -2360,6 +2361,21 @@ impl State { | |||
| 2360 | }; | 2361 | }; |
| 2361 | handle.cancel(self); | 2362 | handle.cancel(self); |
| 2362 | } | 2363 | } |
| 2364 | + | ||
| 2365 | + fn on_switch_toggle<I: InputBackend>(&mut self, evt: I::SwitchToggleEvent) { | ||
| 2366 | + let Some(switch) = evt.switch() else { | ||
| 2367 | + return; | ||
| 2368 | + }; | ||
| 2369 | + | ||
| 2370 | + let action = { | ||
| 2371 | + let bindings = &self.niri.config.borrow().switch_events; | ||
| 2372 | + find_configured_switch_action(bindings, switch, evt.state()) | ||
| 2373 | + }; | ||
| 2374 | + | ||
| 2375 | + if let Some(action) = action { | ||
| 2376 | + self.do_action(action, true); | ||
| 2377 | + } | ||
| 2378 | + } | ||
| 2363 | } | 2379 | } |
| 2364 | 2380 | ||
| 2365 | /// Check whether the key should be intercepted and mark intercepted | 2381 | /// Check whether the key should be intercepted and mark intercepted |
| @@ -2511,6 +2527,23 @@ fn find_configured_bind( | |||
| 2511 | None | 2527 | None |
| 2512 | } | 2528 | } |
| 2513 | 2529 | ||
| 2530 | +fn find_configured_switch_action( | ||
| 2531 | + bindings: &SwitchBinds, | ||
| 2532 | + switch: Switch, | ||
| 2533 | + state: SwitchState, | ||
| 2534 | +) -> Option<Action> { | ||
| 2535 | + let switch_action = match (switch, state) { | ||
| 2536 | + (Switch::Lid, SwitchState::Off) => &bindings.lid_open, | ||
| 2537 | + (Switch::Lid, SwitchState::On) => &bindings.lid_close, | ||
| 2538 | + (Switch::TabletMode, SwitchState::Off) => &bindings.tablet_mode_off, | ||
| 2539 | + (Switch::TabletMode, SwitchState::On) => &bindings.tablet_mode_on, | ||
| 2540 | + _ => unreachable!(), | ||
| 2541 | + }; | ||
| 2542 | + switch_action | ||
| 2543 | + .as_ref() | ||
| 2544 | + .map(|switch_action| Action::Spawn(switch_action.spawn.clone())) | ||
| 2545 | +} | ||
| 2546 | + | ||
| 2514 | fn modifiers_from_state(mods: ModifiersState) -> Modifiers { | 2547 | fn modifiers_from_state(mods: ModifiersState) -> Modifiers { |
| 2515 | let mut modifiers = Modifiers::empty(); | 2548 | let mut modifiers = Modifiers::empty(); |
| 2516 | if mods.ctrl { | 2549 | if mods.ctrl { |
wiki/Configuration:-Overview.mdmodified+1 -0
| @@ -5,6 +5,7 @@ You can find documentation for various sections of the config on these wiki page | |||
| 5 | * [`input {}`](./Configuration:-Input.md) | 5 | * [`input {}`](./Configuration:-Input.md) |
| 6 | * [`output "eDP-1" {}`](./Configuration:-Outputs.md) | 6 | * [`output "eDP-1" {}`](./Configuration:-Outputs.md) |
| 7 | * [`binds {}`](./Configuration:-Key-Bindings.md) | 7 | * [`binds {}`](./Configuration:-Key-Bindings.md) |
| 8 | +* [`switch-events {}`](./Configuration:-Switch-Events.md) | ||
| 8 | * [`layout {}`](./Configuration:-Layout.md) | 9 | * [`layout {}`](./Configuration:-Layout.md) |
| 9 | * [top-level options](./Configuration:-Miscellaneous.md) | 10 | * [top-level options](./Configuration:-Miscellaneous.md) |
| 10 | * [`window-rule {}`](./Configuration:-Window-Rules.md) | 11 | * [`window-rule {}`](./Configuration:-Window-Rules.md) |
wiki/Configuration:-Switch-Events.mdadded+45 -0
| @@ -0,0 +1,45 @@ | |||
| 1 | +### Overview | ||
| 2 | + | ||
| 3 | +Switch event bindings are declared in the `switch-events {}` section of the config. | ||
| 4 | + | ||
| 5 | +Here are all the events that you can bind at a glance: | ||
| 6 | + | ||
| 7 | +```kdl | ||
| 8 | +switch-events { | ||
| 9 | + lid-close { spawn "bash" "-c" "niri msg output \"eDP-1\" off"; } | ||
| 10 | + lid-open { spawn "bash" "-c" "niri msg output \"eDP-1\" on"; } | ||
| 11 | + tablet-mode-on { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true"; } | ||
| 12 | + tablet-mode-off { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false"; } | ||
| 13 | +} | ||
| 14 | +``` | ||
| 15 | + | ||
| 16 | +The syntax is similar to key bindings. | ||
| 17 | +Currently only the `spawn` action are supported. | ||
| 18 | + | ||
| 19 | +> [!NOTE] | ||
| 20 | +> In contrast to key bindings, switch event bindings are *always* executed, even when the session is locked. | ||
| 21 | + | ||
| 22 | +### `lid-close`, `lid-open` | ||
| 23 | + | ||
| 24 | +These events correspond to closing and opening of the laptop lid. | ||
| 25 | + | ||
| 26 | +You could use them to turn the laptop internal monitor off and on (until niri gets this functionality built-in). | ||
| 27 | + | ||
| 28 | +```kdl | ||
| 29 | +switch-events { | ||
| 30 | + lid-close { spawn "bash" "-c" "niri msg output \"eDP-1\" off"; } | ||
| 31 | + lid-open { spawn "bash" "-c" "niri msg output \"eDP-1\" on"; } | ||
| 32 | +} | ||
| 33 | +``` | ||
| 34 | + | ||
| 35 | +### `tablet-mode-on`, `tablet-mode-off` | ||
| 36 | + | ||
| 37 | +These events trigger when a convertible laptop goes into or out of tablet mode. | ||
| 38 | +In tablet mode, the keyboard and mouse are usually inaccessible, so you can use these events to activate the on-screen keyboard. | ||
| 39 | + | ||
| 40 | +```kdl | ||
| 41 | +switch-events { | ||
| 42 | + tablet-mode-on { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled true"; } | ||
| 43 | + tablet-mode-off { spawn "bash" "-c" "gsettings set org.gnome.desktop.a11y.applications screen-keyboard-enabled false"; } | ||
| 44 | +} | ||
| 45 | +``` | ||
wiki/_Sidebar.mdmodified+1 -0
| @@ -14,6 +14,7 @@ | |||
| 14 | * [Input](./Configuration:-Input.md) | 14 | * [Input](./Configuration:-Input.md) |
| 15 | * [Outputs](./Configuration:-Outputs.md) | 15 | * [Outputs](./Configuration:-Outputs.md) |
| 16 | * [Key Bindings](./Configuration:-Key-Bindings.md) | 16 | * [Key Bindings](./Configuration:-Key-Bindings.md) |
| 17 | +* [Switch Events](./Configuration:-Switch-Events.md) | ||
| 17 | * [Layout](./Configuration:-Layout.md) | 18 | * [Layout](./Configuration:-Layout.md) |
| 18 | * [Named Workspaces](./Configuration:-Named-Workspaces.md) | 19 | * [Named Workspaces](./Configuration:-Named-Workspaces.md) |
| 19 | * [Miscellaneous](./Configuration:-Miscellaneous.md) | 20 | * [Miscellaneous](./Configuration:-Miscellaneous.md) |