Skip to content

Commit

Permalink
refresh_rate_millihertz() now returns a NonZeroU32
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jul 23, 2024
1 parent e830ff4 commit ac685d3
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Application {
info!(
" Current mode: {width}x{height}{}",
if let Some(m_hz) = current_mode.refresh_rate_millihertz() {
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000)
format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000)
} else {
String::new()
}
Expand All @@ -258,7 +258,7 @@ impl Application {
let PhysicalSize { width, height } = mode.size();
let bits = mode.bit_depth();
let m_hz = if let Some(m_hz) = mode.refresh_rate_millihertz() {
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000)
format!(" @ {}.{} Hz", m_hz.get() / 1000, m_hz.get() % 1000)
} else {
String::new()
};
Expand Down
2 changes: 1 addition & 1 deletion src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ changelog entry.
- Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
instead of requiring a `&mut` reference to it.
- `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option`.
- `VideoModeHandle::refresh_rate_millihertz()` now returns an `Option<NonZeroU32>`.

### Removed

Expand Down
4 changes: 3 additions & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! methods, which return an iterator of [`MonitorHandle`]:
//! - [`ActiveEventLoop::available_monitors`][crate::event_loop::ActiveEventLoop::available_monitors].
//! - [`Window::available_monitors`][crate::window::Window::available_monitors].
use std::num::NonZeroU32;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::platform_impl;

Expand Down Expand Up @@ -73,7 +75,7 @@ impl VideoModeHandle {
///
/// - **Android / Orbital:** Always returns [`None`].
#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.video_mode.refresh_rate_millihertz()
}

Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::Cell;
use std::collections::VecDeque;
use std::hash::Hash;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -1032,7 +1033,7 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
None
}

Expand Down
21 changes: 12 additions & 9 deletions src/platform_impl/apple/appkit/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::VecDeque;
use std::fmt;
use std::num::NonZeroU32;

use core_foundation::array::{CFArrayGetCount, CFArrayGetValueAtIndex};
use core_foundation::base::{CFRelease, TCFType};
Expand All @@ -21,7 +22,7 @@ use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
pub struct VideoModeHandle {
size: PhysicalSize<u32>,
bit_depth: u16,
refresh_rate_millihertz: Option<u32>,
refresh_rate_millihertz: Option<NonZeroU32>,
pub(crate) monitor: MonitorHandle,
pub(crate) native_mode: NativeDisplayMode,
}
Expand Down Expand Up @@ -83,7 +84,7 @@ impl VideoModeHandle {
fn new(
monitor: MonitorHandle,
mode: NativeDisplayMode,
refresh_rate_millihertz: Option<u32>,
refresh_rate_millihertz: Option<NonZeroU32>,
) -> Self {
unsafe {
let pixel_encoding =
Expand Down Expand Up @@ -120,7 +121,7 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.refresh_rate_millihertz
}

Expand Down Expand Up @@ -192,7 +193,6 @@ impl fmt::Debug for MonitorHandle {
.field("native_identifier", &self.native_identifier())
.field("position", &self.position())
.field("scale_factor", &self.scale_factor())
.field("refresh_rate_millihertz", &self.refresh_rate_millihertz())
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ impl MonitorHandle {
})
}

fn refresh_rate_millihertz(&self) -> Option<u32> {
fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
let current_display_mode =
NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.0) } as _);
refresh_rate_millihertz(self.0, &current_display_mode)
Expand Down Expand Up @@ -272,7 +272,7 @@ impl MonitorHandle {
// CGDisplayModeGetRefreshRate returns 0.0 for any display that
// isn't a CRT
let refresh_rate_millihertz = if cg_refresh_rate_hertz > 0 {
Some((cg_refresh_rate_hertz * 1000) as u32)
NonZeroU32::new((cg_refresh_rate_hertz * 1000) as u32)
} else {
refresh_rate_millihertz
};
Expand Down Expand Up @@ -341,11 +341,11 @@ pub(crate) fn flip_window_screen_coordinates(frame: NSRect) -> NSPoint {
NSPoint::new(frame.origin.x, y)
}

fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> Option<u32> {
fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> Option<NonZeroU32> {
unsafe {
let refresh_rate = ffi::CGDisplayModeGetRefreshRate(mode.0);
if refresh_rate > 0.0 {
return Some((refresh_rate * 1000.0).round() as u32);
return NonZeroU32::new((refresh_rate * 1000.0).round() as u32);
}

let mut display_link = std::ptr::null_mut();
Expand All @@ -360,6 +360,9 @@ fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> O
return None;
}

(time.time_scale as i64).checked_div(time.time_value).map(|v| (v * 1000) as u32)
(time.time_scale as i64)
.checked_div(time.time_value)
.map(|v| (v * 1000) as u32)
.and_then(NonZeroU32::new)
}
}
11 changes: 6 additions & 5 deletions src/platform_impl/apple/uikit/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::unnecessary_cast)]

use std::collections::{BTreeSet, VecDeque};
use std::num::NonZeroU32;
use std::{fmt, hash, ptr};

use objc2::mutability::IsRetainable;
Expand Down Expand Up @@ -45,7 +46,7 @@ impl<T: IsRetainable + Message> Eq for MainThreadBoundDelegateImpls<T> {}
pub struct VideoModeHandle {
pub(crate) size: (u32, u32),
pub(crate) bit_depth: u16,
pub(crate) refresh_rate_millihertz: u32,
pub(crate) refresh_rate_millihertz: Option<NonZeroU32>,
screen_mode: MainThreadBoundDelegateImpls<UIScreenMode>,
pub(crate) monitor: MonitorHandle,
}
Expand Down Expand Up @@ -75,8 +76,8 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.refresh_rate_millihertz)
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> MonitorHandle {
Expand Down Expand Up @@ -213,7 +214,7 @@ impl MonitorHandle {
}
}

fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 {
fn refresh_rate_millihertz(uiscreen: &UIScreen) -> Option<NonZeroU32> {
let refresh_rate_millihertz: NSInteger = {
let os_capabilities = app_state::os_capabilities();
if os_capabilities.maximum_frames_per_second {
Expand All @@ -234,7 +235,7 @@ fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 {
}
};

refresh_rate_millihertz as u32 * 1000
NonZeroU32::new(refresh_rate_millihertz as u32 * 1000)
}

pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque<MonitorHandle> {
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
compile_error!("Please select a feature to build for unix: `x11`, `wayland`");

use std::collections::VecDeque;
use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -265,7 +266,7 @@ impl VideoModeHandle {
}

#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
x11_or_wayland!(match self; VideoModeHandle(m) => m.refresh_rate_millihertz())
}

Expand Down
10 changes: 6 additions & 4 deletions src/platform_impl/linux/wayland/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::NonZeroU32;

use sctk::output::{Mode, OutputData};
use sctk::reexports::client::protocol::wl_output::WlOutput;
use sctk::reexports::client::Proxy;
Expand Down Expand Up @@ -120,15 +122,15 @@ impl std::hash::Hash for MonitorHandle {
pub struct VideoModeHandle {
pub(crate) size: PhysicalSize<u32>,
pub(crate) bit_depth: u16,
pub(crate) refresh_rate_millihertz: u32,
pub(crate) refresh_rate_millihertz: Option<NonZeroU32>,
pub(crate) monitor: MonitorHandle,
}

impl VideoModeHandle {
fn new(monitor: MonitorHandle, mode: Mode) -> Self {
VideoModeHandle {
size: (mode.dimensions.0 as u32, mode.dimensions.1 as u32).into(),
refresh_rate_millihertz: mode.refresh_rate as u32,
refresh_rate_millihertz: NonZeroU32::new(mode.refresh_rate as u32),
bit_depth: 32,
monitor: monitor.clone(),
}
Expand All @@ -145,8 +147,8 @@ impl VideoModeHandle {
}

#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.refresh_rate_millihertz)
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> MonitorHandle {
Expand Down
12 changes: 8 additions & 4 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::NonZeroU32;

use x11rb::connection::RequestConnection;
use x11rb::protocol::randr::{self, ConnectionExt as _};
use x11rb::protocol::xproto;
Expand All @@ -21,7 +23,7 @@ pub struct VideoModeHandle {
pub(crate) current: bool,
pub(crate) size: (u32, u32),
pub(crate) bit_depth: u16,
pub(crate) refresh_rate_millihertz: Option<u32>,
pub(crate) refresh_rate_millihertz: Option<NonZeroU32>,
pub(crate) native_mode: randr::Mode,
pub(crate) monitor: Option<MonitorHandle>,
}
Expand All @@ -38,7 +40,7 @@ impl VideoModeHandle {
}

#[inline]
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.refresh_rate_millihertz
}

Expand Down Expand Up @@ -93,10 +95,12 @@ impl std::hash::Hash for MonitorHandle {
}

#[inline]
pub fn mode_refresh_rate_millihertz(mode: &randr::ModeInfo) -> Option<u32> {
pub fn mode_refresh_rate_millihertz(mode: &randr::ModeInfo) -> Option<NonZeroU32> {
if mode.dot_clock > 0 && mode.htotal > 0 && mode.vtotal > 0 {
#[allow(clippy::unnecessary_cast)]
Some((mode.dot_clock as u64 * 1000 / (mode.htotal as u64 * mode.vtotal as u64)) as u32)
NonZeroU32::new(
(mode.dot_clock as u64 * 1000 / (mode.htotal as u64 * mode.vtotal as u64)) as u32,
)
} else {
None
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/orbital/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(target_os = "redox")]

use std::fmt::{self, Display, Formatter};
use std::num::NonZeroU32;
use std::str;
use std::sync::Arc;

Expand Down Expand Up @@ -229,7 +230,7 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
// TODO
None
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/web/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::iter::Empty;
use std::num::NonZeroU32;

use crate::dpi::{PhysicalPosition, PhysicalSize};

Expand Down Expand Up @@ -39,7 +40,7 @@ impl VideoModeHandle {
unreachable!();
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
unreachable!();
}

Expand Down
9 changes: 5 additions & 4 deletions src/platform_impl/windows/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{BTreeSet, VecDeque};
use std::hash::Hash;
use std::num::NonZeroU32;
use std::{io, mem, ptr};

use windows_sys::Win32::Foundation::{BOOL, HWND, LPARAM, POINT, RECT};
Expand All @@ -21,7 +22,7 @@ use crate::platform_impl::platform::window::Window;
pub struct VideoModeHandle {
pub(crate) size: (u32, u32),
pub(crate) bit_depth: u16,
pub(crate) refresh_rate_millihertz: u32,
pub(crate) refresh_rate_millihertz: Option<NonZeroU32>,
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>,
Expand Down Expand Up @@ -67,7 +68,7 @@ impl VideoModeHandle {
VideoModeHandle {
size: (mode.dmPelsWidth, mode.dmPelsHeight),
bit_depth: mode.dmBitsPerPel as u16,
refresh_rate_millihertz: mode.dmDisplayFrequency * 1000,
refresh_rate_millihertz: NonZeroU32::new(mode.dmDisplayFrequency * 1000),
monitor,
native_video_mode: Box::new(mode),
}
Expand All @@ -81,8 +82,8 @@ impl VideoModeHandle {
self.bit_depth
}

pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.refresh_rate_millihertz)
pub fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> MonitorHandle {
Expand Down

0 comments on commit ac685d3

Please sign in to comment.