Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wm): add tcp event notifications #261

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion komorebi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ pub struct Notification {
pub state: State,
}

pub fn notify_subscribers(notification: &str) -> Result<()> {
pub fn send_notification(notification: &str) -> Result<()> {
notify_subscribers(notification)?;
notify_tcp_clients(notification)
}

fn notify_subscribers(notification: &str) -> Result<()> {
let mut stale_subscriptions = vec![];
let mut subscriptions = SUBSCRIPTION_PIPES.lock();
for (subscriber, pipe) in subscriptions.iter_mut() {
Expand Down Expand Up @@ -362,6 +367,26 @@ pub fn notify_subscribers(notification: &str) -> Result<()> {
Ok(())
}

fn notify_tcp_clients(notification: &str) -> Result<()> {
let mut tcp_clients = TCP_CONNECTIONS.lock();
for (addrs, stream) in tcp_clients.iter_mut() {
match stream.write(notification.as_bytes()) {
Ok(_) => {
tracing::debug!("pushed notification to the tcp_client: {}", addrs);
}
Err(error) => {
tracing::error!(
"failed to push notification to the tcp_client: {}, got error: {}",
addrs,
error
);
}
}
}

Ok(())
}

#[cfg(feature = "deadlock_detection")]
#[tracing::instrument]
fn detect_deadlocks() {
Expand Down
10 changes: 7 additions & 3 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use std::io::Write;
use std::mem::drop;
use std::net::TcpListener;
use std::net::TcpStream;
use std::num::NonZeroUsize;
Expand Down Expand Up @@ -35,7 +36,7 @@ use komorebi_core::WindowKind;

use crate::border::Border;
use crate::current_virtual_desktop;
use crate::notify_subscribers;
use crate::send_notification;
use crate::window::Window;
use crate::window_manager;
use crate::window_manager::WindowManager;
Expand Down Expand Up @@ -112,6 +113,9 @@ pub fn listen_for_commands_tcp(wm: Arc<Mutex<WindowManager>>, port: usize) {
stream.try_clone().expect("stream should be cloneable"),
);

// This has to be done to be able to send notifications to the tcp_connections
drop(connections);

tracing::info!("listening for incoming tcp messages from {}", &addr);

match read_commands_tcp(&wm, &mut stream, &addr) {
Expand Down Expand Up @@ -932,7 +936,7 @@ pub fn read_commands_uds(wm: &Arc<Mutex<WindowManager>>, stream: UnixStream) ->
}

wm.process_command(message.clone())?;
notify_subscribers(&serde_json::to_string(&Notification {
send_notification(&serde_json::to_string(&Notification {
event: NotificationEvent::Socket(message.clone()),
state: wm.as_ref().into(),
})?)?;
Expand Down Expand Up @@ -984,7 +988,7 @@ pub fn read_commands_tcp(
}

wm.process_command(message.clone())?;
notify_subscribers(&serde_json::to_string(&Notification {
send_notification(&serde_json::to_string(&Notification {
event: NotificationEvent::Socket(message.clone()),
state: wm.as_ref().into(),
})?)?;
Expand Down
4 changes: 2 additions & 2 deletions komorebi/src/process_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use komorebi_core::WindowContainerBehaviour;

use crate::border::Border;
use crate::current_virtual_desktop;
use crate::notify_subscribers;
use crate::send_notification;
use crate::window_manager::WindowManager;
use crate::window_manager_event::WindowManagerEvent;
use crate::windows_api::WindowsApi;
Expand Down Expand Up @@ -596,7 +596,7 @@ impl WindowManager {
.open(hwnd_json)?;

serde_json::to_writer_pretty(&file, &known_hwnds)?;
notify_subscribers(&serde_json::to_string(&Notification {
send_notification(&serde_json::to_string(&Notification {
event: NotificationEvent::WindowManager(*event),
state: self.as_ref().into(),
})?)?;
Expand Down