Skip to content

Commit

Permalink
fix(wm): restart cmd processing thread on fail
Browse files Browse the repository at this point in the history
This commit makes some changes to process_command and process_event to
bring them more in line with newer modules, while primarily making sure
that the thread spawned in process_command will be restarted if
necessary.

In the future I might pull out the process_command and process_event fns
from the WindowManager struct and have them take an
Arc<Mutex<WindowManager>> instead which acquires the lock on every call
like the reconciliators and border_manager do.
  • Loading branch information
LGUG2Z committed May 15, 2024
1 parent 54905d0 commit 2e454a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
2 changes: 0 additions & 2 deletions komorebi/src/border_manager/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use komorebi_core::Rect;

use std::sync::atomic::Ordering;
use std::sync::mpsc;
use std::time::Duration;
use windows::core::PCWSTR;
use windows::Win32::Foundation::BOOL;
use windows::Win32::Foundation::COLORREF;
Expand Down Expand Up @@ -104,7 +103,6 @@ impl Border {
while GetMessageW(&mut message, HWND(hwnd), 0, 0).into() {
TranslateMessage(&message);
DispatchMessageW(&message);
std::thread::sleep(Duration::from_millis(10));
}
}

Expand Down
29 changes: 20 additions & 9 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,34 @@ use crate::WORKSPACE_RULES;

#[tracing::instrument]
pub fn listen_for_commands(wm: Arc<Mutex<WindowManager>>) {
let listener = wm
.lock()
.command_listener
.try_clone()
.expect("could not clone unix listener");
std::thread::spawn(move || loop {
let listener = wm
.lock()
.command_listener
.try_clone()
.expect("could not clone unix listener");

std::thread::spawn(move || {
tracing::info!("listening on komorebi.sock");

// client is unique for every komorebic command
for client in listener.incoming() {
match client {
Ok(stream) => match read_commands_uds(&wm, stream) {
Ok(()) => {}
Err(error) => tracing::error!("{}", error),
Err(error) => {
if cfg!(debug_assertions) {
tracing::error!("{:?}", error)
} else {
tracing::error!("{}", error)
}
}
},
Err(error) => {
tracing::error!("{}", error);
break;
if cfg!(debug_assertions) {
tracing::error!("{:?}", error)
} else {
tracing::error!("{}", error)
}
}
}
}
Expand Down
23 changes: 10 additions & 13 deletions komorebi/src/process_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ use crate::TRAY_AND_MULTI_WINDOW_IDENTIFIERS;

#[tracing::instrument]
pub fn listen_for_events(wm: Arc<Mutex<WindowManager>>) {
let receiver = wm.lock().incoming_events.clone();

std::thread::spawn(move || {
std::thread::spawn(move || loop {
tracing::info!("listening");
loop {
if let Ok(event) = receiver.recv() {
match wm.lock().process_event(event) {
Ok(()) => {}
Err(error) => {
if cfg!(debug_assertions) {
tracing::error!("{:?}", error)
} else {
tracing::error!("{}", error)
}
let receiver = wm.lock().incoming_events.clone();
for event in receiver {
match wm.lock().process_event(event) {
Ok(()) => {}
Err(error) => {
if cfg!(debug_assertions) {
tracing::error!("{:?}", error)
} else {
tracing::error!("{}", error)
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions komorebi/src/stackbar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::VecDeque;
use std::sync::atomic::Ordering;
use std::time::Duration;

use color_eyre::eyre::Result;
use schemars::JsonSchema;
Expand Down Expand Up @@ -163,7 +162,6 @@ impl Stackbar {
while GetMessageW(&mut msg, hwnd, 0, 0).into() {
TranslateMessage(&msg);
DispatchMessageW(&msg);
std::thread::sleep(Duration::from_millis(10));
}
}

Expand Down

0 comments on commit 2e454a3

Please sign in to comment.