forge
Explore
Log in

Don't you dare go Hollow

Implement ideal scale factor guessing

Browse files at this commit

dc1d2b706ce43d9fb7caac75b839a0bfc6ded029Ivan MolodetskikhParent: f9b0081
Cargo.lockmodified+44 -0
@@ -668,6 +668,16 @@ version = "1.0.0"
668 source = "registry+https://github.com/rust-lang/crates.io-index"668 source = "registry+https://github.com/rust-lang/crates.io-index"
669 checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"669 checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
670 670
671+[[package]]
672+name = "colored"
673+version = "2.1.0"
674+source = "registry+https://github.com/rust-lang/crates.io-index"
675+checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
676+dependencies = [
677+ "lazy_static",
678+ "windows-sys 0.48.0",
679+]
680+
671 [[package]]681 [[package]]
672 name = "combine"682 name = "combine"
673 version = "4.6.6"683 version = "4.6.6"
@@ -814,6 +824,12 @@ dependencies = [
814 "syn 1.0.109",824 "syn 1.0.109",
815 ]825 ]
816 826
827+[[package]]
828+name = "diff"
829+version = "0.1.13"
830+source = "registry+https://github.com/rust-lang/crates.io-index"
831+checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
832+
817 [[package]]833 [[package]]
818 name = "digest"834 name = "digest"
819 version = "0.10.7"835 version = "0.10.7"
@@ -1789,6 +1805,23 @@ dependencies = [
1789 "wasm-bindgen",1805 "wasm-bindgen",
1790 ]1806 ]
1791 1807
1808+[[package]]
1809+name = "k9"
1810+version = "0.12.0"
1811+source = "registry+https://github.com/rust-lang/crates.io-index"
1812+checksum = "088bcebb5b68b1b14b64d7f05b0f802719250b97fdc0338ec42529ea777ed614"
1813+dependencies = [
1814+ "anyhow",
1815+ "colored",
1816+ "diff",
1817+ "lazy_static",
1818+ "libc",
1819+ "proc-macro2",
1820+ "regex",
1821+ "syn 2.0.55",
1822+ "terminal_size",
1823+]
1824+
1792 [[package]]1825 [[package]]
1793 name = "keyframe"1826 name = "keyframe"
1794 version = "1.1.1"1827 version = "1.1.1"
@@ -2174,6 +2207,7 @@ dependencies = [
2174 "git-version",2207 "git-version",
2175 "glam",2208 "glam",
2176 "input",2209 "input",
2210+ "k9",
2177 "keyframe",2211 "keyframe",
2178 "libc",2212 "libc",
2179 "log",2213 "log",
@@ -3308,6 +3342,16 @@ dependencies = [
3308 "windows-sys 0.52.0",3342 "windows-sys 0.52.0",
3309 ]3343 ]
3310 3344
3345+[[package]]
3346+name = "terminal_size"
3347+version = "0.2.6"
3348+source = "registry+https://github.com/rust-lang/crates.io-index"
3349+checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237"
3350+dependencies = [
3351+ "rustix 0.37.27",
3352+ "windows-sys 0.48.0",
3353+]
3354+
3311 [[package]]3355 [[package]]
3312 name = "thiserror"3356 name = "thiserror"
3313 version = "1.0.58"3357 version = "1.0.58"
Cargo.tomlmodified+1 -0
@@ -96,6 +96,7 @@ features = [
96 ]96 ]
97 97
98 [dev-dependencies]98 [dev-dependencies]
99+k9 = "0.12.0"
99 proptest = "1.4.0"100 proptest = "1.4.0"
100 proptest-derive = "0.4.0"101 proptest-derive = "0.4.0"
101 xshell = "0.2.5"102 xshell = "0.2.5"
src/niri.rsmodified+11 -2
@@ -116,6 +116,7 @@ use crate::ui::config_error_notification::ConfigErrorNotification;
116 use crate::ui::exit_confirm_dialog::ExitConfirmDialog;116 use crate::ui::exit_confirm_dialog::ExitConfirmDialog;
117 use crate::ui::hotkey_overlay::HotkeyOverlay;117 use crate::ui::hotkey_overlay::HotkeyOverlay;
118 use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement};118 use crate::ui::screenshot_ui::{ScreenshotUi, ScreenshotUiRenderElement};
119+use crate::utils::scale::guess_monitor_scale;
119 use crate::utils::spawning::CHILD_ENV;120 use crate::utils::spawning::CHILD_ENV;
120 use crate::utils::{121 use crate::utils::{
121 center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output,122 center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output,
@@ -934,7 +935,11 @@ impl State {
934 let config = self.niri.config.borrow_mut();935 let config = self.niri.config.borrow_mut();
935 let config = config.outputs.iter().find(|o| o.name == name);936 let config = config.outputs.iter().find(|o| o.name == name);
936 937
937- let scale = config.map(|c| c.scale).unwrap_or(1.);938+ let scale = config.map(|c| c.scale).unwrap_or_else(|| {
939+ let size_mm = output.physical_properties().size;
940+ let resolution = output.current_mode().unwrap().size;
941+ guess_monitor_scale(size_mm, resolution)
942+ });
938 let scale = scale.clamp(1., 10.).ceil() as i32;943 let scale = scale.clamp(1., 10.).ceil() as i32;
939 944
940 let mut transform = config945 let mut transform = config
@@ -1565,7 +1570,11 @@ impl Niri {
1565 1570
1566 let config = self.config.borrow();1571 let config = self.config.borrow();
1567 let c = config.outputs.iter().find(|o| o.name == name);1572 let c = config.outputs.iter().find(|o| o.name == name);
1568- let scale = c.map(|c| c.scale).unwrap_or(1.);1573+ let scale = c.map(|c| c.scale).unwrap_or_else(|| {
1574+ let size_mm = output.physical_properties().size;
1575+ let resolution = output.current_mode().unwrap().size;
1576+ guess_monitor_scale(size_mm, resolution)
1577+ });
1569 let scale = scale.clamp(1., 10.).ceil() as i32;1578 let scale = scale.clamp(1., 10.).ceil() as i32;
1570 let mut transform = c1579 let mut transform = c
1571 .map(|c| ipc_transform_to_smithay(c.transform))1580 .map(|c| ipc_transform_to_smithay(c.transform))
src/utils/mod.rsmodified+1 -0
@@ -15,6 +15,7 @@ use smithay::reexports::rustix::time::{clock_gettime, ClockId};
15 use smithay::utils::{Logical, Point, Rectangle, Size, Transform};15 use smithay::utils::{Logical, Point, Rectangle, Size, Transform};
16 16
17 pub mod id;17 pub mod id;
18+pub mod scale;
18 pub mod spawning;19 pub mod spawning;
19 pub mod watcher;20 pub mod watcher;
20 21
src/utils/scale.rsadded+104 -0
@@ -0,0 +1,104 @@
1+//! Default monitor scale calculation.
2+//!
3+//! This module follows logic and tests from Mutter:
4+//! https://gitlab.gnome.org/GNOME/mutter/-/blob/gnome-46/src/backends/meta-monitor.c
5+
6+use smithay::utils::{Physical, Raw, Size};
7+
8+const MIN_SCALE: i32 = 1;
9+const MAX_SCALE: i32 = 4;
10+const MIN_LOGICAL_AREA: i32 = 800 * 480;
11+
12+const MOBILE_TARGET_DPI: f64 = 135.;
13+const LARGE_TARGET_DPI: f64 = 110.;
14+const LARGE_MIN_SIZE_INCHES: f64 = 20.;
15+
16+/// Calculates the ideal scale for a monitor.
17+pub fn guess_monitor_scale(size_mm: Size<i32, Raw>, resolution: Size<i32, Physical>) -> f64 {
18+ if size_mm.w == 0 || size_mm.h == 0 {
19+ return 1.;
20+ }
21+
22+ let diag_inches = f64::from(size_mm.w * size_mm.w + size_mm.h * size_mm.h).sqrt() / 25.4;
23+
24+ let target_dpi = if diag_inches < LARGE_MIN_SIZE_INCHES {
25+ MOBILE_TARGET_DPI
26+ } else {
27+ LARGE_TARGET_DPI
28+ };
29+
30+ let physical_dpi =
31+ f64::from(resolution.w * resolution.w + resolution.h * resolution.h).sqrt() / diag_inches;
32+ let perfect_scale = physical_dpi / target_dpi;
33+
34+ // For integer scaling factors (we currently only do integer), bias the perfect scale down.
35+ let perfect_scale = perfect_scale - 0.15;
36+
37+ supported_scales(resolution)
38+ .map(|scale| (scale, (scale - perfect_scale).abs()))
39+ .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
40+ .map_or(1., |(scale, _)| scale)
41+}
42+
43+fn supported_scales(resolution: Size<i32, Physical>) -> impl Iterator<Item = f64> {
44+ (MIN_SCALE..=MAX_SCALE)
45+ .filter(move |scale| is_valid_for_resolution(resolution, *scale))
46+ .map(f64::from)
47+}
48+
49+fn is_valid_for_resolution(resolution: Size<i32, Physical>, scale: i32) -> bool {
50+ let logical = resolution.to_logical(scale);
51+ logical.w * logical.h >= MIN_LOGICAL_AREA
52+}
53+
54+#[cfg(test)]
55+mod tests {
56+ use k9::snapshot;
57+
58+ use super::*;
59+
60+ fn check(size_mm: (i32, i32), resolution: (i32, i32)) -> f64 {
61+ guess_monitor_scale(Size::from(size_mm), Size::from(resolution))
62+ }
63+
64+ #[test]
65+ fn test_guess_monitor_scale() {
66+ // Librem 5; not enough logical area when scaled
67+ snapshot!(check((65, 129), (720, 1440)), "1.0");
68+ // OnePlus 6
69+ snapshot!(check((68, 144), (1080, 2280)), "2.0");
70+ // Google Pixel 6a
71+ snapshot!(check((64, 142), (1080, 2400)), "2.0");
72+ // 13" MacBook Retina
73+ snapshot!(check((286, 179), (2560, 1600)), "2.0");
74+ // Surface Laptop Studio
75+ snapshot!(check((303, 202), (2400, 1600)), "1.0");
76+ // Dell XPS 9320
77+ snapshot!(check((290, 180), (3840, 2400)), "2.0");
78+ // Lenovo ThinkPad X1 Yoga Gen 6
79+ snapshot!(check((300, 190), (3840, 2400)), "2.0");
80+ // Generic 23" 1080p
81+ snapshot!(check((509, 286), (1920, 1080)), "1.0");
82+ // Generic 23" 4K
83+ snapshot!(check((509, 286), (3840, 2160)), "2.0");
84+ // Generic 27" 4K
85+ snapshot!(check((598, 336), (3840, 2160)), "1.0");
86+ // Generic 32" 4K
87+ snapshot!(check((708, 398), (3840, 2160)), "1.0");
88+ // Generic 25" 4K; ideal scale is 1.60, should round to 1.5 and 1.0
89+ snapshot!(check((554, 312), (3840, 2160)), "1.0");
90+ // Generic 23.5" 4K; ideal scale is 1.70, should round to 1.75 and 2.0
91+ snapshot!(check((522, 294), (3840, 2160)), "2.0");
92+ // Lenovo Legion 7 Gen 7 AMD 16"
93+ snapshot!(check((340, 210), (2560, 1600)), "1.0");
94+ // Acer Nitro XV320QU LV 31.5"
95+ snapshot!(check((700, 390), (2560, 1440)), "1.0");
96+ // Surface Pro 6
97+ snapshot!(check((260, 170), (2736, 1824)), "2.0");
98+ }
99+
100+ #[test]
101+ fn guess_monitor_scale_unknown_size() {
102+ assert_eq!(check((0, 0), (1920, 1080)), 1.);
103+ }
104+}
wiki/Configuration:-Outputs.mdmodified+2 -0
@@ -68,6 +68,8 @@ Set the scale of the monitor.
68 68
69 This is a floating-point number to enable fractional scaling in the future, but at the moment only integer scale values will work.69 This is a floating-point number to enable fractional scaling in the future, but at the moment only integer scale values will work.
70 70
71+<sup>Since: 0.1.6</sup> If scale is unset, niri will guess an appropriate scale based on the physical dimensions and the resolution of the monitor.
72+
71 ```73 ```
72 output "eDP-1" {74 output "eDP-1" {
73 scale 2.075 scale 2.0