From 83855e16ce39102e9f47550e0c57eb1b23072327 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 19 May 2017 23:26:09 +0530 Subject: [PATCH] On linux query xrdb for Xft.dpi and use that, if set as the logical DPI. Fall back to the actual physical dpi as returned by GLFW if that fails. --- README.asciidoc | 2 +- kitty/utils.py | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index fdaebd64f..98295236f 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -82,7 +82,7 @@ the following dependencies are installed first. * glfw >= 3.2 * glew >= 2.0 (not needed on macOS) * fontconfig (not needed on macOS) -* xdpyinfo and xsel (only on X11 based systems) +* xrdb and xsel (only on X11 based systems) * gcc or clang (required only for building) * pkg-config (required only for building) diff --git a/kitty/utils.py b/kitty/utils.py index 0381fe28c..f2db9eab4 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -50,17 +50,32 @@ def sanitize_title(x): return re.sub(r'\s+', ' ', re.sub(r'[\0-\x19]', '', x)) +def x11_dpi(): + try: + raw = subprocess.check_output(['xrdb', '-query']).decode('utf-8') + except Exception: + return None + m = re.search(r'^Xft.dpi:\s+([0-9.]+)', raw, re.IGNORECASE | re.MULTILINE) + if m is not None: + try: + return float(m.group(1)) + except Exception: + pass + + def get_logical_dpi(): if not hasattr(get_logical_dpi, 'ans'): if isosx: # TODO: Investigate if this needs a different implementation on OS X get_logical_dpi.ans = glfw_get_physical_dpi() else: - raw = subprocess.check_output(['xdpyinfo']).decode('utf-8') - m = re.search( - r'^\s*resolution:\s*(\d+)+x(\d+)', raw, flags=re.MULTILINE - ) - get_logical_dpi.ans = int(m.group(1)), int(m.group(2)) + # See https://github.com/glfw/glfw/issues/1019 for why we cant use + # glfw_get_physical_dpi() + dpi = x11_dpi() + if dpi is None: + get_logical_dpi.ans = glfw_get_physical_dpi() + else: + get_logical_dpi.ans = dpi, dpi return get_logical_dpi.ans