cosmic-greeter/src/logind.rs

125 lines
4.4 KiB
Rust
Raw Normal View History

use cosmic::iced::{
Subscription,
futures::{SinkExt, StreamExt, channel::mpsc},
};
2024-04-05 19:02:20 -06:00
use logind_zbus::{
manager::{InhibitType, ManagerProxy},
session::SessionProxy,
};
use std::{any::TypeId, error::Error, os::fd::OwnedFd, sync::Arc, time::Duration};
use zbus::Connection;
2023-11-29 08:02:14 -07:00
2025-06-04 11:03:07 -06:00
use crate::{common, locker::Message};
2024-04-05 19:02:20 -06:00
pub async fn power_off() -> zbus::Result<()> {
2023-11-29 08:02:14 -07:00
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
2024-04-05 06:21:40 -04:00
manager.power_off(false).await
2023-11-29 08:02:14 -07:00
}
pub async fn reboot() -> zbus::Result<()> {
2023-11-29 08:02:14 -07:00
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
manager.reboot(false).await
}
pub async fn suspend() -> zbus::Result<()> {
2023-11-29 08:02:14 -07:00
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
manager.suspend(false).await
}
2024-04-05 19:02:20 -06:00
async fn inhibit(manager: &ManagerProxy<'_>) -> zbus::Result<OwnedFd> {
let what = InhibitType::Sleep;
let who = "COSMIC Greeter";
let why = "COSMIC Greeter needs to display a lock screen";
let mode = "delay";
//TODO: update logind-zbus to fix inhibit signature
let fd: zbus::zvariant::OwnedFd = manager
.inner()
.call("Inhibit", &(what, who, why, mode))
.await?;
// Have to convert to std type to avoid leaking zbus dependency
2024-05-17 09:11:50 -06:00
Ok(fd.into())
2024-04-05 19:02:20 -06:00
}
pub fn subscription() -> Subscription<Message> {
struct LogindSubscription;
2026-02-24 15:49:22 -05:00
Subscription::run_with(TypeId::of::<LogindSubscription>(), |_| {
cosmic::iced_futures::stream::channel(16, |mut msg_tx| async move {
match handler(&mut msg_tx).await {
Ok(()) => {}
Err(err) => {
2025-09-12 17:39:37 -04:00
tracing::warn!("logind error: {}", err);
//TODO: send error
}
}
std::process::exit(1);
2026-02-24 15:49:22 -05:00
})
})
}
//TODO: use never type?
2024-04-05 19:02:20 -06:00
pub async fn handler(msg_tx: &mut mpsc::Sender<Message>) -> Result<(), Box<dyn Error>> {
let connection = Connection::system().await?;
let manager = ManagerProxy::new(&connection).await?;
2024-11-07 09:37:16 -07:00
let session_path = manager
.get_session_by_PID(std::os::unix::process::parent_id())
.await?;
let session = SessionProxy::builder(&connection)
.path(&session_path)?
.build()
.await?;
2024-04-05 19:02:20 -06:00
let mut inhibit_opt = Some(inhibit(&manager).await?);
let mut prepare_for_sleep = manager.receive_prepare_for_sleep().await?;
let mut lock = session.receive_lock().await?;
let mut unlock = session.receive_unlock().await?;
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
2024-04-05 19:02:20 -06:00
// Waits until a signal has been received
tokio::select!(
signal_opt = prepare_for_sleep.next() => {
match signal_opt {
Some(signal) => match signal.args() {
Ok(args) => {
if args.start {
2025-09-12 17:39:37 -04:00
tracing::info!("logind prepare for sleep");
2024-04-05 19:02:20 -06:00
if let Some(inhibit) = inhibit_opt.take() {
msg_tx.send(Message::Inhibit(Arc::new(inhibit))).await?;
}
msg_tx.send(Message::Lock).await?;
} else {
2025-09-12 17:39:37 -04:00
tracing::info!("logind resume");
2024-04-05 19:02:20 -06:00
if inhibit_opt.is_none() {
inhibit_opt = Some(inhibit(&manager).await?);
}
2025-06-04 11:03:07 -06:00
// Immediately update time when resuming from sleep.
msg_tx.send(Message::Common(common::Message::Tick)).await?;
2024-04-05 19:02:20 -06:00
}
},
Err(err) => {
2025-09-12 17:39:37 -04:00
tracing::warn!("logind prepare to sleep invalid data: {}", err);
2024-04-05 19:02:20 -06:00
}
},
None => {
2025-09-12 17:39:37 -04:00
tracing::warn!("logind prepare to sleep missing data");
2024-04-05 19:02:20 -06:00
}
}
},
_ = lock.next() => {
2025-09-12 17:39:37 -04:00
tracing::info!("logind lock");
2024-04-05 19:02:20 -06:00
msg_tx.send(Message::Lock).await?;
}, _ = unlock.next() => {
2025-09-12 17:39:37 -04:00
tracing::info!("logind unlock");
2024-04-05 19:02:20 -06:00
msg_tx.send(Message::Unlock).await?;
});
interval.tick().await;
}
}