document the remaining win32 safety contracts
This commit is contained in:
@@ -14,24 +14,34 @@ use windows::Win32::UI::WindowsAndMessaging::{
|
||||
IsWindowVisible, GWL_EXSTYLE, WS_EX_TOOLWINDOW,
|
||||
};
|
||||
|
||||
unsafe fn visible_window_rect(hwnd: HWND) -> Option<RECT> { unsafe {
|
||||
/// Resolve the visible frame of a window: the DWM extended frame when the
|
||||
/// compositor reports one, otherwise the raw window rect.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must pass a live `HWND`.
|
||||
unsafe fn visible_window_rect(hwnd: HWND) -> Option<RECT> {
|
||||
let mut rect = RECT::default();
|
||||
let r = DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
DWMWA_EXTENDED_FRAME_BOUNDS,
|
||||
(&raw mut rect).cast::<std::ffi::c_void>(),
|
||||
size_of::<RECT>() as u32,
|
||||
);
|
||||
// SAFETY: `rect` is a live RECT local and the size argument matches it.
|
||||
let r = unsafe {
|
||||
DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
DWMWA_EXTENDED_FRAME_BOUNDS,
|
||||
(&raw mut rect).cast::<std::ffi::c_void>(),
|
||||
size_of::<RECT>() as u32,
|
||||
)
|
||||
};
|
||||
if r.is_ok() && rect.right > rect.left && rect.bottom > rect.top {
|
||||
return Some(rect);
|
||||
}
|
||||
let mut fallback = RECT::default();
|
||||
if GetWindowRect(hwnd, &raw mut fallback).is_ok() {
|
||||
// SAFETY: `fallback` is a live RECT local that the call writes through.
|
||||
if unsafe { GetWindowRect(hwnd, &raw mut fallback) }.is_ok() {
|
||||
Some(fallback)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
const VK_LBUTTON: i32 = 0x01;
|
||||
const VK_ESCAPE: i32 = 0x1B;
|
||||
@@ -68,17 +78,33 @@ const SHELL_CLASSES: &[&str] = &[
|
||||
"Windows.UI.Core.CoreWindow",
|
||||
];
|
||||
|
||||
unsafe fn class_of(hwnd: HWND) -> String { unsafe {
|
||||
/// Read a window's class name into a fixed buffer.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must pass a live `HWND`.
|
||||
unsafe fn class_of(hwnd: HWND) -> String {
|
||||
let mut buf = [0u16; 128];
|
||||
let n = GetClassNameW(hwnd, &mut buf);
|
||||
// SAFETY: GetClassNameW writes at most buf.len() UTF-16 units into `buf`;
|
||||
// it returns the count written, which is bounds-checked before slicing.
|
||||
let n = unsafe { GetClassNameW(hwnd, &mut buf) };
|
||||
if n <= 0 {
|
||||
return String::new();
|
||||
}
|
||||
String::from_utf16_lossy(&buf[..n as usize])
|
||||
}}
|
||||
}
|
||||
|
||||
unsafe extern "system" fn enum_at_point(hwnd: HWND, lparam: LPARAM) -> BOOL { unsafe {
|
||||
let ctx = &mut *(lparam.0 as *mut EnumCtx);
|
||||
/// `EnumWindows` callback: records the first top-level window containing the
|
||||
/// target point. Returning FALSE stops the enumeration.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `EnumWindows` hands back the `LPARAM` that `top_window_at` passed in, which
|
||||
/// is a live `*mut EnumCtx` for the duration of the enumeration.
|
||||
unsafe extern "system" fn enum_at_point(hwnd: HWND, lparam: LPARAM) -> BOOL {
|
||||
// SAFETY: `lparam` is the pointer passed to EnumWindows by top_window_at,
|
||||
// and that EnumCtx outlives the call.
|
||||
let ctx = unsafe { &mut *(lparam.0 as *mut EnumCtx) };
|
||||
if ctx.found.is_some() {
|
||||
return BOOL(1);
|
||||
}
|
||||
@@ -86,21 +112,29 @@ unsafe extern "system" fn enum_at_point(hwnd: HWND, lparam: LPARAM) -> BOOL { un
|
||||
if ctx.exclude.contains(&id) {
|
||||
return BOOL(1);
|
||||
}
|
||||
if !IsWindowVisible(hwnd).as_bool() {
|
||||
// The checks below are all read-only queries against the window handle
|
||||
// EnumWindows just handed us, so it is live for this call.
|
||||
//
|
||||
// SAFETY: `hwnd` is a live top-level window handle.
|
||||
if !unsafe { IsWindowVisible(hwnd) }.as_bool() {
|
||||
return BOOL(1);
|
||||
}
|
||||
if IsIconic(hwnd).as_bool() {
|
||||
// SAFETY: same live handle.
|
||||
if unsafe { IsIconic(hwnd) }.as_bool() {
|
||||
return BOOL(1);
|
||||
}
|
||||
let ex = GetWindowLongW(hwnd, GWL_EXSTYLE).cast_unsigned();
|
||||
// SAFETY: same live handle; GWL_EXSTYLE reads a window style word.
|
||||
let ex = unsafe { GetWindowLongW(hwnd, GWL_EXSTYLE) }.cast_unsigned();
|
||||
if (ex & WS_EX_TOOLWINDOW.0) != 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let class_name = class_of(hwnd);
|
||||
// SAFETY: `hwnd` is the live handle described above.
|
||||
let class_name = unsafe { class_of(hwnd) };
|
||||
if SHELL_CLASSES.iter().any(|c| class_name == *c) {
|
||||
return BOOL(1);
|
||||
}
|
||||
let rect = match visible_window_rect(hwnd) {
|
||||
// SAFETY: `hwnd` is the live handle described above.
|
||||
let rect = match unsafe { visible_window_rect(hwnd) } {
|
||||
Some(r) => r,
|
||||
None => return BOOL(1),
|
||||
};
|
||||
@@ -118,7 +152,7 @@ unsafe extern "system" fn enum_at_point(hwnd: HWND, lparam: LPARAM) -> BOOL { un
|
||||
}
|
||||
ctx.found = Some((id, rect));
|
||||
BOOL(0)
|
||||
}}
|
||||
}
|
||||
|
||||
fn top_window_at(pt: POINT, exclude: &[isize]) -> Option<(isize, RECT)> {
|
||||
let mut ctx = EnumCtx {
|
||||
@@ -126,6 +160,8 @@ fn top_window_at(pt: POINT, exclude: &[isize]) -> Option<(isize, RECT)> {
|
||||
exclude: exclude.to_vec(),
|
||||
found: None,
|
||||
};
|
||||
// SAFETY: `ctx` is a live local of exactly the type enum_at_point expects,
|
||||
// and it outlives the call because EnumWindows runs to completion here.
|
||||
unsafe {
|
||||
drop(EnumWindows(
|
||||
Some(enum_at_point),
|
||||
@@ -149,6 +185,7 @@ pub(crate) fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession
|
||||
}
|
||||
|
||||
let mut pt = POINT::default();
|
||||
// SAFETY: `pt` is a live local that GetCursorPos writes through.
|
||||
if unsafe { GetCursorPos(&raw mut pt) }.is_err() {
|
||||
thread::sleep(Duration::from_millis(30));
|
||||
continue;
|
||||
@@ -158,6 +195,7 @@ pub(crate) fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession
|
||||
|
||||
if let Some((id, rect)) = target {
|
||||
if id != last_hwnd {
|
||||
// SAFETY: `id` came from EnumWindows, so it is a live HWND.
|
||||
let class = unsafe { class_of(HWND(id as *mut std::ffi::c_void)) };
|
||||
tracing::info!(
|
||||
"picker hover: hwnd={:x} class={} rect=({},{}) {}x{}",
|
||||
@@ -185,6 +223,7 @@ pub(crate) fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession
|
||||
}
|
||||
|
||||
let pressed =
|
||||
// SAFETY: a self-contained query with no pointer arguments.
|
||||
unsafe { (GetAsyncKeyState(VK_LBUTTON).cast_unsigned() & 0x8000) != 0 };
|
||||
if pressed && !last_pressed {
|
||||
if let Some((_, rect)) = target {
|
||||
@@ -203,6 +242,7 @@ pub(crate) fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession
|
||||
last_pressed = pressed;
|
||||
|
||||
let esc =
|
||||
// SAFETY: a self-contained query with no pointer arguments.
|
||||
unsafe { (GetAsyncKeyState(VK_ESCAPE).cast_unsigned() & 0x8000) != 0 };
|
||||
if esc && !last_esc {
|
||||
drop(app.emit("picker-cancel", ()));
|
||||
|
||||
Reference in New Issue
Block a user