Add suspend key handling
Browse files at this commit14729f772bfc049cfe04d43b5a5ab7186c2bc021Parent: 2c1d2ce5 changed files +40 -4
- Cargo.lock +11 -0
- Cargo.toml +1 -0
- src/input.rs +14 -3
- src/tty.rs +13 -1
- src/winit.rs +1 -0
Cargo.lockmodified+11 -0
| @@ -1109,6 +1109,16 @@ version = "0.4.20" | |||
| 1109 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1109 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1110 | checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" | 1110 | checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" |
| 1111 | 1111 | ||
| 1112 | +[[package]] | ||
| 1113 | +name = "logind-zbus" | ||
| 1114 | +version = "3.1.2" | ||
| 1115 | +source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1116 | +checksum = "c07a2542f6e91ea92780158654852190edb2ba0b232d9d00d649d0c691cb7eb3" | ||
| 1117 | +dependencies = [ | ||
| 1118 | + "serde", | ||
| 1119 | + "zbus", | ||
| 1120 | +] | ||
| 1121 | + | ||
| 1112 | [[package]] | 1122 | [[package]] |
| 1113 | name = "loom" | 1123 | name = "loom" |
| 1114 | version = "0.5.6" | 1124 | version = "0.5.6" |
| @@ -1240,6 +1250,7 @@ dependencies = [ | |||
| 1240 | "directories", | 1250 | "directories", |
| 1241 | "image", | 1251 | "image", |
| 1242 | "keyframe", | 1252 | "keyframe", |
| 1253 | + "logind-zbus", | ||
| 1243 | "profiling", | 1254 | "profiling", |
| 1244 | "sd-notify", | 1255 | "sd-notify", |
| 1245 | "smithay", | 1256 | "smithay", |
Cargo.tomlmodified+1 -0
| @@ -13,6 +13,7 @@ clap = { version = "4.3.21", features = ["derive"] } | |||
| 13 | directories = "5.0.1" | 13 | directories = "5.0.1" |
| 14 | image = { version = "0.24.7", default-features = false, features = ["png"] } | 14 | image = { version = "0.24.7", default-features = false, features = ["png"] } |
| 15 | keyframe = { version = "1.1.1", default-features = false } | 15 | keyframe = { version = "1.1.1", default-features = false } |
| 16 | +logind-zbus = "3.1.2" | ||
| 16 | profiling = "1.0.9" | 17 | profiling = "1.0.9" |
| 17 | sd-notify = "0.4.1" | 18 | sd-notify = "0.4.1" |
| 18 | smithay-drm-extras = { version = "0.1.0", path = "../smithay/smithay-drm-extras" } | 19 | smithay-drm-extras = { version = "0.1.0", path = "../smithay/smithay-drm-extras" } |
src/input.rsmodified+14 -3
| @@ -24,6 +24,7 @@ enum Action { | |||
| 24 | None, | 24 | None, |
| 25 | Quit, | 25 | Quit, |
| 26 | ChangeVt(i32), | 26 | ChangeVt(i32), |
| 27 | + Suspend, | ||
| 27 | Spawn(String), | 28 | Spawn(String), |
| 28 | Screenshot, | 29 | Screenshot, |
| 29 | CloseWindow, | 30 | CloseWindow, |
| @@ -57,6 +58,7 @@ enum Action { | |||
| 57 | pub enum BackendAction { | 58 | pub enum BackendAction { |
| 58 | None, | 59 | None, |
| 59 | ChangeVt(i32), | 60 | ChangeVt(i32), |
| 61 | + Suspend, | ||
| 60 | Screenshot, | 62 | Screenshot, |
| 61 | } | 63 | } |
| 62 | 64 | ||
| @@ -78,9 +80,15 @@ fn action(comp_mod: CompositorMod, keysym: KeysymHandle, mods: ModifiersState) - | |||
| 78 | use keysyms::*; | 80 | use keysyms::*; |
| 79 | 81 | ||
| 80 | let modified = keysym.modified_sym(); | 82 | let modified = keysym.modified_sym(); |
| 81 | - if matches!(modified, KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12) { | 83 | + |
| 82 | - let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; | 84 | + #[allow(non_upper_case_globals)] // wat |
| 83 | - return Action::ChangeVt(vt); | 85 | + match modified { |
| 86 | + modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => { | ||
| 87 | + let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; | ||
| 88 | + return Action::ChangeVt(vt); | ||
| 89 | + } | ||
| 90 | + KEY_XF86PowerOff => return Action::Suspend, | ||
| 91 | + _ => (), | ||
| 84 | } | 92 | } |
| 85 | 93 | ||
| 86 | let mod_down = match comp_mod { | 94 | let mod_down = match comp_mod { |
| @@ -182,6 +190,9 @@ impl Niri { | |||
| 182 | Action::ChangeVt(vt) => { | 190 | Action::ChangeVt(vt) => { |
| 183 | return BackendAction::ChangeVt(vt); | 191 | return BackendAction::ChangeVt(vt); |
| 184 | } | 192 | } |
| 193 | + Action::Suspend => { | ||
| 194 | + return BackendAction::Suspend; | ||
| 195 | + } | ||
| 185 | Action::Spawn(command) => { | 196 | Action::Spawn(command) => { |
| 186 | if let Err(err) = Command::new(command).spawn() { | 197 | if let Err(err) = Command::new(command).spawn() { |
| 187 | warn!("error spawning alacritty: {err}"); | 198 | warn!("error spawning alacritty: {err}"); |
src/tty.rsmodified+13 -1
| @@ -3,7 +3,7 @@ use std::os::fd::FromRawFd; | |||
| 3 | use std::path::{Path, PathBuf}; | 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| 5 | 5 | ||
| 6 | -use anyhow::anyhow; | 6 | +use anyhow::{anyhow, Context}; |
| 7 | use smithay::backend::allocator::dmabuf::Dmabuf; | 7 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 8 | use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice}; | 8 | use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice}; |
| 9 | use smithay::backend::allocator::{Format as DrmFormat, Fourcc}; | 9 | use smithay::backend::allocator::{Format as DrmFormat, Fourcc}; |
| @@ -179,6 +179,11 @@ impl Tty { | |||
| 179 | match niri.process_input_event(CompositorMod::Super, event) { | 179 | match niri.process_input_event(CompositorMod::Super, event) { |
| 180 | BackendAction::None => (), | 180 | BackendAction::None => (), |
| 181 | BackendAction::ChangeVt(vt) => tty.change_vt(vt), | 181 | BackendAction::ChangeVt(vt) => tty.change_vt(vt), |
| 182 | + BackendAction::Suspend => { | ||
| 183 | + if let Err(err) = suspend() { | ||
| 184 | + warn!("error suspending: {err:?}"); | ||
| 185 | + } | ||
| 186 | + } | ||
| 182 | BackendAction::Screenshot => { | 187 | BackendAction::Screenshot => { |
| 183 | let active = niri.monitor_set.active_output().cloned(); | 188 | let active = niri.monitor_set.active_output().cloned(); |
| 184 | if let Some(active) = active { | 189 | if let Some(active) = active { |
| @@ -609,3 +614,10 @@ fn refresh_interval(mode: DrmMode) -> Duration { | |||
| 609 | let refresh_interval = (numerator + denominator / 2) / denominator; | 614 | let refresh_interval = (numerator + denominator / 2) / denominator; |
| 610 | Duration::from_nanos(refresh_interval) | 615 | Duration::from_nanos(refresh_interval) |
| 611 | } | 616 | } |
| 617 | + | ||
| 618 | +fn suspend() -> anyhow::Result<()> { | ||
| 619 | + let conn = zbus::blocking::Connection::system().context("error connecting to system bus")?; | ||
| 620 | + let manager = logind_zbus::manager::ManagerProxyBlocking::new(&conn) | ||
| 621 | + .context("error creating login manager proxy")?; | ||
| 622 | + manager.suspend(true).context("error suspending") | ||
| 623 | +} | ||
src/winit.rsmodified+1 -0
| @@ -149,6 +149,7 @@ impl Winit { | |||
| 149 | match niri.process_input_event(CompositorMod::Alt, event) { | 149 | match niri.process_input_event(CompositorMod::Alt, event) { |
| 150 | BackendAction::None => (), | 150 | BackendAction::None => (), |
| 151 | BackendAction::ChangeVt(_) => (), | 151 | BackendAction::ChangeVt(_) => (), |
| 152 | + BackendAction::Suspend => (), | ||
| 152 | BackendAction::Screenshot => { | 153 | BackendAction::Screenshot => { |
| 153 | let active = niri.monitor_set.active_output().cloned(); | 154 | let active = niri.monitor_set.active_output().cloned(); |
| 154 | if let Some(active) = active { | 155 | if let Some(active) = active { |