Skip to content

Commit

Permalink
feat(wm): allow valid attach_thread_input failures
Browse files Browse the repository at this point in the history
This commit allows the Window.focus() fn to continue execution if
AttachThreadInput fails, as there are valid situations in which this
might fail, but the focusing of the window may/should still succeed.

fix #156
  • Loading branch information
LGUG2Z committed Jun 12, 2022
1 parent 70be6f4 commit 092e36b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
14 changes: 13 additions & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,19 @@ impl Window {
// 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 focus(): {}",
error
);
}
};

// Raise Window to foreground
match WindowsApi::set_foreground_window(self.hwnd()) {
Expand Down
48 changes: 24 additions & 24 deletions komorebi/src/window_manager_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,56 @@ pub enum WindowManagerEvent {
impl Display for WindowManagerEvent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
WindowManagerEvent::Manage(window) => {
Self::Manage(window) => {
write!(f, "Manage (Window: {})", window)
}
WindowManagerEvent::Unmanage(window) => {
Self::Unmanage(window) => {
write!(f, "Unmanage (Window: {})", window)
}
WindowManagerEvent::Destroy(winevent, window) => {
Self::Destroy(winevent, window) => {
write!(f, "Destroy (WinEvent: {}, Window: {})", winevent, window)
}
WindowManagerEvent::FocusChange(winevent, window) => {
Self::FocusChange(winevent, window) => {
write!(
f,
"FocusChange (WinEvent: {}, Window: {})",
winevent, window
)
}
WindowManagerEvent::Hide(winevent, window) => {
Self::Hide(winevent, window) => {
write!(f, "Hide (WinEvent: {}, Window: {})", winevent, window)
}
WindowManagerEvent::Minimize(winevent, window) => {
Self::Minimize(winevent, window) => {
write!(f, "Minimize (WinEvent: {}, Window: {})", winevent, window)
}
WindowManagerEvent::Show(winevent, window) => {
Self::Show(winevent, window) => {
write!(f, "Show (WinEvent: {}, Window: {})", winevent, window)
}
WindowManagerEvent::MoveResizeStart(winevent, window) => {
Self::MoveResizeStart(winevent, window) => {
write!(
f,
"MoveResizeStart (WinEvent: {}, Window: {})",
winevent, window
)
}
WindowManagerEvent::MoveResizeEnd(winevent, window) => {
Self::MoveResizeEnd(winevent, window) => {
write!(
f,
"MoveResizeEnd (WinEvent: {}, Window: {})",
winevent, window
)
}
WindowManagerEvent::MouseCapture(winevent, window) => {
Self::MouseCapture(winevent, window) => {
write!(
f,
"MouseCapture (WinEvent: {}, Window: {})",
winevent, window
)
}
WindowManagerEvent::Raise(window) => {
Self::Raise(window) => {
write!(f, "Raise (Window: {})", window)
}
WindowManagerEvent::MonitorPoll(winevent, window) => {
Self::MonitorPoll(winevent, window) => {
write!(
f,
"MonitorPoll (WinEvent: {}, Window: {})",
Expand All @@ -91,18 +91,18 @@ impl Display for WindowManagerEvent {
impl WindowManagerEvent {
pub const fn window(self) -> Window {
match self {
WindowManagerEvent::Destroy(_, window)
| WindowManagerEvent::FocusChange(_, window)
| WindowManagerEvent::Hide(_, window)
| WindowManagerEvent::Minimize(_, window)
| WindowManagerEvent::Show(_, window)
| WindowManagerEvent::MoveResizeStart(_, window)
| WindowManagerEvent::MoveResizeEnd(_, window)
| WindowManagerEvent::MouseCapture(_, window)
| WindowManagerEvent::MonitorPoll(_, window)
| WindowManagerEvent::Raise(window)
| WindowManagerEvent::Manage(window)
| WindowManagerEvent::Unmanage(window) => window,
Self::Destroy(_, window)
| Self::FocusChange(_, window)
| Self::Hide(_, window)
| Self::Minimize(_, window)
| Self::Show(_, window)
| Self::MoveResizeStart(_, window)
| Self::MoveResizeEnd(_, window)
| Self::MouseCapture(_, window)
| Self::MonitorPoll(_, window)
| Self::Raise(window)
| Self::Manage(window)
| Self::Unmanage(window) => window,
}
}

Expand Down
7 changes: 4 additions & 3 deletions komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ pub fn send_message(bytes: &[u8]) -> Result<()> {
let socket = socket.as_path();

let mut stream = UnixStream::connect(&socket)?;
Ok(stream.write_all(&*bytes)?)
Ok(stream.write_all(bytes)?)
}

#[allow(clippy::too_many_lines)]
Expand Down Expand Up @@ -720,8 +720,9 @@ fn main() -> Result<()> {
color_log.push("komorebi.log");
let file = TailedFile::new(File::open(color_log)?);
let locked = file.lock();
for line in locked.lines() {
println!("{}", line?);
#[allow(clippy::significant_drop_in_scrutinee)]
for line in locked.lines().flatten() {
println!("{}", line);
}
}
SubCommand::Focus(arg) => {
Expand Down

0 comments on commit 092e36b

Please sign in to comment.