How to Get Screen Dpi (Linux,Mac) Programatically

How to get screen DPI (linux,mac) programatically?

In X on Linux, call XOpenDisplay() to get the Display, then use DisplayWidthMM() and DisplayHeightMM() together with DisplayWidth() and DisplayHeight() to compute the DPI.

On the Mac, there's almost certainly a more native API to use than X. Mac OS X does not run X Window by default, it has a native windowing environment.

How do I get the screen DPI using wxPython?

I don't think there is any such function in wxPython, what you can do instead is, by using ctypes call win32api function GetDeviceCaps and get LOGPIXELSX/LOGPIXELSY

How to obtain the correct physical size of the monitor?

I found another way. The physical size of the monitor are stored in the EDID, and Windows are almost always copies of its value in the registry. If you can parse EDID, you can read the width and height of the monitor in centimeters.

Update: Added code

BOOL GetMonitorDevice( TCHAR* adapterName, DISPLAY_DEVICE &ddMon )
{
DWORD devMon = 0;

while (EnumDisplayDevices(adapterName, devMon, &ddMon, 0))
{
if (ddMon.StateFlags & DISPLAY_DEVICE_ACTIVE &&
ddMon.StateFlags & DISPLAY_DEVICE_ATTACHED) // for ATI, Windows XP
break;

devMon++;
}

if (ddMon.DeviceString[0] == '\0')
{
EnumDisplayDevices(adapterName, 0, &ddMon, 0);
if (ddMon.DeviceString[0] == '\0')
_tcscpy_s(ddMon.DeviceString, _T("Default Monitor"));
}
return ddMon.DeviceID[0] != '\0';
}

BOOL GetMonitorSizeFromEDID(TCHAR* adapterName, DWORD& Width, DWORD& Height)
{
DISPLAY_DEVICE ddMon;
ZeroMemory(&ddMon, sizeof(ddMon));
ddMon.cb = sizeof(ddMon);

//read edid
bool result = false;
Width = 0;
Height = 0;
if (GetMonitorDevice(adapterName, ddMon))
{
TCHAR model[8];
TCHAR* s = _tcschr(ddMon.DeviceID, '\\') + 1;
size_t len = _tcschr(s, '\\') - s;
if (len >= _countof(model))
len = _countof(model) - 1;
_tcsncpy_s(model, s, len);

TCHAR *path = _tcschr(ddMon.DeviceID, '\\') + 1;
TCHAR str[MAX_PATH] = _T("SYSTEM\\CurrentControlSet\\Enum\\DISPLAY\\");
_tcsncat_s(str, path, _tcschr(path, '\\')-path);
path = _tcschr(path, '\\') + 1;
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, str, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD i = 0;
DWORD size = MAX_PATH;
FILETIME ft;
while(RegEnumKeyEx(hKey, i, str, &size, NULL, NULL, NULL, &ft) == ERROR_SUCCESS)
{
HKEY hKey2;
if(RegOpenKeyEx(hKey, str, 0, KEY_READ, &hKey2) == ERROR_SUCCESS)
{
size = MAX_PATH;
if(RegQueryValueEx(hKey2, _T("Driver"), NULL, NULL, (LPBYTE)&str, &size) == ERROR_SUCCESS)
{
if (_tcscmp(str, path) == 0)
{
HKEY hKey3;
if(RegOpenKeyEx(hKey2, _T("Device Parameters"), 0, KEY_READ, &hKey3) == ERROR_SUCCESS)
{
BYTE EDID[256];
size = 256;
if(RegQueryValueEx(hKey3, _T("EDID"), NULL, NULL, (LPBYTE)&EDID, &size) == ERROR_SUCCESS)
{
DWORD p = 8;
TCHAR model2[9];

char byte1 = EDID[p];
char byte2 = EDID[p+1];
model2[0]=((byte1 & 0x7C) >> 2) + 64;
model2[1]=((byte1 & 3) << 3) + ((byte2 & 0xE0) >> 5) + 64;
model2[2]=(byte2 & 0x1F) + 64;
_stprintf(model2 + 3, _T("%X%X%X%X"), (EDID[p+3] & 0xf0) >> 4, EDID[p+3] & 0xf, (EDID[p+2] & 0xf0) >> 4, EDID[p+2] & 0x0f);
if (_tcscmp(model, model2) == 0)
{
Width = EDID[22];
Height = EDID[21];
result = true;
}
else
{
// EDID incorrect
}
}
RegCloseKey(hKey3);
}
}
}
RegCloseKey(hKey2);
}
i++;
}
RegCloseKey(hKey);
}
}

return result;
}

How to read the physical screen size of OSX?

You can use CGDisplayScreenSize to get the physical size of a screen in millimetres. From that you can compute the DPI given that you already know the resolution.

So e.g.

NSScreen *screen = [NSScreen mainScreen];
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(
[[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);

NSLog(@"DPI is %0.2f",
(displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
// there being 25.4 mm in an inch

That @"NSScreenNumber" thing looks dodgy but is the explicit documented means of obtaining a CGDirectDisplayID from an NSScreen.

Can DPI scaling be enabled/disabled programmatically on a per-session basis?

Here's the answer I was looking for, based on comments by IInspectable and andlabs (many thanks):

  import ctypes

# Query DPI Awareness (Windows 10 and 8)
awareness = ctypes.c_int()
errorCode = ctypes.windll.shcore.GetProcessDpiAwareness(0, ctypes.byref(awareness))
print(awareness.value)

# Set DPI Awareness (Windows 10 and 8)
errorCode = ctypes.windll.shcore.SetProcessDpiAwareness(2)
# the argument is the awareness level, which can be 0, 1 or 2:
# for 1-to-1 pixel control I seem to need it to be non-zero (I'm using level 2)

# Set DPI Awareness (Windows 7 and Vista)
success = ctypes.windll.user32.SetProcessDPIAware()
# behaviour on later OSes is undefined, although when I run it on my Windows 10 machine, it seems to work with effects identical to SetProcessDpiAwareness(1)

The awareness levels are defined as follows:

typedef enum _PROCESS_DPI_AWARENESS { 
PROCESS_DPI_UNAWARE = 0,
/* DPI unaware. This app does not scale for DPI changes and is
always assumed to have a scale factor of 100% (96 DPI). It
will be automatically scaled by the system on any other DPI
setting. */

PROCESS_SYSTEM_DPI_AWARE = 1,
/* System DPI aware. This app does not scale for DPI changes.
It will query for the DPI once and use that value for the
lifetime of the app. If the DPI changes, the app will not
adjust to the new DPI value. It will be automatically scaled
up or down by the system when the DPI changes from the system
value. */

PROCESS_PER_MONITOR_DPI_AWARE = 2
/* Per monitor DPI aware. This app checks for the DPI when it is
created and adjusts the scale factor whenever the DPI changes.
These applications are not automatically scaled by the system. */
} PROCESS_DPI_AWARENESS;

Level 2 sounds most appropriate for my goal although 1 will also work provided there's no change in system resolution / DPI scaling.

SetProcessDpiAwareness will fail with errorCode = -2147024891 = 0x80070005 = E_ACCESSDENIED if it has previously been called for the current process (and that includes being called by the system when the process is launched, due to a registry key or .manifest file)

How to get system scale factor in X11

To answer my own question, I've now tried three approaches:

  1. XRandR
  2. X11's DisplayWidth/Height and DisplayWidthMM/HeightMM
  3. Looking at xdpyinfo output

Neither returns the correct DPI. Instead, the Xft.dpi Xresource seems to be the key to this problem. Xft.dpi always seems to carry the correct DPI so we can just read it to get the system scale factor.

Here's some source taken from here:

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>

double _glfwPlatformGetMonitorDPI(_GLFWmonitor* monitor)
{
char *resourceString = XResourceManagerString(_glfw.x11.display);
XrmDatabase db;
XrmValue value;
char *type = NULL;
double dpi = 0.0;

XrmInitialize(); /* Need to initialize the DB before calling Xrm* functions */

db = XrmGetStringDatabase(resourceString);

if (resourceString) {
printf("Entire DB:\n%s\n", resourceString);
if (XrmGetResource(db, "Xft.dpi", "String", &type, &value) == True) {
if (value.addr) {
dpi = atof(value.addr);
}
}
}

printf("DPI: %f\n", dpi);
return dpi;
}

This does the trick for me.



Related Topics



Leave a reply



Submit