Skip to content

Commit

Permalink
fix(wm): detach thread inputs explicitly
Browse files Browse the repository at this point in the history
This commit ensures that any calls to AttachThreadInput which are used
to allow the focusing or raising of a window are paired with a closing
call to detach the thread input.

Although undocumented, it seems that when attaching the input thread of
a window to an admin/sudo process, this prevents that window from
handling inputs from any unelevated processes (including regular
keyboard and mouse inputs), until the input thread is detached.

fix #86
  • Loading branch information
LGUG2Z committed Aug 27, 2022
1 parent 7c41460 commit 5a0ba4c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions komorebi.generated.ahk
Expand Up @@ -311,6 +311,11 @@ Run, komorebic.exe float-rule exe "TranslucentTB.exe", , Hide
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
Run, komorebic.exe identify-tray-application exe "TranslucentTB.exe", , Hide

; Unreal Editor
Run, komorebic.exe identify-border-overflow-application exe "UnrealEditor.exe", , Hide
; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line
Run, komorebic.exe identify-tray-application exe "UnrealEditor.exe", , Hide

; Visual Studio Code
Run, komorebic.exe identify-border-overflow-application exe "Code.exe", , Hide

Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/process_event.rs
Expand Up @@ -135,7 +135,7 @@ impl WindowManager {

match event {
WindowManagerEvent::Raise(window) => {
window.raise()?;
window.raise();
self.has_pending_raise_op = false;
}
WindowManagerEvent::Destroy(_, window) | WindowManagerEvent::Unmanage(window) => {
Expand Down
48 changes: 44 additions & 4 deletions komorebi/src/window.rs
Expand Up @@ -187,25 +187,55 @@ impl Window {
WindowsApi::unmaximize_window(self.hwnd());
}

pub fn raise(self) -> Result<()> {
pub fn raise(self) {
// Attach komorebi thread to Window thread
let (_, window_thread_id) = WindowsApi::window_thread_process_id(self.hwnd());
let current_thread_id = WindowsApi::current_thread_id();
WindowsApi::attach_thread_input(current_thread_id, window_thread_id, true)?;

// This can be allowed to fail if a window doesn't have a message queue or if a journal record
// hook has been installed
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-attachthreadinput#remarks
match WindowsApi::attach_thread_input(current_thread_id, window_thread_id, true) {
Ok(()) => {}
Err(error) => {
tracing::error!(
"could not attach to window thread input processing mechanism, but continuing execution of raise(): {}",
error
);
}
};

// Raise Window to foreground
match WindowsApi::set_foreground_window(self.hwnd()) {
Ok(_) => {}
Err(error) => {
tracing::error!(
"could not set as foreground window, but continuing execution of focus(): {}",
"could not set as foreground window, but continuing execution of raise(): {}",
error
);
}
};

// This isn't really needed when the above command works as expected via AHK
WindowsApi::set_focus(self.hwnd())
match WindowsApi::set_focus(self.hwnd()) {
Ok(_) => {}
Err(error) => {
tracing::error!(
"could not set focus, but continuing execution of raise(): {}",
error
);
}
};

match WindowsApi::attach_thread_input(current_thread_id, window_thread_id, false) {
Ok(()) => {}
Err(error) => {
tracing::error!(
"could not detach from window thread input processing mechanism, but continuing execution of raise(): {}",
error
);
}
};
}

pub fn focus(self, mouse_follows_focus: bool) -> Result<()> {
Expand Down Expand Up @@ -253,6 +283,16 @@ impl Window {
}
};

match WindowsApi::attach_thread_input(current_thread_id, window_thread_id, false) {
Ok(()) => {}
Err(error) => {
tracing::error!(
"could not detach from window thread input processing mechanism, but continuing execution of focus(): {}",
error
);
}
};

Ok(())
}

Expand Down

0 comments on commit 5a0ba4c

Please sign in to comment.