mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-12-04 05:56:31 +01:00
The daemon takes its settings from a komorebi.json: the file --config names, or the one in the config home ($KOMOREBI_CONFIG_HOME, else ~/.config/komorebi). Without a file it runs on its defaults, as before. StaticConfig reads the file in three steps, because the monitors are known only after the backend has been asked. preload makes the window manager and applies the settings that are process-wide, postload gives each monitor and workspace the entry of its place (by the index display_index_preferences gives a monitor's serial number id, else in order), and main asks KWin for a desktop for each workspace the file lists. A watcher on the file sends the daemon a ReloadStaticConfiguration about two seconds after it changes, whether an editor wrote the file in place or renamed a new one over it, and a file that does not parse changes nothing. A workspace can change its layout, its work area offset and its layout options with the number of its containers, from the file or with komorebic (workspace-layout-rule, its named form and the two that clear), and layout_defaults gives every workspace of a layout the options it sets none of. One file is meant to serve every platform, so a key that does nothing on Plasma is accepted and ignored. config_parity checks the example and the files of other platforms; the keys of features that are not in yet are listed there, and the test fails when a list names a key that is read. The file also sets what the stacks, the float override, the floating layer and monocle set while the daemon runs: window_container_behaviour, with its rules by container count in a workspace, float_override, float_override_placement, floating_layer_behaviour (for a monitor and a workspace too), floating_layer_placement and monocle_focus_behaviour. Their features come before this commit, but a file to put the keys in does not, so they are part of this one. komorebic gains configuration, generate-static-config and start --config. The NixOS module gets configFile, which the unit gives the daemon as --config.
263 lines
8.8 KiB
Rust
263 lines
8.8 KiB
Rust
//! The komorebi daemon: a tiling window manager for KDE Plasma.
|
|
//!
|
|
//! The daemon owns the layout of every window and asks the compositor, through a backend, to
|
|
//! put each one where the layout says.
|
|
//!
|
|
//! The backend is KWin's ([`komorebi::kwin`]): the daemon connects to the session bus, loads its
|
|
//! script into KWin and waits for the script to report the desktop. `--fake-backend` replaces it
|
|
//! with an in-memory desktop with one monitor and no windows, for development and testing.
|
|
//!
|
|
//! The daemon starts from `komorebi.json`: the file `--config` names, or else the one in
|
|
//! [`struct@HOME_DIR`] when there is one ([`komorebi::static_config`]), which it reloads whenever
|
|
//! it changes. Without a file it runs on its defaults. It loads the monitors and the windows
|
|
//! that are open, gives each monitor the configuration the file has for it, tiles them, listens for
|
|
//! messages on `komorebi.sock` in [`struct@DATA_DIR`], handles the events the backend reports on
|
|
//! a thread of their own, and runs until it is stopped, with `SIGINT`, `SIGTERM` or a `Stop`
|
|
//! message.
|
|
//!
|
|
//! It logs to standard output and to a file a day in [`struct@LOG_DIR`], which `komorebic log`
|
|
//! follows.
|
|
//!
|
|
//! When it stops, it brings back the windows it hid (a stack's windows that were not in front,
|
|
//! except after a `StopIgnoreRestore` message), then connects to the socket of each subscriber
|
|
//! and closes the connection without writing anything, so a subscriber sees the daemon stop as a
|
|
//! connection with nothing on it.
|
|
|
|
use clap::Parser;
|
|
use clap::ValueEnum;
|
|
use color_eyre::eyre;
|
|
use komorebi::DATA_DIR;
|
|
use komorebi::HOME_DIR;
|
|
use komorebi::LOG_DIR;
|
|
use komorebi::backend;
|
|
use komorebi::backend::fake::FakeBackend;
|
|
use komorebi::core::pathext::replace_env_in_path;
|
|
use komorebi::process_command::listen_for_commands;
|
|
use komorebi::process_event::listen_for_events;
|
|
use komorebi::static_config::StaticConfig;
|
|
use komorebi::window_manager::WindowManager;
|
|
use komorebi::window_manager_event_listener;
|
|
use parking_lot::Mutex;
|
|
use serde::Deserialize;
|
|
use std::net::Shutdown;
|
|
use std::os::unix::net::UnixStream;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
use tracing_appender::non_blocking::WorkerGuard;
|
|
use tracing_subscriber::EnvFilter;
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
fn setup(log_level: LogLevel) -> eyre::Result<(WorkerGuard, WorkerGuard)> {
|
|
if std::env::var("RUST_LIB_BACKTRACE").is_err() {
|
|
unsafe {
|
|
std::env::set_var("RUST_LIB_BACKTRACE", "1");
|
|
}
|
|
}
|
|
|
|
color_eyre::install()?;
|
|
|
|
if std::env::var("RUST_LOG").is_err() {
|
|
unsafe {
|
|
std::env::set_var(
|
|
"RUST_LOG",
|
|
match log_level {
|
|
LogLevel::Error => "komorebi=error",
|
|
LogLevel::Warn => "komorebi=warn",
|
|
LogLevel::Info => "komorebi=info",
|
|
LogLevel::Debug => "komorebi=debug",
|
|
LogLevel::Trace => "komorebi=trace",
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
std::fs::create_dir_all(&*LOG_DIR)?;
|
|
let appender = tracing_appender::rolling::daily(&*LOG_DIR, "komorebi_plaintext.log");
|
|
let color_appender = tracing_appender::rolling::daily(&*LOG_DIR, "komorebi.log");
|
|
let (non_blocking, guard) = tracing_appender::non_blocking(appender);
|
|
let (color_non_blocking, color_guard) = tracing_appender::non_blocking(color_appender);
|
|
|
|
tracing::subscriber::set_global_default(
|
|
tracing_subscriber::fmt::Subscriber::builder()
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
.finish()
|
|
.with(
|
|
tracing_subscriber::fmt::Layer::default()
|
|
.with_writer(non_blocking)
|
|
.with_ansi(false),
|
|
)
|
|
.with(
|
|
tracing_subscriber::fmt::Layer::default()
|
|
.with_writer(color_non_blocking)
|
|
.with_ansi(true),
|
|
),
|
|
)?;
|
|
|
|
// https://github.com/tokio-rs/tracing/blob/master/examples/examples/panic_hook.rs
|
|
// Set a panic hook that records the panic as a `tracing` event at the
|
|
// `ERROR` verbosity level.
|
|
std::panic::set_hook(Box::new(|panic| {
|
|
panic.location().map_or_else(
|
|
|| {
|
|
tracing::error!(message = %panic);
|
|
},
|
|
|location| {
|
|
tracing::error!(
|
|
message = %panic,
|
|
panic.file = location.file(),
|
|
panic.line = location.line(),
|
|
panic.column = location.column(),
|
|
);
|
|
},
|
|
);
|
|
}));
|
|
|
|
Ok((guard, color_guard))
|
|
}
|
|
|
|
#[cfg(feature = "deadlock_detection")]
|
|
#[tracing::instrument]
|
|
fn detect_deadlocks() {
|
|
// Create a background thread which checks for deadlocks every 10s
|
|
std::thread::spawn(move || {
|
|
loop {
|
|
tracing::info!("running deadlock detector");
|
|
std::thread::sleep(std::time::Duration::from_secs(5));
|
|
let deadlocks = parking_lot::deadlock::check_deadlock();
|
|
if deadlocks.is_empty() {
|
|
continue;
|
|
}
|
|
|
|
tracing::error!("{} deadlocks detected", deadlocks.len());
|
|
for (i, threads) in deadlocks.iter().enumerate() {
|
|
tracing::error!("deadlock #{}", i);
|
|
for t in threads {
|
|
tracing::error!("thread id: {:#?}", t.thread_id());
|
|
tracing::error!("{:#?}", t.backtrace());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
#[derive(Default, Deserialize, ValueEnum, Clone)]
|
|
#[serde(rename_all = "snake_case")]
|
|
enum LogLevel {
|
|
Error,
|
|
Warn,
|
|
#[default]
|
|
Info,
|
|
Debug,
|
|
Trace,
|
|
}
|
|
|
|
/// A tiling window manager for KDE Plasma.
|
|
#[derive(Parser)]
|
|
#[clap(author, about, version = version::LONG_VERSION)]
|
|
struct Opts {
|
|
/// Level of log output verbosity
|
|
#[clap(long, value_enum, default_value_t=LogLevel::Info)]
|
|
log_level: LogLevel,
|
|
/// Path to a static configuration JSON file
|
|
#[clap(short, long)]
|
|
#[clap(value_parser = replace_env_in_path)]
|
|
config: Option<PathBuf>,
|
|
/// Run against an in-memory desktop instead of KWin, for development and testing of the
|
|
/// socket interface. Windows and monitors are not real.
|
|
#[clap(long, hide = true)]
|
|
fake_backend: bool,
|
|
}
|
|
|
|
fn main() -> eyre::Result<()> {
|
|
let opts: Opts = Opts::parse();
|
|
let (_guard, _color_guard) = setup(opts.log_level)?;
|
|
|
|
if !DATA_DIR.is_dir() {
|
|
std::fs::create_dir_all(&*DATA_DIR)?;
|
|
}
|
|
|
|
if opts.fake_backend {
|
|
tracing::warn!("running against an in-memory desktop; nothing here is a real window");
|
|
backend::set_backend(Arc::new(FakeBackend::with_one_monitor()));
|
|
} else {
|
|
tracing::info!("connecting to KWin");
|
|
backend::set_backend(komorebi::kwin::connect()?);
|
|
}
|
|
|
|
#[cfg(feature = "deadlock_detection")]
|
|
detect_deadlocks();
|
|
|
|
let static_config = opts.config.map_or_else(
|
|
|| {
|
|
let komorebi_json = HOME_DIR.join("komorebi.json");
|
|
if komorebi_json.is_file() {
|
|
Option::from(komorebi_json)
|
|
} else {
|
|
None
|
|
}
|
|
},
|
|
Option::from,
|
|
);
|
|
|
|
let wm = if let Some(config) = &static_config {
|
|
tracing::info!(
|
|
"creating window manager from static configuration file: {}",
|
|
config.display()
|
|
);
|
|
|
|
Arc::new(Mutex::new(StaticConfig::preload(
|
|
config,
|
|
window_manager_event_listener::event_rx(),
|
|
None,
|
|
)?))
|
|
} else {
|
|
Arc::new(Mutex::new(WindowManager::new(
|
|
window_manager_event_listener::event_rx(),
|
|
None,
|
|
)?))
|
|
};
|
|
|
|
wm.lock().init()?;
|
|
|
|
if let Some(config) = &static_config {
|
|
StaticConfig::postload(config, &wm)?;
|
|
}
|
|
|
|
// the configuration may have given monitors more workspaces than they started with (and named
|
|
// them): the compositor needs a desktop for each
|
|
wm.lock().sync_workspace_desktops();
|
|
|
|
wm.lock().retile_all(false)?;
|
|
|
|
listen_for_commands(wm.clone());
|
|
listen_for_events(wm.clone());
|
|
|
|
let (quit_sender, quit_receiver) = std::sync::mpsc::channel();
|
|
ctrlc::set_handler(move || {
|
|
let _ = quit_sender.send(());
|
|
})
|
|
.expect("could not set ctrl-c handler");
|
|
|
|
// everything runs on its own threads; this one only waits to be told to stop
|
|
let _ = quit_receiver.recv();
|
|
tracing::info!("stop signal received");
|
|
|
|
{
|
|
let mut wm = wm.lock();
|
|
wm.restore_all_windows(false)?;
|
|
}
|
|
backend::backend().shutdown();
|
|
|
|
let sockets = komorebi::SUBSCRIPTION_SOCKETS.lock();
|
|
for path in (*sockets).values() {
|
|
if let Ok(stream) = UnixStream::connect(path) {
|
|
stream.shutdown(Shutdown::Both)?;
|
|
}
|
|
}
|
|
|
|
let socket = DATA_DIR.join("komorebi.sock");
|
|
let _ = std::fs::remove_file(socket);
|
|
|
|
std::process::exit(130);
|
|
}
|