| 1 | use std::os::unix::net::UnixStream; |
| 2 | |
| 3 | use zbus::{fdo, interface, zvariant}; |
| 4 | |
| 5 | use super::Start; |
| 6 | use crate::niri::NewClient; |
| 7 | |
| 8 | pub struct ServiceChannel { |
| 9 | to_niri: calloop::channel::Sender<NewClient>, |
| 10 | } |
| 11 | |
| 12 | #[interface(name = "org.gnome.Mutter.ServiceChannel")] |
| 13 | impl ServiceChannel { |
| 14 | async fn open_wayland_service_connection( |
| 15 | &mut self, |
| 16 | service_client_type: u32, |
| 17 | ) -> fdo::Result<zvariant::OwnedFd> { |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | if !matches!(service_client_type, 1..=3) { |
| 26 | return Err(fdo::Error::InvalidArgs( |
| 27 | "Invalid service client type".to_owned(), |
| 28 | )); |
| 29 | } |
| 30 | |
| 31 | let (sock1, sock2) = UnixStream::pair().unwrap(); |
| 32 | let client = NewClient { |
| 33 | client: sock2, |
| 34 | restricted: false, |
| 35 | |
| 36 | credentials_unknown: true, |
| 37 | }; |
| 38 | if let Err(err) = self.to_niri.send(client) { |
| 39 | warn!("error sending message to niri: {err:?}"); |
| 40 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 41 | } |
| 42 | |
| 43 | Ok(zvariant::OwnedFd::from(std::os::fd::OwnedFd::from(sock1))) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl ServiceChannel { |
| 48 | pub fn new(to_niri: calloop::channel::Sender<NewClient>) -> Self { |
| 49 | Self { to_niri } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Start for ServiceChannel { |
| 54 | fn start(self, _monitor: bool) -> anyhow::Result<zbus::blocking::Connection> { |
| 55 | let conn = zbus::blocking::connection::Builder::session()? |
| 56 | .name("org.gnome.Mutter.ServiceChannel")? |
| 57 | .serve_at("/org/gnome/Mutter/ServiceChannel", self)? |
| 58 | .build()?; |
| 59 | Ok(conn) |
| 60 | } |
| 61 | } |