| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use zbus::fdo::{self, RequestNameFlags}; |
| 4 | use zbus::interface; |
| 5 | use zbus::object_server::SignalEmitter; |
| 6 | use zbus::zvariant::{SerializeDict, Type, Value}; |
| 7 | |
| 8 | use super::Start; |
| 9 | |
| 10 | pub struct Introspect { |
| 11 | to_niri: calloop::channel::Sender<IntrospectToNiri>, |
| 12 | from_niri: async_channel::Receiver<NiriToIntrospect>, |
| 13 | } |
| 14 | |
| 15 | pub enum IntrospectToNiri { |
| 16 | GetWindows, |
| 17 | } |
| 18 | |
| 19 | pub enum NiriToIntrospect { |
| 20 | Windows(HashMap<u64, WindowProperties>), |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, SerializeDict, Type, Value)] |
| 24 | #[zvariant(signature = "dict")] |
| 25 | pub struct WindowProperties { |
| 26 | |
| 27 | pub title: String, |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | #[zvariant(rename = "app-id")] |
| 34 | pub app_id: String, |
| 35 | } |
| 36 | |
| 37 | #[interface(name = "org.gnome.Shell.Introspect")] |
| 38 | impl 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 | |
| 55 | |
| 56 | #[zbus(signal)] |
| 57 | pub async fn windows_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>; |
| 58 | } |
| 59 | |
| 60 | impl 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 | |
| 69 | impl 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 | } |