From 2b7cbdda1ab09ac8b1c52bc2aee2543d0377cf49 Mon Sep 17 00:00:00 2001 From: kaolinite7 Date: Sat, 29 Aug 2026 16:45:10 +0500 Subject: [PATCH] Add Support for DPI Auto-Detection This PR re-adds support for DPI Auto-Detection which was available in X.Org till 21.1, but was removed. This reverts commit 35af1299 and allows to get Auto DPI Detection back only when specified by user as: $ X -dpi auto --- dix/globals.c | 1 + hw/xfree86/common/xf86Helper.c | 6 ++++++ hw/xfree86/modes/xf86Crtc.c | 6 ++++-- hw/xfree86/modes/xf86RandR12.c | 6 ++++++ include/globals.h | 1 + os/utils.c | 5 ++++- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/dix/globals.c b/dix/globals.c index 7bf5393b0e..83e2403ef4 100644 --- a/dix/globals.c +++ b/dix/globals.c @@ -119,6 +119,7 @@ TimeStamp currentTime; int defaultColorVisualClass = -1; int monitorResolution = 0; +Bool autosetDPI = FALSE; Bool explicit_display = FALSE; char *ConnectionInfo; diff --git a/hw/xfree86/common/xf86Helper.c b/hw/xfree86/common/xf86Helper.c index 1959486b80..34918ea937 100644 --- a/hw/xfree86/common/xf86Helper.c +++ b/hw/xfree86/common/xf86Helper.c @@ -65,6 +65,7 @@ #include "xf86InPriv.h" #include "xf86Config.h" #include "xf86Module_priv.h" +#include "xf86Crtc.h" static int xf86ScrnInfoPrivateCount = 0; @@ -848,6 +849,7 @@ xf86SetDpi(ScrnInfoPtr pScrn, int x, int y) xf86MonPtr DDC = (xf86MonPtr) (pScrn->monitor->DDC); int ddcWidthmm, ddcHeightmm; int widthErr, heightErr; + xf86OutputPtr compat = xf86CompatOutput(pScrn); /* XXX Maybe there is no need for widthmm/heightmm in ScrnInfoRec */ pScrn->widthmm = pScrn->monitor->widthmm; @@ -860,6 +862,10 @@ xf86SetDpi(ScrnInfoPtr pScrn, int x, int y) ddcWidthmm = DDC->features.hsize * 10; /* 10mm in 1cm */ ddcHeightmm = DDC->features.vsize * 10; /* 10mm in 1cm */ } + else if (autosetDPI && compat && compat->mm_width > 0 && compat->mm_height > 0) { + ddcWidthmm = compat->mm_width; + ddcHeightmm = compat->mm_height; + } else { ddcWidthmm = ddcHeightmm = 0; } diff --git a/hw/xfree86/modes/xf86Crtc.c b/hw/xfree86/modes/xf86Crtc.c index f122f21617..469bd0c941 100644 --- a/hw/xfree86/modes/xf86Crtc.c +++ b/hw/xfree86/modes/xf86Crtc.c @@ -3228,8 +3228,10 @@ xf86OutputSetEDID(xf86OutputPtr output, xf86MonPtr edid_mon) free(output->MonInfo); output->MonInfo = edid_mon; - output->mm_width = 0; - output->mm_height = 0; + if (!autosetDPI || edid_mon) { + output->mm_width = 0; + output->mm_height = 0; + } if (debug_modes) { xf86DrvMsg(scrn->scrnIndex, X_INFO, "EDID for output %s\n", diff --git a/hw/xfree86/modes/xf86RandR12.c b/hw/xfree86/modes/xf86RandR12.c index a1cf4b3256..33ae91faa2 100644 --- a/hw/xfree86/modes/xf86RandR12.c +++ b/hw/xfree86/modes/xf86RandR12.c @@ -806,6 +806,12 @@ xf86RandR12CreateScreenResources(ScreenPtr pScreen) mmWidth = output->conf_monitor->mon_width; mmHeight = output->conf_monitor->mon_height; } + else if (autosetDPI && output && + (output->mm_width > 0 && + output->mm_height > 0)) { + mmWidth = output->mm_width; + mmHeight = output->mm_height; + } else { /* * Otherwise, just set the screen to DEFAULT_DPI diff --git a/include/globals.h b/include/globals.h index ecb7b2bb06..fd8427da3a 100644 --- a/include/globals.h +++ b/include/globals.h @@ -8,6 +8,7 @@ extern _X_EXPORT const char *defaultFontPath; extern _X_EXPORT int monitorResolution; +extern _X_EXPORT Bool autosetDPI; extern _X_EXPORT int defaultColorVisualClass; #endif /* !_XSERV_GLOBAL_H_ */ diff --git a/os/utils.c b/os/utils.c index 46b2484760..c93f755447 100644 --- a/os/utils.c +++ b/os/utils.c @@ -502,7 +502,10 @@ ProcessCommandLine(int argc, char *argv[]) } else if (strcmp(argv[i], "-dpi") == 0) { if (++i < argc) - monitorResolution = atoi(argv[i]); + if (strcmp(argv[i], "auto") == 0) + autosetDPI = TRUE; + else + monitorResolution = atoi(argv[i]); else UseMsg(); }