mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-12-03 13:44:14 +01:00
KWin has no protocol that lets another process read or set the geometry of a window, but its scripting API has both, and a signal for every change the window manager follows. So the KWin backend is in two parts: bridge.js, a script the daemon loads into KWin, which reports what KWin sees and holds no window manager logic, and a D-Bus service in the daemon, org.komorebi.Bridge, which the script calls with every event as a JSON string. Every decision about what an event means is made by the table in kwin::state, which is pure and is unit-tested with the JSON the script sends: which windows may be managed, the ids komorebi gives KWin's windows, and the Show, Destroy, FocusChange, Minimize and Restore events of the window manager. Events reach the window manager in the order KWin raised them: the Event interface handles its calls inline (zbus reordered them otherwise), and one channel and one thread, window_manager_event_listener and listen_for_events, carry them on. Commands go to the script as the answer to a Poll call that the script always has open and the daemon holds until it has something to send: in the spikes this was as fast as any other channel, and it makes one call every 10 seconds when nothing happens. Here the only command is the cursor query. Every command of the Backend trait is refused with "the KWin backend only reads the desktop", so the daemon follows KWin and moves nothing. The bridge is JavaScript that KWin runs as it is, and it gets types and tests without a JavaScript build chain. tsc checks it as strict TypeScript through JSDoc types of every message, against kwin-api.d.ts, a declaration of the part of KWin's scripting API it uses, in which every property is read-only and callDBus takes one string. bridge.test.js runs it under node --test against a fake KWin in a node:vm context. The messages of a session are JSON fixtures that both sides' tests read: the bridge's tests check that it sends those events, and the table's and the backend's tests read the same events and commands, so a message cannot change on one side alone. just check-js and the flake's komorebi-bridge-js check run both, with nodejs and typescript from nixpkgs; there is no package.json. The daemon now connects to KWin by default, and --fake-backend is hidden. The VM suite vm-kwin-events starts it against a headless KWin and checks, from its debug log, that the script loads and reports what was open, that KWin's signals arrive as events and in order, and that stopping the daemon unloads the script. docs/dev/PLATFORM.md starts with what komorebi needs from Plasma. zbus 5's zvariant, zbus_names and zvariant_derive are flagged unmaintained, since their code moved into zbus 6; deny.toml ignores the three advisories.
102 lines
3.4 KiB
Rust
102 lines
3.4 KiB
Rust
//! [`WindowManagerEvent`], an event the compositor reported about a window, in the form the
|
|
//! window manager handles it
|
|
//! ([`WindowManager::process_event`](crate::window_manager::WindowManager::process_event)).
|
|
//!
|
|
//! An event is about one window and carries it, and the compositor signal it came from
|
|
//! ([`SystemNotification`]), which is only for logging.
|
|
|
|
use crate::window::Window;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use strum::Display;
|
|
|
|
/// The compositor signal a [`WindowManagerEvent`] came from
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display, PartialEq, Eq)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
#[serde(tag = "type", content = "content")]
|
|
pub enum SystemNotification {
|
|
KWin(KWinNotification),
|
|
}
|
|
|
|
/// The KWin script signals the bridge listens to
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display, PartialEq, Eq)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
pub enum KWinNotification {
|
|
/// `workspace.windowAdded`
|
|
WindowAdded,
|
|
/// `workspace.windowRemoved`
|
|
WindowRemoved,
|
|
/// `workspace.windowActivated`
|
|
WindowActivated,
|
|
/// `window.minimizedChanged` to true
|
|
WindowMinimized,
|
|
/// `window.minimizedChanged` to false
|
|
WindowRestored,
|
|
}
|
|
|
|
/// Window events: each carries the window it is about.
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
#[serde(tag = "type", content = "content")]
|
|
pub enum WindowManagerEvent {
|
|
FocusChange(SystemNotification, Window),
|
|
Show(SystemNotification, Window),
|
|
Destroy(SystemNotification, Window),
|
|
Minimize(SystemNotification, Window),
|
|
Restore(SystemNotification, Window),
|
|
}
|
|
|
|
impl WindowManagerEvent {
|
|
/// The window this event is about, if it is about one
|
|
pub fn window(&self) -> Option<Window> {
|
|
Some(match self {
|
|
WindowManagerEvent::FocusChange(_, window)
|
|
| WindowManagerEvent::Show(_, window)
|
|
| WindowManagerEvent::Destroy(_, window)
|
|
| WindowManagerEvent::Minimize(_, window)
|
|
| WindowManagerEvent::Restore(_, window) => *window,
|
|
})
|
|
}
|
|
|
|
pub fn window_id(&self) -> Option<u32> {
|
|
self.window().map(|w| w.id)
|
|
}
|
|
|
|
pub fn process_id(&self) -> Option<i32> {
|
|
self.window().map(|w| w.process_id())
|
|
}
|
|
|
|
/// The notification that caused this event, for logging
|
|
pub fn notification(&self) -> String {
|
|
match self {
|
|
WindowManagerEvent::FocusChange(n, _)
|
|
| WindowManagerEvent::Show(n, _)
|
|
| WindowManagerEvent::Destroy(n, _)
|
|
| WindowManagerEvent::Minimize(n, _)
|
|
| WindowManagerEvent::Restore(n, _) => match n {
|
|
SystemNotification::KWin(k) => k.to_string(),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn an_event_names_its_window_and_the_signal_it_came_from() {
|
|
let event = WindowManagerEvent::Minimize(
|
|
SystemNotification::KWin(KWinNotification::WindowMinimized),
|
|
Window::from(301),
|
|
);
|
|
|
|
assert_eq!(event.window(), Some(Window::from(301)));
|
|
assert_eq!(event.window_id(), Some(301));
|
|
// the crate's unit tests get a permissive fake, which gives a window the pid id + 1000
|
|
assert_eq!(event.process_id(), Some(1301));
|
|
assert_eq!(event.notification(), "WindowMinimized");
|
|
assert_eq!(event.to_string(), "Minimize");
|
|
}
|
|
}
|