diff --git a/Cargo.lock b/Cargo.lock index d14dd414..87ac0c9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2709,10 +2709,10 @@ dependencies = [ "which", "widestring", "win32-display-data", - "windows 0.57.0", - "windows-core 0.57.0", - "windows-implement 0.57.0", - "windows-interface 0.57.0", + "windows 0.58.0", + "windows-core 0.58.0", + "windows-implement 0.58.0", + "windows-interface 0.58.0", "winput", "winreg", ] @@ -2747,7 +2747,7 @@ dependencies = [ "tracing", "tracing-appender", "tracing-subscriber", - "windows 0.57.0", + "windows 0.58.0", "windows-icons", ] @@ -2769,7 +2769,7 @@ dependencies = [ "komorebi-client", "random_word", "serde_json_lenient", - "windows 0.57.0", + "windows 0.58.0", ] [[package]] @@ -2809,7 +2809,7 @@ dependencies = [ "uds_windows", "which", "win32-display-data", - "windows 0.57.0", + "windows 0.58.0", ] [[package]] @@ -6019,11 +6019,11 @@ checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311" [[package]] name = "win32-display-data" version = "0.1.0" -source = "git+https://github.com/LGUG2Z/win32-display-data?rev=790d56567bd15b4ba3d49ed1ac9701ede6fab4a3#790d56567bd15b4ba3d49ed1ac9701ede6fab4a3" +source = "git+https://github.com/LGUG2Z/win32-display-data?rev=dd65e3f22d0521b78fcddde11abc2a3e9dcc32a8#dd65e3f22d0521b78fcddde11abc2a3e9dcc32a8" dependencies = [ "itertools", "thiserror", - "windows 0.57.0", + "windows 0.58.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6f65675a..7557f860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,15 +33,15 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } paste = "1" sysinfo = "0.31" uds_windows = "1" -win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "790d56567bd15b4ba3d49ed1ac9701ede6fab4a3" } -windows-implement = { version = "0.57" } -windows-interface = { version = "0.57" } -windows-core = { version = "0.57" } +win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "dd65e3f22d0521b78fcddde11abc2a3e9dcc32a8" } +windows-implement = { version = "0.58" } +windows-interface = { version = "0.58" } +windows-core = { version = "0.58" } shadow-rs = "0.35" which = "6" [workspace.dependencies.windows] -version = "0.57" +version = "0.58" features = [ "implement", "Win32_System_Com", diff --git a/komorebi-gui/src/main.rs b/komorebi-gui/src/main.rs index 00a85608..061717e2 100644 --- a/komorebi-gui/src/main.rs +++ b/komorebi-gui/src/main.rs @@ -218,7 +218,7 @@ extern "system" fn enum_window( lparam: windows::Win32::Foundation::LPARAM, ) -> windows::Win32::Foundation::BOOL { let windows = unsafe { &mut *(lparam.0 as *mut Vec) }; - let window = Window::from(hwnd.0); + let window = Window::from(hwnd.0 as isize); if window.is_window() && !window.is_miminized() diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index faecbc8b..0aa25000 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -5,6 +5,7 @@ use crate::border_manager::BORDER_WIDTH; use crate::border_manager::FOCUS_STATE; use crate::border_manager::STYLE; use crate::border_manager::Z_ORDER; +use crate::windows_api; use crate::WindowsApi; use crate::WINDOWS_11; @@ -46,10 +47,11 @@ use windows::Win32::UI::WindowsAndMessaging::WNDCLASSW; pub extern "system" fn border_hwnds(hwnd: HWND, lparam: LPARAM) -> BOOL { let hwnds = unsafe { &mut *(lparam.0 as *mut Vec) }; + let hwnd = hwnd.0 as isize; if let Ok(class) = WindowsApi::real_window_class_w(hwnd) { if class.starts_with("komoborder") { - hwnds.push(hwnd.0); + hwnds.push(hwnd); } } @@ -69,7 +71,7 @@ impl From for Border { impl Border { pub const fn hwnd(&self) -> HWND { - HWND(self.hwnd) + HWND(windows_api::as_ptr!(self.hwnd)) } pub fn create(id: &str) -> color_eyre::Result { @@ -91,8 +93,9 @@ impl Border { let (hwnd_sender, hwnd_receiver) = mpsc::channel(); + let instance = h_module.0 as isize; std::thread::spawn(move || -> color_eyre::Result<()> { - let hwnd = WindowsApi::create_border_window(PCWSTR(name.as_ptr()), h_module)?; + let hwnd = WindowsApi::create_border_window(PCWSTR(name.as_ptr()), instance)?; hwnd_sender.send(hwnd)?; let mut msg: MSG = MSG::default(); @@ -120,7 +123,7 @@ impl Border { } pub fn destroy(&self) -> color_eyre::Result<()> { - WindowsApi::close_window(self.hwnd()) + WindowsApi::close_window(self.hwnd) } pub fn update(&self, rect: &Rect, mut should_invalidate: bool) -> color_eyre::Result<()> { @@ -130,8 +133,8 @@ impl Border { rect.add_padding(-BORDER_OFFSET.load(Ordering::SeqCst)); // Update the position of the border if required - if !WindowsApi::window_rect(self.hwnd())?.eq(&rect) { - WindowsApi::set_border_pos(self.hwnd(), &rect, HWND((Z_ORDER.load()).into()))?; + if !WindowsApi::window_rect(self.hwnd)?.eq(&rect) { + WindowsApi::set_border_pos(self.hwnd, &rect, Z_ORDER.load().into())?; should_invalidate = true; } @@ -160,13 +163,13 @@ impl Border { let hdc = BeginPaint(window, &mut ps); // With the rect that we set in Self::update - match WindowsApi::window_rect(window) { + match WindowsApi::window_rect(window.0 as isize) { Ok(rect) => { // Grab the focus kind for this border let window_kind = { FOCUS_STATE .lock() - .get(&window.0) + .get(&(window.0 as isize)) .copied() .unwrap_or(WindowKind::Unfocused) }; diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index cfe125dd..42757fb4 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -4,6 +4,15 @@ mod border; use crate::core::BorderImplementation; use crate::core::BorderStyle; +use crate::core::WindowKind; +use crate::ring::Ring; +use crate::workspace_reconciliator::ALT_TAB_HWND; +use crate::Colour; +use crate::Rgb; +use crate::WindowManager; +use crate::WindowsApi; +use border::border_hwnds; +use border::Border; use crossbeam_channel::Receiver; use crossbeam_channel::Sender; use crossbeam_utils::atomic::AtomicCell; @@ -21,17 +30,6 @@ use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; use std::sync::Arc; use std::sync::OnceLock; -use windows::Win32::Foundation::HWND; - -use crate::core::WindowKind; -use crate::ring::Ring; -use crate::workspace_reconciliator::ALT_TAB_HWND; -use crate::Colour; -use crate::Rgb; -use crate::WindowManager; -use crate::WindowsApi; -use border::border_hwnds; -use border::Border; pub static BORDER_WIDTH: AtomicI32 = AtomicI32::new(8); pub static BORDER_OFFSET: AtomicI32 = AtomicI32::new(-1); @@ -300,7 +298,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } let rect = WindowsApi::window_rect( - monocle.focused_window().copied().unwrap_or_default().hwnd(), + monocle.focused_window().copied().unwrap_or_default().hwnd, )?; border.update(&rect, true)?; @@ -324,9 +322,9 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result continue 'monitors; } - let is_maximized = WindowsApi::is_zoomed(HWND( + let is_maximized = WindowsApi::is_zoomed( WindowsApi::foreground_window().unwrap_or_default(), - )); + ); if is_maximized { let mut to_remove = vec![]; @@ -374,7 +372,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result Z_ORDER.store(ZOrder::TopMost); let mut rect = WindowsApi::window_rect( - c.focused_window().copied().unwrap_or_default().hwnd(), + c.focused_window().copied().unwrap_or_default().hwnd, )?; while WindowsApi::lbutton_is_pressed() { @@ -390,7 +388,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result }; let new_rect = WindowsApi::window_rect( - c.focused_window().copied().unwrap_or_default().hwnd(), + c.focused_window().copied().unwrap_or_default().hwnd, )?; if rect != new_rect { @@ -438,7 +436,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } let rect = WindowsApi::window_rect( - c.focused_window().copied().unwrap_or_default().hwnd(), + c.focused_window().copied().unwrap_or_default().hwnd, )?; let should_invalidate = match last_focus_state { diff --git a/komorebi/src/com/mod.rs b/komorebi/src/com/mod.rs index 3907e058..8067a051 100644 --- a/komorebi/src/com/mod.rs +++ b/komorebi/src/com/mod.rs @@ -77,7 +77,7 @@ pub extern "C" fn SetCloak(hwnd: HWND, cloak_type: u32, flags: i32) { if view_collection.get_view_for_hwnd(hwnd, &mut view).is_err() { tracing::error!( "could not get view for hwnd {} due to os error: {}", - hwnd.0, + hwnd.0 as isize, std::io::Error::last_os_error() ); } @@ -85,14 +85,14 @@ pub extern "C" fn SetCloak(hwnd: HWND, cloak_type: u32, flags: i32) { view.map_or_else( || { - tracing::error!("no view was found for {}", hwnd.0,); + tracing::error!("no view was found for {}", hwnd.0 as isize); }, |view| { unsafe { if view.set_cloak(cloak_type, flags).is_err() { tracing::error!( "could not change the cloaking status for hwnd {} due to os error: {}", - hwnd.0, + hwnd.0 as isize, std::io::Error::last_os_error() ); } diff --git a/komorebi/src/monitor_reconciliator/hidden.rs b/komorebi/src/monitor_reconciliator/hidden.rs index 5a54e5f6..5d54c854 100644 --- a/komorebi/src/monitor_reconciliator/hidden.rs +++ b/komorebi/src/monitor_reconciliator/hidden.rs @@ -28,6 +28,7 @@ use windows::Win32::UI::WindowsAndMessaging::WTS_SESSION_LOCK; use windows::Win32::UI::WindowsAndMessaging::WTS_SESSION_UNLOCK; use crate::monitor_reconciliator; +use crate::windows_api; use crate::WindowsApi; // This is a hidden window specifically spawned to listen to system-wide events related to monitors @@ -44,7 +45,7 @@ impl From for Hidden { impl Hidden { pub const fn hwnd(self) -> HWND { - HWND(self.hwnd) + HWND(windows_api::as_ptr!(self.hwnd)) } pub fn create(name: &str) -> color_eyre::Result { @@ -65,8 +66,9 @@ impl Hidden { let (hwnd_sender, hwnd_receiver) = mpsc::channel(); + let instance = h_module.0 as isize; std::thread::spawn(move || -> color_eyre::Result<()> { - let hwnd = WindowsApi::create_hidden_window(PCWSTR(name.as_ptr()), h_module)?; + let hwnd = WindowsApi::create_hidden_window(PCWSTR(name.as_ptr()), instance)?; hwnd_sender.send(hwnd)?; let mut msg: MSG = MSG::default(); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 0c3ec459..2d137ea5 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -236,7 +236,7 @@ impl WindowManager { } SocketMessage::ForceFocus => { let focused_window = self.focused_window()?; - let focused_window_rect = WindowsApi::window_rect(focused_window.hwnd())?; + let focused_window_rect = WindowsApi::window_rect(focused_window.hwnd)?; WindowsApi::center_cursor_in_rect(&focused_window_rect)?; WindowsApi::left_click(); } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 9750d01e..fc3970a7 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -383,7 +383,7 @@ impl WindowManager { .ok_or_else(|| anyhow!("there is no workspace with this idx"))? .focused_container_idx(); - WindowsApi::bring_window_to_top(window.hwnd())?; + WindowsApi::bring_window_to_top(window.hwnd)?; self.pending_move_op = Option::from((monitor_idx, workspace_idx, container_idx)); @@ -407,7 +407,7 @@ impl WindowManager { let workspace = self.focused_workspace_mut()?; let focused_container_idx = workspace.focused_container_idx(); - let new_position = WindowsApi::window_rect(window.hwnd())?; + let new_position = WindowsApi::window_rect(window.hwnd)?; let old_position = *workspace .latest_layout() .get(focused_container_idx) diff --git a/komorebi/src/stackbar_manager/mod.rs b/komorebi/src/stackbar_manager/mod.rs index 26415041..9749d8b2 100644 --- a/komorebi/src/stackbar_manager/mod.rs +++ b/komorebi/src/stackbar_manager/mod.rs @@ -21,7 +21,6 @@ use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; use std::sync::Arc; use std::sync::OnceLock; -use windows::Win32::Foundation::HWND; pub static STACKBAR_FONT_SIZE: AtomicI32 = AtomicI32::new(0); // 0 will produce the system default pub static STACKBAR_FOCUSED_TEXT_COLOUR: AtomicU32 = AtomicU32::new(16777215); // white @@ -128,9 +127,8 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result continue 'receiver; } - let is_maximized = WindowsApi::is_zoomed(HWND( - WindowsApi::foreground_window().unwrap_or_default(), - )); + let is_maximized = + WindowsApi::is_zoomed(WindowsApi::foreground_window().unwrap_or_default()); // Handle the monocle container separately if ws.monocle_container().is_some() || is_maximized { @@ -207,11 +205,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result stackbars_monitors.insert(container.id().clone(), monitor_idx); let rect = WindowsApi::window_rect( - container - .focused_window() - .copied() - .unwrap_or_default() - .hwnd(), + container.focused_window().copied().unwrap_or_default().hwnd, )?; stackbar.update(container_padding, container, &rect)?; diff --git a/komorebi/src/stackbar_manager/stackbar.rs b/komorebi/src/stackbar_manager/stackbar.rs index 46e5d1c7..71f05252 100644 --- a/komorebi/src/stackbar_manager/stackbar.rs +++ b/komorebi/src/stackbar_manager/stackbar.rs @@ -14,6 +14,7 @@ use crate::stackbar_manager::STACKBAR_TAB_BACKGROUND_COLOUR; use crate::stackbar_manager::STACKBAR_TAB_HEIGHT; use crate::stackbar_manager::STACKBAR_TAB_WIDTH; use crate::stackbar_manager::STACKBAR_UNFOCUSED_TEXT_COLOUR; +use crate::windows_api; use crate::WindowsApi; use crate::DEFAULT_CONTAINER_PADDING; use crate::WINDOWS_11; @@ -24,6 +25,7 @@ use std::sync::mpsc; use std::time::Duration; use windows::core::PCWSTR; use windows::Win32::Foundation::COLORREF; +use windows::Win32::Foundation::HINSTANCE; use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::LPARAM; use windows::Win32::Foundation::LRESULT; @@ -84,7 +86,7 @@ impl From for Stackbar { impl Stackbar { pub const fn hwnd(&self) -> HWND { - HWND(self.hwnd) + HWND(windows_api::as_ptr!(self.hwnd)) } pub fn create(id: &str) -> color_eyre::Result { @@ -107,6 +109,7 @@ impl Stackbar { let (hwnd_sender, hwnd_receiver) = mpsc::channel(); let name_cl = name.clone(); + let instance = h_module.0 as isize; std::thread::spawn(move || -> color_eyre::Result<()> { unsafe { let hwnd = CreateWindowExW( @@ -120,12 +123,12 @@ impl Stackbar { 0, None, None, - h_module, + HINSTANCE(windows_api::as_ptr!(instance)), None, - ); + )?; SetLayeredWindowAttributes(hwnd, COLORREF(0), 0, LWA_COLORKEY)?; - hwnd_sender.send(hwnd)?; + hwnd_sender.send(hwnd.0 as isize)?; let mut msg: MSG = MSG::default(); @@ -146,12 +149,12 @@ impl Stackbar { }); Ok(Self { - hwnd: hwnd_receiver.recv()?.0, + hwnd: hwnd_receiver.recv()?, }) } pub fn destroy(&self) -> color_eyre::Result<()> { - WindowsApi::close_window(self.hwnd()) + WindowsApi::close_window(self.hwnd) } pub fn update( @@ -177,7 +180,7 @@ impl Stackbar { layout.top -= workspace_specific_offset + STACKBAR_TAB_HEIGHT.load_consume(); layout.left -= workspace_specific_offset; - WindowsApi::position_window(self.hwnd(), &layout, false)?; + WindowsApi::position_window(self.hwnd, &layout, false)?; unsafe { let hdc = GetDC(self.hwnd()); @@ -309,7 +312,7 @@ impl Stackbar { match msg { WM_LBUTTONDOWN => { let stackbars_containers = STACKBARS_CONTAINERS.lock(); - if let Some(container) = stackbars_containers.get(&hwnd.0) { + if let Some(container) = stackbars_containers.get(&(hwnd.0 as isize)) { let x = l_param.0 as i32 & 0xFFFF; let y = (l_param.0 as i32 >> 16) & 0xFFFF; @@ -319,11 +322,7 @@ impl Stackbar { let focused_window_idx = container.focused_window_idx(); let focused_window_rect = WindowsApi::window_rect( - container - .focused_window() - .cloned() - .unwrap_or_default() - .hwnd(), + container.focused_window().cloned().unwrap_or_default().hwnd, ) .unwrap_or_default(); diff --git a/komorebi/src/transparency_manager.rs b/komorebi/src/transparency_manager.rs index f5aed700..ed503a4d 100644 --- a/komorebi/src/transparency_manager.rs +++ b/komorebi/src/transparency_manager.rs @@ -8,7 +8,6 @@ use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicU8; use std::sync::Arc; use std::sync::OnceLock; -use windows::Win32::Foundation::HWND; use crate::Window; use crate::WindowManager; @@ -123,7 +122,7 @@ pub fn handle_notifications(wm: Arc>) -> color_eyre::Result } let foreground_hwnd = WindowsApi::foreground_window().unwrap_or_default(); - let is_maximized = WindowsApi::is_zoomed(HWND(foreground_hwnd)); + let is_maximized = WindowsApi::is_zoomed(foreground_hwnd); if is_maximized { if let Err(error) = Window::from(foreground_hwnd).opaque() { diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 5a531c6a..d6b15dfc 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -2,6 +2,7 @@ use crate::border_manager; use crate::com::SetCloak; use crate::focus_manager; use crate::stackbar_manager; +use crate::windows_api; use crate::ANIMATIONS_IN_PROGRESS; use crate::ANIMATION_DURATION; use crate::ANIMATION_ENABLED; @@ -70,8 +71,8 @@ impl From for Window { impl From for Window { fn from(value: HWND) -> Self { Self { - hwnd: value.0, - animation: Animation::new(value.0), + hwnd: value.0 as isize, + animation: Animation::new(value.0 as isize), } } } @@ -145,7 +146,7 @@ impl Serialize for Window { )?; state.serialize_field( "rect", - &WindowsApi::window_rect(self.hwnd()).unwrap_or_default(), + &WindowsApi::window_rect(self.hwnd).unwrap_or_default(), )?; state.end() } @@ -153,7 +154,7 @@ impl Serialize for Window { impl Window { pub const fn hwnd(self) -> HWND { - HWND(self.hwnd) + HWND(windows_api::as_ptr!(self.hwnd)) } pub fn center(&mut self, work_area: &Rect) -> Result<()> { @@ -172,7 +173,6 @@ impl Window { } pub fn animate_position(&self, start_rect: &Rect, target_rect: &Rect, top: bool) -> Result<()> { - let hwnd = self.hwnd(); let start_rect = *start_rect; let target_rect = *target_rect; let duration = Duration::from_millis(ANIMATION_DURATION.load(Ordering::SeqCst)); @@ -184,14 +184,16 @@ impl Window { stackbar_manager::STACKBAR_TEMPORARILY_DISABLED.store(true, Ordering::SeqCst); stackbar_manager::send_notification(); + let hwnd = self.hwnd; + std::thread::spawn(move || { animation.animate(duration, |progress: f64| { let new_rect = Animation::lerp_rect(&start_rect, &target_rect, progress); if progress == 1.0 { WindowsApi::position_window(hwnd, &new_rect, top)?; - if WindowsApi::foreground_window().unwrap_or_default() == hwnd.0 { - focus_manager::send_notification(hwnd.0) + if WindowsApi::foreground_window().unwrap_or_default() == hwnd { + focus_manager::send_notification(hwnd) } if ANIMATIONS_IN_PROGRESS.load(Ordering::Acquire) == 0 { @@ -218,7 +220,7 @@ impl Window { } pub fn set_position(&self, layout: &Rect, top: bool) -> Result<()> { - let window_rect = WindowsApi::window_rect(self.hwnd())?; + let window_rect = WindowsApi::window_rect(self.hwnd)?; if window_rect.eq(layout) { return Ok(()); @@ -227,20 +229,20 @@ impl Window { if ANIMATION_ENABLED.load(Ordering::SeqCst) { self.animate_position(&window_rect, layout, top) } else { - WindowsApi::position_window(self.hwnd(), layout, top) + WindowsApi::position_window(self.hwnd, layout, top) } } pub fn is_maximized(self) -> bool { - WindowsApi::is_zoomed(self.hwnd()) + WindowsApi::is_zoomed(self.hwnd) } pub fn is_miminized(self) -> bool { - WindowsApi::is_iconic(self.hwnd()) + WindowsApi::is_iconic(self.hwnd) } pub fn is_visible(self) -> bool { - WindowsApi::is_window_visible(self.hwnd()) + WindowsApi::is_window_visible(self.hwnd) } pub fn hide(self) { @@ -251,8 +253,8 @@ impl Window { let hiding_behaviour = HIDING_BEHAVIOUR.lock(); match *hiding_behaviour { - HidingBehaviour::Hide => WindowsApi::hide_window(self.hwnd()), - HidingBehaviour::Minimize => WindowsApi::minimize_window(self.hwnd()), + HidingBehaviour::Hide => WindowsApi::hide_window(self.hwnd), + HidingBehaviour::Minimize => WindowsApi::minimize_window(self.hwnd), HidingBehaviour::Cloak => SetCloak(self.hwnd(), 1, 2), } } @@ -269,18 +271,18 @@ impl Window { let hiding_behaviour = HIDING_BEHAVIOUR.lock(); match *hiding_behaviour { HidingBehaviour::Hide | HidingBehaviour::Minimize => { - WindowsApi::restore_window(self.hwnd()); + WindowsApi::restore_window(self.hwnd); } HidingBehaviour::Cloak => SetCloak(self.hwnd(), 1, 0), } } pub fn minimize(self) { - WindowsApi::minimize_window(self.hwnd()); + WindowsApi::minimize_window(self.hwnd); } pub fn close(self) -> Result<()> { - WindowsApi::close_window(self.hwnd()) + WindowsApi::close_window(self.hwnd) } pub fn maximize(self) { @@ -292,7 +294,7 @@ impl Window { programmatically_hidden_hwnds.remove(idx); } - WindowsApi::maximize_window(self.hwnd()); + WindowsApi::maximize_window(self.hwnd); } pub fn unmaximize(self) { @@ -304,27 +306,27 @@ impl Window { programmatically_hidden_hwnds.remove(idx); } - WindowsApi::unmaximize_window(self.hwnd()); + WindowsApi::unmaximize_window(self.hwnd); } pub fn focus(self, mouse_follows_focus: bool) -> Result<()> { // If the target window is already focused, do nothing. if let Ok(ihwnd) = WindowsApi::foreground_window() { - if HWND(ihwnd) == self.hwnd() { + if ihwnd == self.hwnd { // Center cursor in Window if mouse_follows_focus { - WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd())?)?; + WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd)?)?; } return Ok(()); } } - WindowsApi::raise_and_focus_window(self.hwnd())?; + WindowsApi::raise_and_focus_window(self.hwnd)?; // Center cursor in Window if mouse_follows_focus { - WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd())?)?; + WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd)?)?; } Ok(()) @@ -335,7 +337,7 @@ impl Window { ex_style.insert(ExtendedWindowStyle::LAYERED); self.update_ex_style(&ex_style)?; WindowsApi::set_transparent( - self.hwnd(), + self.hwnd, transparency_manager::TRANSPARENCY_ALPHA.load_consume(), ) } @@ -356,29 +358,29 @@ impl Window { #[allow(dead_code)] pub fn update_style(self, style: &WindowStyle) -> Result<()> { - WindowsApi::update_style(self.hwnd(), isize::try_from(style.bits())?) + WindowsApi::update_style(self.hwnd, isize::try_from(style.bits())?) } pub fn update_ex_style(self, style: &ExtendedWindowStyle) -> Result<()> { - WindowsApi::update_ex_style(self.hwnd(), isize::try_from(style.bits())?) + WindowsApi::update_ex_style(self.hwnd, isize::try_from(style.bits())?) } pub fn style(self) -> Result { - let bits = u32::try_from(WindowsApi::gwl_style(self.hwnd())?)?; + let bits = u32::try_from(WindowsApi::gwl_style(self.hwnd)?)?; Ok(WindowStyle::from_bits_truncate(bits)) } pub fn ex_style(self) -> Result { - let bits = u32::try_from(WindowsApi::gwl_ex_style(self.hwnd())?)?; + let bits = u32::try_from(WindowsApi::gwl_ex_style(self.hwnd)?)?; Ok(ExtendedWindowStyle::from_bits_truncate(bits)) } pub fn title(self) -> Result { - WindowsApi::window_text_w(self.hwnd()) + WindowsApi::window_text_w(self.hwnd) } pub fn path(self) -> Result { - let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd()); + let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); let handle = WindowsApi::process_handle(process_id)?; let path = WindowsApi::exe_path(handle); WindowsApi::close_process(handle)?; @@ -386,7 +388,7 @@ impl Window { } pub fn exe(self) -> Result { - let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd()); + let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); let handle = WindowsApi::process_handle(process_id)?; let exe = WindowsApi::exe(handle); WindowsApi::close_process(handle)?; @@ -394,20 +396,20 @@ impl Window { } pub fn process_id(self) -> u32 { - let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd()); + let (process_id, _) = WindowsApi::window_thread_process_id(self.hwnd); process_id } pub fn class(self) -> Result { - WindowsApi::real_window_class_w(self.hwnd()) + WindowsApi::real_window_class_w(self.hwnd) } pub fn is_cloaked(self) -> Result { - WindowsApi::is_window_cloaked(self.hwnd()) + WindowsApi::is_window_cloaked(self.hwnd) } pub fn is_window(self) -> bool { - WindowsApi::is_window(self.hwnd()) + WindowsApi::is_window(self.hwnd) } pub fn remove_title_bar(self) -> Result<()> { @@ -436,7 +438,7 @@ impl Window { debug.is_window = true; - let rect = WindowsApi::window_rect(self.hwnd()).unwrap_or_default(); + let rect = WindowsApi::window_rect(self.hwnd).unwrap_or_default(); if rect.right < MINIMUM_WIDTH.load(Ordering::SeqCst) { return Ok(false); diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 02051002..c9d6b3ba 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -819,7 +819,7 @@ impl WindowManager { let rect = self.focused_monitor_size()?; WindowsApi::center_cursor_in_rect(&rect)?; - match WindowsApi::raise_and_focus_window(desktop_window.hwnd()) { + match WindowsApi::raise_and_focus_window(desktop_window.hwnd) { Ok(()) => {} Err(error) => { tracing::warn!("{} {}:{}", error, file!(), line!()); @@ -1338,7 +1338,7 @@ impl WindowManager { if let Some(window) = monocle.focused_window() { window.focus(mouse_follows_focus)?; WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect( - window.hwnd(), + window.hwnd, )?)?; cross_monitor_monocle = true; @@ -2605,7 +2605,7 @@ impl WindowManager { } pub fn monitor_idx_from_window(&mut self, window: Window) -> Option { - let hmonitor = WindowsApi::monitor_from_window(window.hwnd()); + let hmonitor = WindowsApi::monitor_from_window(window.hwnd); for (i, monitor) in self.monitors().iter().enumerate() { if monitor.id() == hmonitor { diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 42cfb3f5..5021c54b 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -14,6 +14,7 @@ use windows::Win32::Foundation::CloseHandle; use windows::Win32::Foundation::BOOL; use windows::Win32::Foundation::COLORREF; use windows::Win32::Foundation::HANDLE; +use windows::Win32::Foundation::HINSTANCE; use windows::Win32::Foundation::HMODULE; use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::LPARAM; @@ -146,6 +147,14 @@ use crate::Window; use crate::DISPLAY_INDEX_PREFERENCES; use crate::MONITOR_INDEX_PREFERENCES; +macro_rules! as_ptr { + ($value:expr) => { + $value as *mut core::ffi::c_void + }; +} + +pub(crate) use as_ptr; + pub enum WindowsResult { Err(E), Ok(T), @@ -187,10 +196,10 @@ macro_rules! impl_process_windows_crate_integer_wrapper_result { $( impl ProcessWindowsCrateResult<$deref> for $input { fn process(self) -> Result<$deref> { - if self == $input(0) { + if self == $input(std::ptr::null_mut()) { Err(std::io::Error::last_os_error().into()) } else { - Ok(self.0) + Ok(self.0 as $deref) } } } @@ -219,9 +228,16 @@ impl WindowsApi { callback: MONITORENUMPROC, callback_data_address: isize, ) -> Result<()> { - unsafe { EnumDisplayMonitors(HDC(0), None, callback, LPARAM(callback_data_address)) } - .ok() - .process() + unsafe { + EnumDisplayMonitors( + HDC(std::ptr::null_mut()), + None, + callback, + LPARAM(callback_data_address), + ) + } + .ok() + .process() } pub fn valid_hmonitors() -> Result> { @@ -328,8 +344,8 @@ impl WindowsApi { for container in workspace.containers_mut() { for window in container.windows() { - if Self::monitor_name_from_window(window.hwnd())? != monitor_name { - windows_on_other_monitors.push(window.hwnd().0); + if Self::monitor_name_from_window(window.hwnd)? != monitor_name { + windows_on_other_monitors.push(window.hwnd); } } } @@ -347,32 +363,34 @@ impl WindowsApi { unsafe { AllowSetForegroundWindow(process_id) }.process() } - pub fn monitor_from_window(hwnd: HWND) -> isize { + pub fn monitor_from_window(hwnd: isize) -> isize { // MONITOR_DEFAULTTONEAREST ensures that the return value will never be NULL // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow - unsafe { MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) }.0 + unsafe { MonitorFromWindow(HWND(as_ptr!(hwnd)), MONITOR_DEFAULTTONEAREST) }.0 as isize } - pub fn monitor_name_from_window(hwnd: HWND) -> Result { + pub fn monitor_name_from_window(hwnd: isize) -> Result { // MONITOR_DEFAULTTONEAREST ensures that the return value will never be NULL // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow - Ok( - Self::monitor(unsafe { MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) }.0)? - .name() - .to_string(), - ) + Ok(Self::monitor( + unsafe { MonitorFromWindow(HWND(as_ptr!(hwnd)), MONITOR_DEFAULTTONEAREST) }.0 as isize, + )? + .name() + .to_string()) } pub fn monitor_from_point(point: POINT) -> isize { // MONITOR_DEFAULTTONEAREST ensures that the return value will never be NULL // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow - unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST) }.0 + unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST) }.0 as isize } /// position window resizes the target window to the given layout, adjusting /// the layout to account for any window shadow borders (the window painted /// region will match layout on completion). - pub fn position_window(hwnd: HWND, layout: &Rect, top: bool) -> Result<()> { + pub fn position_window(hwnd: isize, layout: &Rect, top: bool) -> Result<()> { + let hwnd = HWND(as_ptr!(hwnd)); + let mut flags = SetWindowPosition::NO_ACTIVATE | SetWindowPosition::NO_SEND_CHANGING | SetWindowPosition::NO_COPY_BITS @@ -408,22 +426,32 @@ impl WindowsApi { Self::set_window_pos(hwnd, &rect, HWND_TOP, flags.bits()) } - pub fn bring_window_to_top(hwnd: HWND) -> Result<()> { - unsafe { BringWindowToTop(hwnd) }.process() + pub fn bring_window_to_top(hwnd: isize) -> Result<()> { + unsafe { BringWindowToTop(HWND(as_ptr!(hwnd))) }.process() } // Raise the window to the top of the Z order, but do not activate or focus // it. Use raise_and_focus_window to activate and focus a window. - pub fn raise_window(hwnd: HWND) -> Result<()> { + pub fn raise_window(hwnd: isize) -> Result<()> { let flags = SetWindowPosition::NO_MOVE | SetWindowPosition::NO_ACTIVATE; let position = HWND_TOP; - Self::set_window_pos(hwnd, &Rect::default(), position, flags.bits()) + Self::set_window_pos( + HWND(as_ptr!(hwnd)), + &Rect::default(), + position, + flags.bits(), + ) } - pub fn set_border_pos(hwnd: HWND, layout: &Rect, position: HWND) -> Result<()> { + pub fn set_border_pos(hwnd: isize, layout: &Rect, position: isize) -> Result<()> { let flags = { SetWindowPosition::SHOW_WINDOW | SetWindowPosition::NO_ACTIVATE }; - Self::set_window_pos(hwnd, layout, position, flags.bits()) + Self::set_window_pos( + HWND(as_ptr!(hwnd)), + layout, + HWND(as_ptr!(position)), + flags.bits(), + ) } /// set_window_pos calls SetWindowPos without any accounting for Window decorations. @@ -442,7 +470,9 @@ impl WindowsApi { .process() } - pub fn move_window(hwnd: HWND, layout: &Rect, repaint: bool) -> Result<()> { + pub fn move_window(hwnd: isize, layout: &Rect, repaint: bool) -> Result<()> { + let hwnd = HWND(as_ptr!(hwnd)); + let shadow_rect = Self::shadow_rect(hwnd).unwrap_or_default(); let rect = Rect { left: layout.left + shadow_rect.left, @@ -453,16 +483,16 @@ impl WindowsApi { unsafe { MoveWindow(hwnd, rect.left, rect.top, rect.right, rect.bottom, repaint) }.process() } - pub fn show_window(hwnd: HWND, command: SHOW_WINDOW_CMD) { + pub fn show_window(hwnd: isize, command: SHOW_WINDOW_CMD) { // BOOL is returned but does not signify whether or not the operation was succesful // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow // TODO: error handling unsafe { - let _ = ShowWindow(hwnd, command); + let _ = ShowWindow(HWND(as_ptr!(hwnd)), command); }; } - pub fn minimize_window(hwnd: HWND) { + pub fn minimize_window(hwnd: isize) { Self::show_window(hwnd, SW_MINIMIZE); } @@ -470,26 +500,26 @@ impl WindowsApi { unsafe { PostMessageW(hwnd, message, wparam, lparam) }.process() } - pub fn close_window(hwnd: HWND) -> Result<()> { - match Self::post_message(hwnd, WM_CLOSE, WPARAM(0), LPARAM(0)) { + pub fn close_window(hwnd: isize) -> Result<()> { + match Self::post_message(HWND(as_ptr!(hwnd)), WM_CLOSE, WPARAM(0), LPARAM(0)) { Ok(()) => Ok(()), Err(_) => Err(anyhow!("could not close window")), } } - pub fn hide_window(hwnd: HWND) { + pub fn hide_window(hwnd: isize) { Self::show_window(hwnd, SW_HIDE); } - pub fn restore_window(hwnd: HWND) { + pub fn restore_window(hwnd: isize) { Self::show_window(hwnd, SW_SHOWNOACTIVATE); } - pub fn unmaximize_window(hwnd: HWND) { + pub fn unmaximize_window(hwnd: isize) { Self::show_window(hwnd, SW_NORMAL); } - pub fn maximize_window(hwnd: HWND) { + pub fn maximize_window(hwnd: isize) { Self::show_window(hwnd, SW_MAXIMIZE); } @@ -497,7 +527,7 @@ impl WindowsApi { unsafe { GetForegroundWindow() }.process() } - pub fn raise_and_focus_window(hwnd: HWND) -> Result<()> { + pub fn raise_and_focus_window(hwnd: isize) -> Result<()> { let event = [INPUT { r#type: INPUT_MOUSE, ..Default::default() @@ -509,7 +539,7 @@ impl WindowsApi { SendInput(&event, size_of::() as i32); // Error ignored, as the operation is not always necessary. let _ = SetWindowPos( - hwnd, + HWND(as_ptr!(hwnd)), HWND_TOP, 0, 0, @@ -518,7 +548,7 @@ impl WindowsApi { SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW, ) .process(); - SetForegroundWindow(hwnd) + SetForegroundWindow(HWND(as_ptr!(hwnd))) } .ok() .process() @@ -526,7 +556,7 @@ impl WindowsApi { #[allow(dead_code)] pub fn top_window() -> Result { - unsafe { GetTopWindow(HWND::default()) }.process() + unsafe { GetTopWindow(HWND::default())? }.process() } pub fn desktop_window() -> Result { @@ -534,8 +564,8 @@ impl WindowsApi { } #[allow(dead_code)] - pub fn next_window(hwnd: HWND) -> Result { - unsafe { GetWindow(hwnd, GW_HWNDNEXT) }.process() + pub fn next_window(hwnd: isize) -> Result { + unsafe { GetWindow(HWND(as_ptr!(hwnd)), GW_HWNDNEXT)? }.process() } pub fn alt_tab_windows() -> Result> { @@ -554,17 +584,17 @@ impl WindowsApi { let mut next_hwnd = hwnd; while next_hwnd != 0 { - if Self::is_window_visible(HWND(next_hwnd)) { + if Self::is_window_visible(next_hwnd) { return Ok(next_hwnd); } - next_hwnd = Self::next_window(HWND(next_hwnd))?; + next_hwnd = Self::next_window(next_hwnd)?; } Err(anyhow!("could not find next window")) } - pub fn window_rect(hwnd: HWND) -> Result { + pub fn window_rect(hwnd: isize) -> Result { let mut rect = unsafe { std::mem::zeroed() }; if Self::dwm_get_window_attribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &mut rect).is_ok() { @@ -574,7 +604,7 @@ impl WindowsApi { // Ok(Rect::from(rect).scale(system_scale.try_into()?, window_scale.try_into()?)) Ok(Rect::from(rect)) } else { - unsafe { GetWindowRect(hwnd, &mut rect) }.process()?; + unsafe { GetWindowRect(HWND(as_ptr!(hwnd)), &mut rect) }.process()?; Ok(Rect::from(rect)) } } @@ -584,7 +614,7 @@ impl WindowsApi { /// added to a position rect to compute a size for set_window_pos that will /// fill the target area, ignoring shadows. fn shadow_rect(hwnd: HWND) -> Result { - let window_rect = Self::window_rect(hwnd)?; + let window_rect = Self::window_rect(hwnd.0 as isize)?; let mut srect = Default::default(); unsafe { GetWindowRect(hwnd, &mut srect) }.process()?; @@ -641,13 +671,16 @@ impl WindowsApi { Self::set_cursor_pos(rect.left + (rect.right / 2), rect.top + (rect.bottom / 2)) } - pub fn window_thread_process_id(hwnd: HWND) -> (u32, u32) { + pub fn window_thread_process_id(hwnd: isize) -> (u32, u32) { let mut process_id: u32 = 0; // Behaviour is undefined if an invalid HWND is given // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowthreadprocessid let thread_id = unsafe { - GetWindowThreadProcessId(hwnd, Option::from(std::ptr::addr_of_mut!(process_id))) + GetWindowThreadProcessId( + HWND(as_ptr!(hwnd)), + Option::from(std::ptr::addr_of_mut!(process_id)), + ) }; (process_id, thread_id) @@ -682,12 +715,12 @@ impl WindowsApi { .map(|_| {}) } - pub fn gwl_style(hwnd: HWND) -> Result { - Self::window_long_ptr_w(hwnd, GWL_STYLE) + pub fn gwl_style(hwnd: isize) -> Result { + Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE) } - pub fn gwl_ex_style(hwnd: HWND) -> Result { - Self::window_long_ptr_w(hwnd, GWL_EXSTYLE) + pub fn gwl_ex_style(hwnd: isize) -> Result { + Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE) } fn window_long_ptr_w(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> Result { @@ -699,18 +732,18 @@ impl WindowsApi { } #[allow(dead_code)] - pub fn update_style(hwnd: HWND, new_value: isize) -> Result<()> { - Self::set_window_long_ptr_w(hwnd, GWL_STYLE, new_value) + pub fn update_style(hwnd: isize, new_value: isize) -> Result<()> { + Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE, new_value) } #[allow(dead_code)] - pub fn update_ex_style(hwnd: HWND, new_value: isize) -> Result<()> { - Self::set_window_long_ptr_w(hwnd, GWL_EXSTYLE, new_value) + pub fn update_ex_style(hwnd: isize, new_value: isize) -> Result<()> { + Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE, new_value) } - pub fn window_text_w(hwnd: HWND) -> Result { + pub fn window_text_w(hwnd: isize) -> Result { let mut text: [u16; 512] = [0; 512]; - match WindowsResult::from(unsafe { GetWindowTextW(hwnd, &mut text) }) { + match WindowsResult::from(unsafe { GetWindowTextW(HWND(as_ptr!(hwnd)), &mut text) }) { WindowsResult::Ok(len) => { let length = usize::try_from(len)?; Ok(String::from_utf16(&text[..length])?) @@ -756,25 +789,25 @@ impl WindowsApi { .to_string()) } - pub fn real_window_class_w(hwnd: HWND) -> Result { + pub fn real_window_class_w(hwnd: isize) -> Result { const BUF_SIZE: usize = 512; let mut class: [u16; BUF_SIZE] = [0; BUF_SIZE]; let len = Result::from(WindowsResult::from(unsafe { - RealGetWindowClassW(hwnd, &mut class) + RealGetWindowClassW(HWND(as_ptr!(hwnd)), &mut class) }))?; Ok(String::from_utf16(&class[0..len as usize])?) } pub fn dwm_get_window_attribute( - hwnd: HWND, + hwnd: isize, attribute: DWMWINDOWATTRIBUTE, value: &mut T, ) -> Result<()> { unsafe { DwmGetWindowAttribute( - hwnd, + HWND(as_ptr!(hwnd)), attribute, (value as *mut T).cast(), u32::try_from(std::mem::size_of::())?, @@ -784,7 +817,7 @@ impl WindowsApi { Ok(()) } - pub fn is_window_cloaked(hwnd: HWND) -> Result { + pub fn is_window_cloaked(hwnd: isize) -> Result { let mut cloaked: u32 = 0; Self::dwm_get_window_attribute(hwnd, DWMWA_CLOAKED, &mut cloaked)?; @@ -794,20 +827,20 @@ impl WindowsApi { )) } - pub fn is_window(hwnd: HWND) -> bool { - unsafe { IsWindow(hwnd) }.into() + pub fn is_window(hwnd: isize) -> bool { + unsafe { IsWindow(HWND(as_ptr!(hwnd))) }.into() } - pub fn is_window_visible(hwnd: HWND) -> bool { - unsafe { IsWindowVisible(hwnd) }.into() + pub fn is_window_visible(hwnd: isize) -> bool { + unsafe { IsWindowVisible(HWND(as_ptr!(hwnd))) }.into() } - pub fn is_iconic(hwnd: HWND) -> bool { - unsafe { IsIconic(hwnd) }.into() + pub fn is_iconic(hwnd: isize) -> bool { + unsafe { IsIconic(HWND(as_ptr!(hwnd))) }.into() } - pub fn is_zoomed(hwnd: HWND) -> bool { - unsafe { IsZoomed(hwnd) }.into() + pub fn is_zoomed(hwnd: isize) -> bool { + unsafe { IsZoomed(HWND(as_ptr!(hwnd))) }.into() } pub fn monitor_info_w(hmonitor: HMONITOR) -> Result { @@ -959,7 +992,7 @@ impl WindowsApi { unsafe { GetDpiForMonitor( - HMONITOR(hmonitor), + HMONITOR(as_ptr!(hmonitor)), MDT_EFFECTIVE_DPI, std::ptr::addr_of_mut!(dpi_x), std::ptr::addr_of_mut!(dpi_y), @@ -983,7 +1016,7 @@ impl WindowsApi { unsafe { DwmSetWindowAttribute( - HWND(hwnd), + HWND(as_ptr!(hwnd)), DWMWA_WINDOW_CORNER_PREFERENCE, std::ptr::addr_of!(round).cast(), 4, @@ -996,7 +1029,7 @@ impl WindowsApi { let col_ref = COLORREF(color.unwrap_or(DWMWA_COLOR_NONE)); unsafe { DwmSetWindowAttribute( - HWND(hwnd), + HWND(as_ptr!(hwnd)), DWMWA_BORDER_COLOR, std::ptr::addr_of!(col_ref).cast(), 4, @@ -1005,7 +1038,7 @@ impl WindowsApi { .process() } - pub fn create_border_window(name: PCWSTR, instance: HMODULE) -> Result { + pub fn create_border_window(name: PCWSTR, instance: isize) -> Result { unsafe { let hwnd = CreateWindowExW( WS_EX_TOOLWINDOW | WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_NOACTIVATE, @@ -1018,9 +1051,9 @@ impl WindowsApi { CW_USEDEFAULT, None, None, - instance, + HINSTANCE(as_ptr!(instance)), None, - ); + )?; SetLayeredWindowAttributes(hwnd, COLORREF(0), 0, LWA_COLORKEY)?; @@ -1029,16 +1062,21 @@ impl WindowsApi { .process() } - pub fn set_transparent(hwnd: HWND, alpha: u8) -> Result<()> { + pub fn set_transparent(hwnd: isize, alpha: u8) -> Result<()> { unsafe { #[allow(clippy::cast_sign_loss)] - SetLayeredWindowAttributes(hwnd, COLORREF(-1i32 as u32), alpha, LWA_ALPHA)?; + SetLayeredWindowAttributes( + HWND(as_ptr!(hwnd)), + COLORREF(-1i32 as u32), + alpha, + LWA_ALPHA, + )?; } Ok(()) } - pub fn create_hidden_window(name: PCWSTR, instance: HMODULE) -> Result { + pub fn create_hidden_window(name: PCWSTR, instance: isize) -> Result { unsafe { CreateWindowExW( WS_EX_NOACTIVATE, @@ -1051,16 +1089,16 @@ impl WindowsApi { CW_USEDEFAULT, None, None, - instance, + HINSTANCE(as_ptr!(instance)), None, - ) + )? } .process() } - pub fn invalidate_rect(hwnd: HWND, rect: Option<&Rect>, erase: bool) -> bool { + pub fn invalidate_rect(hwnd: isize, rect: Option<&Rect>, erase: bool) -> bool { let rect = rect.map(|rect| &rect.rect() as *const RECT); - unsafe { InvalidateRect(hwnd, rect, erase) }.as_bool() + unsafe { InvalidateRect(HWND(as_ptr!(hwnd)), rect, erase) }.as_bool() } pub fn alt_is_pressed() -> bool { @@ -1114,6 +1152,6 @@ impl WindowsApi { } pub fn wts_register_session_notification(hwnd: isize) -> Result<()> { - unsafe { WTSRegisterSessionNotification(HWND(hwnd), 1) }.process() + unsafe { WTSRegisterSessionNotification(HWND(as_ptr!(hwnd)), 1) }.process() } } diff --git a/komorebi/src/windows_callbacks.rs b/komorebi/src/windows_callbacks.rs index 14c8e5a3..c7551fa5 100644 --- a/komorebi/src/windows_callbacks.rs +++ b/komorebi/src/windows_callbacks.rs @@ -16,10 +16,10 @@ use crate::winevent_listener; pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { let containers = unsafe { &mut *(lparam.0 as *mut VecDeque) }; - let is_visible = WindowsApi::is_window_visible(hwnd); - let is_window = WindowsApi::is_window(hwnd); - let is_minimized = WindowsApi::is_iconic(hwnd); - let is_maximized = WindowsApi::is_zoomed(hwnd); + let is_visible = WindowsApi::is_window_visible(hwnd.0 as isize); + let is_window = WindowsApi::is_window(hwnd.0 as isize); + let is_minimized = WindowsApi::is_iconic(hwnd.0 as isize); + let is_maximized = WindowsApi::is_zoomed(hwnd.0 as isize); if is_visible && is_window && !is_minimized { let window = Window::from(hwnd); @@ -27,7 +27,7 @@ pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { if let Ok(should_manage) = window.should_manage(None, &mut RuleDebug::default()) { if should_manage { if is_maximized { - WindowsApi::restore_window(hwnd); + WindowsApi::restore_window(window.hwnd); } let mut container = Container::default(); @@ -43,9 +43,9 @@ pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { pub extern "system" fn alt_tab_windows(hwnd: HWND, lparam: LPARAM) -> BOOL { let windows = unsafe { &mut *(lparam.0 as *mut Vec) }; - let is_visible = WindowsApi::is_window_visible(hwnd); - let is_window = WindowsApi::is_window(hwnd); - let is_minimized = WindowsApi::is_iconic(hwnd); + let is_visible = WindowsApi::is_window_visible(hwnd.0 as isize); + let is_window = WindowsApi::is_window(hwnd.0 as isize); + let is_minimized = WindowsApi::is_iconic(hwnd.0 as isize); if is_visible && is_window && !is_minimized { let window = Window::from(hwnd); diff --git a/komorebi/src/winevent_listener.rs b/komorebi/src/winevent_listener.rs index fa841045..2bbfc456 100644 --- a/komorebi/src/winevent_listener.rs +++ b/komorebi/src/winevent_listener.rs @@ -39,7 +39,7 @@ pub fn start() { loop { unsafe { - if !GetMessageW(&mut msg, HWND(0), 0, 0).as_bool() { + if !GetMessageW(&mut msg, HWND(std::ptr::null_mut()), 0, 0).as_bool() { tracing::debug!("windows event processing thread shutdown"); break; }; diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 37f1b01e..688c742f 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -357,7 +357,7 @@ impl Workspace { // If a window has been unmaximized via toggle-maximize, this block // will make sure that it is unmaximized via restore_window if window.is_maximized() && !managed_maximized_window { - WindowsApi::restore_window(window.hwnd()); + WindowsApi::restore_window(window.hwnd); } { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 32a8c373..1cb24a50 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -2118,7 +2118,7 @@ Stop-Process -Name:komorebi -ErrorAction SilentlyContinue let hwnds: Vec = serde_json::from_reader(reader)?; for hwnd in hwnds { - restore_window(HWND(hwnd)); + restore_window(hwnd); } } Err(error) => { @@ -2297,7 +2297,7 @@ Stop-Process -Name:komorebi -ErrorAction SilentlyContinue let hwnds: Vec = serde_json::from_reader(reader)?; for hwnd in hwnds { - restore_window(HWND(hwnd)); + restore_window(hwnd); } } SubCommand::ResizeEdge(resize) => { @@ -2595,11 +2595,11 @@ fn show_window(hwnd: HWND, command: SHOW_WINDOW_CMD) { }; } -fn remove_transparency(hwnd: HWND) { - let _ = komorebi_client::Window::from(hwnd.0).opaque(); +fn remove_transparency(hwnd: isize) { + let _ = komorebi_client::Window::from(hwnd).opaque(); } -fn restore_window(hwnd: HWND) { - show_window(hwnd, SW_RESTORE); +fn restore_window(hwnd: isize) { + show_window(HWND(hwnd as *mut core::ffi::c_void), SW_RESTORE); remove_transparency(hwnd); }