Implement org.freedesktop.ScreenSaver Inhibit
Browse files at this commit5e553c2679e62bed702483ca47cf23fd8008bc24Parent: cabf7123 changed files +178 -6
- src/dbus/freedesktop_screensaver.rs +163 -0
- src/dbus/mod.rs +6 -0
- src/niri.rs +9 -6
src/dbus/freedesktop_screensaver.rsadded+163 -0
| @@ -0,0 +1,163 @@ | |||
| 1 | +use std::collections::hash_map::Entry; | ||
| 2 | +use std::collections::HashMap; | ||
| 3 | +use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 4 | +use std::sync::{Arc, Mutex, OnceLock}; | ||
| 5 | + | ||
| 6 | +use anyhow::Context; | ||
| 7 | +use futures_util::StreamExt; | ||
| 8 | +use zbus::fdo::{self, RequestNameFlags}; | ||
| 9 | +use zbus::names::OwnedUniqueName; | ||
| 10 | +use zbus::{dbus_interface, MessageHeader, Task}; | ||
| 11 | + | ||
| 12 | +use super::Start; | ||
| 13 | + | ||
| 14 | +pub struct ScreenSaver { | ||
| 15 | + is_inhibited: Arc<AtomicBool>, | ||
| 16 | + is_broken: Arc<AtomicBool>, | ||
| 17 | + inhibitors: Arc<Mutex<HashMap<u32, OwnedUniqueName>>>, | ||
| 18 | + counter: u32, | ||
| 19 | + monitor_task: Arc<OnceLock<Task<()>>>, | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +#[dbus_interface(name = "org.freedesktop.ScreenSaver")] | ||
| 23 | +impl ScreenSaver { | ||
| 24 | + async fn inhibit( | ||
| 25 | + &mut self, | ||
| 26 | + #[zbus(header)] hdr: MessageHeader<'_>, | ||
| 27 | + application_name: &str, | ||
| 28 | + reason_for_inhibit: &str, | ||
| 29 | + ) -> fdo::Result<u32> { | ||
| 30 | + trace!( | ||
| 31 | + "fdo inhibit, app: `{application_name}`, reason: `{reason_for_inhibit}`, owner: {:?}", | ||
| 32 | + hdr.sender() | ||
| 33 | + ); | ||
| 34 | + | ||
| 35 | + let Ok(Some(name)) = hdr.sender() else { | ||
| 36 | + return Err(fdo::Error::Failed(String::from("no sender"))); | ||
| 37 | + }; | ||
| 38 | + let name = OwnedUniqueName::from(name.to_owned()); | ||
| 39 | + | ||
| 40 | + let mut inhibitors = self.inhibitors.lock().unwrap(); | ||
| 41 | + | ||
| 42 | + let mut cookie = None; | ||
| 43 | + for _ in 0..3 { | ||
| 44 | + // Start from 1 because some clients don't like 0. | ||
| 45 | + self.counter = self.counter.wrapping_add(1); | ||
| 46 | + if self.counter == 0 { | ||
| 47 | + self.counter += 1; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + if let Entry::Vacant(entry) = inhibitors.entry(self.counter) { | ||
| 51 | + entry.insert(name); | ||
| 52 | + self.is_inhibited.store(true, Ordering::SeqCst); | ||
| 53 | + cookie = Some(self.counter); | ||
| 54 | + break; | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + cookie.ok_or_else(|| fdo::Error::Failed(String::from("no available cookie"))) | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + async fn un_inhibit(&mut self, cookie: u32) -> fdo::Result<()> { | ||
| 62 | + trace!("fdo uninhibit, cookie: {cookie}"); | ||
| 63 | + | ||
| 64 | + let mut inhibitors = self.inhibitors.lock().unwrap(); | ||
| 65 | + | ||
| 66 | + if inhibitors.remove(&cookie).is_some() { | ||
| 67 | + if inhibitors.is_empty() { | ||
| 68 | + self.is_inhibited.store(false, Ordering::SeqCst); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + Ok(()) | ||
| 72 | + } else { | ||
| 73 | + Err(fdo::Error::Failed(String::from("invalid cookie"))) | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +impl ScreenSaver { | ||
| 79 | + pub fn new(is_inhibited: Arc<AtomicBool>) -> Self { | ||
| 80 | + Self { | ||
| 81 | + is_inhibited, | ||
| 82 | + is_broken: Arc::new(AtomicBool::new(false)), | ||
| 83 | + inhibitors: Arc::new(Mutex::new(HashMap::new())), | ||
| 84 | + counter: 0, | ||
| 85 | + monitor_task: Arc::new(OnceLock::new()), | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +async fn monitor_disappeared_clients( | ||
| 91 | + conn: &zbus::Connection, | ||
| 92 | + is_inhibited: Arc<AtomicBool>, | ||
| 93 | + inhibitors: Arc<Mutex<HashMap<u32, OwnedUniqueName>>>, | ||
| 94 | +) -> anyhow::Result<()> { | ||
| 95 | + let proxy = fdo::DBusProxy::new(conn) | ||
| 96 | + .await | ||
| 97 | + .context("error creating a DBusProxy")?; | ||
| 98 | + | ||
| 99 | + let mut stream = proxy | ||
| 100 | + .receive_name_owner_changed() | ||
| 101 | + .await | ||
| 102 | + .context("error creating a NameOwnerChanged stream")?; | ||
| 103 | + | ||
| 104 | + while let Some(signal) = stream.next().await { | ||
| 105 | + let args = signal | ||
| 106 | + .args() | ||
| 107 | + .context("error retrieving NameOwnerChanged args")?; | ||
| 108 | + | ||
| 109 | + let Some(name) = &**args.old_owner() else { | ||
| 110 | + continue; | ||
| 111 | + }; | ||
| 112 | + | ||
| 113 | + if args.new_owner().is_none() { | ||
| 114 | + trace!("fdo ScreenSaver client disappeared: {name}"); | ||
| 115 | + | ||
| 116 | + let mut inhibitors = inhibitors.lock().unwrap(); | ||
| 117 | + inhibitors.retain(|_, owner| owner != name); | ||
| 118 | + is_inhibited.store(!inhibitors.is_empty(), Ordering::SeqCst); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + Ok(()) | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +impl Start for ScreenSaver { | ||
| 126 | + fn start(self) -> anyhow::Result<zbus::blocking::Connection> { | ||
| 127 | + let is_inhibited = self.is_inhibited.clone(); | ||
| 128 | + let is_broken = self.is_broken.clone(); | ||
| 129 | + let inhibitors = self.inhibitors.clone(); | ||
| 130 | + let monitor_task = self.monitor_task.clone(); | ||
| 131 | + | ||
| 132 | + let conn = zbus::blocking::Connection::session()?; | ||
| 133 | + let flags = RequestNameFlags::AllowReplacement | ||
| 134 | + | RequestNameFlags::ReplaceExisting | ||
| 135 | + | RequestNameFlags::DoNotQueue; | ||
| 136 | + | ||
| 137 | + conn.object_server() | ||
| 138 | + .at("/org/freedesktop/ScreenSaver", self)?; | ||
| 139 | + conn.request_name_with_flags("org.freedesktop.ScreenSaver", flags)?; | ||
| 140 | + | ||
| 141 | + let async_conn = conn.inner(); | ||
| 142 | + let future = { | ||
| 143 | + let conn = async_conn.clone(); | ||
| 144 | + async move { | ||
| 145 | + if let Err(err) = | ||
| 146 | + monitor_disappeared_clients(&conn, is_inhibited.clone(), inhibitors.clone()) | ||
| 147 | + .await | ||
| 148 | + { | ||
| 149 | + warn!("error monitoring org.freedesktop.ScreenSaver clients: {err:?}"); | ||
| 150 | + is_broken.store(true, Ordering::SeqCst); | ||
| 151 | + is_inhibited.store(false, Ordering::SeqCst); | ||
| 152 | + inhibitors.lock().unwrap().clear(); | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + }; | ||
| 156 | + let task = async_conn | ||
| 157 | + .executor() | ||
| 158 | + .spawn(future, "monitor disappearing clients"); | ||
| 159 | + monitor_task.set(task).unwrap(); | ||
| 160 | + | ||
| 161 | + Ok(conn) | ||
| 162 | + } | ||
| 163 | +} | ||
src/dbus/mod.rsmodified+6 -0
| @@ -4,6 +4,7 @@ use zbus::Interface; | |||
| 4 | 4 | ||
| 5 | use crate::niri::State; | 5 | use crate::niri::State; |
| 6 | 6 | ||
| 7 | +pub mod freedesktop_screensaver; | ||
| 7 | pub mod gnome_shell_screenshot; | 8 | pub mod gnome_shell_screenshot; |
| 8 | pub mod mutter_display_config; | 9 | pub mod mutter_display_config; |
| 9 | pub mod mutter_service_channel; | 10 | pub mod mutter_service_channel; |
| @@ -13,6 +14,7 @@ pub mod mutter_screen_cast; | |||
| 13 | #[cfg(feature = "xdp-gnome-screencast")] | 14 | #[cfg(feature = "xdp-gnome-screencast")] |
| 14 | use mutter_screen_cast::ScreenCast; | 15 | use mutter_screen_cast::ScreenCast; |
| 15 | 16 | ||
| 17 | +use self::freedesktop_screensaver::ScreenSaver; | ||
| 16 | use self::mutter_display_config::DisplayConfig; | 18 | use self::mutter_display_config::DisplayConfig; |
| 17 | use self::mutter_service_channel::ServiceChannel; | 19 | use self::mutter_service_channel::ServiceChannel; |
| 18 | 20 | ||
| @@ -24,6 +26,7 @@ trait Start: Interface { | |||
| 24 | pub struct DBusServers { | 26 | pub struct DBusServers { |
| 25 | pub conn_service_channel: Option<Connection>, | 27 | pub conn_service_channel: Option<Connection>, |
| 26 | pub conn_display_config: Option<Connection>, | 28 | pub conn_display_config: Option<Connection>, |
| 29 | + pub conn_screen_saver: Option<Connection>, | ||
| 27 | pub conn_screen_shot: Option<Connection>, | 30 | pub conn_screen_shot: Option<Connection>, |
| 28 | #[cfg(feature = "xdp-gnome-screencast")] | 31 | #[cfg(feature = "xdp-gnome-screencast")] |
| 29 | pub conn_screen_cast: Option<Connection>, | 32 | pub conn_screen_cast: Option<Connection>, |
| @@ -48,6 +51,9 @@ impl DBusServers { | |||
| 48 | let display_config = DisplayConfig::new(backend.enabled_outputs()); | 51 | let display_config = DisplayConfig::new(backend.enabled_outputs()); |
| 49 | dbus.conn_display_config = try_start(display_config); | 52 | dbus.conn_display_config = try_start(display_config); |
| 50 | 53 | ||
| 54 | + let screen_saver = ScreenSaver::new(niri.is_fdo_idle_inhibited.clone()); | ||
| 55 | + dbus.conn_screen_saver = try_start(screen_saver); | ||
| 56 | + | ||
| 51 | let (to_niri, from_screenshot) = calloop::channel::channel(); | 57 | let (to_niri, from_screenshot) = calloop::channel::channel(); |
| 52 | let (to_screenshot, from_niri) = async_channel::unbounded(); | 58 | let (to_screenshot, from_niri) = async_channel::unbounded(); |
| 53 | niri.event_loop | 59 | niri.event_loop |
src/niri.rsmodified+9 -6
| @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet}; | |||
| 3 | use std::ffi::OsString; | 3 | use std::ffi::OsString; |
| 4 | use std::path::PathBuf; | 4 | use std::path::PathBuf; |
| 5 | use std::rc::Rc; | 5 | use std::rc::Rc; |
| 6 | -use std::sync::atomic::Ordering; | 6 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use std::sync::{Arc, Mutex}; | 7 | use std::sync::{Arc, Mutex}; |
| 8 | use std::time::{Duration, Instant}; | 8 | use std::time::{Duration, Instant}; |
| 9 | use std::{env, mem, thread}; | 9 | use std::{env, mem, thread}; |
| @@ -181,6 +181,7 @@ pub struct Niri { | |||
| 181 | pub keyboard_focus: Option<WlSurface>, | 181 | pub keyboard_focus: Option<WlSurface>, |
| 182 | 182 | ||
| 183 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, | 183 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, |
| 184 | + pub is_fdo_idle_inhibited: Arc<AtomicBool>, | ||
| 184 | 185 | ||
| 185 | pub cursor_manager: CursorManager, | 186 | pub cursor_manager: CursorManager, |
| 186 | pub cursor_texture_cache: CursorTextureCache, | 187 | pub cursor_texture_cache: CursorTextureCache, |
| @@ -1029,6 +1030,7 @@ impl Niri { | |||
| 1029 | seat, | 1030 | seat, |
| 1030 | keyboard_focus: None, | 1031 | keyboard_focus: None, |
| 1031 | idle_inhibiting_surfaces: HashSet::new(), | 1032 | idle_inhibiting_surfaces: HashSet::new(), |
| 1033 | + is_fdo_idle_inhibited: Arc::new(AtomicBool::new(false)), | ||
| 1032 | cursor_manager, | 1034 | cursor_manager, |
| 1033 | cursor_texture_cache: Default::default(), | 1035 | cursor_texture_cache: Default::default(), |
| 1034 | cursor_shape_manager_state, | 1036 | cursor_shape_manager_state, |
| @@ -1880,11 +1882,12 @@ impl Niri { | |||
| 1880 | 1882 | ||
| 1881 | self.idle_inhibiting_surfaces.retain(|s| s.is_alive()); | 1883 | self.idle_inhibiting_surfaces.retain(|s| s.is_alive()); |
| 1882 | 1884 | ||
| 1883 | - let is_inhibited = self.idle_inhibiting_surfaces.iter().any(|surface| { | 1885 | + let is_inhibited = self.is_fdo_idle_inhibited.load(Ordering::SeqCst) |
| 1884 | - with_states(surface, |states| { | 1886 | + || self.idle_inhibiting_surfaces.iter().any(|surface| { |
| 1885 | - surface_primary_scanout_output(surface, states).is_some() | 1887 | + with_states(surface, |states| { |
| 1886 | - }) | 1888 | + surface_primary_scanout_output(surface, states).is_some() |
| 1887 | - }); | 1889 | + }) |
| 1890 | + }); | ||
| 1888 | self.idle_notifier_state.set_is_inhibited(is_inhibited); | 1891 | self.idle_notifier_state.set_is_inhibited(is_inhibited); |
| 1889 | } | 1892 | } |
| 1890 | 1893 | ||