refactor(kde): remove the Shared wrapper, KDE's Backend extends the shared one

komorebi-for-kde's Backend is now `trait Backend: komorebi_backend::Backend`
and declares only what is KDE's own: drag_origin and drag_end, apply_look,
set_border_colours, set_wallpaper and shutdown. The 26 operations that
have shared names and semantics (docs/dev/seam.md section 2.1) are the
shared trait's; KWinBackend and the KDE FakeBackend implement it directly
(supports() is true for every capability, batches are the default no-ops)
and the backend::shared module with its Shared<B> adapter is deleted. The
shared contract tests (check_all and the KDE-specific assertions) now run
against FakeBackend itself. Integration tests import the shared trait to
call the fake's operations.

No behaviour change. Coverage removed with the duplicated declarations:
the doc examples of the KDE set_opacity and set_focus_follows_mouse
(the fake tests and the shared contract still exercise both) and the
'id too wide for u32' wrapper test (the same rule is in KWinBackend raw()).

Verified: KDE cargo test --workspace --no-fail-fast, 878 passed, 0 failed
(native, FakeBackend; was 881 before this revision, the 3 removed tests
are the ones named above). Not run: KDE VM suites.
This commit is contained in:
LGUG2Z committed 2026-10-31 10:50:39 -07:00
1 parent 68d1e49442
commit 42d8ea4656
13 files changed
+176 -541

No files matched your search

+117 -32
View File
@@ -9,7 +9,7 @@
//! # Example
//!
//! ```rust
//! use komorebi::backend::Backend;
//! use komorebi_backend::Backend;
//! use komorebi::backend::fake::Command;
//! use komorebi::backend::fake::FakeBackend;
//! use komorebi::core::Rect;
@@ -45,7 +45,6 @@ use komorebi_backend::MonitorId;
use komorebi_backend::WindowId;
use komorebi_backend::fake::Command as SharedCommand;
use parking_lot::Mutex;
use shared::Backend as SharedBackend;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
@@ -53,40 +52,40 @@ use std::path::PathBuf;
/// A command the window manager sent to the backend.
#[derive(Clone, Debug, PartialEq)]
pub enum Command {
/// [`Backend::set_rect`]: the window and the rectangle it was asked to take.
/// [`komorebi_backend::Backend::set_rect`]: the window and the rectangle it was asked to take.
SetRect(u32, Rect),
/// [`Backend::focus`]: the window, and whether the pointer was to follow.
/// [`komorebi_backend::Backend::focus`]: the window, and whether the pointer was to follow.
Focus(u32, bool),
/// [`Backend::raise`].
/// [`komorebi_backend::Backend::raise`].
Raise(u32),
/// [`Backend::hide`].
/// [`komorebi_backend::Backend::hide`].
Hide(u32),
/// [`Backend::restore`].
/// [`komorebi_backend::Backend::restore`].
Restore(u32),
/// [`Backend::minimize`].
/// [`komorebi_backend::Backend::minimize`].
Minimize(u32),
/// [`Backend::unminimize`].
/// [`komorebi_backend::Backend::unminimize`].
Unminimize(u32),
/// [`Backend::close`].
/// [`komorebi_backend::Backend::close`].
Close(u32),
/// [`Backend::center_cursor_in`].
/// [`komorebi_backend::Backend::center_cursor_in`].
CenterCursor(Rect),
/// [`Backend::ensure_workspaces`]: how many workspaces were asked for.
/// [`komorebi_backend::Backend::ensure_workspaces`]: how many workspaces were asked for.
EnsureWorkspaces(usize),
/// [`Backend::maximize`]: the window, and whether it was to be maximized or restored.
/// [`komorebi_backend::Backend::maximize`]: the window, and whether it was to be maximized or restored.
Maximize(u32, bool),
/// [`Backend::switch_workspace`]: the monitor, and the workspace it was asked to show.
/// [`komorebi_backend::Backend::switch_workspace`]: the monitor, and the workspace it was asked to show.
SwitchWorkspace(u32, usize),
/// [`Backend::set_window_workspace`]: the window, and the workspace it was put on.
/// [`komorebi_backend::Backend::set_window_workspace`]: the window, and the workspace it was put on.
SetWindowWorkspace(u32, usize),
/// [`Backend::apply_look`]: the look, when it is not the one applied before.
ApplyLook(Look),
/// [`Backend::set_border_colours`]: the colors of each window's border, when they are not the
/// ones given before.
BorderColours(Vec<(u32, u32)>),
/// [`Backend::set_opacity`]: the window, and how opaque it was asked to be.
/// [`komorebi_backend::Backend::set_opacity`]: the window, and how opaque it was asked to be.
SetOpacity(u32, u8),
/// [`Backend::set_focus_follows_mouse`]: whether it was turned on or off.
/// [`komorebi_backend::Backend::set_focus_follows_mouse`]: whether it was turned on or off.
FocusFollowsMouse(bool),
/// [`Backend::set_wallpaper`]: the monitor, and the picture it was asked to show.
SetWallpaper(u32, PathBuf),
@@ -252,7 +251,7 @@ impl FakeBackend {
self.kde.lock().drag_ends.insert(id, rect);
}
/// Gives a window a minimum size. A [`Backend::set_rect`] for something smaller leaves it at
/// Gives a window a minimum size. A [`komorebi_backend::Backend::set_rect`] for something smaller leaves it at
/// the minimum, which is how a GTK4 client answers.
pub fn set_min_size(&self, id: u32, width: i32, height: i32) {
self.inner.set_min_size(wid(id), width, height);
@@ -299,7 +298,7 @@ impl FakeBackend {
self.inner.rect_of(wid(id))
}
/// Returns true if and only if the window is hidden ([`Backend::hide`]), as opposed to being
/// Returns true if and only if the window is hidden ([`komorebi_backend::Backend::hide`]), as opposed to being
/// on a workspace that is not showing.
pub fn is_hidden(&self, id: u32) -> bool {
self.inner.is_hidden(wid(id))
@@ -382,7 +381,7 @@ impl FakeBackend {
}
}
impl Backend for FakeBackend {
impl komorebi_backend::Backend for FakeBackend {
fn windows(&self) -> Vec<komorebi_backend::WindowInfo> {
self.inner.windows()
}
@@ -403,10 +402,6 @@ impl Backend for FakeBackend {
self.inner.cursor_pos()
}
fn drag_end(&self, id: WindowId) -> Option<Rect> {
self.kde.lock().drag_ends.get(&raw(id)).copied()
}
fn set_rect(&self, id: WindowId, rect: &Rect) -> shared::Result<()> {
self.inner.set_rect(id, rect)
}
@@ -467,6 +462,24 @@ impl Backend for FakeBackend {
self.inner.maximize(id, maximize)
}
fn set_focus_follows_mouse(&self, enable: bool) -> shared::Result<()> {
self.inner.set_focus_follows_mouse(enable)
}
fn set_opacity(&self, id: WindowId, alpha: u8) -> shared::Result<()> {
self.inner.set_opacity(id, alpha)
}
fn supports(&self, _capability: komorebi_backend::Capability) -> bool {
true
}
}
impl Backend for FakeBackend {
fn drag_end(&self, id: WindowId) -> Option<Rect> {
self.kde.lock().drag_ends.get(&raw(id)).copied()
}
fn apply_look(&self, look: &Look) -> eyre::Result<bool> {
let mut kde = self.kde.lock();
// like a real backend, remember what was applied and say nothing twice
@@ -477,10 +490,6 @@ impl Backend for FakeBackend {
Ok(kde.borders_drawn && look.border.is_some())
}
fn set_focus_follows_mouse(&self, enable: bool) -> shared::Result<()> {
self.inner.set_focus_follows_mouse(enable)
}
fn set_wallpaper(&self, monitor_id: MonitorId, path: &Path) -> eyre::Result<()> {
self.kde
.lock()
@@ -493,10 +502,6 @@ impl Backend for FakeBackend {
Ok(())
}
fn set_opacity(&self, id: WindowId, alpha: u8) -> shared::Result<()> {
self.inner.set_opacity(id, alpha)
}
fn set_border_colours(&self, colours: &[(u32, u32)]) -> eyre::Result<bool> {
let mut kde = self.kde.lock();
let colours_now: HashMap<u32, u32> = colours.iter().copied().collect();
@@ -513,6 +518,7 @@ impl Backend for FakeBackend {
#[cfg(test)]
mod tests {
use super::*;
use komorebi_backend::Backend as _;
fn tile(left: i32, width: i32) -> Rect {
Rect {
@@ -962,3 +968,82 @@ mod tests {
Ok(())
}
}
/// The KDE fake is a [`komorebi_backend::Backend`] itself, and passes what every backend has to.
#[cfg(test)]
mod shared_contract {
use super::*;
use komorebi_backend::Backend as _;
use komorebi_backend::Capability;
use komorebi_backend::Error;
use komorebi_backend::contract::Harness;
use komorebi_backend::contract::check_all;
struct KdeFake(FakeBackend);
impl Harness for KdeFake {
type B = FakeBackend;
fn new() -> Self {
Self(FakeBackend::with_one_monitor())
}
fn backend(&self) -> &Self::B {
&self.0
}
fn open_window(&self, seed: u32) -> WindowId {
self.0.open_window(seed, "foot", "foot", "a terminal");
WindowId::from_u32(seed)
}
fn destroy_window(&self, id: WindowId) {
self.0.close_window_now(id.to_u32().expect("a KDE id"));
}
}
#[test]
fn the_kde_fake_passes_the_shared_contract() {
check_all::<KdeFake>();
}
#[test]
fn kde_is_the_first_implementation_and_supports_every_capability() {
let kde = KdeFake::new();
for capability in Capability::ALL {
assert!(kde.backend().supports(capability));
}
}
#[test]
fn a_command_about_a_window_that_is_gone_is_window_gone_and_other_failures_are_not() {
let kde = KdeFake::new();
let id = kde.open_window(4);
let gone = kde.backend().focus(WindowId::from_u32(9), false);
assert!(matches!(gone, Err(Error::WindowGone(WindowId(9)))));
assert_eq!(gone.unwrap_err().to_string(), "no such window: 9");
assert!(kde.backend().focus(id, false).is_ok());
}
#[test]
fn every_field_kde_knows_comes_through() {
let kde = KdeFake::new();
let id = kde.open_window(4);
let info = kde.backend().window(id).expect("window 4 was opened");
assert_eq!(info.id, id);
assert_eq!(info.class.as_deref(), Some("foot"));
assert_eq!(info.normal, Some(true));
assert_eq!(info.fullscreen, Some(false));
assert_eq!(
kde.backend().monitors().expect("one monitor")[0]
.name
.as_deref(),
Some("Virtual-0")
);
}
}
+14 -244
View File
@@ -24,16 +24,16 @@
//! # Workspaces
//!
//! komorebi's workspaces are the compositor's virtual desktops, per monitor: the window manager
//! asks for workspace `i` to be shown on a monitor ([`Backend::switch_workspace`]) and for a window
//! to be put on workspace `i` ([`Backend::set_window_workspace`]), and the compositor does the
//! asks for workspace `i` to be shown on a monitor ([`komorebi_backend::Backend::switch_workspace`]) and for a window
//! to be put on workspace `i` ([`komorebi_backend::Backend::set_window_workspace`]), and the compositor does the
//! showing, the hiding and the focusing. When something else changes what a monitor shows, the
//! backend raises an event and the window manager follows it.
//!
//! # Hiding
//!
//! The one thing the window manager hides itself is the back of a stack: a container tiles one
//! window, so the others in it are taken out of sight ([`Backend::hide`]) and brought back
//! ([`Backend::restore`]) when they are given the tile. A hidden window stays alive, stays on its
//! window, so the others in it are taken out of sight ([`komorebi_backend::Backend::hide`]) and brought back
//! ([`komorebi_backend::Backend::restore`]) when they are given the tile. A hidden window stays alive, stays on its
//! workspace and is not minimized, so what the compositor reports about it is not taken for the
//! user's doing (the window manager keeps a table of the windows it has hidden).
//!
@@ -59,7 +59,6 @@ use std::path::Path;
use std::sync::Arc;
pub mod fake;
pub mod shared;
/// What the compositor currently knows about one window.
#[derive(Clone, Debug, Default, PartialEq)]
@@ -84,7 +83,7 @@ pub struct WindowInfo {
/// manager leaves it where it is.
pub fullscreen: bool,
/// Whether the window is maximized, whether by the user (the title bar, a double click, a
/// shortcut) or because komorebi asked ([`Backend::maximize`]).
/// shortcut) or because komorebi asked ([`komorebi_backend::Backend::maximize`]).
pub maximized: bool,
/// Whether the compositor considers this a normal application window, as opposed to a dialog,
/// a splash screen, a utility window, a dock and so on.
@@ -243,17 +242,17 @@ pub struct BorderLook {
///
/// # Contract
///
/// - Queries never block on the compositor and never fail, except [`Backend::monitors`], which may
/// - Queries never block on the compositor and never fail, except [`komorebi_backend::Backend::monitors`], which may
/// fail when the compositor cannot say.
/// - A command about a window that does not exist, or is no longer alive, returns an error. It does
/// not panic.
/// - A backend is `Send + Sync`: the window manager calls it from several threads.
/// - Workspace `i` is the compositor's virtual desktop `i` on every monitor, and each monitor shows
/// one of them at a time ([`Backend::switch_workspace`]). The compositor shows the windows of the
/// one of them at a time ([`komorebi_backend::Backend::switch_workspace`]). The compositor shows the windows of the
/// workspace in front and hides the others; the window manager never hides a window to leave a
/// workspace.
/// - A hidden window ([`Backend::hide`]) is still alive and still on its workspace: it is out of
/// sight until [`Backend::restore`], and [`Backend::window_workspace`] names the workspace it
/// - A hidden window ([`komorebi_backend::Backend::hide`]) is still alive and still on its workspace: it is out of
/// sight until [`komorebi_backend::Backend::restore`], and [`komorebi_backend::Backend::window_workspace`] names the workspace it
/// comes back to.
/// - A backend asked to show a workspace, or to put a window on one, that has no desktop yet makes
/// the desktops up to it first.
@@ -275,55 +274,16 @@ pub struct BorderLook {
///
/// ```rust,compile_fail,E0277
/// # use std::rc::Rc;
/// # use color_eyre::eyre;
/// # use komorebi::backend::Backend;
/// # use komorebi::core::Rect;
/// # use komorebi_backend::{MonitorId, WindowId};
/// struct NotShareable(Rc<u8>);
///
/// # impl Backend for NotShareable {
/// # fn windows(&self) -> Vec<komorebi_backend::WindowInfo> { vec![] }
/// # fn window(&self, _: WindowId) -> Option<komorebi_backend::WindowInfo> { None }
/// # fn foreground_window(&self) -> Option<WindowId> { None }
/// # fn monitors(&self) -> komorebi_backend::Result<Vec<komorebi_backend::MonitorInfo>> { Ok(vec![]) }
/// # fn cursor_pos(&self) -> Option<(i32, i32)> { None }
/// # fn set_rect(&self, _: WindowId, _: &Rect) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn focus(&self, _: WindowId, _: bool) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn raise(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn hide(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn restore(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn minimize(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn unminimize(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn close(&self, _: WindowId) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn center_cursor_in(&self, _: &Rect) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn ensure_workspaces(&self, _: &[String]) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn switch_workspace(&self, _: MonitorId, _: usize) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn set_window_workspace(&self, _: WindowId, _: usize) -> komorebi_backend::Result<()> { Ok(()) }
/// # fn window_workspace(&self, _: WindowId) -> komorebi_backend::Result<Option<usize>> { Ok(None) }
/// # fn current_workspaces(&self) -> komorebi_backend::Result<Vec<(MonitorId, usize)>> { Ok(vec![]) }
/// # }
/// # impl komorebi_backend::Backend for NotShareable {}
/// impl Backend for NotShareable {}
/// ```
pub trait Backend: Send + Sync + 'static {
// ---- queries -------------------------------------------------------------------------------
/// Returns every window on the desktop that is a candidate for management, ordered by id.
fn windows(&self) -> Vec<komorebi_backend::WindowInfo>;
/// Returns what is known about the window, or `None` if the compositor has never heard of it.
fn window(&self, id: WindowId) -> Option<komorebi_backend::WindowInfo>;
/// Returns the id of the window that has the focus, if any window does.
fn foreground_window(&self) -> Option<WindowId>;
/// Returns every connected monitor.
///
/// # Errors
///
/// When the compositor cannot report its outputs.
fn monitors(&self) -> komorebi_backend::Result<Vec<komorebi_backend::MonitorInfo>>;
/// Returns where the pointer is, if the compositor says.
fn cursor_pos(&self) -> Option<(i32, i32)>;
pub trait Backend: komorebi_backend::Backend + 'static {
// ---- drag frames: KWin remembers them, and the shared trait has no place for them ----------
/// Returns the frame the window had when the user last began to drag it, if the backend has
/// kept it.
@@ -380,82 +340,6 @@ pub trait Backend: Send + Sync + 'static {
None
}
// ---- commands ------------------------------------------------------------------------------
/// Moves and resizes the window to `rect`, a position and a size.
///
/// A client may refuse the size (a minimum size, for one), so the frame the window ends up with
/// is [`Backend::window_rect`], not `rect`.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn set_rect(&self, id: WindowId, rect: &Rect) -> komorebi_backend::Result<()>;
/// Gives the window the focus, and, if `mouse_follows_focus`, puts the pointer in its middle.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn focus(&self, id: WindowId, mouse_follows_focus: bool) -> komorebi_backend::Result<()>;
/// Raises the window above the others.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn raise(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Takes the window out of sight without closing or minimizing it, which is how the window
/// manager keeps the back of a stack out of the way. On KWin it goes to a virtual desktop that
/// nobody looks at.
///
/// The window stays on its workspace ([`Backend::window_workspace`]), and
/// [`Backend::set_window_workspace`] changes the workspace it comes back to, not where it is
/// hidden. A window that is hidden already is left as it is.
///
/// # Errors
///
/// When the window does not exist or is no longer alive, or the compositor cannot make the
/// place the window is hidden in.
fn hide(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Brings a hidden window back, to the workspace it is on. A window that is not hidden is left
/// as it is.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn restore(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Minimizes the window.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn minimize(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Brings a minimized window back.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn unminimize(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Asks the window to close.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn close(&self, id: WindowId) -> komorebi_backend::Result<()>;
/// Puts the pointer in the middle of `rect`.
///
/// # Errors
///
/// When the compositor refuses to move the pointer.
fn center_cursor_in(&self, rect: &Rect) -> komorebi_backend::Result<()>;
// ---- workspaces ----------------------------------------------------------------------------
//
// komorebi's workspaces are the compositor's own virtual desktops: workspace `i` is desktop `i`
@@ -464,41 +348,6 @@ pub trait Backend: Send + Sync + 'static {
// follows the compositor when something else (a pager, a shortcut, an app raising a window)
// changes what a monitor shows.
/// Makes sure there are at least as many desktops as `names`, creating the missing ones with
/// those names.
///
/// A desktop that is already there keeps its name, unless nobody chose it (the compositor's
/// own "Desktop 2"), in which case it may take the name it is given.
///
/// # Errors
///
/// When the compositor refuses to create or rename a desktop.
fn ensure_workspaces(&self, names: &[String]) -> komorebi_backend::Result<()>;
/// Shows workspace `idx` on the monitor `monitor_id`, leaving the other monitors as they are.
///
/// The compositor shows the windows on that workspace, hides the rest, and gives the focus to a
/// window of that workspace by itself.
///
/// # Errors
///
/// When there is no desktop for `idx` and one cannot be made.
fn switch_workspace(&self, monitor_id: MonitorId, idx: usize) -> komorebi_backend::Result<()>;
/// Puts a window on workspace `idx`. It is visible at once to [`Backend::window_workspace`].
///
/// # Errors
///
/// When the window does not exist, or there is no desktop for `idx` and one cannot be made.
fn set_window_workspace(&self, id: WindowId, idx: usize) -> komorebi_backend::Result<()>;
/// Returns the workspace a window is on, or `None` for a window the compositor does not know or
/// that is on every workspace.
fn window_workspace(&self, id: WindowId) -> komorebi_backend::Result<Option<usize>>;
/// Returns the workspace each monitor shows, as `(monitor id, workspace index)`.
fn current_workspaces(&self) -> komorebi_backend::Result<Vec<(MonitorId, usize)>>;
// ---- look, which a backend that draws nothing need not say anything about --------------------
/// Asks the compositor to draw this look, and returns true if and only if window borders are
@@ -573,55 +422,6 @@ pub trait Backend: Send + Sync + 'static {
Ok(false)
}
/// Asks the compositor to draw the window with this opacity: 255 is not see-through at all.
/// The default does nothing.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
///
/// # Example
///
/// ```rust
/// use komorebi::backend::Backend;
/// use komorebi::backend::fake::FakeBackend;
///
/// let fake = FakeBackend::with_one_monitor();
/// fake.open_window(7, "foot", "foot", "a terminal");
/// assert_eq!(fake.opacity_of(7), 255);
///
/// fake.set_opacity(7.into(), 150)?;
/// assert_eq!(fake.opacity_of(7), 150);
/// # Ok::<(), color_eyre::eyre::Report>(())
/// ```
fn set_opacity(&self, _id: WindowId, _alpha: u8) -> komorebi_backend::Result<()> {
Ok(())
}
/// Asks the compositor to give the focus to the window under the pointer, as the pointer moves
/// (`true`), or to stop doing that and go back to the focus policy the user had (`false`). The
/// default does nothing.
///
/// # Errors
///
/// When the compositor's configuration cannot be changed.
///
/// # Example
///
/// ```rust
/// use komorebi::backend::Backend;
/// use komorebi::backend::fake::Command;
/// use komorebi::backend::fake::FakeBackend;
///
/// let fake = FakeBackend::with_one_monitor();
/// fake.set_focus_follows_mouse(true)?;
/// assert_eq!(fake.commands(), [Command::FocusFollowsMouse(true)]);
/// # Ok::<(), color_eyre::eyre::Report>(())
/// ```
fn set_focus_follows_mouse(&self, _enable: bool) -> komorebi_backend::Result<()> {
Ok(())
}
/// Shows this picture as the wallpaper of a monitor, filling the screen. The window manager
/// asks for it every time a workspace of the monitor comes into view, since the user may have
/// changed the wallpaper by hand since, so a backend does nothing when the monitor already
@@ -663,37 +463,6 @@ pub trait Backend: Send + Sync + 'static {
/// The default does nothing. Implementors should not need to override it unless they created
/// something in the compositor.
fn shutdown(&self) {}
/// Has the compositor maximize the window to the work area of its monitor, panels left alone,
/// or, with `maximize` false, give it back the frame it had before.
///
/// The default does nothing, for a compositor that has no maximize of its own.
///
/// # Errors
///
/// When the window does not exist or is no longer alive.
fn maximize(&self, _id: WindowId, _maximize: bool) -> komorebi_backend::Result<()> {
Ok(())
}
/// Returns true if and only if the compositor has a live window with this id.
fn is_window(&self, id: WindowId) -> bool {
self.window(id).is_some_and(|w| w.alive)
}
/// Returns the window's frame, if the compositor knows the window.
fn window_rect(&self, id: WindowId) -> Option<Rect> {
self.window(id).map(|w| w.rect)
}
/// Returns the id of the monitor whose bounds contain the point.
fn monitor_from_point(&self, x: i32, y: i32) -> Option<MonitorId> {
self.monitors().ok()?.into_iter().find_map(|m| {
let (l, t) = (m.size.left, m.size.top);
let (r, b) = (l + m.size.right, t + m.size.bottom);
(x >= l && x < r && y >= t && y < b).then_some(m.id)
})
}
}
static BACKEND: RwLock<Option<Arc<dyn Backend>>> = RwLock::new(None);
@@ -778,6 +547,7 @@ pub fn with_backend<R>(backend: Arc<dyn Backend>, f: impl FnOnce() -> R) -> R {
mod tests {
use super::*;
use fake::FakeBackend;
use komorebi_backend::Backend as _;
#[test]
fn a_thread_that_installs_nothing_gets_a_permissive_fake_in_unit_tests() {
@@ -1,223 +0,0 @@
//! komorebi for KDE's backends, seen as the shared [`komorebi_backend::Backend`].
//!
//! [`Shared`] wraps any KDE [`Backend`](super::Backend) (the real KWin one or the fake) and
//! implements the trait that every komorebi shares, which is how KDE is its first implementation:
//! the daemon keeps calling its own trait (`u32` ids, `eyre` errors, the look, the wallpaper and
//! the drag frames) until the shared core is adopted, and everything the shared trait asks for is
//! answered by the KDE methods underneath. Nothing about how KDE behaves changes.
//!
//! What the translation does:
//!
//! - an id is a [`WindowId`] made from the `u32` ([`WindowId::from_u32`]); an id too wide for a
//! `u32` is a window KDE never had;
//! - errors need no translation: KDE's trait already fails with the shared [`Error`];
//! - KDE fills every field of a window, so the optional fields are all `Some`;
//! - every [`Capability`] is supported: KDE has workspaces, maximize, opacity and focus follows
//! mouse;
//! - a batch does nothing: KDE batches by construction (the answer to a held poll carries every
//! command that was queued).
use super::Backend;
use komorebi_backend as shared;
use komorebi_backend::Capability;
use komorebi_backend::MonitorId;
use komorebi_backend::Rect;
use komorebi_backend::WindowId;
use std::sync::Arc;
/// A KDE [`Backend`] as the shared [`komorebi_backend::Backend`].
pub struct Shared<B: Backend + ?Sized>(pub Arc<B>);
impl<B: Backend + ?Sized> Shared<B> {
/// Returns the KDE backend underneath.
pub fn kde(&self) -> &B {
&self.0
}
}
impl<B: Backend + ?Sized> shared::Backend for Shared<B> {
fn windows(&self) -> Vec<shared::WindowInfo> {
self.0.windows()
}
fn window(&self, id: WindowId) -> Option<shared::WindowInfo> {
self.0.window(id)
}
fn foreground_window(&self) -> Option<WindowId> {
self.0.foreground_window()
}
fn monitors(&self) -> shared::Result<Vec<shared::MonitorInfo>> {
self.0.monitors()
}
fn cursor_pos(&self) -> Option<(i32, i32)> {
self.0.cursor_pos()
}
fn set_rect(&self, id: WindowId, rect: &Rect) -> shared::Result<()> {
self.0.set_rect(id, rect)
}
fn focus(&self, id: WindowId, mouse_follows_focus: bool) -> shared::Result<()> {
self.0.focus(id, mouse_follows_focus)
}
fn raise(&self, id: WindowId) -> shared::Result<()> {
self.0.raise(id)
}
fn hide(&self, id: WindowId) -> shared::Result<()> {
self.0.hide(id)
}
fn restore(&self, id: WindowId) -> shared::Result<()> {
self.0.restore(id)
}
fn minimize(&self, id: WindowId) -> shared::Result<()> {
self.0.minimize(id)
}
fn unminimize(&self, id: WindowId) -> shared::Result<()> {
self.0.unminimize(id)
}
fn close(&self, id: WindowId) -> shared::Result<()> {
self.0.close(id)
}
fn center_cursor_in(&self, rect: &Rect) -> shared::Result<()> {
self.0.center_cursor_in(rect)
}
fn supports(&self, _capability: Capability) -> bool {
true
}
fn maximize(&self, id: WindowId, maximize: bool) -> shared::Result<()> {
self.0.maximize(id, maximize)
}
fn set_opacity(&self, id: WindowId, alpha: u8) -> shared::Result<()> {
self.0.set_opacity(id, alpha)
}
fn set_focus_follows_mouse(&self, enable: bool) -> shared::Result<()> {
self.0.set_focus_follows_mouse(enable)
}
fn ensure_workspaces(&self, names: &[String]) -> shared::Result<()> {
self.0.ensure_workspaces(names)
}
fn switch_workspace(&self, monitor: MonitorId, idx: usize) -> shared::Result<()> {
self.0.switch_workspace(monitor, idx)
}
fn set_window_workspace(&self, id: WindowId, idx: usize) -> shared::Result<()> {
self.0.set_window_workspace(id, idx)
}
fn window_workspace(&self, id: WindowId) -> shared::Result<Option<usize>> {
self.0.window_workspace(id)
}
fn current_workspaces(&self) -> shared::Result<Vec<(MonitorId, usize)>> {
self.0.current_workspaces()
}
// `is_window`, `window_rect` and `monitor_from_point` are the provided ones, over `window` and
// `monitors`, exactly as KDE's own are; `begin_batch` and `end_batch` do nothing, as KDE's
// queue is flushed as a whole.
}
#[cfg(test)]
mod tests {
use super::*;
use crate::backend::fake::FakeBackend;
use komorebi_backend::Backend as _;
use komorebi_backend::Error;
use komorebi_backend::contract::Harness;
use komorebi_backend::contract::check_all;
struct KdeFake(Shared<FakeBackend>);
impl Harness for KdeFake {
type B = Shared<FakeBackend>;
fn new() -> Self {
Self(Shared(Arc::new(FakeBackend::with_one_monitor())))
}
fn backend(&self) -> &Self::B {
&self.0
}
fn open_window(&self, seed: u32) -> WindowId {
self.0.kde().open_window(seed, "foot", "foot", "a terminal");
WindowId::from_u32(seed)
}
fn destroy_window(&self, id: WindowId) {
self.0
.kde()
.close_window_now(id.to_u32().expect("a KDE id"));
}
}
#[test]
fn the_kde_fake_passes_the_shared_contract() {
check_all::<KdeFake>();
}
#[test]
fn kde_is_the_first_implementation_and_supports_every_capability() {
let kde = KdeFake::new();
for capability in Capability::ALL {
assert!(kde.backend().supports(capability));
}
}
#[test]
fn a_command_about_a_window_that_is_gone_is_window_gone_and_other_failures_are_not() {
let kde = KdeFake::new();
let id = kde.open_window(4);
let gone = kde.backend().focus(WindowId::from_u32(9), false);
assert!(matches!(gone, Err(Error::WindowGone(WindowId(9)))));
assert_eq!(gone.unwrap_err().to_string(), "no such window: 9");
assert!(kde.backend().focus(id, false).is_ok());
}
#[test]
fn an_id_too_wide_for_kde_is_a_window_kde_never_had() {
let kde = KdeFake::new();
let wide = WindowId(u64::from(u32::MAX) + 1);
assert!(kde.backend().window(wide).is_none());
assert!(kde.backend().hide(wide).unwrap_err().is_window_gone());
}
#[test]
fn every_field_kde_knows_comes_through() {
let kde = KdeFake::new();
let id = kde.open_window(4);
let info = kde.backend().window(id).expect("window 4 was opened");
assert_eq!(info.id, id);
assert_eq!(info.class.as_deref(), Some("foot"));
assert_eq!(info.normal, Some(true));
assert_eq!(info.fullscreen, Some(false));
assert_eq!(
kde.backend().monitors().expect("one monitor")[0]
.name
.as_deref(),
Some("Virtual-0")
);
}
}
+3 -3
View File
@@ -61,7 +61,7 @@ fn contains_point(area: &Rect, x: i32, y: i32) -> bool {
/// # Errors
///
/// When the backend cannot report its monitors
/// ([`Backend::monitors`](crate::backend::Backend::monitors)).
/// ([`Backend::monitors`](komorebi_backend::Backend::monitors)).
///
/// # Example
///
@@ -103,7 +103,7 @@ pub fn latest_monitor_information() -> eyre::Result<Vec<Monitor>> {
/// # Errors
///
/// When the backend cannot report its monitors
/// ([`Backend::monitors`](crate::backend::Backend::monitors)).
/// ([`Backend::monitors`](komorebi_backend::Backend::monitors)).
///
/// # Example
///
@@ -227,7 +227,7 @@ pub fn load_monitor_information(wm: &mut WindowManager) -> eyre::Result<()> {
/// # Errors
///
/// - The backend cannot report its monitors
/// ([`Backend::monitors`](crate::backend::Backend::monitors)).
/// ([`Backend::monitors`](komorebi_backend::Backend::monitors)).
/// - There is no focused monitor or workspace, or the windows of the focused workspace cannot be
/// put in their tiles.
///
+32 -25
View File
@@ -99,6 +99,7 @@ use crate::window_manager_event_listener;
use color_eyre::eyre;
use color_eyre::eyre::OptionExt;
use futures_util::StreamExt;
use komorebi_backend::Backend as _;
use komorebi_backend::MonitorId;
use komorebi_backend::WindowId;
use parking_lot::Condvar;
@@ -822,7 +823,7 @@ impl KWinBackend {
}
}
impl Backend for KWinBackend {
impl komorebi_backend::Backend for KWinBackend {
fn windows(&self) -> Vec<komorebi_backend::WindowInfo> {
self.shared
.table
@@ -846,16 +847,6 @@ impl Backend for KWinBackend {
.map(WindowId::from_u32)
}
fn drag_origin(&self, id: WindowId) -> Option<Rect> {
let id = id.to_u32()?;
self.shared.table.lock().drag_origin(id)
}
fn drag_end(&self, id: WindowId) -> Option<Rect> {
let id = id.to_u32()?;
self.shared.table.lock().drag_end(id)
}
fn monitors(&self) -> komorebi_backend::Result<Vec<komorebi_backend::MonitorInfo>> {
let monitors = self.shared.table.lock().monitors();
if monitors.is_empty() {
@@ -1049,20 +1040,6 @@ impl Backend for KWinBackend {
Ok(())
}
fn apply_look(&self, look: &Look) -> eyre::Result<bool> {
let shared = self.shared.clone();
let look = look.clone();
self.shared.block_on(async move {
let Some(kwinrc) = &shared.kwinrc else {
return Ok(false);
};
let connection = shared.connection().await?;
let mut effects = shared.effects.lock().await;
effects.apply(&connection, kwinrc, &look).await
})?
}
fn set_opacity(&self, id: WindowId, alpha: u8) -> komorebi_backend::Result<()> {
let id = raw(id)?;
self.command(
@@ -1100,6 +1077,36 @@ impl Backend for KWinBackend {
.map_err(platform)
}
fn supports(&self, _capability: komorebi_backend::Capability) -> bool {
true
}
}
impl Backend for KWinBackend {
fn drag_origin(&self, id: WindowId) -> Option<Rect> {
let id = id.to_u32()?;
self.shared.table.lock().drag_origin(id)
}
fn drag_end(&self, id: WindowId) -> Option<Rect> {
let id = id.to_u32()?;
self.shared.table.lock().drag_end(id)
}
fn apply_look(&self, look: &Look) -> eyre::Result<bool> {
let shared = self.shared.clone();
let look = look.clone();
self.shared.block_on(async move {
let Some(kwinrc) = &shared.kwinrc else {
return Ok(false);
};
let connection = shared.connection().await?;
let mut effects = shared.effects.lock().await;
effects.apply(&connection, kwinrc, &look).await
})?
}
fn set_wallpaper(&self, monitor_id: MonitorId, path: &std::path::Path) -> eyre::Result<()> {
if !self.shared.alive.load(Ordering::SeqCst) {
eyre::bail!("KWin is gone");
@@ -192,7 +192,7 @@
//! - **FocusFollowsMouse** and **ToggleFocusFollowsMouse** set whether the compositor gives the
//! focus to the window under the pointer, as the pointer moves: KWin's own focus policy, whichever
//! of the two implementations is named, put back when it is turned off and when komorebi exits
//! ([`Backend::set_focus_follows_mouse`](crate::backend::Backend::set_focus_follows_mouse)).
//! ([`Backend::set_focus_follows_mouse`](komorebi_backend::Backend::set_focus_follows_mouse)).
//! - **Border** sets whether the compositor draws a border around each window, and **BorderWidth**
//! and **BorderOffset** the room the layout leaves for it: each asks the backend for the look
//! again ([`border_manager::send_notification`]), as does every message once it has been
@@ -2105,8 +2105,7 @@ impl WindowManager {
| SocketMessage::WatchConfiguration(..)
| SocketMessage::WindowHidingBehaviour(..)
| SocketMessage::WorkspaceLayoutCustom(..)
| SocketMessage::WorkspaceLayoutCustomRule(..)
=> {
| SocketMessage::WorkspaceLayoutCustomRule(..) => {
reply.write_all(message.not_supported_reply().as_bytes())?;
return Ok(());
}
@@ -2149,10 +2148,7 @@ impl WindowManager {
/// - The connection cannot be read, or cloned to be read and written at once.
/// - A line is not a [`SocketMessage`]: the lines after it are not read.
/// - [`WindowManager::process_command`] fails: the lines after it are not read.
pub fn read_commands_uds(
wm: &Arc<Mutex<WindowManager>>,
stream: UnixStream,
) -> eyre::Result<()> {
pub fn read_commands_uds(wm: &Arc<Mutex<WindowManager>>, stream: UnixStream) -> eyre::Result<()> {
komorebi_transport::read_commands::<WindowManager, _>(wm, stream, &NoPolicy)
}
+1 -1
View File
@@ -311,7 +311,7 @@ impl Window {
backend().unminimize(self.window_id())
}
/// Takes the window out of sight ([`Backend::hide`](crate::backend::Backend::hide)): the back
/// Takes the window out of sight ([`Backend::hide`](komorebi_backend::Backend::hide)): the back
/// of a stack, which on KDE goes to a virtual desktop nobody looks at
///
/// The id is recorded in [`struct@HIDDEN_WINDOWS`] first, so that what the compositor reports
+1 -1
View File
@@ -4,7 +4,6 @@
//!
//! Window ids are in the 10_000s and the 51_000s.
use komorebi::backend::Backend;
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::Rect;
@@ -14,6 +13,7 @@ use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use komorebi_backend::Backend as _;
use std::sync::Arc;
fn added(id: u32) -> WindowManagerEvent {
@@ -5,7 +5,6 @@
//!
//! Window ids are in the 36_000s.
use komorebi::backend::Backend;
use komorebi::backend::fake::Command;
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
@@ -19,6 +18,7 @@ use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use komorebi_backend::Backend as _;
use std::sync::Arc;
fn added(id: u32) -> WindowManagerEvent {
+1 -1
View File
@@ -6,7 +6,6 @@
//!
//! Window ids are in the 32_000s.
use komorebi::backend::Backend;
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::CycleDirection;
@@ -19,6 +18,7 @@ use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use komorebi_backend::Backend as _;
use std::sync::Arc;
const LEFT: u32 = 1;
+1 -1
View File
@@ -5,7 +5,6 @@
//!
//! Window ids are in the 35_000s.
use komorebi::backend::Backend;
use komorebi::backend::fake::Command;
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
@@ -20,6 +19,7 @@ use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use komorebi_backend::Backend as _;
use std::sync::Arc;
fn added(id: u32) -> WindowManagerEvent {
+1 -1
View File
@@ -9,7 +9,6 @@
//!
//! Window ids are in the 33_000s; the hidden-window table is process-wide.
use komorebi::backend::Backend;
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::CycleDirection;
@@ -21,6 +20,7 @@ use komorebi::window_manager::WindowManager;
use komorebi::window_manager_event::KWinNotification;
use komorebi::window_manager_event::SystemNotification;
use komorebi::window_manager_event::WindowManagerEvent;
use komorebi_backend::Backend as _;
use std::sync::Arc;
fn added(id: u32) -> WindowManagerEvent {
@@ -263,7 +263,7 @@ fn a_window_open_on_a_desktop_at_startup_starts_on_that_workspace() {
// the user had put one on the second desktop, and was looking at the first
fake.show_workspace_by_user(MONITOR, 0);
{
use komorebi::backend::Backend;
use komorebi_backend::Backend;
fake.set_window_workspace(10_112.into(), 1).unwrap();
fake.clear_commands();
}