Skip to content

Commit f2fc1e9

Browse files
committed
dbus: Take lid-inhibitor-locks on systemd systems
1 parent 8648d44 commit f2fc1e9

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ reis = { version = "0.5", features = ["calloop"] }
6666
# CLI arguments
6767
clap_lex = "0.7"
6868
parking_lot = "0.12.3"
69+
logind-zbus = { version = "5.3.2", optional = true }
6970

7071
[dependencies.id_tree]
7172
branch = "feature/copy_clone"
@@ -100,7 +101,7 @@ optional = true
100101
[features]
101102
debug = ["egui", "egui_plot", "smithay-egui", "anyhow/backtrace"]
102103
default = ["systemd"]
103-
systemd = ["libsystemd"]
104+
systemd = ["libsystemd", "logind-zbus"]
104105
profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"]
105106

106107
[profile.dev.package.tiny-skia]

src/dbus/logind.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::os::fd::OwnedFd;
2+
3+
use logind_zbus::manager::{InhibitType::HandleLidSwitch, ManagerProxyBlocking};
4+
use zbus::blocking::Connection;
5+
6+
pub fn inhibit_lid() -> anyhow::Result<OwnedFd> {
7+
let conn = Connection::system()?;
8+
let proxy = ManagerProxyBlocking::new(&conn)?;
9+
let fd = proxy.inhibit(
10+
HandleLidSwitch,
11+
"cosmic-comp",
12+
"External output connected",
13+
"block",
14+
)?;
15+
16+
Ok(fd.into())
17+
}

src/dbus/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use calloop::{InsertError, LoopHandle, RegistrationToken};
44
use std::collections::HashMap;
55
use zbus::blocking::{fdo::DBusProxy, Connection};
66

7+
#[cfg(feature = "systemd")]
8+
pub mod logind;
79
mod power;
810

911
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> {

src/state.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ use smithay::{
112112
};
113113
use time::UtcOffset;
114114

115+
#[cfg(feature = "systemd")]
116+
use std::os::fd::OwnedFd;
117+
115118
use std::{
116119
cell::RefCell,
117120
cmp::min,
@@ -249,6 +252,9 @@ pub struct Common {
249252

250253
pub atspi_state: AtspiState,
251254
pub atspi_ei: crate::wayland::handlers::atspi::AtspiEiState,
255+
256+
#[cfg(feature = "systemd")]
257+
inhibit_lid_fd: Option<OwnedFd>,
252258
}
253259

254260
#[derive(Debug)]
@@ -504,6 +510,7 @@ impl<'a> LockedBackend<'a> {
504510
}
505511

506512
loop_handle.insert_idle(move |state| {
513+
state.update_inhibitor_locks();
507514
state.common.update_xwayland_scale();
508515
state.common.update_xwayland_primary_output();
509516
});
@@ -707,6 +714,9 @@ impl State {
707714

708715
atspi_state,
709716
atspi_ei: Default::default(),
717+
718+
#[cfg(feature = "systemd")]
719+
inhibit_lid_fd: None,
710720
},
711721
backend: BackendData::Unset,
712722
ready: Once::new(),
@@ -726,6 +736,34 @@ impl State {
726736
security_context: None,
727737
}
728738
}
739+
740+
fn update_inhibitor_locks(&mut self) {
741+
#[cfg(feature = "systemd")]
742+
{
743+
use tracing::{debug, error};
744+
745+
let outputs = self.backend.lock().all_outputs();
746+
let should_handle_lid = outputs.iter().any(|o| o.is_internal()) && outputs.len() >= 2;
747+
748+
if should_handle_lid {
749+
if self.common.inhibit_lid_fd.is_none() {
750+
match crate::dbus::logind::inhibit_lid() {
751+
Ok(fd) => {
752+
debug!("Inhibiting lid switch");
753+
self.common.inhibit_lid_fd = Some(fd);
754+
}
755+
Err(err) => {
756+
error!("Failed to inhibit lid switch: {}", err);
757+
}
758+
}
759+
}
760+
} else {
761+
if self.common.inhibit_lid_fd.take().is_some() {
762+
debug!("Removing inhibitor-lock on lid switch")
763+
}
764+
}
765+
}
766+
}
729767
}
730768

731769
fn primary_scanout_output_compare<'a>(

0 commit comments

Comments
 (0)