use super::{CaptureRequest, Display, Launched, Shot, Window, xcap_common as common};
use windows::Win32::{
Foundation::{HWND, LPARAM, POINT, WPARAM},
UI::WindowsAndMessaging::{
GetCursorPos, PostMessageW, SW_MAXIMIZE, SW_MINIMIZE, SW_RESTORE, SetForegroundWindow,
ShowWindow, WM_CLOSE,
},
};
pub fn available() -> bool {
true
}
pub fn describe() -> String {
"windows, capture: xcap, input: enigo (SendInput), windows: Win32".to_string()
}
pub fn displays() -> Result<Vec<Display>, String> {
common::displays()
}
pub fn windows() -> Result<Vec<Window>, String> {
common::windows()
}
pub fn focus_window(id: u64) -> Result<(), String> {
let hwnd = HWND(id as usize as *mut core::ffi::c_void);
let ok = unsafe { SetForegroundWindow(hwnd) };
if ok.as_bool() {
Ok(())
} else {
Err(format!(
"Windows refused to bring window {id} to the foreground (the foreground lock is held by another process)"
))
}
}
pub fn window_action(id: u64, action: &str) -> Result<String, String> {
let hwnd = HWND(id as usize as *mut core::ffi::c_void);
match action {
"close" => unsafe {
PostMessageW(Some(hwnd), WM_CLOSE, WPARAM(0), LPARAM(0))
.map_err(|e| format!("closing window {id} failed: {e}"))?;
Ok(format!("asked window {id} to close"))
},
"minimize" => unsafe {
_ = ShowWindow(hwnd, SW_MINIMIZE);
Ok(format!("minimized window {id}"))
},
"maximize" => unsafe {
_ = ShowWindow(hwnd, SW_MAXIMIZE);
Ok(format!("maximized window {id}"))
},
"restore" => unsafe {
_ = ShowWindow(hwnd, SW_RESTORE);
Ok(format!("restored window {id}"))
},
other => Err(format!("unknown window action {other:?}")),
}
}
pub fn capture(req: &CaptureRequest) -> Result<Shot, String> {
common::capture(req)
}
pub fn cursor() -> Option<(i32, i32)> {
let mut point = POINT::default();
unsafe { GetCursorPos(&mut point).ok()? };
Some((point.x, point.y))
}
pub fn launch(target: &str, args: &[String], wait_ms: u64) -> Result<Launched, String> {
common::launch(target, args, wait_ms)
}