Set logind LockedHint on lock/unlock (#1763)
Browse files at this commit3 changed files +91 -0
- src/main.rs +1 -0
- src/niri.rs +89 -0
- src/tests/server.rs +1 -0
src/main.rsmodified+1 -0
| @@ -216,6 +216,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 216 | display, | 216 | display, |
| 217 | false, | 217 | false, |
| 218 | true, | 218 | true, |
| 219 | + cli.session, | ||
| 219 | ) | 220 | ) |
| 220 | .unwrap(); | 221 | .unwrap(); |
| 221 | 222 | ||
src/niri.rsmodified+89 -0
| @@ -193,6 +193,9 @@ pub struct Niri { | |||
| 193 | pub stop_signal: LoopSignal, | 193 | pub stop_signal: LoopSignal, |
| 194 | pub display_handle: DisplayHandle, | 194 | pub display_handle: DisplayHandle, |
| 195 | 195 | ||
| 196 | + /// Whether niri was run with `--session` | ||
| 197 | + pub is_session_instance: bool, | ||
| 198 | + | ||
| 196 | /// Name of the Wayland socket. | 199 | /// Name of the Wayland socket. |
| 197 | /// | 200 | /// |
| 198 | /// This is `None` when creating `Niri` without a Wayland socket. | 201 | /// This is `None` when creating `Niri` without a Wayland socket. |
| @@ -360,6 +363,9 @@ pub struct Niri { | |||
| 360 | 363 | ||
| 361 | pub lock_state: LockState, | 364 | pub lock_state: LockState, |
| 362 | 365 | ||
| 366 | + // State that we last sent to the logind LockedHint. | ||
| 367 | + pub locked_hint: Option<bool>, | ||
| 368 | + | ||
| 363 | pub screenshot_ui: ScreenshotUi, | 369 | pub screenshot_ui: ScreenshotUi, |
| 364 | pub config_error_notification: ConfigErrorNotification, | 370 | pub config_error_notification: ConfigErrorNotification, |
| 365 | pub hotkey_overlay: HotkeyOverlay, | 371 | pub hotkey_overlay: HotkeyOverlay, |
| @@ -629,6 +635,7 @@ impl State { | |||
| 629 | display: Display<State>, | 635 | display: Display<State>, |
| 630 | headless: bool, | 636 | headless: bool, |
| 631 | create_wayland_socket: bool, | 637 | create_wayland_socket: bool, |
| 638 | + is_session_instance: bool, | ||
| 632 | ) -> Result<Self, Box<dyn std::error::Error>> { | 639 | ) -> Result<Self, Box<dyn std::error::Error>> { |
| 633 | let _span = tracy_client::span!("State::new"); | 640 | let _span = tracy_client::span!("State::new"); |
| 634 | 641 | ||
| @@ -657,6 +664,7 @@ impl State { | |||
| 657 | display, | 664 | display, |
| 658 | &backend, | 665 | &backend, |
| 659 | create_wayland_socket, | 666 | create_wayland_socket, |
| 667 | + is_session_instance, | ||
| 660 | ); | 668 | ); |
| 661 | backend.init(&mut niri); | 669 | backend.init(&mut niri); |
| 662 | 670 | ||
| @@ -690,6 +698,9 @@ impl State { | |||
| 690 | self.niri.display_handle.flush_clients().unwrap(); | 698 | self.niri.display_handle.flush_clients().unwrap(); |
| 691 | } | 699 | } |
| 692 | 700 | ||
| 701 | + #[cfg(feature = "dbus")] | ||
| 702 | + self.niri.update_locked_hint(); | ||
| 703 | + | ||
| 693 | // Clear the time so it's fetched afresh next iteration. | 704 | // Clear the time so it's fetched afresh next iteration. |
| 694 | self.niri.clock.clear(); | 705 | self.niri.clock.clear(); |
| 695 | self.niri.pointer_inactivity_timer_got_reset = false; | 706 | self.niri.pointer_inactivity_timer_got_reset = false; |
| @@ -2225,6 +2236,7 @@ impl Niri { | |||
| 2225 | display: Display<State>, | 2236 | display: Display<State>, |
| 2226 | backend: &Backend, | 2237 | backend: &Backend, |
| 2227 | create_wayland_socket: bool, | 2238 | create_wayland_socket: bool, |
| 2239 | + is_session_instance: bool, | ||
| 2228 | ) -> Self { | 2240 | ) -> Self { |
| 2229 | let _span = tracy_client::span!("Niri::new"); | 2241 | let _span = tracy_client::span!("Niri::new"); |
| 2230 | 2242 | ||
| @@ -2495,6 +2507,7 @@ impl Niri { | |||
| 2495 | stop_signal, | 2507 | stop_signal, |
| 2496 | socket_name, | 2508 | socket_name, |
| 2497 | display_handle, | 2509 | display_handle, |
| 2510 | + is_session_instance, | ||
| 2498 | start_time: Instant::now(), | 2511 | start_time: Instant::now(), |
| 2499 | is_at_startup: true, | 2512 | is_at_startup: true, |
| 2500 | clock: animation_clock, | 2513 | clock: animation_clock, |
| @@ -2594,6 +2607,7 @@ impl Niri { | |||
| 2594 | mods_with_finger_scroll_binds, | 2607 | mods_with_finger_scroll_binds, |
| 2595 | 2608 | ||
| 2596 | lock_state: LockState::Unlocked, | 2609 | lock_state: LockState::Unlocked, |
| 2610 | + locked_hint: None, | ||
| 2597 | 2611 | ||
| 2598 | screenshot_ui, | 2612 | screenshot_ui, |
| 2599 | config_error_notification, | 2613 | config_error_notification, |
| @@ -5741,6 +5755,81 @@ impl Niri { | |||
| 5741 | self.queue_redraw_all(); | 5755 | self.queue_redraw_all(); |
| 5742 | } | 5756 | } |
| 5743 | 5757 | ||
| 5758 | + #[cfg(feature = "dbus")] | ||
| 5759 | + fn update_locked_hint(&mut self) { | ||
| 5760 | + use std::sync::LazyLock; | ||
| 5761 | + | ||
| 5762 | + if !self.is_session_instance { | ||
| 5763 | + return; | ||
| 5764 | + } | ||
| 5765 | + | ||
| 5766 | + static XDG_SESSION_ID: LazyLock<Option<String>> = LazyLock::new(|| { | ||
| 5767 | + let id = std::env::var("XDG_SESSION_ID").ok(); | ||
| 5768 | + if id.is_none() { | ||
| 5769 | + warn!( | ||
| 5770 | + "env var 'XDG_SESSION_ID' is unset or invalid; logind LockedHint won't be set" | ||
| 5771 | + ); | ||
| 5772 | + } | ||
| 5773 | + id | ||
| 5774 | + }); | ||
| 5775 | + | ||
| 5776 | + let Some(session_id) = &*XDG_SESSION_ID else { | ||
| 5777 | + return; | ||
| 5778 | + }; | ||
| 5779 | + | ||
| 5780 | + fn call(session_id: &str, locked: bool) -> anyhow::Result<()> { | ||
| 5781 | + let conn = zbus::blocking::Connection::system() | ||
| 5782 | + .context("error connecting to the system bus")?; | ||
| 5783 | + | ||
| 5784 | + let message = conn | ||
| 5785 | + .call_method( | ||
| 5786 | + Some("org.freedesktop.login1"), | ||
| 5787 | + "/org/freedesktop/login1", | ||
| 5788 | + Some("org.freedesktop.login1.Manager"), | ||
| 5789 | + "GetSession", | ||
| 5790 | + &(session_id), | ||
| 5791 | + ) | ||
| 5792 | + .context("failed to call GetSession")?; | ||
| 5793 | + | ||
| 5794 | + let message_body = message.body(); | ||
| 5795 | + let session_path: zbus::zvariant::ObjectPath = message_body | ||
| 5796 | + .deserialize() | ||
| 5797 | + .context("failed to deserialize GetSession reply")?; | ||
| 5798 | + | ||
| 5799 | + conn.call_method( | ||
| 5800 | + Some("org.freedesktop.login1"), | ||
| 5801 | + session_path, | ||
| 5802 | + Some("org.freedesktop.login1.Session"), | ||
| 5803 | + "SetLockedHint", | ||
| 5804 | + &(locked), | ||
| 5805 | + ) | ||
| 5806 | + .context("failed to call SetLockedHint")?; | ||
| 5807 | + | ||
| 5808 | + Ok(()) | ||
| 5809 | + } | ||
| 5810 | + | ||
| 5811 | + let locked = self.is_locked(); | ||
| 5812 | + if self.locked_hint.is_some_and(|h| h == locked) { | ||
| 5813 | + return; | ||
| 5814 | + } | ||
| 5815 | + | ||
| 5816 | + self.locked_hint = Some(locked); | ||
| 5817 | + | ||
| 5818 | + let res = thread::Builder::new() | ||
| 5819 | + .name("Logind LockedHint Updater".to_owned()) | ||
| 5820 | + .spawn(move || { | ||
| 5821 | + let _span = tracy_client::span!("LockedHint"); | ||
| 5822 | + | ||
| 5823 | + if let Err(err) = call(session_id, locked) { | ||
| 5824 | + warn!("failed to set logind LockedHint: {err:?}"); | ||
| 5825 | + } | ||
| 5826 | + }); | ||
| 5827 | + | ||
| 5828 | + if let Err(err) = res { | ||
| 5829 | + warn!("error spawning a thread to set logind LockedHint: {err:?}"); | ||
| 5830 | + } | ||
| 5831 | + } | ||
| 5832 | + | ||
| 5744 | pub fn new_lock_surface(&mut self, surface: LockSurface, output: &Output) { | 5833 | pub fn new_lock_surface(&mut self, surface: LockSurface, output: &Output) { |
| 5745 | let lock = match &self.lock_state { | 5834 | let lock = match &self.lock_state { |
| 5746 | LockState::Unlocked => { | 5835 | LockState::Unlocked => { |
src/tests/server.rsmodified+1 -0
| @@ -23,6 +23,7 @@ impl Server { | |||
| 23 | display, | 23 | display, |
| 24 | true, | 24 | true, |
| 25 | false, | 25 | false, |
| 26 | + false, | ||
| 26 | ) | 27 | ) |
| 27 | .unwrap(); | 28 | .unwrap(); |
| 28 | 29 | ||