Skip to content

Commit

Permalink
On Windows, avoid panic in video_modes()
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 4, 2023
1 parent 73718c9 commit 079f4fa
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/platform_impl/windows/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,29 +235,35 @@ impl MonitorHandle {

loop {
unsafe {
let monitor_info = get_monitor_info(self.0).unwrap();
let device_name = monitor_info.szDevice.as_ptr();
let mut mode: DEVMODEW = mem::zeroed();
mode.dmSize = mem::size_of_val(&mode) as u16;
if EnumDisplaySettingsExW(device_name, i, &mut mode, 0) == false.into() {
break;
match get_monitor_info(self.0) {
Ok(monitor_info) => {
let device_name = monitor_info.szDevice.as_ptr();
let mut mode: DEVMODEW = mem::zeroed();
mode.dmSize = mem::size_of_val(&mode) as u16;
if EnumDisplaySettingsExW(device_name, i, &mut mode, 0) == false.into() {
break;
}
i += 1;

const REQUIRED_FIELDS: u32 =
DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
assert!(has_flag(mode.dmFields, REQUIRED_FIELDS));

// Use Ord impl of RootVideoMode
modes.insert(RootVideoMode {
video_mode: VideoMode {
size: (mode.dmPelsWidth, mode.dmPelsHeight),
bit_depth: mode.dmBitsPerPel as u16,
refresh_rate_millihertz: mode.dmDisplayFrequency * 1000,
monitor: self.clone(),
native_video_mode: Box::new(mode),
},
});
}
Err(error) => {
log::warn!("Error from get_monitor_info: {error}");
}
}
i += 1;

const REQUIRED_FIELDS: u32 =
DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
assert!(has_flag(mode.dmFields, REQUIRED_FIELDS));

// Use Ord impl of RootVideoMode
modes.insert(RootVideoMode {
video_mode: VideoMode {
size: (mode.dmPelsWidth, mode.dmPelsHeight),
bit_depth: mode.dmBitsPerPel as u16,
refresh_rate_millihertz: mode.dmDisplayFrequency * 1000,
monitor: self.clone(),
native_video_mode: Box::new(mode),
},
});
}
}

Expand Down

0 comments on commit 079f4fa

Please sign in to comment.