mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-19 15:18:40 +01:00
refactor(mac): use the shared command listener
Same change as the KDE revision: listen_for_commands and read_commands_uds call komorebi_transport::listen_for_commands and read_commands with NoPolicy (macOS sets no read timeouts), and WindowManager implements CommandTarget (clone of command_listener under the lock, try_lock_for on parking_lot, is_paused, process_command). #[tracing::instrument] stays on listen_for_commands. read_commands_uds keeps its name and signature for the unit test. Behaviour-preserving, not a literal text move; logs now use the target komorebi::transport (D29). 75 lines of loop code removed from process_command.rs. Verified: macOS fake-CC cargo check --locked --target aarch64-apple-darwin --workspace --all-targets: 0 errors. macOS unit tests cannot run on Linux.
This commit is contained in:
1 parent
d366c58efa
commit
866b284060
2 files changed
+36
-69
No files matched your search
Generated
+3
@@ -3310,7 +3310,10 @@ dependencies = [
|
|||||||
name = "komorebi-transport"
|
name = "komorebi-transport"
|
||||||
version = "0.1.42"
|
version = "0.1.42"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"color-eyre",
|
||||||
"komorebi-protocol",
|
"komorebi-protocol",
|
||||||
|
"tracing",
|
||||||
|
"uds_windows",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -59,17 +59,16 @@ use color_eyre::eyre;
|
|||||||
use color_eyre::eyre::Context;
|
use color_eyre::eyre::Context;
|
||||||
use color_eyre::eyre::OptionExt;
|
use color_eyre::eyre::OptionExt;
|
||||||
use komorebi_themes::colour::Rgb;
|
use komorebi_themes::colour::Rgb;
|
||||||
use komorebi_transport::allowed_while_paused;
|
use komorebi_transport::CommandTarget;
|
||||||
|
use komorebi_transport::NoPolicy;
|
||||||
use objc2_core_foundation::CFDictionary;
|
use objc2_core_foundation::CFDictionary;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::BufRead;
|
|
||||||
use std::io::BufReader;
|
|
||||||
use std::num::NonZeroUsize;
|
use std::num::NonZeroUsize;
|
||||||
|
use std::os::unix::net::UnixListener;
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use std::str::FromStr;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -77,43 +76,37 @@ use version::build;
|
|||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
pub fn listen_for_commands(wm: Arc<Mutex<WindowManager>>) {
|
pub fn listen_for_commands(wm: Arc<Mutex<WindowManager>>) {
|
||||||
std::thread::spawn(move || {
|
komorebi_transport::listen_for_commands::<WindowManager, _>(wm, NoPolicy);
|
||||||
loop {
|
}
|
||||||
let wm = wm.clone();
|
|
||||||
|
|
||||||
let _ = std::thread::spawn(move || {
|
/// What the generic command listener needs of the window manager
|
||||||
let listener = wm
|
impl CommandTarget for WindowManager {
|
||||||
.lock()
|
type Listener = UnixListener;
|
||||||
.command_listener
|
type Shared = Arc<Mutex<WindowManager>>;
|
||||||
.try_clone()
|
|
||||||
.expect("could not clone unix listener");
|
|
||||||
|
|
||||||
tracing::info!("listening on komorebi.sock");
|
fn command_listener(shared: &Self::Shared) -> std::io::Result<UnixListener> {
|
||||||
for client in listener.incoming() {
|
shared.lock().command_listener.try_clone()
|
||||||
match client {
|
}
|
||||||
Ok(stream) => {
|
|
||||||
let wm_clone = wm.clone();
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
match read_commands_uds(&wm_clone, stream) {
|
|
||||||
Ok(()) => {}
|
|
||||||
Err(error) => {
|
|
||||||
tracing::error!("{error}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Err(error) => {
|
|
||||||
tracing::error!("failed to get unix stream {}", error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.join();
|
|
||||||
|
|
||||||
tracing::error!("restarting failed thread");
|
fn try_lock_for<R>(
|
||||||
}
|
shared: &Self::Shared,
|
||||||
});
|
timeout: Duration,
|
||||||
|
f: impl FnOnce(&mut Self) -> R,
|
||||||
|
) -> Option<R> {
|
||||||
|
shared.try_lock_for(timeout).map(|mut wm| f(&mut wm))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_paused(&self) -> bool {
|
||||||
|
self.is_paused
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_command(
|
||||||
|
&mut self,
|
||||||
|
message: SocketMessage,
|
||||||
|
reply: &mut UnixStream,
|
||||||
|
) -> eyre::Result<()> {
|
||||||
|
WindowManager::process_command(self, message, reply)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowManager {
|
impl WindowManager {
|
||||||
@@ -1916,38 +1909,9 @@ impl WindowManager {
|
|||||||
|
|
||||||
pub fn read_commands_uds(
|
pub fn read_commands_uds(
|
||||||
wm: &Arc<Mutex<WindowManager>>,
|
wm: &Arc<Mutex<WindowManager>>,
|
||||||
mut stream: UnixStream,
|
stream: UnixStream,
|
||||||
) -> eyre::Result<()> {
|
) -> eyre::Result<()> {
|
||||||
let reader = BufReader::new(stream.try_clone()?);
|
komorebi_transport::read_commands::<WindowManager, _>(wm, stream, &NoPolicy)
|
||||||
// TODO(raggi): while this processes more than one command, if there are
|
|
||||||
// replies there is no clearly defined protocol for framing yet - it's
|
|
||||||
// perhaps whole-json objects for now, but termination is signalled by
|
|
||||||
// socket shutdown.
|
|
||||||
for line in reader.lines() {
|
|
||||||
let message = SocketMessage::from_str(&line?)?;
|
|
||||||
|
|
||||||
match wm.try_lock_for(Duration::from_secs(1)) {
|
|
||||||
None => {
|
|
||||||
tracing::warn!(
|
|
||||||
"could not acquire window manager lock, not processing message: {message}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Some(mut wm) => {
|
|
||||||
if wm.is_paused {
|
|
||||||
return if allowed_while_paused(&message) {
|
|
||||||
Ok(wm.process_command(message, &mut stream)?)
|
|
||||||
} else {
|
|
||||||
tracing::trace!("ignoring while paused");
|
|
||||||
Ok(())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
wm.process_command(message.clone(), &mut stream)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in new issue
Block a user