From 50097acb2da620078e4ad8b2123d69d0e7c2b9f1 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Thu, 31 May 2018 19:00:43 +0200 Subject: [PATCH 1/6] Fix DPI with 0 width/hight reported by xorg --- src/platform/linux/x11/util/randr.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/platform/linux/x11/util/randr.rs b/src/platform/linux/x11/util/randr.rs index 53a2017978..65ec8629fe 100644 --- a/src/platform/linux/x11/util/randr.rs +++ b/src/platform/linux/x11/util/randr.rs @@ -53,6 +53,11 @@ pub fn calc_dpi_factor( (width_px, height_px): (u32, u32), (width_mm, height_mm): (u64, u64), ) -> f64 { + // 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(); From e5e46efbe5a23df20e8c6e4ab8bfec5897fe73e6 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 2 Jun 2018 20:30:30 +0200 Subject: [PATCH 2/6] Add `WINIT_HIDPI_FACTOR` env variable It is now possible to override the DPI factor using the `WINIT_HIDPI_FACTOR` environment variable on X11. The changelog also has been updated to introduce all current changes made. --- CHANGELOG.md | 2 ++ src/platform/linux/x11/util/randr.rs | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15fc634fe1..11f7a4925a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - On X11, the `Moved` event is no longer sent when the window is resized without changing position. - `MouseCursor` and `CursorState` now implement `Default`. +- 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 65ec8629fe..c0044824db 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::str::FromStr; +use std::{slice, env}; use super::*; use super::ffi::{ @@ -53,6 +54,13 @@ 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) { + return dpi_factor; + } + } + // See http://xpra.org/trac/ticket/728 for more information if width_mm == 0 || width_mm == 0 { return 1.0; From 025c9ceb1717fbe4e580f0c8c3bb7246d249265d Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 3 Jun 2018 16:46:17 +0200 Subject: [PATCH 3/6] Add documentation for the environment variable --- src/window.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/window.rs b/src/window.rs index cae1f239a2..290e237a88 100644 --- a/src/window.rs +++ b/src/window.rs @@ -333,6 +333,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 overwritten using the `WINIT_HIDPI_FACTOR` environment + /// variable. #[inline] pub fn hidpi_factor(&self) -> f32 { self.window.hidpi_factor() @@ -456,6 +460,10 @@ impl MonitorId { } /// Returns the ratio between the monitor's physical pixels and logical pixels. + /// + /// ## Platform-specific + /// On X11 the DPI factor can be overwritten using the `WINIT_HIDPI_FACTOR` environment + /// variable. #[inline] pub fn get_hidpi_factor(&self) -> f32 { self.inner.get_hidpi_factor() From 7fabc3c438324eddfcae9481189fa703716b7337 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 3 Jun 2018 17:07:39 +0200 Subject: [PATCH 4/6] Fix nitpicks --- src/platform/linux/x11/util/randr.rs | 2 +- src/window.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform/linux/x11/util/randr.rs b/src/platform/linux/x11/util/randr.rs index c0044824db..8e07d67eb1 100644 --- a/src/platform/linux/x11/util/randr.rs +++ b/src/platform/linux/x11/util/randr.rs @@ -1,5 +1,5 @@ -use std::str::FromStr; use std::{slice, env}; +use std::str::FromStr; use super::*; use super::ffi::{ diff --git a/src/window.rs b/src/window.rs index 4d44d4afaa..5fd741cbad 100644 --- a/src/window.rs +++ b/src/window.rs @@ -346,7 +346,7 @@ impl Window { /// and two for a retina display. /// /// ## Platform-specific - /// On X11 the DPI factor can be overwritten using the `WINIT_HIDPI_FACTOR` environment + /// On X11 the DPI factor can be overridden using the `WINIT_HIDPI_FACTOR` environment /// variable. #[inline] pub fn hidpi_factor(&self) -> f32 { @@ -473,7 +473,7 @@ impl MonitorId { /// Returns the ratio between the monitor's physical pixels and logical pixels. /// /// ## Platform-specific - /// On X11 the DPI factor can be overwritten using the `WINIT_HIDPI_FACTOR` environment + /// On X11 the DPI factor can be overridden using the `WINIT_HIDPI_FACTOR` environment /// variable. #[inline] pub fn get_hidpi_factor(&self) -> f32 { From 24e189f8e0f347d7fb2aac7708183e9ec6e2bdec Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 3 Jun 2018 17:14:28 +0200 Subject: [PATCH 5/6] Learning the alphabet --- src/platform/linux/x11/util/randr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/linux/x11/util/randr.rs b/src/platform/linux/x11/util/randr.rs index 8e07d67eb1..69e223ed7f 100644 --- a/src/platform/linux/x11/util/randr.rs +++ b/src/platform/linux/x11/util/randr.rs @@ -1,4 +1,4 @@ -use std::{slice, env}; +use std::{env, slice}; use std::str::FromStr; use super::*; From d244dac96b1a0418ab12aaeef6deba3586d2dc09 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 3 Jun 2018 18:22:34 +0200 Subject: [PATCH 6/6] Panic with error message if DPI env var is <= 0 --- src/platform/linux/x11/util/randr.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/platform/linux/x11/util/randr.rs b/src/platform/linux/x11/util/randr.rs index 69e223ed7f..dccfbdd395 100644 --- a/src/platform/linux/x11/util/randr.rs +++ b/src/platform/linux/x11/util/randr.rs @@ -57,6 +57,10 @@ pub fn calc_dpi_factor( // 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; } }