Extract Niri::insert_client()
Browse files at this commitec43493522b82907943b4f5371d2c24d238f2c3cParent: baa05184 changed files +56 -35
- src/dbus/mod.rs +10 -1
- src/dbus/mutter_service_channel.rs +12 -13
- src/handlers/mod.rs +4 -12
- src/niri.rs +30 -9
src/dbus/mod.rsmodified+10 -1
| @@ -45,7 +45,16 @@ impl DBusServers { | |||
| 45 | let mut dbus = Self::default(); | 45 | let mut dbus = Self::default(); |
| 46 | 46 | ||
| 47 | if is_session_instance { | 47 | if is_session_instance { |
| 48 | - let service_channel = ServiceChannel::new(niri.display_handle.clone()); | 48 | + let (to_niri, from_service_channel) = calloop::channel::channel(); |
| 49 | + let service_channel = ServiceChannel::new(to_niri); | ||
| 50 | + niri.event_loop | ||
| 51 | + .insert_source(from_service_channel, move |event, _, state| match event { | ||
| 52 | + calloop::channel::Event::Msg(new_client) => { | ||
| 53 | + state.niri.insert_client(new_client); | ||
| 54 | + } | ||
| 55 | + calloop::channel::Event::Closed => (), | ||
| 56 | + }) | ||
| 57 | + .unwrap(); | ||
| 49 | dbus.conn_service_channel = try_start(service_channel); | 58 | dbus.conn_service_channel = try_start(service_channel); |
| 50 | } | 59 | } |
| 51 | 60 | ||
src/dbus/mutter_service_channel.rsmodified+12 -13
| @@ -1,14 +1,12 @@ | |||
| 1 | use std::os::unix::net::UnixStream; | 1 | use std::os::unix::net::UnixStream; |
| 2 | -use std::sync::Arc; | ||
| 3 | 2 | ||
| 4 | -use smithay::reexports::wayland_server::DisplayHandle; | ||
| 5 | use zbus::{fdo, interface, zvariant}; | 3 | use zbus::{fdo, interface, zvariant}; |
| 6 | 4 | ||
| 7 | use super::Start; | 5 | use super::Start; |
| 8 | -use crate::niri::ClientState; | 6 | +use crate::niri::NewClient; |
| 9 | 7 | ||
| 10 | pub struct ServiceChannel { | 8 | pub struct ServiceChannel { |
| 11 | - display: DisplayHandle, | 9 | + to_niri: calloop::channel::Sender<NewClient>, |
| 12 | } | 10 | } |
| 13 | 11 | ||
| 14 | #[interface(name = "org.gnome.Mutter.ServiceChannel")] | 12 | #[interface(name = "org.gnome.Mutter.ServiceChannel")] |
| @@ -24,23 +22,24 @@ impl ServiceChannel { | |||
| 24 | } | 22 | } |
| 25 | 23 | ||
| 26 | let (sock1, sock2) = UnixStream::pair().unwrap(); | 24 | let (sock1, sock2) = UnixStream::pair().unwrap(); |
| 27 | - let data = Arc::new(ClientState { | 25 | + let client = NewClient { |
| 28 | - compositor_state: Default::default(), | 26 | + client: sock2, |
| 29 | - // Would be nice to thread config here but for now it's fine. | ||
| 30 | - can_view_decoration_globals: false, | ||
| 31 | - primary_selection_disabled: false, | ||
| 32 | restricted: false, | 27 | restricted: false, |
| 33 | // FIXME: maybe you can get the PID from D-Bus somehow? | 28 | // FIXME: maybe you can get the PID from D-Bus somehow? |
| 34 | credentials_unknown: true, | 29 | credentials_unknown: true, |
| 35 | - }); | 30 | + }; |
| 36 | - self.display.insert_client(sock2, data).unwrap(); | 31 | + if let Err(err) = self.to_niri.send(client) { |
| 32 | + warn!("error sending message to niri: {err:?}"); | ||
| 33 | + return Err(fdo::Error::Failed("internal error".to_owned())); | ||
| 34 | + } | ||
| 35 | + | ||
| 37 | Ok(zvariant::OwnedFd::from(std::os::fd::OwnedFd::from(sock1))) | 36 | Ok(zvariant::OwnedFd::from(std::os::fd::OwnedFd::from(sock1))) |
| 38 | } | 37 | } |
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | impl ServiceChannel { | 40 | impl ServiceChannel { |
| 42 | - pub fn new(display: DisplayHandle) -> Self { | 41 | + pub fn new(to_niri: calloop::channel::Sender<NewClient>) -> Self { |
| 43 | - Self { display } | 42 | + Self { to_niri } |
| 44 | } | 43 | } |
| 45 | } | 44 | } |
| 46 | 45 | ||
src/handlers/mod.rsmodified+4 -12
| @@ -71,7 +71,7 @@ use smithay::{ | |||
| 71 | }; | 71 | }; |
| 72 | 72 | ||
| 73 | pub use crate::handlers::xdg_shell::KdeDecorationsModeState; | 73 | pub use crate::handlers::xdg_shell::KdeDecorationsModeState; |
| 74 | -use crate::niri::{ClientState, DndIcon, State}; | 74 | +use crate::niri::{DndIcon, NewClient, State}; |
| 75 | use crate::protocols::foreign_toplevel::{ | 75 | use crate::protocols::foreign_toplevel::{ |
| 76 | self, ForeignToplevelHandler, ForeignToplevelManagerState, | 76 | self, ForeignToplevelHandler, ForeignToplevelManagerState, |
| 77 | }; | 77 | }; |
| @@ -471,20 +471,12 @@ impl SecurityContextHandler for State { | |||
| 471 | self.niri | 471 | self.niri |
| 472 | .event_loop | 472 | .event_loop |
| 473 | .insert_source(source, move |client, _, state| { | 473 | .insert_source(source, move |client, _, state| { |
| 474 | - let config = state.niri.config.borrow(); | 474 | + trace!("inserting a new restricted client, context={context:?}"); |
| 475 | - let data = Arc::new(ClientState { | 475 | + state.niri.insert_client(NewClient { |
| 476 | - compositor_state: Default::default(), | 476 | + client, |
| 477 | - can_view_decoration_globals: config.prefer_no_csd, | ||
| 478 | - primary_selection_disabled: config.clipboard.disable_primary, | ||
| 479 | restricted: true, | 477 | restricted: true, |
| 480 | credentials_unknown: false, | 478 | credentials_unknown: false, |
| 481 | }); | 479 | }); |
| 482 | - | ||
| 483 | - if let Err(err) = state.niri.display_handle.insert_client(client, data) { | ||
| 484 | - warn!("error inserting client: {err}"); | ||
| 485 | - } else { | ||
| 486 | - trace!("inserted a new restricted client, context={context:?}"); | ||
| 487 | - } | ||
| 488 | }) | 480 | }) |
| 489 | .unwrap(); | 481 | .unwrap(); |
| 490 | } | 482 | } |
src/niri.rsmodified+30 -9
| @@ -1,6 +1,7 @@ | |||
| 1 | use std::cell::{Cell, OnceCell, RefCell}; | 1 | use std::cell::{Cell, OnceCell, RefCell}; |
| 2 | use std::collections::{HashMap, HashSet}; | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::ffi::OsString; | 3 | use std::ffi::OsString; |
| 4 | +use std::os::unix::net::UnixStream; | ||
| 4 | use std::path::PathBuf; | 5 | use std::path::PathBuf; |
| 5 | use std::rc::Rc; | 6 | use std::rc::Rc; |
| 6 | use std::sync::atomic::{AtomicBool, Ordering}; | 7 | use std::sync::atomic::{AtomicBool, Ordering}; |
| @@ -1932,18 +1933,11 @@ impl Niri { | |||
| 1932 | let socket_name = socket_source.socket_name().to_os_string(); | 1933 | let socket_name = socket_source.socket_name().to_os_string(); |
| 1933 | event_loop | 1934 | event_loop |
| 1934 | .insert_source(socket_source, move |client, _, state| { | 1935 | .insert_source(socket_source, move |client, _, state| { |
| 1935 | - let config = state.niri.config.borrow(); | 1936 | + state.niri.insert_client(NewClient { |
| 1936 | - let data = Arc::new(ClientState { | 1937 | + client, |
| 1937 | - compositor_state: Default::default(), | ||
| 1938 | - can_view_decoration_globals: config.prefer_no_csd, | ||
| 1939 | - primary_selection_disabled: config.clipboard.disable_primary, | ||
| 1940 | restricted: false, | 1938 | restricted: false, |
| 1941 | credentials_unknown: false, | 1939 | credentials_unknown: false, |
| 1942 | }); | 1940 | }); |
| 1943 | - | ||
| 1944 | - if let Err(err) = state.niri.display_handle.insert_client(client, data) { | ||
| 1945 | - warn!("error inserting client: {err}"); | ||
| 1946 | - } | ||
| 1947 | }) | 1941 | }) |
| 1948 | .unwrap(); | 1942 | .unwrap(); |
| 1949 | 1943 | ||
| @@ -2125,6 +2119,27 @@ impl Niri { | |||
| 2125 | niri | 2119 | niri |
| 2126 | } | 2120 | } |
| 2127 | 2121 | ||
| 2122 | + pub fn insert_client(&mut self, client: NewClient) { | ||
| 2123 | + let NewClient { | ||
| 2124 | + client, | ||
| 2125 | + restricted, | ||
| 2126 | + credentials_unknown, | ||
| 2127 | + } = client; | ||
| 2128 | + | ||
| 2129 | + let config = self.config.borrow(); | ||
| 2130 | + let data = Arc::new(ClientState { | ||
| 2131 | + compositor_state: Default::default(), | ||
| 2132 | + can_view_decoration_globals: config.prefer_no_csd, | ||
| 2133 | + primary_selection_disabled: config.clipboard.disable_primary, | ||
| 2134 | + restricted, | ||
| 2135 | + credentials_unknown, | ||
| 2136 | + }); | ||
| 2137 | + | ||
| 2138 | + if let Err(err) = self.display_handle.insert_client(client, data) { | ||
| 2139 | + warn!("error inserting client: {err}"); | ||
| 2140 | + } | ||
| 2141 | + } | ||
| 2142 | + | ||
| 2128 | #[cfg(feature = "dbus")] | 2143 | #[cfg(feature = "dbus")] |
| 2129 | pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> { | 2144 | pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> { |
| 2130 | use std::os::fd::{AsRawFd, BorrowedFd}; | 2145 | use std::os::fd::{AsRawFd, BorrowedFd}; |
| @@ -5171,6 +5186,12 @@ impl Niri { | |||
| 5171 | } | 5186 | } |
| 5172 | } | 5187 | } |
| 5173 | 5188 | ||
| 5189 | +pub struct NewClient { | ||
| 5190 | + pub client: UnixStream, | ||
| 5191 | + pub restricted: bool, | ||
| 5192 | + pub credentials_unknown: bool, | ||
| 5193 | +} | ||
| 5194 | + | ||
| 5174 | pub struct ClientState { | 5195 | pub struct ClientState { |
| 5175 | pub compositor_state: CompositorClientState, | 5196 | pub compositor_state: CompositorClientState, |
| 5176 | pub can_view_decoration_globals: bool, | 5197 | pub can_view_decoration_globals: bool, |