diff --git a/CHANGELOG.md b/CHANGELOG.md index 87df57b8..ddf697da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ - On X11, the `Moved` event is no longer sent when the window is resized without changing position. - `MouseCursor` and `CursorState` now implement `Default`. - `WindowBuilder::with_resizable` implemented for Windows. +- On X11, if width or height is reported as 0, the DPI is now 1.0 instead of +inf. +- On X11, the environment variable `WINIT_HIDPI_FACTOR` has been added for overriding DPI factor. # Version 0.15.0 (2018-05-22) diff --git a/src/platform/linux/x11/util/randr.rs b/src/platform/linux/x11/util/randr.rs index 53a20179..dccfbdd3 100644 --- a/src/platform/linux/x11/util/randr.rs +++ b/src/platform/linux/x11/util/randr.rs @@ -1,4 +1,5 @@ -use std::slice; +use std::{env, slice}; +use std::str::FromStr; use super::*; use super::ffi::{ @@ -53,6 +54,22 @@ pub fn calc_dpi_factor( (width_px, height_px): (u32, u32), (width_mm, height_mm): (u64, u64), ) -> f64 { + // Override DPI if `WINIT_HIDPI_FACTOR` variable is set + if let Ok(dpi_factor_str) = env::var("WINIT_HIDPI_FACTOR") { + if let Ok(dpi_factor) = f64::from_str(&dpi_factor_str) { + if dpi_factor <= 0. { + panic!("Expected `WINIT_HIDPI_FACTOR` to be bigger than 0, got '{}'", dpi_factor); + } + + return dpi_factor; + } + } + + // See http://xpra.org/trac/ticket/728 for more information + if width_mm == 0 || width_mm == 0 { + return 1.0; + } + let ppmm = ( (width_px as f64 * height_px as f64) / (width_mm as f64 * height_mm as f64) ).sqrt(); diff --git a/src/window.rs b/src/window.rs index 1568306c..5fd741cb 100644 --- a/src/window.rs +++ b/src/window.rs @@ -344,6 +344,10 @@ impl Window { /// Returns the ratio between the backing framebuffer resolution and the /// window size in screen pixels. This is typically one for a normal display /// and two for a retina display. + /// + /// ## Platform-specific + /// On X11 the DPI factor can be overridden using the `WINIT_HIDPI_FACTOR` environment + /// variable. #[inline] pub fn hidpi_factor(&self) -> f32 { self.window.hidpi_factor() @@ -467,6 +471,10 @@ impl MonitorId { } /// Returns the ratio between the monitor's physical pixels and logical pixels. + /// + /// ## Platform-specific + /// On X11 the DPI factor can be overridden using the `WINIT_HIDPI_FACTOR` environment + /// variable. #[inline] pub fn get_hidpi_factor(&self) -> f32 { self.inner.get_hidpi_factor()