diff --git a/src/capture.rs b/src/capture.rs index 1516d11..00cb375 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -187,21 +187,23 @@ impl CaptureControl { #[derive(thiserror::Error, Eq, PartialEq, Clone, Debug)] pub enum GraphicsCaptureApiError { - #[error("Failed To Join Thread")] + #[error("Failed to join thread")] FailedToJoinThread, - #[error("Failed To Initialize WinRT")] + #[error("Failed to initialize WinRT")] FailedToInitWinRT, - #[error("Failed To Create Dispatcher Queue Controller")] + #[error("Failed to translate message")] + FailedToTranslateMessage, + #[error("Failed to create dispatcher queue controller")] FailedToCreateDispatcherQueueController, - #[error("Failed To Shutdown Dispatcher Queue")] + #[error("Failed to shutdown dispatcher queue")] FailedToShutdownDispatcherQueue, - #[error("Failed To Set Dispatcher Queue Completed Handler")] + #[error("Failed to set dispatcher queue completed handler")] FailedToSetDispatcherQueueCompletedHandler, - #[error("Graphics Capture Error")] + #[error("Graphics capture error")] GraphicsCaptureApiError(graphics_capture_api::Error), - #[error("New Handler Error")] + #[error("New handler error")] NewHandlerError(E), - #[error("Frame Handler Error")] + #[error("Frame handler error")] FrameHandlerError(E), } @@ -271,7 +273,9 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message); + TranslateMessage(&message) + .ok() + .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; DispatchMessageW(&message); } } @@ -293,7 +297,9 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message); + TranslateMessage(&message) + .ok() + .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; DispatchMessageW(&message); } } @@ -384,7 +390,9 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message); + TranslateMessage(&message) + .ok() + .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; DispatchMessageW(&message); } } @@ -409,7 +417,9 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message); + TranslateMessage(&message) + .ok() + .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; DispatchMessageW(&message); } } diff --git a/src/monitor.rs b/src/monitor.rs index 1df90d7..13fe481 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -129,7 +129,7 @@ impl Monitor { }; if unsafe { !GetMonitorInfoW( - self.as_raw_hmonitor(), + HMONITOR(self.as_raw_hmonitor()), std::ptr::addr_of_mut!(monitor_info).cast(), ) .as_bool() @@ -242,7 +242,7 @@ impl Monitor { }; if unsafe { !GetMonitorInfoW( - self.as_raw_hmonitor(), + HMONITOR(self.as_raw_hmonitor()), std::ptr::addr_of_mut!(monitor_info).cast(), ) .as_bool() @@ -280,7 +280,7 @@ impl Monitor { }; if unsafe { !GetMonitorInfoW( - self.as_raw_hmonitor(), + HMONITOR(self.as_raw_hmonitor()), std::ptr::addr_of_mut!(monitor_info).cast(), ) .as_bool() @@ -422,16 +422,18 @@ impl Monitor { /// /// # Arguments /// - /// * `monitor` - The raw HMONITOR. + /// * `hmonitor` - The raw HMONITOR. #[must_use] - pub const fn from_raw_hmonitor(monitor: HMONITOR) -> Self { - Self { monitor } + pub const fn from_raw_hmonitor(monitor: isize) -> Self { + Self { + monitor: HMONITOR(monitor), + } } /// Returns the raw HMONITOR of the monitor. #[must_use] - pub const fn as_raw_hmonitor(&self) -> HMONITOR { - self.monitor + pub const fn as_raw_hmonitor(&self) -> isize { + self.monitor.0 } // Callback Used For Enumerating All Monitors @@ -454,7 +456,7 @@ impl TryFrom for GraphicsCaptureItem { type Error = Error; fn try_from(value: Monitor) -> Result { - let monitor = value.as_raw_hmonitor(); + let monitor = HMONITOR(value.as_raw_hmonitor()); let interop = windows::core::factory::()?; Ok(unsafe { interop.CreateForMonitor(monitor)? }) diff --git a/src/window.rs b/src/window.rs index fae9503..03da2cc 100644 --- a/src/window.rs +++ b/src/window.rs @@ -148,36 +148,32 @@ impl Window { if monitor.is_invalid() { None } else { - Some(Monitor::from_raw_hmonitor(monitor)) + Some(Monitor::from_raw_hmonitor(monitor.0)) } } /// Checks if the window is a valid window. /// - /// # Arguments - /// - /// * `window` - The window handle to check. - /// /// # Returns /// /// Returns `true` if the window is valid, `false` otherwise. #[must_use] - pub fn is_window_valid(window: HWND) -> bool { - if !unsafe { IsWindowVisible(window).as_bool() } { + pub fn is_valid(&self) -> bool { + if !unsafe { IsWindowVisible(self.window).as_bool() } { return false; } let mut id = 0; - unsafe { GetWindowThreadProcessId(window, Some(&mut id)) }; + unsafe { GetWindowThreadProcessId(self.window, Some(&mut id)) }; if id == unsafe { GetCurrentProcessId() } { return false; } let mut rect = RECT::default(); - let result = unsafe { GetClientRect(window, &mut rect) }; + let result = unsafe { GetClientRect(self.window, &mut rect) }; if result.is_ok() { - let styles = unsafe { GetWindowLongPtrW(window, GWL_STYLE) }; - let ex_styles = unsafe { GetWindowLongPtrW(window, GWL_EXSTYLE) }; + let styles = unsafe { GetWindowLongPtrW(self.window, GWL_STYLE) }; + let ex_styles = unsafe { GetWindowLongPtrW(self.window, GWL_EXSTYLE) }; if (ex_styles & isize::try_from(WS_EX_TOOLWINDOW.0).unwrap()) != 0 { return false; @@ -216,10 +212,10 @@ impl Window { /// /// # Arguments /// - /// * `hwnd` - The hwnd. + /// * `hwnd` - The raw HWND. #[must_use] pub const fn from_raw_hwnd(hwnd: isize) -> Self { - Self { window: HWND(hwnd)} + Self { window: HWND(hwnd) } } /// Returns the raw HWND of the window. @@ -232,7 +228,7 @@ impl Window { unsafe extern "system" fn enum_windows_callback(window: HWND, vec: LPARAM) -> BOOL { let windows = &mut *(vec.0 as *mut Vec); - if Self::is_window_valid(window) { + if Self::from_raw_hwnd(window.0).is_valid() { windows.push(Self { window }); }