Add disable-power-key-handling option

This commit is contained in:
Ivan Molodetskikh
2023-12-28 09:36:10 +04:00
parent f3e5e13c45
commit 249f2b7a21
4 changed files with 30 additions and 4 deletions
+6
View File
@@ -37,6 +37,12 @@ input {
// existing outputs. // existing outputs.
map-to-output "eDP-1" map-to-output "eDP-1"
} }
// By default, niri will take over the power button to make it sleep
// instead of power off.
// Uncomment this if you would like to configure the power button elsewhere
// (i.e. logind.conf).
// disable-power-key-handling
} }
// You can configure outputs by their name, which you can find with wayland-info(1). // You can configure outputs by their name, which you can find with wayland-info(1).
+5
View File
@@ -55,6 +55,8 @@ pub struct Input {
pub touchpad: Touchpad, pub touchpad: Touchpad,
#[knuffel(child, default)] #[knuffel(child, default)]
pub tablet: Tablet, pub tablet: Tablet,
#[knuffel(child)]
pub disable_power_key_handling: bool,
} }
#[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
@@ -585,6 +587,8 @@ mod tests {
tablet { tablet {
map-to-output "eDP-1" map-to-output "eDP-1"
} }
disable-power-key-handling
} }
output "eDP-1" { output "eDP-1" {
@@ -666,6 +670,7 @@ mod tests {
tablet: Tablet { tablet: Tablet {
map_to_output: Some("eDP-1".to_owned()), map_to_output: Some("eDP-1".to_owned()),
}, },
disable_power_key_handling: true,
}, },
outputs: vec![Output { outputs: vec![Output {
off: false, off: false,
+15 -2
View File
@@ -209,6 +209,7 @@ impl State {
pressed, pressed,
*mods, *mods,
&this.niri.screenshot_ui, &this.niri.screenshot_ui,
this.niri.config.borrow().input.disable_power_key_handling,
) )
}, },
) else { ) else {
@@ -1165,6 +1166,7 @@ fn should_intercept_key(
pressed: bool, pressed: bool,
mods: ModifiersState, mods: ModifiersState,
screenshot_ui: &ScreenshotUi, screenshot_ui: &ScreenshotUi,
disable_power_key_handling: bool,
) -> FilterResult<Option<Action>> { ) -> FilterResult<Option<Action>> {
// Actions are only triggered on presses, release of the key // Actions are only triggered on presses, release of the key
// shouldn't try to intercept anything unless we have marked // shouldn't try to intercept anything unless we have marked
@@ -1173,7 +1175,14 @@ fn should_intercept_key(
return FilterResult::Forward; return FilterResult::Forward;
} }
let mut final_action = action(bindings, comp_mod, modified, raw, mods); let mut final_action = action(
bindings,
comp_mod,
modified,
raw,
mods,
disable_power_key_handling,
);
// Allow only a subset of compositor actions while the screenshot UI is open, since the user // Allow only a subset of compositor actions while the screenshot UI is open, since the user
// cannot see the screen. // cannot see the screen.
@@ -1210,6 +1219,7 @@ fn action(
modified: Keysym, modified: Keysym,
raw: Option<Keysym>, raw: Option<Keysym>,
mods: ModifiersState, mods: ModifiersState,
disable_power_key_handling: bool,
) -> Option<Action> { ) -> Option<Action> {
use keysyms::*; use keysyms::*;
@@ -1220,7 +1230,7 @@ fn action(
let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32;
return Some(Action::ChangeVt(vt)); return Some(Action::ChangeVt(vt));
} }
KEY_XF86PowerOff => return Some(Action::Suspend), KEY_XF86PowerOff if !disable_power_key_handling => return Some(Action::Suspend),
_ => (), _ => (),
} }
@@ -1325,6 +1335,7 @@ mod tests {
let mut suppressed_keys = HashSet::new(); let mut suppressed_keys = HashSet::new();
let screenshot_ui = ScreenshotUi::new(); let screenshot_ui = ScreenshotUi::new();
let disable_power_key_handling = false;
// The key_code we pick is arbitrary, the only thing // The key_code we pick is arbitrary, the only thing
// that matters is that they are different between cases. // that matters is that they are different between cases.
@@ -1341,6 +1352,7 @@ mod tests {
pressed, pressed,
mods, mods,
&screenshot_ui, &screenshot_ui,
disable_power_key_handling,
) )
}; };
@@ -1356,6 +1368,7 @@ mod tests {
pressed, pressed,
mods, mods,
&screenshot_ui, &screenshot_ui,
disable_power_key_handling,
) )
}; };
+2
View File
@@ -163,10 +163,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Inhibit power key handling so we can suspend on it. // Inhibit power key handling so we can suspend on it.
#[cfg(feature = "dbus")] #[cfg(feature = "dbus")]
if !state.niri.config.borrow().input.disable_power_key_handling {
if let Err(err) = state.niri.inhibit_power_key() { if let Err(err) = state.niri.inhibit_power_key() {
warn!("error inhibiting power key: {err:?}"); warn!("error inhibiting power key: {err:?}");
} }
} }
}
#[cfg(feature = "dbus")] #[cfg(feature = "dbus")]
dbus::DBusServers::start(&mut state, is_systemd_service); dbus::DBusServers::start(&mut state, is_systemd_service);