mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-30 20:24:09 +01:00
Pure renames, no content changes. This revision does not build on its own; the next one routes the workspace to the new paths.
455 lines
17 KiB
Rust
455 lines
17 KiB
Rust
#![warn(clippy::all)]
|
|
|
|
#[macro_use]
|
|
extern crate komorebi_collections;
|
|
|
|
pub mod animation;
|
|
pub mod border_manager;
|
|
pub mod cloaked;
|
|
pub mod com;
|
|
pub mod daemon;
|
|
pub use komorebi_collections::ring;
|
|
pub mod container;
|
|
pub mod core;
|
|
pub mod focus_manager;
|
|
pub mod live_resize;
|
|
pub use komorebi_collections::Lockable;
|
|
pub use komorebi_collections::lockable_sequence;
|
|
pub mod heartbeat;
|
|
pub mod lock_holder;
|
|
pub mod monitor;
|
|
pub mod monitor_reconciliator;
|
|
pub mod pan;
|
|
pub mod process_command;
|
|
pub mod process_event;
|
|
pub mod process_movement;
|
|
pub mod reaper;
|
|
pub mod scrolling;
|
|
pub mod set_window_position;
|
|
pub mod settings;
|
|
pub mod splash;
|
|
pub mod stackbar_manager;
|
|
pub mod state;
|
|
pub mod static_config;
|
|
pub mod styles;
|
|
pub mod theme_manager;
|
|
pub mod transparency_manager;
|
|
pub mod window;
|
|
pub mod window_manager;
|
|
pub mod window_manager_event;
|
|
pub mod windows_api;
|
|
pub mod windows_backend;
|
|
pub mod windows_callbacks;
|
|
pub mod winevent;
|
|
pub mod winevent_listener;
|
|
pub mod workspace;
|
|
|
|
use lazy_static::lazy_static;
|
|
use monitor_reconciliator::MonitorNotification;
|
|
use std::collections::HashMap;
|
|
use std::collections::VecDeque;
|
|
use std::fs::File;
|
|
use std::io::Write;
|
|
use std::net::TcpStream;
|
|
use std::path::PathBuf;
|
|
use std::process::Command;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::AtomicBool;
|
|
use std::sync::atomic::AtomicU32;
|
|
use std::sync::atomic::AtomicU64;
|
|
use std::sync::atomic::Ordering;
|
|
|
|
pub use core::*;
|
|
pub use komorebi_themes::colour::*;
|
|
pub use process_command::*;
|
|
pub use process_event::*;
|
|
pub use static_config::*;
|
|
pub use win32_display_data;
|
|
pub use window::*;
|
|
pub use window_manager::*;
|
|
pub use window_manager_event::*;
|
|
pub use windows_api::WindowsApi;
|
|
pub use windows_api::*;
|
|
|
|
use crate::core::config_generation::IdWithIdentifier;
|
|
use crate::core::config_generation::MatchingRule;
|
|
use crate::core::config_generation::MatchingStrategy;
|
|
use crate::core::config_generation::WorkspaceMatchingRule;
|
|
use color_eyre::eyre;
|
|
use os_info::Version;
|
|
use parking_lot::Mutex;
|
|
use parking_lot::RwLock;
|
|
use regex::Regex;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use state::State;
|
|
use uds_windows::UnixStream;
|
|
use which::which;
|
|
use winreg::RegKey;
|
|
use winreg::enums::HKEY_CURRENT_USER;
|
|
|
|
// LOCK ORDER of the rule lists. A thread that holds one of these and takes another must take them
|
|
// in this order (a thread that takes two in another order can wait for one that holds what it
|
|
// needs, and both wait for ever):
|
|
//
|
|
// FLOATING_APPLICATIONS, SESSION_FLOATING_APPLICATIONS, TRANSPARENCY_BLACKLIST,
|
|
// IGNORE_IDENTIFIERS, MANAGE_IDENTIFIERS, LAYERED_WHITELIST, TRAY_AND_MULTI_WINDOW_IDENTIFIERS,
|
|
// OBJECT_NAME_CHANGE_ON_LAUNCH, OBJECT_NAME_CHANGE_TITLE_IGNORE_LIST,
|
|
// SLOW_APPLICATION_IDENTIFIERS, NO_TITLEBAR, WORKSPACE_MATCHING_RULES, REGEX_IDENTIFIERS
|
|
//
|
|
// REGEX_IDENTIFIERS is last: every rule check holds the list it matches against and then the
|
|
// compiled regexes. PERMAIGNORE_CLASSES and WSL2_UI_PROCESSES are leaves (nothing is taken while
|
|
// they are held). Places that nest the others: window.rs (window_is_eligible), static_config.rs
|
|
// (apply_globals),
|
|
// state.rs (the State snapshot), process_command.rs (session float rules), process_event.rs,
|
|
// window_manager_event.rs, transparency_manager.rs, window_manager.rs. The same rule applies to
|
|
// any new place: lock in this order, or lock one at a time.
|
|
|
|
lazy_static! {
|
|
static ref HIDDEN_HWNDS: Arc<Mutex<Vec<isize>>> = Arc::new(Mutex::new(vec![]));
|
|
static ref LAYERED_WHITELIST: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("steam.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
]));
|
|
static ref TRAY_AND_MULTI_WINDOW_IDENTIFIERS: Arc<Mutex<Vec<MatchingRule>>> =
|
|
Arc::new(Mutex::new(vec![
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("explorer.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("firefox.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("chrome.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("idea64.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("ApplicationFrameHost.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("steam.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
})
|
|
]));
|
|
static ref OBJECT_NAME_CHANGE_ON_LAUNCH: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("firefox.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("idea64.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
]));
|
|
static ref OBJECT_NAME_CHANGE_TITLE_IGNORE_LIST: Arc<Mutex<Vec<Regex>>> = Arc::new(Mutex::new(Vec::new()));
|
|
static ref TRANSPARENCY_BLACKLIST: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(Vec::new()));
|
|
static ref MONITOR_INDEX_PREFERENCES: Arc<Mutex<HashMap<usize, Rect>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
static ref WORKSPACE_MATCHING_RULES: Arc<Mutex<Vec<WorkspaceMatchingRule>>> =
|
|
Arc::new(Mutex::new(Vec::new()));
|
|
static ref REGEX_IDENTIFIERS: Arc<Mutex<HashMap<String, Regex>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
static ref MANAGE_IDENTIFIERS: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![]));
|
|
static ref IGNORE_IDENTIFIERS: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![
|
|
// mstsc.exe creates these on Windows 11 when a WSL process is launched
|
|
// https://github.com/LGUG2Z/komorebi/issues/74
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Class,
|
|
id: String::from("OPContainerClass"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Class,
|
|
id: String::from("IHWindowClass"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("komorebi-bar.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
})
|
|
]));
|
|
static ref SESSION_FLOATING_APPLICATIONS: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(Vec::new()));
|
|
static ref FLOATING_APPLICATIONS: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("komorebi-shortcuts.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
})
|
|
|
|
]));
|
|
static ref PERMAIGNORE_CLASSES: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![
|
|
"Chrome_RenderWidgetHostHWND".to_string(),
|
|
]));
|
|
static ref WSL2_UI_PROCESSES: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![
|
|
"X410.exe".to_string(),
|
|
"vcxsrv.exe".to_string(),
|
|
]));
|
|
static ref SLOW_APPLICATION_IDENTIFIERS: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![
|
|
MatchingRule::Simple(IdWithIdentifier {
|
|
kind: ApplicationIdentifier::Exe,
|
|
id: String::from("firefox.exe"),
|
|
matching_strategy: Option::from(MatchingStrategy::Equals),
|
|
}),
|
|
]));
|
|
static ref DUPLICATE_MONITOR_SERIAL_IDS: Arc<RwLock<Vec<String>>> =
|
|
Arc::new(RwLock::new(Vec::new()));
|
|
static ref SUBSCRIPTION_PIPES: Arc<Mutex<HashMap<String, File>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
pub static ref SUBSCRIPTION_SOCKETS: Arc<Mutex<HashMap<String, PathBuf>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
pub static ref SUBSCRIPTION_SOCKET_OPTIONS: Arc<Mutex<HashMap<String, SubscribeOptions>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
static ref TCP_CONNECTIONS: Arc<Mutex<HashMap<String, TcpStream>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
static ref HIDING_BEHAVIOUR: Arc<Mutex<HidingBehaviour>> =
|
|
Arc::new(Mutex::new(HidingBehaviour::Cloak));
|
|
pub static ref HOME_DIR: PathBuf = {
|
|
std::env::var("KOMOREBI_CONFIG_HOME").map_or_else(|_| dirs::home_dir().expect("there is no home directory"), |home_path| {
|
|
let home = home_path.replace_env();
|
|
|
|
assert!(
|
|
home.is_dir(),
|
|
"$Env:KOMOREBI_CONFIG_HOME is set to '{home_path}', which is not a valid directory"
|
|
);
|
|
|
|
|
|
home
|
|
})
|
|
};
|
|
pub static ref DATA_DIR: PathBuf = dirs::data_local_dir().expect("there is no local data directory").join("komorebi");
|
|
pub static ref AHK_EXE: String = {
|
|
let mut ahk: String = String::from("autohotkey.exe");
|
|
|
|
if let Ok(komorebi_ahk_exe) = std::env::var("KOMOREBI_AHK_EXE")
|
|
&& which(&komorebi_ahk_exe).is_ok() {
|
|
ahk = komorebi_ahk_exe;
|
|
}
|
|
|
|
ahk
|
|
};
|
|
static ref WINDOWS_11: bool = {
|
|
matches!(
|
|
os_info::get().version(),
|
|
Version::Semantic(_, _, x) if x >= &22000
|
|
)
|
|
};
|
|
|
|
// Use app-specific titlebar removal options where possible
|
|
// eg. Windows Terminal, IntelliJ IDEA, Firefox
|
|
static ref NO_TITLEBAR: Arc<Mutex<Vec<MatchingRule>>> = Arc::new(Mutex::new(vec![]));
|
|
|
|
static ref WINDOWS_BY_BAR_HWNDS: Arc<Mutex<HashMap<isize, VecDeque<isize>>>> =
|
|
Arc::new(Mutex::new(HashMap::new()));
|
|
|
|
|
|
static ref CURRENT_VIRTUAL_DESKTOP: Arc<Mutex<Option<Vec<u8>>>> = Arc::new(Mutex::new(None));
|
|
|
|
}
|
|
|
|
pub static DEFAULT_RESIZE_DELTA: i32 = komorebi_settings::shared::DEFAULT_RESIZE_DELTA;
|
|
|
|
pub static DEFAULT_MOUSE_FOLLOWS_FOCUS: bool =
|
|
komorebi_settings::shared::DEFAULT_MOUSE_FOLLOWS_FOCUS;
|
|
/// Counts the commands the window manager has processed. An answer computed from the rule lists
|
|
/// outside the lock (see `process_event::ManageCheck`) is only used if no command, which is what
|
|
/// changes those lists, has run since.
|
|
pub static COMMAND_GENERATION: AtomicU64 = AtomicU64::new(0);
|
|
pub static INITIAL_CONFIGURATION_LOADED: AtomicBool = AtomicBool::new(false);
|
|
pub static CUSTOM_FFM: AtomicBool = AtomicBool::new(false);
|
|
pub static SESSION_ID: AtomicU32 = AtomicU32::new(0);
|
|
|
|
pub static REMOVE_TITLEBARS: AtomicBool = AtomicBool::new(false);
|
|
|
|
pub static SLOW_APPLICATION_COMPENSATION_TIME: AtomicU64 = AtomicU64::new(20);
|
|
|
|
shadow_rs::shadow!(build);
|
|
|
|
pub const PUBLIC_KEY: [u8; 32] = [
|
|
0x5a, 0x69, 0x4a, 0xe1, 0x3c, 0x4b, 0xc8, 0x4e, 0xc3, 0x79, 0x0f, 0xab, 0x27, 0x6b, 0x7e, 0xdd,
|
|
0x6b, 0x39, 0x6f, 0xa2, 0xc3, 0x9f, 0x3d, 0x48, 0xf2, 0x72, 0x56, 0x41, 0x1b, 0xc8, 0x08, 0xdb,
|
|
];
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
|
|
pub struct License {
|
|
#[serde(rename = "hasValidSubscription")]
|
|
pub has_valid_subscription: bool,
|
|
pub timestamp: i64,
|
|
#[serde(rename = "currentEndPeriod")]
|
|
pub current_end_period: Option<i64>,
|
|
pub signature: String,
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn current_virtual_desktop() -> Option<Vec<u8>> {
|
|
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
|
|
|
// This is the path on Windows 10
|
|
let mut current = hkcu
|
|
.open_subkey(format!(
|
|
r#"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\{}\VirtualDesktops"#,
|
|
SESSION_ID.load(Ordering::SeqCst)
|
|
))
|
|
.ok()
|
|
.and_then(
|
|
|desktops| match desktops.get_raw_value("CurrentVirtualDesktop") {
|
|
Ok(current) => Option::from(current.bytes),
|
|
Err(_) => None,
|
|
},
|
|
);
|
|
|
|
// This is the path on Windows 11
|
|
if current.is_none() {
|
|
current = hkcu
|
|
.open_subkey(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops")
|
|
.ok()
|
|
.and_then(
|
|
|desktops| match desktops.get_raw_value("CurrentVirtualDesktop") {
|
|
Ok(current) => Option::from(current.bytes),
|
|
Err(_) => None,
|
|
},
|
|
);
|
|
}
|
|
|
|
// For Win10 users that do not use virtual desktops, the CurrentVirtualDesktop value will not
|
|
// exist until one has been created in the task view
|
|
|
|
// The registry value will also not exist on user login if virtual desktops have been created
|
|
// but the task view has not been initiated
|
|
|
|
// In both of these cases, we return None, and the virtual desktop validation will never run. In
|
|
// the latter case, if the user desires this validation after initiating the task view, komorebi
|
|
// should be restarted, and then when this // fn runs again for the first time, it will pick up
|
|
// the value of CurrentVirtualDesktop and validate against it accordingly
|
|
current.map(|current| current.to_vec())
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
#[serde(untagged)]
|
|
pub enum NotificationEvent {
|
|
WindowManager(WindowManagerEvent),
|
|
Socket(SocketMessage),
|
|
Monitor(MonitorNotification),
|
|
VirtualDesktop(VirtualDesktopNotification),
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
pub enum VirtualDesktopNotification {
|
|
EnteredAssociatedVirtualDesktop,
|
|
LeftAssociatedVirtualDesktop,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
|
pub struct Notification {
|
|
pub event: NotificationEvent,
|
|
pub state: State,
|
|
}
|
|
|
|
pub fn notify_subscribers(
|
|
notification: Notification,
|
|
state_has_been_modified: bool,
|
|
) -> eyre::Result<()> {
|
|
let is_override_event = matches!(
|
|
notification.event,
|
|
NotificationEvent::Socket(SocketMessage::AddSubscriberSocket(_))
|
|
| NotificationEvent::Socket(SocketMessage::AddSubscriberSocketWithOptions(_, _))
|
|
| NotificationEvent::Socket(SocketMessage::Theme(_))
|
|
| NotificationEvent::Socket(SocketMessage::ReloadStaticConfiguration(_))
|
|
| NotificationEvent::WindowManager(WindowManagerEvent::TitleUpdate(_, _))
|
|
| NotificationEvent::WindowManager(WindowManagerEvent::Show(_, _))
|
|
| NotificationEvent::WindowManager(WindowManagerEvent::Uncloak(_, _))
|
|
);
|
|
|
|
let notification = &serde_json::to_string(¬ification)?;
|
|
let mut sockets = SUBSCRIPTION_SOCKETS.lock();
|
|
let options = SUBSCRIPTION_SOCKET_OPTIONS.lock();
|
|
|
|
komorebi_transport::notify_sockets(
|
|
notification,
|
|
&mut sockets,
|
|
&options,
|
|
state_has_been_modified || is_override_event,
|
|
&DATA_DIR,
|
|
|path| UnixStream::connect(path),
|
|
)?;
|
|
|
|
let mut stale_pipes = vec![];
|
|
let mut pipes = SUBSCRIPTION_PIPES.lock();
|
|
for (subscriber, pipe) in &mut *pipes {
|
|
match writeln!(pipe, "{notification}") {
|
|
Ok(()) => {
|
|
tracing::debug!("pushed notification to subscriber: {subscriber}");
|
|
}
|
|
Err(error) => {
|
|
// ERROR_FILE_NOT_FOUND
|
|
// 2 (0x2)
|
|
// The system cannot find the file specified.
|
|
|
|
// ERROR_NO_DATA
|
|
// 232 (0xE8)
|
|
// The pipe is being closed.
|
|
|
|
// Remove the subscription; the process will have to subscribe again
|
|
if let Some(2 | 232) = error.raw_os_error() {
|
|
stale_pipes.push(subscriber.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for subscriber in stale_pipes {
|
|
tracing::warn!("removing stale subscription: {}", subscriber);
|
|
pipes.remove(&subscriber);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load_configuration() -> eyre::Result<()> {
|
|
let config_pwsh = HOME_DIR.join("komorebi.ps1");
|
|
let config_ahk = HOME_DIR.join("komorebi.ahk");
|
|
|
|
if config_pwsh.exists() {
|
|
let powershell_exe = if which("pwsh.exe").is_ok() {
|
|
"pwsh.exe"
|
|
} else {
|
|
"powershell.exe"
|
|
};
|
|
|
|
tracing::info!("loading configuration file: {}", config_pwsh.display());
|
|
|
|
Command::new(powershell_exe)
|
|
.arg(config_pwsh.as_os_str())
|
|
.output()?;
|
|
} else if config_ahk.exists() && which(&*AHK_EXE).is_ok() {
|
|
tracing::info!("loading configuration file: {}", config_ahk.display());
|
|
|
|
Command::new(&*AHK_EXE)
|
|
.arg(config_ahk.as_os_str())
|
|
.output()?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|