forge
Explore
Log in

Don't you dare go Hollow

Extract dbus server startup

Browse files at this commit

02f37c8573afb8cce9f38e8d4a809ea58ca3eae8Ivan MolodetskikhParent: 66533ae
src/dbus/gnome_shell_screenshot.rsmodified+19 -1
@@ -1,7 +1,10 @@
1 use std::path::PathBuf;1 use std::path::PathBuf;
2 2
3 use smithay::reexports::calloop;3 use smithay::reexports::calloop;
4-use zbus::{dbus_interface, fdo};4+use zbus::dbus_interface;
5+use zbus::fdo::{self, RequestNameFlags};
6+
7+use super::Start;
5 8
6 pub struct Screenshot {9 pub struct Screenshot {
7 to_niri: calloop::channel::Sender<ScreenshotToNiri>,10 to_niri: calloop::channel::Sender<ScreenshotToNiri>,
@@ -55,3 +58,18 @@ impl Screenshot {
55 Self { to_niri, from_niri }58 Self { to_niri, from_niri }
56 }59 }
57 }60 }
61+
62+impl Start for Screenshot {
63+ fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
64+ let conn = zbus::blocking::Connection::session()?;
65+ let flags = RequestNameFlags::AllowReplacement
66+ | RequestNameFlags::ReplaceExisting
67+ | RequestNameFlags::DoNotQueue;
68+
69+ conn.object_server()
70+ .at("/org/gnome/Shell/Screenshot", self)?;
71+ conn.request_name_with_flags("org.gnome.Shell.Screenshot", flags)?;
72+
73+ Ok(conn)
74+ }
75+}
src/dbus/mod.rsmodified+91 -1
@@ -1,5 +1,95 @@
1+use smithay::reexports::calloop;
2+use zbus::blocking::Connection;
3+use zbus::Interface;
4+
5+use crate::niri::State;
6+
1 pub mod gnome_shell_screenshot;7 pub mod gnome_shell_screenshot;
2 pub mod mutter_display_config;8 pub mod mutter_display_config;
9+pub mod mutter_service_channel;
10+
3 #[cfg(feature = "xdp-gnome-screencast")]11 #[cfg(feature = "xdp-gnome-screencast")]
4 pub mod mutter_screen_cast;12 pub mod mutter_screen_cast;
5-pub mod mutter_service_channel;13+#[cfg(feature = "xdp-gnome-screencast")]
14+use mutter_screen_cast::ScreenCast;
15+
16+use self::mutter_display_config::DisplayConfig;
17+use self::mutter_service_channel::ServiceChannel;
18+
19+trait Start: Interface {
20+ fn start(self) -> anyhow::Result<zbus::blocking::Connection>;
21+}
22+
23+#[derive(Default)]
24+pub struct DBusServers {
25+ pub conn_service_channel: Option<Connection>,
26+ pub conn_display_config: Option<Connection>,
27+ pub conn_screen_shot: Option<Connection>,
28+ #[cfg(feature = "xdp-gnome-screencast")]
29+ pub conn_screen_cast: Option<Connection>,
30+}
31+
32+impl DBusServers {
33+ pub fn start(state: &mut State, is_session_instance: bool) {
34+ let _span = tracy_client::span!("DBusServers::start");
35+
36+ let backend = &state.backend;
37+ let niri = &mut state.niri;
38+ let config = niri.config.borrow();
39+
40+ let mut dbus = Self::default();
41+
42+ if is_session_instance {
43+ let service_channel = ServiceChannel::new(niri.display_handle.clone());
44+ dbus.conn_service_channel = try_start(service_channel);
45+ }
46+
47+ if is_session_instance || config.debug.dbus_interfaces_in_non_session_instances {
48+ let display_config = DisplayConfig::new(backend.connectors());
49+ dbus.conn_display_config = try_start(display_config);
50+
51+ let (to_niri, from_screenshot) = calloop::channel::channel();
52+ let (to_screenshot, from_niri) = async_channel::unbounded();
53+ niri.event_loop
54+ .insert_source(from_screenshot, move |event, _, state| match event {
55+ calloop::channel::Event::Msg(msg) => {
56+ state.on_screen_shot_msg(&to_screenshot, msg)
57+ }
58+ calloop::channel::Event::Closed => (),
59+ })
60+ .unwrap();
61+ let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);
62+ dbus.conn_screen_shot = try_start(screenshot);
63+
64+ #[cfg(feature = "xdp-gnome-screencast")]
65+ {
66+ let (to_niri, from_screen_cast) = calloop::channel::channel();
67+ niri.event_loop
68+ .insert_source(from_screen_cast, {
69+ let to_niri = to_niri.clone();
70+ move |event, _, state| match event {
71+ calloop::channel::Event::Msg(msg) => {
72+ state.on_screen_cast_msg(&to_niri, msg)
73+ }
74+ calloop::channel::Event::Closed => (),
75+ }
76+ })
77+ .unwrap();
78+ let screen_cast = ScreenCast::new(backend.connectors(), to_niri);
79+ dbus.conn_screen_cast = try_start(screen_cast);
80+ }
81+ }
82+
83+ niri.dbus = Some(dbus);
84+ }
85+}
86+
87+fn try_start<I: Start>(iface: I) -> Option<Connection> {
88+ match iface.start() {
89+ Ok(conn) => Some(conn),
90+ Err(err) => {
91+ warn!("error starting {}: {err:?}", I::name());
92+ None
93+ }
94+ }
95+}
src/dbus/mutter_display_config.rsmodified+18 -0
@@ -3,9 +3,12 @@ use std::sync::{Arc, Mutex};
3 3
4 use serde::Serialize;4 use serde::Serialize;
5 use smithay::output::Output;5 use smithay::output::Output;
6+use zbus::fdo::RequestNameFlags;
6 use zbus::zvariant::{OwnedValue, Type};7 use zbus::zvariant::{OwnedValue, Type};
7 use zbus::{dbus_interface, fdo};8 use zbus::{dbus_interface, fdo};
8 9
10+use super::Start;
11+
9 pub struct DisplayConfig {12 pub struct DisplayConfig {
10 connectors: Arc<Mutex<HashMap<String, Output>>>,13 connectors: Arc<Mutex<HashMap<String, Output>>>,
11 }14 }
@@ -86,3 +89,18 @@ impl DisplayConfig {
86 Self { connectors }89 Self { connectors }
87 }90 }
88 }91 }
92+
93+impl Start for DisplayConfig {
94+ fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
95+ let conn = zbus::blocking::Connection::session()?;
96+ let flags = RequestNameFlags::AllowReplacement
97+ | RequestNameFlags::ReplaceExisting
98+ | RequestNameFlags::DoNotQueue;
99+
100+ conn.object_server()
101+ .at("/org/gnome/Mutter/DisplayConfig", self)?;
102+ conn.request_name_with_flags("org.gnome.Mutter.DisplayConfig", flags)?;
103+
104+ Ok(conn)
105+ }
106+}
src/dbus/mutter_screen_cast.rsmodified+18 -0
@@ -6,9 +6,12 @@ use std::sync::{Arc, Mutex};
6 use serde::Deserialize;6 use serde::Deserialize;
7 use smithay::output::Output;7 use smithay::output::Output;
8 use smithay::reexports::calloop;8 use smithay::reexports::calloop;
9+use zbus::fdo::RequestNameFlags;
9 use zbus::zvariant::{DeserializeDict, OwnedObjectPath, Type, Value};10 use zbus::zvariant::{DeserializeDict, OwnedObjectPath, Type, Value};
10 use zbus::{dbus_interface, fdo, InterfaceRef, ObjectServer, SignalContext};11 use zbus::{dbus_interface, fdo, InterfaceRef, ObjectServer, SignalContext};
11 12
13+use super::Start;
14+
12 #[derive(Clone)]15 #[derive(Clone)]
13 pub struct ScreenCast {16 pub struct ScreenCast {
14 connectors: Arc<Mutex<HashMap<String, Output>>>,17 connectors: Arc<Mutex<HashMap<String, Output>>>,
@@ -200,6 +203,21 @@ impl ScreenCast {
200 }203 }
201 }204 }
202 205
206+impl Start for ScreenCast {
207+ fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
208+ let conn = zbus::blocking::Connection::session()?;
209+ let flags = RequestNameFlags::AllowReplacement
210+ | RequestNameFlags::ReplaceExisting
211+ | RequestNameFlags::DoNotQueue;
212+
213+ conn.object_server()
214+ .at("/org/gnome/Mutter/ScreenCast", self)?;
215+ conn.request_name_with_flags("org.gnome.Mutter.ScreenCast", flags)?;
216+
217+ Ok(conn)
218+ }
219+}
220+
203 impl Session {221 impl Session {
204 pub fn new(222 pub fn new(
205 id: usize,223 id: usize,
src/dbus/mutter_service_channel.rsmodified+11 -0
@@ -5,6 +5,7 @@ use std::sync::Arc;
5 use smithay::reexports::wayland_server::DisplayHandle;5 use smithay::reexports::wayland_server::DisplayHandle;
6 use zbus::dbus_interface;6 use zbus::dbus_interface;
7 7
8+use super::Start;
8 use crate::niri::ClientState;9 use crate::niri::ClientState;
9 10
10 pub struct ServiceChannel {11 pub struct ServiceChannel {
@@ -36,3 +37,13 @@ impl ServiceChannel {
36 Self { display }37 Self { display }
37 }38 }
38 }39 }
40+
41+impl Start for ServiceChannel {
42+ fn start(self) -> anyhow::Result<zbus::blocking::Connection> {
43+ let conn = zbus::blocking::ConnectionBuilder::session()?
44+ .name("org.gnome.Mutter.ServiceChannel")?
45+ .serve_at("/org/gnome/Mutter/ServiceChannel", self)?
46+ .build()?;
47+ Ok(conn)
48+ }
49+}
src/main.rsmodified+3 -1
@@ -37,6 +37,8 @@ use tracing_subscriber::EnvFilter;
37 use utils::spawn;37 use utils::spawn;
38 use watcher::Watcher;38 use watcher::Watcher;
39 39
40+use crate::dbus::DBusServers;
41+
40 #[derive(Parser)]42 #[derive(Parser)]
41 #[command(author, version, about, long_about = None)]43 #[command(author, version, about, long_about = None)]
42 struct Cli {44 struct Cli {
@@ -107,7 +109,7 @@ fn main() {
107 }109 }
108 }110 }
109 111
110- state.niri.start_dbus(&state.backend, is_systemd_service);112+ DBusServers::start(&mut state, is_systemd_service);
111 113
112 // Notify systemd we're ready.114 // Notify systemd we're ready.
113 if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) {115 if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) {
src/niri.rsmodified+11 -126
@@ -36,7 +36,7 @@ use smithay::output::Output;
36 use smithay::reexports::calloop::generic::Generic;36 use smithay::reexports::calloop::generic::Generic;
37 use smithay::reexports::calloop::timer::{TimeoutAction, Timer};37 use smithay::reexports::calloop::timer::{TimeoutAction, Timer};
38 use smithay::reexports::calloop::{38 use smithay::reexports::calloop::{
39- self, Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken,39+ Idle, Interest, LoopHandle, LoopSignal, Mode, PostAction, RegistrationToken,
40 };40 };
41 use smithay::reexports::nix::libc::CLOCK_MONOTONIC;41 use smithay::reexports::nix::libc::CLOCK_MONOTONIC;
42 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities;42 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::WmCapabilities;
@@ -70,16 +70,14 @@ use smithay::wayland::socket::ListeningSocketSource;
70 use smithay::wayland::tablet_manager::TabletManagerState;70 use smithay::wayland::tablet_manager::TabletManagerState;
71 use smithay::wayland::text_input::TextInputManagerState;71 use smithay::wayland::text_input::TextInputManagerState;
72 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;72 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;
73-use zbus::fdo::RequestNameFlags;
74 73
75 use crate::backend::{Backend, Tty, Winit};74 use crate::backend::{Backend, Tty, Winit};
76 use crate::config::Config;75 use crate::config::Config;
77 use crate::cursor::Cursor;76 use crate::cursor::Cursor;
78-use crate::dbus::gnome_shell_screenshot::{self, NiriToScreenshot, ScreenshotToNiri};77+use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
79-use crate::dbus::mutter_display_config::DisplayConfig;
80 #[cfg(feature = "xdp-gnome-screencast")]78 #[cfg(feature = "xdp-gnome-screencast")]
81-use crate::dbus::mutter_screen_cast::{self, ScreenCast, ScreenCastToNiri};79+use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri};
82-use crate::dbus::mutter_service_channel::ServiceChannel;80+use crate::dbus::DBusServers;
83 use crate::frame_clock::FrameClock;81 use crate::frame_clock::FrameClock;
84 use crate::layout::{output_size, Layout, MonitorRenderElement};82 use crate::layout::{output_size, Layout, MonitorRenderElement};
85 use crate::pw_utils::{Cast, PipeWire};83 use crate::pw_utils::{Cast, PipeWire};
@@ -136,7 +134,7 @@ pub struct Niri {
136 pub cursor_image: CursorImageStatus,134 pub cursor_image: CursorImageStatus,
137 pub dnd_icon: Option<WlSurface>,135 pub dnd_icon: Option<WlSurface>,
138 136
139- pub zbus_conn: Option<zbus::blocking::Connection>,137+ pub dbus: Option<DBusServers>,
140 pub inhibit_power_key_fd: Option<zbus::zvariant::OwnedFd>,138 pub inhibit_power_key_fd: Option<zbus::zvariant::OwnedFd>,
141 139
142 // Casts are dropped before PipeWire to prevent a double-free (yay).140 // Casts are dropped before PipeWire to prevent a double-free (yay).
@@ -308,9 +306,9 @@ impl State {
308 }306 }
309 307
310 #[cfg(feature = "xdp-gnome-screencast")]308 #[cfg(feature = "xdp-gnome-screencast")]
311- fn on_screen_cast_msg(309+ pub fn on_screen_cast_msg(
312 &mut self,310 &mut self,
313- to_niri: &calloop::channel::Sender<ScreenCastToNiri>,311+ to_niri: &smithay::reexports::calloop::channel::Sender<ScreenCastToNiri>,
314 msg: ScreenCastToNiri,312 msg: ScreenCastToNiri,
315 ) {313 ) {
316 match msg {314 match msg {
@@ -354,7 +352,7 @@ impl State {
354 }352 }
355 }353 }
356 354
357- fn on_screen_shot_msg(355+ pub fn on_screen_shot_msg(
358 &mut self,356 &mut self,
359 to_screenshot: &async_channel::Sender<NiriToScreenshot>,357 to_screenshot: &async_channel::Sender<NiriToScreenshot>,
360 msg: ScreenshotToNiri,358 msg: ScreenshotToNiri,
@@ -535,127 +533,13 @@ impl Niri {
535 cursor_image: CursorImageStatus::default_named(),533 cursor_image: CursorImageStatus::default_named(),
536 dnd_icon: None,534 dnd_icon: None,
537 535
538- zbus_conn: None,536+ dbus: None,
539 inhibit_power_key_fd: None,537 inhibit_power_key_fd: None,
540 pipewire,538 pipewire,
541 casts: vec![],539 casts: vec![],
542 }540 }
543 }541 }
544 542
545- pub fn start_dbus(&mut self, backend: &Backend, is_session_instance: bool) {
546- let config = self.config.borrow();
547-
548- #[cfg(feature = "xdp-gnome-screencast")]
549- let (to_niri, from_screen_cast) = calloop::channel::channel();
550- #[cfg(feature = "xdp-gnome-screencast")]
551- self.event_loop
552- .insert_source(from_screen_cast, {
553- let to_niri = to_niri.clone();
554- move |event, _, state| match event {
555- calloop::channel::Event::Msg(msg) => state.on_screen_cast_msg(&to_niri, msg),
556- calloop::channel::Event::Closed => (),
557- }
558- })
559- .unwrap();
560- #[cfg(feature = "xdp-gnome-screencast")]
561- let screen_cast = ScreenCast::new(backend.connectors(), to_niri);
562-
563- let (to_niri, from_screenshot) = calloop::channel::channel();
564- let (to_screenshot, from_niri) = async_channel::unbounded();
565- self.event_loop
566- .insert_source(from_screenshot, move |event, _, state| match event {
567- calloop::channel::Event::Msg(msg) => state.on_screen_shot_msg(&to_screenshot, msg),
568- calloop::channel::Event::Closed => (),
569- })
570- .unwrap();
571- let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);
572-
573- let mut zbus_conn = None;
574- if is_session_instance {
575- let conn = zbus::blocking::ConnectionBuilder::session()
576- .unwrap()
577- .name("org.gnome.Mutter.ServiceChannel")
578- .unwrap()
579- .serve_at(
580- "/org/gnome/Mutter/ServiceChannel",
581- ServiceChannel::new(self.display_handle.clone()),
582- )
583- .unwrap()
584- .build()
585- .unwrap();
586-
587- {
588- let server = conn.object_server();
589- let flags = RequestNameFlags::AllowReplacement
590- | RequestNameFlags::ReplaceExisting
591- | RequestNameFlags::DoNotQueue;
592-
593- server
594- .at("/org/gnome/Shell/Screenshot", screenshot)
595- .unwrap();
596- conn.request_name_with_flags("org.gnome.Shell.Screenshot", flags)
597- .unwrap();
598-
599- server
600- .at(
601- "/org/gnome/Mutter/DisplayConfig",
602- DisplayConfig::new(backend.connectors()),
603- )
604- .unwrap();
605- conn.request_name_with_flags("org.gnome.Mutter.DisplayConfig", flags)
606- .unwrap();
607-
608- #[cfg(feature = "xdp-gnome-screencast")]
609- if self.pipewire.is_some() {
610- server
611- .at("/org/gnome/Mutter/ScreenCast", screen_cast.clone())
612- .unwrap();
613- conn.request_name_with_flags("org.gnome.Mutter.ScreenCast", flags)
614- .unwrap();
615- }
616- }
617-
618- zbus_conn = Some(conn);
619- } else if config.debug.dbus_interfaces_in_non_session_instances {
620- let conn = zbus::blocking::Connection::session().unwrap();
621- let flags = RequestNameFlags::AllowReplacement
622- | RequestNameFlags::ReplaceExisting
623- | RequestNameFlags::DoNotQueue;
624-
625- {
626- let server = conn.object_server();
627-
628- server
629- .at("/org/gnome/Shell/Screenshot", screenshot)
630- .unwrap();
631- conn.request_name_with_flags("org.gnome.Shell.Screenshot", flags)
632- .unwrap();
633-
634- server
635- .at(
636- "/org/gnome/Mutter/DisplayConfig",
637- DisplayConfig::new(backend.connectors()),
638- )
639- .unwrap();
640- conn.request_name_with_flags("org.gnome.Mutter.DisplayConfig", flags)
641- .unwrap();
642-
643- #[cfg(feature = "xdp-gnome-screencast")]
644- if self.pipewire.is_some() {
645- server
646- .at("/org/gnome/Mutter/ScreenCast", screen_cast.clone())
647- .unwrap();
648- conn.request_name_with_flags("org.gnome.Mutter.ScreenCast", flags)
649- .unwrap();
650- }
651- }
652-
653- zbus_conn = Some(conn);
654- }
655-
656- self.zbus_conn = zbus_conn;
657- }
658-
659 pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> {543 pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> {
660 let conn = zbus::blocking::ConnectionBuilder::system()?.build()?;544 let conn = zbus::blocking::ConnectionBuilder::system()?.build()?;
661 545
@@ -1580,7 +1464,8 @@ impl Niri {
1580 }1464 }
1581 }1465 }
1582 1466
1583- let server = self.zbus_conn.as_ref().unwrap().object_server();1467+ let dbus = &self.dbus.as_ref().unwrap();
1468+ let server = dbus.conn_screen_cast.as_ref().unwrap().object_server();
1584 let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id);1469 let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id);
1585 if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) {1470 if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) {
1586 let _span = tracy_client::span!("invoking Session::stop");1471 let _span = tracy_client::span!("invoking Session::stop");