Refactor CompositorInterface to expose fewer multibg implementation details

This commit is contained in:
Florian Finkernagel 2025-04-08 08:47:56 +02:00 committed by Gergő Sályi
parent 1fdbd7ffb0
commit 4b02797e16
3 changed files with 60 additions and 97 deletions

View file

@ -1,15 +1,13 @@
mod niri;
mod sway;
use std::{
env,
os::unix::ffi::OsStrExt,
};
use std::{env, os::unix::ffi::OsStrExt};
use log::{debug, warn};
use mio::Waker;
use std::{
sync::{mpsc::Sender, Arc}, thread::spawn
sync::{mpsc::Sender, Arc},
thread::spawn,
};
#[derive(Clone, Copy, Debug, clap::ValueEnum)]
@ -34,8 +32,10 @@ impl Compositor {
debug!("Selecting compositor Niri based on {xdg_desktop_var}");
Some(Compositor::Niri)
} else {
warn!("Unrecognized compositor from {xdg_desktop_var} \
environment variable: {xdg_desktop:?}");
warn!(
"Unrecognized compositor from {xdg_desktop_var} \
environment variable: {xdg_desktop:?}"
);
None
}
} else {
@ -67,13 +67,7 @@ impl Compositor {
// }
pub trait CompositorInterface: Send + Sync {
fn request_visible_workspace(
&mut self,
output: &str,
tx: Sender<WorkspaceVisible>,
waker: Arc<Waker>,
);
fn request_visible_workspaces(&mut self, tx: Sender<WorkspaceVisible>, waker: Arc<Waker>);
fn request_visible_workspaces(&mut self) -> Vec<WorkspaceVisible>;
fn subscribe_event_loop(self, tx: Sender<WorkspaceVisible>, waker: Arc<Waker>);
}
@ -115,13 +109,37 @@ impl ConnectionTask {
}
pub fn request_visible_workspace(&mut self, output: &str) {
self.interface
.request_visible_workspace(output, self.tx.clone(), self.waker.clone());
if let Some(workspace) = self
.interface
.request_visible_workspaces()
.into_iter()
.find(|w| w.output == output)
{
self.tx
.send(WorkspaceVisible {
output: workspace.output,
workspace_name: workspace.workspace_name,
})
.unwrap();
self.waker.wake().unwrap();
}
}
pub fn request_visible_workspaces(&mut self) {
self.interface
.request_visible_workspaces(self.tx.clone(), self.waker.clone());
for workspace in self
.interface
.request_visible_workspaces()
.into_iter()
{
self.tx.send(WorkspaceVisible {
output: workspace.output,
workspace_name: workspace.workspace_name,
})
.unwrap();
self.waker.wake().unwrap();
}
}
}

View file

@ -29,46 +29,24 @@ impl NiriConnectionTask {
}
}
impl CompositorInterface for NiriConnectionTask {
fn request_visible_workspace(
&mut self,
output: &str,
tx: Sender<WorkspaceVisible>,
waker: Arc<Waker>,
) {
fn request_visible_workspaces(&mut self) -> Vec<WorkspaceVisible>{
if let Ok((Ok(Response::Workspaces(workspaces)), _)) = Socket::connect()
.expect("failed to connect to niri socket")
.send(Request::Workspaces)
{
if let Some(workspace) = workspaces
return workspaces
.into_iter()
.filter(|w| w.is_focused)
.find(|w| w.output.as_ref().map_or("", |v| v) == output)
{
tx.send(WorkspaceVisible {
output: workspace.output.unwrap_or_else(String::new),
workspace_name: workspace.name.unwrap_or_else(String::new),
.filter(|w| w.is_active)
.map(|workspace| {
WorkspaceVisible {
output: workspace.output.unwrap_or_else(String::new),
workspace_name: workspace.name.unwrap_or_else(String::new),
}
})
.unwrap();
waker.wake().unwrap();
}
}
}
fn request_visible_workspaces(&mut self, tx: Sender<WorkspaceVisible>, waker: Arc<Waker>) {
if let Ok((Ok(Response::Workspaces(workspaces)), _)) = Socket::connect()
.expect("failed to connect to niri socket")
.send(Request::Workspaces)
.collect()
} else
{
for workspace in workspaces.into_iter().filter(|w| w.is_active) {
tx.send(WorkspaceVisible {
output: workspace.output.unwrap_or_else(String::new),
workspace_name: workspace.name.unwrap_or_else(String::new),
})
.unwrap();
waker.wake().unwrap();
}
panic!("unable to retrieve niri workspaces")
}
}

View file

@ -1,6 +1,4 @@
use std::{
sync::{mpsc::Sender, Arc},
};
use std::sync::{mpsc::Sender, Arc};
use super::{CompositorInterface, WorkspaceVisible};
use mio::Waker;
@ -19,47 +17,17 @@ impl SwayConnectionTask {
}
impl CompositorInterface for SwayConnectionTask {
fn request_visible_workspace(
&mut self,
output: &str,
tx: Sender<WorkspaceVisible>,
waker: Arc<Waker>,
) {
if let Some(workspace) = self
.sway_conn
fn request_visible_workspaces(&mut self) -> Vec<WorkspaceVisible> {
self.sway_conn
.get_workspaces()
.unwrap()
.into_iter()
.filter(|w| w.visible)
.find(|w| w.output == output)
{
tx
.send(WorkspaceVisible {
output: workspace.output,
workspace_name: workspace.name,
})
.unwrap();
waker.wake().unwrap();
}
}
fn request_visible_workspaces(&mut self, tx: Sender<WorkspaceVisible>, waker: Arc<Waker>) {
for workspace in self
.sway_conn
.get_workspaces()
.unwrap()
.into_iter()
.filter(|w| w.visible)
{
tx
.send(WorkspaceVisible {
output: workspace.output,
workspace_name: workspace.name,
})
.unwrap();
}
waker.wake().unwrap();
.map(|workspace| WorkspaceVisible {
output: workspace.output,
workspace_name: workspace.name,
})
.collect()
}
fn subscribe_event_loop(self, tx: Sender<WorkspaceVisible>, waker: Arc<Waker>) {
@ -72,12 +40,11 @@ impl CompositorInterface for SwayConnectionTask {
if let WorkspaceChange::Focus = workspace_event.change {
let current_workspace = workspace_event.current.unwrap();
tx
.send(WorkspaceVisible {
output: current_workspace.output.unwrap(),
workspace_name: current_workspace.name.unwrap(),
})
.unwrap();
tx.send(WorkspaceVisible {
output: current_workspace.output.unwrap(),
workspace_name: current_workspace.name.unwrap(),
})
.unwrap();
waker.wake().unwrap();
}