dbus: Refactor to share DBus connections

We should avoid creating more than one session connection or more than
one system connection. We should also ideally avoid blocking the main
thread.

For now this still uses blocking in a couple places, but wrapping async
code (which is how `zbus::blocking` is implemented anyway).

This moves the `NameOwners` creation out of `A11yKeyboardMonitorState`,
so it can be shared with other things. We will likely want that for
https://github.com/pop-os/cosmic-comp/pull/465 to define a secured
protocol to pass an fd to the portal, and potentially any other
sensitive DBus protocols implemented by the compositor.
This commit is contained in:
Ian Douglas Scott 2026-03-05 15:56:06 -08:00 committed by Victoria Brekenfeld
parent c8d9ff1215
commit 4eaaf4f55c
9 changed files with 238 additions and 173 deletions

View file

@ -1668,9 +1668,9 @@ impl State {
.unwrap_or(false)
});
self.common
.a11y_keyboard_monitor_state
.key_event(modifiers, &handle, event.state());
if let Some(a11y_keyboard_monitor) = self.common.dbus_state.a11y_keyboard_monitor() {
a11y_keyboard_monitor.key_event(modifiers, &handle, event.state());
}
// Leave move overview mode, if any modifier was released
if let Some(Trigger::KeyboardMove(action_modifiers)) =
@ -1830,61 +1830,54 @@ impl State {
)));
}
if event.state() == KeyState::Released {
let removed = self
.common
.a11y_keyboard_monitor_state
.remove_active_virtual_mod(handle.modified_sym());
// If `Caps_Lock` is a virtual modifier, and is in locked state, clear it
if removed
&& handle.modified_sym() == Keysym::Caps_Lock
&& (modifiers.serialized.locked & 2) != 0
if let Some(mut a11y_keyboard_monitor) = self.common.dbus_state.a11y_keyboard_monitor() {
if event.state() == KeyState::Released {
let removed =
a11y_keyboard_monitor.remove_active_virtual_mod(handle.modified_sym());
// If `Caps_Lock` is a virtual modifier, and is in locked state, clear it
if removed
&& handle.modified_sym() == Keysym::Caps_Lock
&& (modifiers.serialized.locked & 2) != 0
{
let seat = seat.clone();
let key_code = event.key_code();
self.common.event_loop_handle.insert_idle(move |state| {
if let Some(keyboard) = seat.get_keyboard() {
let serial = SERIAL_COUNTER.next_serial();
let time = state.common.clock.now().as_millis();
keyboard.input(
state,
key_code,
KeyState::Pressed,
serial,
time,
|_, _, _| FilterResult::<()>::Forward,
);
let serial = SERIAL_COUNTER.next_serial();
keyboard.input(
state,
key_code,
KeyState::Released,
serial,
time,
|_, _, _| FilterResult::<()>::Forward,
);
}
});
}
} else if event.state() == KeyState::Pressed
&& a11y_keyboard_monitor.has_virtual_mod(handle.modified_sym())
{
let seat = seat.clone();
let key_code = event.key_code();
self.common.event_loop_handle.insert_idle(move |state| {
if let Some(keyboard) = seat.get_keyboard() {
let serial = SERIAL_COUNTER.next_serial();
let time = state.common.clock.now().as_millis();
keyboard.input(
state,
key_code,
KeyState::Pressed,
serial,
time,
|_, _, _| FilterResult::<()>::Forward,
);
let serial = SERIAL_COUNTER.next_serial();
keyboard.input(
state,
key_code,
KeyState::Released,
serial,
time,
|_, _, _| FilterResult::<()>::Forward,
);
}
});
a11y_keyboard_monitor.add_active_virtual_mod(handle.modified_sym());
tracing::debug!(
"active virtual mods: {:?}",
a11y_keyboard_monitor.active_virtual_mods()
);
seat.supressed_keys().add(&handle, None);
return FilterResult::Intercept(None);
}
} else if event.state() == KeyState::Pressed
&& self
.common
.a11y_keyboard_monitor_state
.has_virtual_mod(handle.modified_sym())
{
self.common
.a11y_keyboard_monitor_state
.add_active_virtual_mod(handle.modified_sym());
tracing::debug!(
"active virtual mods: {:?}",
self.common
.a11y_keyboard_monitor_state
.active_virtual_mods()
);
seat.supressed_keys().add(&handle, None);
return FilterResult::Intercept(None);
}
// Skip released events for initially surpressed keys
@ -1911,12 +1904,10 @@ impl State {
return FilterResult::Intercept(None);
}
if event.state() == KeyState::Pressed
&& (self.common.a11y_keyboard_monitor_state.has_keyboard_grab()
|| self
.common
.a11y_keyboard_monitor_state
.has_key_grab(modifiers, handle.modified_sym()))
if let Some(a11y_keyboard_monitor) = self.common.dbus_state.a11y_keyboard_monitor()
&& event.state() == KeyState::Pressed
&& (a11y_keyboard_monitor.has_keyboard_grab()
|| a11y_keyboard_monitor.has_key_grab(modifiers, handle.modified_sym()))
{
let modifiers_queue = seat.modifiers_shortcut_queue();
modifiers_queue.clear();