niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use zbus::object_server::SignalEmitter; |
| 4 | use zbus::zvariant::{SerializeDict, Type, Value}; |
| 5 | use zbus::{fdo, interface}; |
| 6 | |
| 7 | use super::{request_name, 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 | #[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 | #[zbus(signal)] |
| 56 | pub async fn windows_changed(ctxt: &SignalEmitter<'_>) -> 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, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> { |
| 70 | let conn = zbus::blocking::Connection::session()?; |
| 71 | conn.object_server() |
| 72 | .at("/org/gnome/Shell/Introspect", self)?; |
| 73 | request_name(&conn, "org.gnome.Shell.Introspect", monitor)?; |
| 74 | |
| 75 | Ok(conn) |
| 76 | } |
| 77 | } |