fix(app): the virtual monitor's driver never changes under running services and its failures can be retried
- installing or removing the driver is refused unless the services are stopped (or failed), and a service start or restart (and a second driver task) is refused while one runs, so the monitor never vanishes under the bot browser's window - a failed or declined install undoes what that attempt made (device, and the registry value and settings folder when the driver was not ours before); a declined install's 0xE0000243 reads as cancelled; installed means ours with its monitor there - devices are found among all root devices whatever their class: a device whose driver was declined drops out of the display class and was left behind - uninstall keeps the registry value (what makes it ours) until the device and the driver package are gone (a package not in the store, 1168, counts as gone), so a failed removal can be retried - checked on the win-ci VM: a declined install leaves nothing, the next install gives one working device, the uninstall leaves nothing Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -232,10 +232,12 @@ mod win {
|
||||
devices().into_iter().find(|d| d.string == ADAPTER_NAME)
|
||||
}
|
||||
|
||||
/// Installed means ours with its monitor there (the driver loaded), so a device left without
|
||||
/// its driver shows as not installed and can be installed again.
|
||||
pub fn status() -> DriverStatus {
|
||||
let present = adapter().is_some() || !hardware_devices(false).is_empty();
|
||||
let present = adapter().is_some() || !hardware_devices().is_empty();
|
||||
let ours = ours();
|
||||
DriverStatus { supported: true, installed: ours && present, other: present && !ours }
|
||||
DriverStatus { supported: true, installed: ours && adapter().is_some(), other: present && !ours }
|
||||
}
|
||||
|
||||
/// Put the virtual monitor on the desktop at `corner` and return where it is.
|
||||
@@ -316,9 +318,10 @@ mod win {
|
||||
changed
|
||||
}
|
||||
|
||||
/// The display-class devices with the driver's hardware ID (`present`: only those plugged in).
|
||||
fn hardware_devices(present: bool) -> Vec<SP_DEVINFO_DATA> {
|
||||
let (set, list) = hardware_set(present);
|
||||
/// The root devices with the driver's hardware ID, in any class (a device whose driver install
|
||||
/// was declined has none), present or not.
|
||||
fn hardware_devices() -> Vec<SP_DEVINFO_DATA> {
|
||||
let (set, list) = hardware_set();
|
||||
if set != INVALID_HANDLE_VALUE as HDEVINFO {
|
||||
// SAFETY: the list from `hardware_set`, no longer used.
|
||||
unsafe { SetupDiDestroyDeviceInfoList(set) };
|
||||
@@ -326,9 +329,11 @@ mod win {
|
||||
list
|
||||
}
|
||||
|
||||
fn hardware_set(present: bool) -> (HDEVINFO, Vec<SP_DEVINFO_DATA>) {
|
||||
// SAFETY: a device list for the display class; destroyed by the callers that keep it.
|
||||
let set = unsafe { SetupDiGetClassDevsW(&GUID_DEVCLASS_DISPLAY, std::ptr::null(), std::ptr::null_mut(), if present { DIGCF_PRESENT } else { 0 }) };
|
||||
fn hardware_set() -> (HDEVINFO, Vec<SP_DEVINFO_DATA>) {
|
||||
let root = wide("ROOT");
|
||||
let flags = DIGCF_ALLCLASSES;
|
||||
// SAFETY: a device list of the root enumerator; destroyed by the callers that keep it.
|
||||
let set = unsafe { SetupDiGetClassDevsW(std::ptr::null(), root.as_ptr(), std::ptr::null_mut(), flags) };
|
||||
let mut out = vec![];
|
||||
if set == INVALID_HANDLE_VALUE as HDEVINFO {
|
||||
return (set, out);
|
||||
@@ -361,16 +366,33 @@ mod win {
|
||||
}
|
||||
|
||||
/// Elevated: settings folder, the driver's registry value, the device, and its driver. Windows
|
||||
/// asks the user once whether to install software from the SignPath Foundation.
|
||||
/// asks the user once whether to install software from the SignPath Foundation. A failure (also
|
||||
/// the user saying no) undoes what this attempt made, so nothing looks installed and the next
|
||||
/// attempt starts clean.
|
||||
pub(super) fn install(driver: &Path) -> Result<(), String> {
|
||||
let inf = driver.join("MttVDD.inf");
|
||||
if !inf.is_file() {
|
||||
return Err("드라이버 파일이 없습니다 (display-driver\\MttVDD.inf)".into());
|
||||
}
|
||||
let (present, ours) = (adapter().is_some() || !hardware_devices(false).is_empty(), ours());
|
||||
if present && !ours {
|
||||
let (was_ours, had_device) = (ours(), !hardware_devices().is_empty());
|
||||
if (had_device || adapter().is_some()) && !was_ours {
|
||||
return Err("다른 프로그램이 설치한 가상 디스플레이 드라이버가 이미 있습니다".into());
|
||||
}
|
||||
let result = install_steps(&inf, had_device);
|
||||
if result.is_err() {
|
||||
if !had_device {
|
||||
remove_devices();
|
||||
}
|
||||
if !was_ours {
|
||||
let _ = uninstall_package(&inf);
|
||||
let _ = run("reg", &["delete", &format!("HKLM\\{REG_KEY}"), "/f"]);
|
||||
remove_config_dir();
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn install_steps(inf: &Path, had_device: bool) -> Result<(), String> {
|
||||
let dir = config_dir().ok_or("ProgramData 폴더를 찾지 못했습니다")?;
|
||||
std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
std::fs::write(dir.join("vdd_settings.xml"), settings_xml()).map_err(|e| e.to_string())?;
|
||||
@@ -380,16 +402,19 @@ mod win {
|
||||
run("icacls", &[&d, "/inheritance:r", "/grant:r", "*S-1-5-18:(OI)(CI)F", "*S-1-5-32-544:(OI)(CI)F",
|
||||
"*S-1-5-19:(OI)(CI)RX", "*S-1-5-32-545:(OI)(CI)RX"])?;
|
||||
run("reg", &["add", &format!("HKLM\\{REG_KEY}"), "/v", "VDDPATH", "/t", "REG_SZ", "/d", &d, "/f"])?;
|
||||
if hardware_devices(false).is_empty() {
|
||||
if !had_device {
|
||||
create_device()?;
|
||||
}
|
||||
let (hw, inf_w) = (wide(HARDWARE_ID), wide_path(&inf));
|
||||
let (hw, inf_w) = (wide(HARDWARE_ID), wide_path(inf));
|
||||
let mut reboot: BOOL = 0;
|
||||
// SAFETY: valid NUL-terminated strings; no parent window.
|
||||
if unsafe { UpdateDriverForPlugAndPlayDevicesW(std::ptr::null_mut(), hw.as_ptr(), inf_w.as_ptr(), INSTALLFLAG_FORCE, &mut reboot) } == 0 {
|
||||
// SAFETY: plain call.
|
||||
let e = unsafe { GetLastError() };
|
||||
return Err(if e == ERROR_CANCELLED { "드라이버 설치를 취소했습니다".into() } else { format!("드라이버를 설치하지 못했습니다 ({e})") });
|
||||
// No to Windows' question about the publisher comes back as "publisher not trusted".
|
||||
const PUBLISHER_NOT_TRUSTED: u32 = 0xE000_0243;
|
||||
return Err(if e == ERROR_CANCELLED || e == PUBLISHER_NOT_TRUSTED { "드라이버 설치를 취소했습니다".into() }
|
||||
else { format!("드라이버를 설치하지 못했습니다 ({e})") });
|
||||
}
|
||||
// The new monitor joins the desktop at once; it stays off until the bot browser uses it.
|
||||
for _ in 0..50 {
|
||||
@@ -402,6 +427,41 @@ mod win {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove every device with the driver's hardware ID; returns how many are left.
|
||||
fn remove_devices() -> usize {
|
||||
let (set, list) = hardware_set();
|
||||
for di in &list {
|
||||
// SAFETY: removing a device of the list `set`, which is destroyed after the loop.
|
||||
unsafe { SetupDiCallClassInstaller(DIF_REMOVE, set, di) };
|
||||
}
|
||||
if set != INVALID_HANDLE_VALUE as HDEVINFO {
|
||||
// SAFETY: the list from `hardware_set`.
|
||||
unsafe { SetupDiDestroyDeviceInfoList(set) };
|
||||
}
|
||||
hardware_devices().len()
|
||||
}
|
||||
|
||||
/// Remove the driver package from the driver store; `Err` with the Windows error code.
|
||||
fn uninstall_package(inf: &Path) -> Result<(), u32> {
|
||||
let inf = wide_path(inf);
|
||||
let mut reboot: BOOL = 0;
|
||||
// SAFETY: valid NUL-terminated path; no parent window.
|
||||
if unsafe { DiUninstallDriverW(std::ptr::null_mut(), inf.as_ptr(), 0, &mut reboot) } != 0 {
|
||||
return Ok(());
|
||||
}
|
||||
// SAFETY: plain call.
|
||||
Err(unsafe { GetLastError() })
|
||||
}
|
||||
|
||||
fn remove_config_dir() {
|
||||
if let Some(dir) = config_dir() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
if let Some(parent) = dir.parent() {
|
||||
let _ = std::fs::remove_dir(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_device() -> Result<(), String> {
|
||||
let (name, hw) = (wide("Display"), {
|
||||
let mut v: Vec<u16> = HARDWARE_ID.encode_utf16().collect();
|
||||
@@ -426,32 +486,19 @@ mod win {
|
||||
}
|
||||
|
||||
/// Elevated: remove the device, the driver, the registry value and the settings folder; only
|
||||
/// when this app installed it.
|
||||
/// when this app installed it. The registry value (what makes it ours) goes only once the
|
||||
/// device and the driver are gone, so a failed removal can be retried.
|
||||
pub(super) fn uninstall(driver: &Path) -> Result<(), String> {
|
||||
if !ours() {
|
||||
return Err("이 앱이 설치한 가상 디스플레이 드라이버가 아닙니다".into());
|
||||
}
|
||||
let (set, list) = hardware_set(false);
|
||||
for di in &list {
|
||||
// SAFETY: removing a device of the list `set`, which is destroyed after the loop.
|
||||
unsafe { SetupDiCallClassInstaller(DIF_REMOVE, set, di) };
|
||||
}
|
||||
if set != INVALID_HANDLE_VALUE as HDEVINFO {
|
||||
// SAFETY: the list from `hardware_set`.
|
||||
unsafe { SetupDiDestroyDeviceInfoList(set) };
|
||||
}
|
||||
let inf = wide_path(&driver.join("MttVDD.inf"));
|
||||
let mut reboot: BOOL = 0;
|
||||
// SAFETY: valid NUL-terminated path; a driver not in the store is just a failure we ignore.
|
||||
unsafe { DiUninstallDriverW(std::ptr::null_mut(), inf.as_ptr(), 0, &mut reboot) };
|
||||
let _ = run("reg", &["delete", &format!("HKLM\\{REG_KEY}"), "/f"]);
|
||||
if let Some(dir) = config_dir() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
if let Some(parent) = dir.parent() {
|
||||
let _ = std::fs::remove_dir(parent);
|
||||
}
|
||||
}
|
||||
if hardware_devices(false).is_empty() { Ok(()) } else { Err("가상 모니터 장치를 지우지 못했습니다".into()) }
|
||||
let left = remove_devices();
|
||||
let package = if left == 0 { uninstall_package(&driver.join("MttVDD.inf")) } else { Ok(()) };
|
||||
removal_done(left, package)?;
|
||||
run("reg", &["delete", &format!("HKLM\\{REG_KEY}"), "/f"])
|
||||
.map_err(|e| format!("드라이버 등록 정보를 지우지 못했습니다: {e}"))?;
|
||||
remove_config_dir();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run `jarvis.exe --display-driver <action>` as administrator (Windows asks the user) and wait.
|
||||
@@ -494,10 +541,31 @@ mod win {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a removal may forget the driver was ours: no device left, and the driver package
|
||||
/// removed or not in the driver store (Windows error 1168, ERROR_NOT_FOUND).
|
||||
#[cfg_attr(not(windows), allow(dead_code))]
|
||||
fn removal_done(devices_left: usize, package: Result<(), u32>) -> Result<(), String> {
|
||||
if devices_left > 0 {
|
||||
return Err(format!("가상 모니터 장치 {devices_left}개를 지우지 못했습니다. 다시 시도해 주세요"));
|
||||
}
|
||||
match package {
|
||||
Ok(()) | Err(1168) => Ok(()),
|
||||
Err(e) => Err(format!("드라이버 패키지를 지우지 못했습니다 ({e}). 다시 시도해 주세요")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_removal_forgets_the_driver_only_when_device_and_package_are_gone() {
|
||||
assert_eq!(removal_done(0, Ok(())), Ok(()));
|
||||
assert_eq!(removal_done(0, Err(1168)), Ok(()), "a package not in the store is already gone");
|
||||
assert!(removal_done(1, Ok(())).is_err(), "a device left keeps it ours, so it can be retried");
|
||||
assert!(removal_done(0, Err(5)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_monitor_touches_the_rightmost_monitor_only_at_its_corner() {
|
||||
let one = [Rect { x: 0, y: 0, w: 1920, h: 1080 }];
|
||||
|
||||
Reference in New Issue
Block a user