fix check virtual display on windows

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-05-18 21:44:09 +08:00
parent c6ccee67aa
commit b8ea705a21

View File

@ -1891,25 +1891,25 @@ pub fn current_resolution(name: &str) -> ResultType<Resolution> {
} }
pub fn is_virtual_display(name: &str) -> ResultType<bool> { pub fn is_virtual_display(name: &str) -> ResultType<bool> {
let device_name = str_to_device_name(name);
let mut dd: DISPLAY_DEVICEW = unsafe { std::mem::zeroed() }; let mut dd: DISPLAY_DEVICEW = unsafe { std::mem::zeroed() };
dd.cb = std::mem::size_of::<DISPLAY_DEVICEW>() as _; dd.cb = std::mem::size_of::<DISPLAY_DEVICEW>() as DWORD;
let ok = unsafe { EnumDisplayDevicesW(device_name.as_ptr(), 0, &mut dd as _, 0) }; let mut i_dev_num = 0;
if ok == FALSE { loop {
bail!( let result = unsafe { EnumDisplayDevicesW(null_mut(), i_dev_num, &mut dd, 0) };
"enumerate display devices with device name '{}', errno {}", if result == 0 {
name, break;
unsafe { GetLastError() } }
); if let Ok(device_name) = String::from_utf16(&dd.DeviceName) {
} if device_name == name {
match std::string::String::from_utf16(&dd.DeviceString) { return match std::string::String::from_utf16(&dd.DeviceString) {
Ok(s) => Ok(&s[..IDD_DEVICE_STRING.len()] == IDD_DEVICE_STRING), Ok(s) => Ok(&s[..IDD_DEVICE_STRING.len()] == IDD_DEVICE_STRING),
Err(e) => bail!( Err(e) => bail!("convert the device string of '{}' to string: {}", name, e),
"convert the device string of '{}' to string: {}", };
name, }
e }
), i_dev_num += 1;
} }
bail!("No such display '{}'", name)
} }
pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> { pub fn change_resolution(name: &str, width: usize, height: usize) -> ResultType<()> {