document the remaining win32 safety contracts
This commit is contained in:
@@ -1212,6 +1212,7 @@ fn start_overlay_clickthrough_poll(
|
||||
}
|
||||
|
||||
let mut pt = POINT::default();
|
||||
// SAFETY: `pt` is a live local that GetCursorPos writes through.
|
||||
if unsafe { GetCursorPos(&raw mut pt) }.is_err() {
|
||||
continue;
|
||||
}
|
||||
@@ -1225,6 +1226,7 @@ fn start_overlay_clickthrough_poll(
|
||||
None => continue,
|
||||
};
|
||||
|
||||
// SAFETY: `raw` is the overlay's own live HWND from Tauri.
|
||||
let dpi = unsafe { GetDpiForWindow(HWND(raw.0.cast::<std::ffi::c_void>())) };
|
||||
let scale = if dpi > 0 { f64::from(dpi) / 96.0 } else { 1.0 };
|
||||
let border = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
|
||||
@@ -1256,6 +1258,8 @@ fn mark_overlay_excluded_from_capture(win: &tauri::WebviewWindow) -> Result<(),
|
||||
};
|
||||
let raw = win.hwnd().map_err(|e| format!("hwnd: {e}"))?;
|
||||
let hwnd = HWND(raw.0.cast::<std::ffi::c_void>());
|
||||
// SAFETY: `hwnd` is the overlay's live window handle and the affinity flag
|
||||
// is a plain enum with no pointer arguments.
|
||||
unsafe {
|
||||
SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE)
|
||||
.map_err(|e| format!("SetWindowDisplayAffinity: {e}"))?;
|
||||
@@ -1288,11 +1292,21 @@ fn show_overlay(app: AppHandle) -> Result<(), String> {
|
||||
if let Ok(raw) = win.hwnd() {
|
||||
if let Some((vl, vt, vr, vb)) = visible_window_bounds(raw.0 as isize) {
|
||||
let center = POINT { x: i32::midpoint(vl, vr), y: i32::midpoint(vt, vb) };
|
||||
// MonitorFromPoint only reads the POINT by value and returns null
|
||||
// for "no monitor", which the next branch handles.
|
||||
//
|
||||
// SAFETY: the POINTs passed in are plain values constructed here.
|
||||
let hmon = unsafe { MonitorFromPoint(center, MONITOR_DEFAULTTONULL) };
|
||||
if hmon.0.is_null() {
|
||||
let hwnd = HWND(raw.0.cast::<std::ffi::c_void>());
|
||||
// SAFETY: same value-only call; MONITOR_DEFAULTTOPRIMARY cannot
|
||||
// fail on a running desktop.
|
||||
let primary = unsafe { MonitorFromPoint(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY) };
|
||||
// cbSize is required by GetMonitorInfoW and is set from the
|
||||
// actual struct size, so the write below stays in bounds.
|
||||
let mut mi = MONITORINFO { cbSize: size_of::<MONITORINFO>() as u32, ..Default::default() };
|
||||
// SAFETY: `mi` is a live local of the declared size, and the
|
||||
// result is checked with as_bool() before mi is read.
|
||||
if unsafe { GetMonitorInfoW(primary, &raw mut mi) }.as_bool() {
|
||||
let work = mi.rcWork;
|
||||
let w = vr - vl;
|
||||
@@ -1300,6 +1314,8 @@ fn show_overlay(app: AppHandle) -> Result<(), String> {
|
||||
let cx = i32::midpoint(work.left, work.right) - w / 2;
|
||||
let cy = i32::midpoint(work.top, work.bottom) - h / 2;
|
||||
drop(
|
||||
// SAFETY: `hwnd` is the overlay's live handle; the flags
|
||||
// keep this a size-only, non-activating move.
|
||||
unsafe { SetWindowPos(hwnd, Some(HWND_TOP), cx, cy, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE) },
|
||||
);
|
||||
}
|
||||
@@ -1400,6 +1416,8 @@ fn enforce_overlay_aspect(app: AppHandle, state: State<'_, AppState>) -> Result<
|
||||
let titlebar = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
|
||||
let outer_w = sz.width.cast_signed().max(2);
|
||||
let outer_h = (target_h * scale + f64::from(titlebar) + f64::from(border) * 2.0).round() as i32;
|
||||
// SAFETY: `hwnd` is the overlay's live handle; the flags keep the resize
|
||||
// non-activating and non-reordering, and no pointers are involved.
|
||||
unsafe {
|
||||
SetWindowPos(hwnd, Some(HWND_TOP), 0, 0, outer_w, outer_h,
|
||||
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE)
|
||||
@@ -1454,8 +1472,11 @@ fn open_window_picker(
|
||||
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
|
||||
let hwnd = HWND(h.0.cast::<std::ffi::c_void>());
|
||||
let mut gr = RECT::default();
|
||||
// SAFETY: `gr` is a live local that GetWindowRect writes through.
|
||||
drop(unsafe { GetWindowRect(hwnd, &raw mut gr) });
|
||||
let mut dr = RECT::default();
|
||||
// SAFETY: `dr` is a live RECT local, the attribute is the documented
|
||||
// DWMWA_EXTENDED_FRAME_BOUNDS, and the size passed matches its type.
|
||||
drop(unsafe {
|
||||
DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
@@ -1501,6 +1522,8 @@ fn visible_window_bounds(hwnd_raw: isize) -> Option<(i32, i32, i32, i32)> {
|
||||
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
|
||||
let hwnd = HWND(hwnd_raw as *mut std::ffi::c_void);
|
||||
let mut dwm = RECT::default();
|
||||
// SAFETY: `dwm` is a live RECT local and the size argument matches it; the
|
||||
// result is checked and the rect is only read when it is well-formed.
|
||||
if unsafe {
|
||||
DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
@@ -1516,6 +1539,7 @@ fn visible_window_bounds(hwnd_raw: isize) -> Option<(i32, i32, i32, i32)> {
|
||||
return Some((dwm.left, dwm.top, dwm.right, dwm.bottom));
|
||||
}
|
||||
let mut outer = RECT::default();
|
||||
// SAFETY: `outer` is a live RECT local that the call writes through.
|
||||
if unsafe { GetWindowRect(hwnd, &raw mut outer) }.is_ok() {
|
||||
Some((outer.left, outer.top, outer.right, outer.bottom))
|
||||
} else {
|
||||
@@ -1530,10 +1554,12 @@ fn dwm_insets(hwnd_raw: isize) -> (i32, i32, i32, i32) {
|
||||
|
||||
let hwnd = HWND(hwnd_raw as *mut std::ffi::c_void);
|
||||
let mut outer = RECT::default();
|
||||
// SAFETY: `outer` is a live RECT local that the call writes through.
|
||||
if unsafe { GetWindowRect(hwnd, &raw mut outer) }.is_err() {
|
||||
return (0, 0, 0, 0);
|
||||
}
|
||||
let mut dwm = RECT::default();
|
||||
// SAFETY: `dwm` is a live RECT local and the size argument matches it.
|
||||
let r = unsafe {
|
||||
DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
@@ -1560,12 +1586,15 @@ fn scale_factor_at_point(x: i32, y: i32) -> f64 {
|
||||
use windows::Win32::UI::HiDpi::{GetDpiForMonitor, MONITOR_DPI_TYPE};
|
||||
|
||||
let pt = POINT { x, y };
|
||||
// SAFETY: value-only call; a null result is handled on the next line.
|
||||
let hmonitor = unsafe { MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST) };
|
||||
if hmonitor.0.is_null() {
|
||||
return 1.0;
|
||||
}
|
||||
let mut dpi_x = 0u32;
|
||||
let mut dpi_y = 0u32;
|
||||
// SAFETY: both out-params are live u32 locals that the call writes through,
|
||||
// and `hmonitor` was checked non-null above.
|
||||
let hr = unsafe {
|
||||
GetDpiForMonitor(
|
||||
hmonitor,
|
||||
@@ -1627,6 +1656,8 @@ fn set_overlay_to_window(
|
||||
SetWindowPos, HWND_TOP, SWP_NOACTIVATE, SWP_NOZORDER,
|
||||
};
|
||||
let hwnd = HWND(raw.0.cast::<std::ffi::c_void>());
|
||||
// SAFETY: `hwnd` is the overlay's live handle and every dimension is a
|
||||
// plain integer computed above; no pointers are passed.
|
||||
unsafe {
|
||||
SetWindowPos(
|
||||
hwnd,
|
||||
|
||||
Reference in New Issue
Block a user