forge
Explore
Log in

Don't you dare go Hollow

Implement org/gnome/shell/Introspect/GetWindows

Browse files at this commit

40aff3a094756b2cfa9c0ce48c1ab2be26b1f9eeIvan MolodetskikhParent: 6c5f100
src/dbus/gnome_shell_introspect.rsadded+81 -0
@@ -0,0 +1,81 @@
1+use std::collections::HashMap;
2+
3+use zbus::fdo::{self, RequestNameFlags};
4+use zbus::zvariant::{SerializeDict, Type, Value};
5+use zbus::{dbus_interface, SignalContext};
6+
7+use super::Start;
8+
9+pub struct Introspect {
10+ to_niri: calloop::channel::Sender<IntrospectToNiri>,
11+ from_niri: async_channel::Receiver<NiriToIntrospect>,
12+}
13+
14+pub enum IntrospectToNiri {
15+ GetWindows,
16+}
17+
18+pub enum NiriToIntrospect {
19+ Windows(HashMap<u64, WindowProperties>),
20+}
21+
22+#[derive(Debug, SerializeDict, Type, Value)]
23+#[zvariant(signature = "dict")]
24+pub struct WindowProperties {
25+ /// Window title.
26+ pub title: String,
27+ /// Window app ID.
28+ ///
29+ /// This is actually the name of the .desktop file, and Shell does internal tracking to match
30+ /// Wayland app IDs to desktop files. We don't do that yet, which is the reason why
31+ /// xdg-desktop-portal-gnome's window list is missing icons.
32+ #[zvariant(rename = "app-id")]
33+ pub app_id: String,
34+}
35+
36+#[dbus_interface(name = "org.gnome.Shell.Introspect")]
37+impl Introspect {
38+ async fn get_windows(&self) -> fdo::Result<HashMap<u64, WindowProperties>> {
39+ if let Err(err) = self.to_niri.send(IntrospectToNiri::GetWindows) {
40+ warn!("error sending message to niri: {err:?}");
41+ return Err(fdo::Error::Failed("internal error".to_owned()));
42+ }
43+
44+ match self.from_niri.recv().await {
45+ Ok(NiriToIntrospect::Windows(windows)) => Ok(windows),
46+ Err(err) => {
47+ warn!("error receiving message from niri: {err:?}");
48+ Err(fdo::Error::Failed("internal error".to_owned()))
49+ }
50+ }
51+ }
52+
53+ // FIXME: call this upon window changes, once more of the infrastructure is there (will be
54+ // needed for the event stream IPC anyway).
55+ #[dbus_interface(signal)]
56+ pub async fn windows_changed(ctxt: &SignalContext<'_>) -> zbus::Result<()>;
57+}
58+
59+impl Introspect {
60+ pub fn new(
61+ to_niri: calloop::channel::Sender<IntrospectToNiri>,
62+ from_niri: async_channel::Receiver<NiriToIntrospect>,
63+ ) -> Self {
64+ Self { to_niri, from_niri }
65+ }
66+}
67+
68+impl Start for Introspect {
69+ fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
70+ let conn = zbus::blocking::Connection::session()?;
71+ let flags = RequestNameFlags::AllowReplacement
72+ | RequestNameFlags::ReplaceExisting
73+ | RequestNameFlags::DoNotQueue;
74+
75+ conn.object_server()
76+ .at("/org/gnome/Shell/Introspect", self)?;
77+ conn.request_name_with_flags("org.gnome.Shell.Introspect", flags)?;
78+
79+ Ok(conn)
80+ }
81+}
src/dbus/mod.rsmodified+16 -0
@@ -4,6 +4,7 @@ use zbus::Interface;
4 use crate::niri::State;4 use crate::niri::State;
5 5
6 pub mod freedesktop_screensaver;6 pub mod freedesktop_screensaver;
7+pub mod gnome_shell_introspect;
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;
@@ -14,6 +15,7 @@ pub mod mutter_screen_cast;
14 use mutter_screen_cast::ScreenCast;15 use mutter_screen_cast::ScreenCast;
15 16
16 use self::freedesktop_screensaver::ScreenSaver;17 use self::freedesktop_screensaver::ScreenSaver;
18+use self::gnome_shell_introspect::Introspect;
17 use self::mutter_display_config::DisplayConfig;19 use self::mutter_display_config::DisplayConfig;
18 use self::mutter_service_channel::ServiceChannel;20 use self::mutter_service_channel::ServiceChannel;
19 21
@@ -27,6 +29,7 @@ pub struct DBusServers {
27 pub conn_display_config: Option<Connection>,29 pub conn_display_config: Option<Connection>,
28 pub conn_screen_saver: Option<Connection>,30 pub conn_screen_saver: Option<Connection>,
29 pub conn_screen_shot: Option<Connection>,31 pub conn_screen_shot: Option<Connection>,
32+ pub conn_introspect: Option<Connection>,
30 #[cfg(feature = "xdp-gnome-screencast")]33 #[cfg(feature = "xdp-gnome-screencast")]
31 pub conn_screen_cast: Option<Connection>,34 pub conn_screen_cast: Option<Connection>,
32 }35 }
@@ -66,6 +69,19 @@ impl DBusServers {
66 let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);69 let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);
67 dbus.conn_screen_shot = try_start(screenshot);70 dbus.conn_screen_shot = try_start(screenshot);
68 71
72+ let (to_niri, from_introspect) = calloop::channel::channel();
73+ let (to_introspect, from_niri) = async_channel::unbounded();
74+ niri.event_loop
75+ .insert_source(from_introspect, move |event, _, state| match event {
76+ calloop::channel::Event::Msg(msg) => {
77+ state.on_introspect_msg(&to_introspect, msg)
78+ }
79+ calloop::channel::Event::Closed => (),
80+ })
81+ .unwrap();
82+ let introspect = Introspect::new(to_niri, from_niri);
83+ dbus.conn_introspect = try_start(introspect);
84+
69 #[cfg(feature = "xdp-gnome-screencast")]85 #[cfg(feature = "xdp-gnome-screencast")]
70 if niri.pipewire.is_some() {86 if niri.pipewire.is_some() {
71 let (to_niri, from_screen_cast) = calloop::channel::channel();87 let (to_niri, from_screen_cast) = calloop::channel::channel();
src/niri.rsmodified+46 -0
@@ -99,6 +99,8 @@ use crate::backend::tty::SurfaceDmabufFeedback;
99 use crate::backend::{Backend, RenderResult, Tty, Winit};99 use crate::backend::{Backend, RenderResult, Tty, Winit};
100 use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};100 use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};
101 #[cfg(feature = "dbus")]101 #[cfg(feature = "dbus")]
102+use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect};
103+#[cfg(feature = "dbus")]
102 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};104 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
103 #[cfg(feature = "xdp-gnome-screencast")]105 #[cfg(feature = "xdp-gnome-screencast")]
104 use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri};106 use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri};
@@ -1273,6 +1275,50 @@ impl State {
1273 }1275 }
1274 }1276 }
1275 }1277 }
1278+
1279+ #[cfg(feature = "dbus")]
1280+ pub fn on_introspect_msg(
1281+ &mut self,
1282+ to_introspect: &async_channel::Sender<NiriToIntrospect>,
1283+ msg: IntrospectToNiri,
1284+ ) {
1285+ use smithay::wayland::shell::xdg::XdgToplevelSurfaceData;
1286+
1287+ let IntrospectToNiri::GetWindows = msg;
1288+ let _span = tracy_client::span!("GetWindows");
1289+
1290+ let mut windows = HashMap::new();
1291+
1292+ self.niri.layout.with_windows(|mapped, _| {
1293+ let wl_surface = mapped
1294+ .window
1295+ .toplevel()
1296+ .expect("no X11 support")
1297+ .wl_surface();
1298+
1299+ let id = u64::from(mapped.id().get());
1300+ let props = with_states(wl_surface, |states| {
1301+ let role = states
1302+ .data_map
1303+ .get::<XdgToplevelSurfaceData>()
1304+ .unwrap()
1305+ .lock()
1306+ .unwrap();
1307+
1308+ gnome_shell_introspect::WindowProperties {
1309+ title: role.title.clone().unwrap_or_default(),
1310+ app_id: role.app_id.clone().unwrap_or_default(),
1311+ }
1312+ });
1313+
1314+ windows.insert(id, props);
1315+ });
1316+
1317+ let msg = NiriToIntrospect::Windows(windows);
1318+ if let Err(err) = to_introspect.send_blocking(msg) {
1319+ warn!("error sending windows to introspect: {err:?}");
1320+ }
1321+ }
1276 }1322 }
1277 1323
1278 impl Niri {1324 impl Niri {