Add refresh_rate_millihertz for MonitorHandle

This also alters `VideoMode::refresh_rate` to
`VideoMode::refresh_rate_millihertz` which now returns monitor refresh rate in
mHz.
This commit is contained in:
Kirill Chibisov 2022-07-08 13:25:56 +03:00 committed by GitHub
parent e289f30e5d
commit a06bb3f992
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 172 additions and 81 deletions

View file

@ -9,8 +9,8 @@ use windows_sys::Win32::{
Graphics::Gdi::{
EnumDisplayMonitors, EnumDisplaySettingsExW, GetMonitorInfoW, MonitorFromPoint,
MonitorFromWindow, DEVMODEW, DM_BITSPERPEL, DM_DISPLAYFREQUENCY, DM_PELSHEIGHT,
DM_PELSWIDTH, HDC, HMONITOR, MONITORINFO, MONITORINFOEXW, MONITOR_DEFAULTTONEAREST,
MONITOR_DEFAULTTOPRIMARY,
DM_PELSWIDTH, ENUM_CURRENT_SETTINGS, HDC, HMONITOR, MONITORINFO, MONITORINFOEXW,
MONITOR_DEFAULTTONEAREST, MONITOR_DEFAULTTOPRIMARY,
},
};
@ -29,7 +29,7 @@ use crate::{
pub struct VideoMode {
pub(crate) size: (u32, u32),
pub(crate) bit_depth: u16,
pub(crate) refresh_rate: u16,
pub(crate) refresh_rate_millihertz: u32,
pub(crate) monitor: MonitorHandle,
// DEVMODEW is huge so we box it to avoid blowing up the size of winit::window::Fullscreen
pub(crate) native_video_mode: Box<DEVMODEW>,
@ -39,7 +39,7 @@ impl PartialEq for VideoMode {
fn eq(&self, other: &Self) -> bool {
self.size == other.size
&& self.bit_depth == other.bit_depth
&& self.refresh_rate == other.refresh_rate
&& self.refresh_rate_millihertz == other.refresh_rate_millihertz
&& self.monitor == other.monitor
}
}
@ -50,7 +50,7 @@ impl std::hash::Hash for VideoMode {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.size.hash(state);
self.bit_depth.hash(state);
self.refresh_rate.hash(state);
self.refresh_rate_millihertz.hash(state);
self.monitor.hash(state);
}
}
@ -60,7 +60,7 @@ impl std::fmt::Debug for VideoMode {
f.debug_struct("VideoMode")
.field("size", &self.size)
.field("bit_depth", &self.bit_depth)
.field("refresh_rate", &self.refresh_rate)
.field("refresh_rate_millihertz", &self.refresh_rate_millihertz)
.field("monitor", &self.monitor)
.finish()
}
@ -75,8 +75,8 @@ impl VideoMode {
self.bit_depth
}
pub fn refresh_rate(&self) -> u16 {
self.refresh_rate
pub fn refresh_rate_millihertz(&self) -> u32 {
self.refresh_rate_millihertz
}
pub fn monitor(&self) -> RootMonitorHandle {
@ -192,6 +192,23 @@ impl MonitorHandle {
}
}
#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
let monitor_info = get_monitor_info(self.0).unwrap();
let device_name = monitor_info.szDevice.as_ptr();
unsafe {
let mut mode: DEVMODEW = mem::zeroed();
mode.dmSize = mem::size_of_val(&mode) as u16;
if EnumDisplaySettingsExW(device_name, ENUM_CURRENT_SETTINGS, &mut mode, 0)
== false.into()
{
None
} else {
Some(mode.dmDisplayFrequency * 1000)
}
}
}
#[inline]
pub fn position(&self) -> PhysicalPosition<i32> {
let rc_monitor = get_monitor_info(self.0).unwrap().monitorInfo.rcMonitor;
@ -233,7 +250,7 @@ impl MonitorHandle {
video_mode: VideoMode {
size: (mode.dmPelsWidth, mode.dmPelsHeight),
bit_depth: mode.dmBitsPerPel as u16,
refresh_rate: mode.dmDisplayFrequency as u16,
refresh_rate_millihertz: mode.dmDisplayFrequency as u32 * 1000,
monitor: self.clone(),
native_video_mode: Box::new(mode),
},