forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

gnome_shell_introspect.rs82 lines, 2.5 KiBHistoryRawDownload
1use std::collections::HashMap;
2
3use zbus::fdo::{self, RequestNameFlags};
4use zbus::interface;
5use zbus::object_server::SignalEmitter;
6use zbus::zvariant::{SerializeDict, Type, Value};
7
8use super::Start;
9
10pub struct Introspect {
11 to_niri: calloop::channel::Sender<IntrospectToNiri>,
12 from_niri: async_channel::Receiver<NiriToIntrospect>,
13}
14
15pub enum IntrospectToNiri {
16 GetWindows,
17}
18
19pub enum NiriToIntrospect {
20 Windows(HashMap<u64, WindowProperties>),
21}
22
23#[derive(Debug, SerializeDict, Type, Value)]
24#[zvariant(signature = "dict")]
25pub struct WindowProperties {
26 /// Window title.
27 pub title: String,
28 /// Window app ID.
29 ///
30 /// This is actually the name of the .desktop file, and Shell does internal tracking to match
31 /// Wayland app IDs to desktop files. We don't do that yet, which is the reason why
32 /// xdg-desktop-portal-gnome's window list is missing icons.
33 #[zvariant(rename = "app-id")]
34 pub app_id: String,
35}
36
37#[interface(name = "org.gnome.Shell.Introspect")]
38impl Introspect {
39 async fn get_windows(&self) -> fdo::Result<HashMap<u64, WindowProperties>> {
40 if let Err(err) = self.to_niri.send(IntrospectToNiri::GetWindows) {
41 warn!("error sending message to niri: {err:?}");
42 return Err(fdo::Error::Failed("internal error".to_owned()));
43 }
44
45 match self.from_niri.recv().await {
46 Ok(NiriToIntrospect::Windows(windows)) => Ok(windows),
47 Err(err) => {
48 warn!("error receiving message from niri: {err:?}");
49 Err(fdo::Error::Failed("internal error".to_owned()))
50 }
51 }
52 }
53
54 // FIXME: call this upon window changes, once more of the infrastructure is there (will be
55 // needed for the event stream IPC anyway).
56 #[zbus(signal)]
57 pub async fn windows_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>;
58}
59
60impl Introspect {
61 pub fn new(
62 to_niri: calloop::channel::Sender<IntrospectToNiri>,
63 from_niri: async_channel::Receiver<NiriToIntrospect>,
64 ) -> Self {
65 Self { to_niri, from_niri }
66 }
67}
68
69impl Start for Introspect {
70 fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
71 let conn = zbus::blocking::Connection::session()?;
72 let flags = RequestNameFlags::AllowReplacement
73 | RequestNameFlags::ReplaceExisting
74 | RequestNameFlags::DoNotQueue;
75
76 conn.object_server()
77 .at("/org/gnome/Shell/Introspect", self)?;
78 conn.request_name_with_flags("org.gnome.Shell.Introspect", flags)?;
79
80 Ok(conn)
81 }
82}