Implement suspend with logind

This commit is contained in:
Jeremy Soller 2023-11-29 08:02:14 -07:00
parent 5d0999df83
commit 13e3dcd911
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
5 changed files with 58 additions and 1 deletions

12
Cargo.lock generated
View file

@ -800,11 +800,13 @@ dependencies = [
"greetd_ipc",
"libcosmic",
"log",
"logind-zbus",
"pam-client",
"pwd",
"shlex",
"tokio",
"wayland-client 0.31.1",
"zbus",
]
[[package]]
@ -2376,6 +2378,16 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "logind-zbus"
version = "3.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c07a2542f6e91ea92780158654852190edb2ba0b232d9d00d649d0c691cb7eb3"
dependencies = [
"serde",
"zbus",
]
[[package]]
name = "lru"
version = "0.11.1"

View file

@ -8,10 +8,12 @@ chrono = "0.4.31"
env_logger = "0.10.0"
freedesktop_entry_parser = "1.3.0"
log = "0.4.20"
logind-zbus = { version = "3.1.2", optional = true }
pam-client = "0.5.0"
pwd = "1.4.0"
shlex = "1.2.0"
wayland-client = "0.31.1"
zbus = { version = "3.14.1", optional = true }
[dependencies.cosmic-bg-config]
git = "https://github.com/pop-os/cosmic-bg"
@ -31,3 +33,7 @@ features = ["tokio", "wayland"]
[dependencies.tokio]
version = "1.33.0"
features = ["full"]
[features]
default = ["logind"]
logind = ["logind-zbus", "zbus"]

View file

@ -198,6 +198,7 @@ pub enum Message {
BackgroundState(cosmic_bg_config::state::State),
Prompt(String, bool, Option<String>),
Submit,
Suspend,
Error(String),
Exit,
}
@ -422,6 +423,21 @@ impl cosmic::Application for App {
},
None => log::warn!("tried to submit without prompt"),
},
Message::Suspend => {
#[cfg(feature = "logind")]
return Command::perform(
async move {
match crate::logind::suspend().await {
Ok(()) => message::none(),
Err(err) => {
log::error!("failed to suspend: {:?}", err);
message::app(Message::Error(err.to_string()))
}
}
},
|x| x,
);
}
Message::Error(error) => {
self.error_opt = Some(error);
}
@ -499,7 +515,7 @@ impl cosmic::Application for App {
.on_press(Message::None),
widget::button(widget::icon::from_name("system-suspend-symbolic"))
.padding(12.0)
.on_press(Message::None),
.on_press(Message::Suspend),
]
.padding([16.0, 0.0, 0.0, 0.0])
.spacing(8.0);

20
src/logind.rs Normal file
View file

@ -0,0 +1,20 @@
use logind_zbus::manager::ManagerProxy;
use zbus::{Connection, Result};
pub async fn power_off() -> Result<()> {
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
manager.reboot(false).await
}
pub async fn reboot() -> Result<()> {
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
manager.reboot(false).await
}
pub async fn suspend() -> Result<()> {
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
manager.suspend(false).await
}

View file

@ -5,6 +5,9 @@ mod greeter;
mod image_container;
mod locker;
#[cfg(feature = "logind")]
mod logind;
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();