Skip to content

Commit

Permalink
Fix deadlock on change workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Jun 29, 2024
1 parent cc06373 commit 7b36f73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
32 changes: 18 additions & 14 deletions src/background/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,24 @@ pub extern "system" fn win_event_hook(
pub fn register_win_hook() -> Result<()> {
log::trace!("Registering Windows and Virtual Desktop Hooks");

std::thread::spawn(move || unsafe {
SetWinEventHook(EVENT_MIN, EVENT_MAX, None, Some(win_event_hook), 0, 0, 0);

let mut msg: MSG = MSG::default();
loop {
if !GetMessageW(&mut msg, HWND(0), 0, 0).as_bool() {
log::info!("windows event processing shutdown");
break;
};
let _ = TranslateMessage(&msg);
DispatchMessageW(&msg);
std::thread::sleep(Duration::from_millis(10));
}
});
let stack_size = 5 * 1024 * 1024; // 5 MB
std::thread::Builder::new()
.name("win_event_hook".into())
.stack_size(stack_size)
.spawn(move || unsafe {
SetWinEventHook(EVENT_MIN, EVENT_MAX, None, Some(win_event_hook), 0, 0, 0);

let mut msg: MSG = MSG::default();
loop {
if !GetMessageW(&mut msg, HWND(0), 0, 0).as_bool() {
log::info!("windows event processing shutdown");
break;
};
let _ = TranslateMessage(&msg);
DispatchMessageW(&msg);
std::thread::sleep(Duration::from_millis(10));
}
})?;

// Todo search why virtual desktop events are not working on windows 10
if is_windows_11() {
Expand Down
17 changes: 6 additions & 11 deletions src/background/seelen_wm/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use windows::Win32::Foundation::HWND;

use crate::error_handler::Result;
use crate::seelen::SEELEN;
use crate::utils::sleep_millis;
use crate::utils::virtual_desktop::VirtualDesktopManager;
use crate::windows_api::WindowsApi;
use crate::{get_subcommands, trace_lock};
Expand Down Expand Up @@ -99,9 +98,9 @@ impl WindowManager {
Some(_) => {
self.pseudo_pause()?;
winvd::switch_desktop(index as u32)?;
sleep_millis(35); // to ensure avoid any artifacts
/* sleep_millis(35); // to ensure avoid any artifacts */
if let Some(next) = Self::get_next_by_order(HWND(0)) {
WindowsApi::force_set_foreground(next)?;
WindowsApi::async_force_set_foreground(next);
}
self.pseudo_resume()?;
}
Expand All @@ -116,14 +115,10 @@ impl WindowManager {
if self.is_managed(to_move) && !self.is_floating(to_move) {
self.emit_send_to_workspace(to_move, desktop.id())?;
}
let guid = desktop.guid();
std::thread::spawn(move || -> Result<()> {
winvd::move_window_to_desktop(guid, &to_move)?;
if let Some(next) = Self::get_next_by_order(to_move) {
WindowsApi::force_set_foreground(next)?;
}
Ok(())
});
winvd::move_window_to_desktop(desktop.guid(), &to_move)?;
if let Some(next) = Self::get_next_by_order(to_move) {
WindowsApi::async_force_set_foreground(next);
}
}
None => log::error!("Invalid workspace index: {}", index),
}
Expand Down
4 changes: 4 additions & 0 deletions src/background/windows_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ impl WindowsApi {
Ok(())
}

pub fn async_force_set_foreground(hwnd: HWND) {
std::thread::spawn(move || log_error!(Self::force_set_foreground(hwnd)));
}

pub fn force_set_foreground(hwnd: HWND) -> Result<()> {
Self::set_minimize_animation(false)?;

Expand Down

0 comments on commit 7b36f73

Please sign in to comment.