From a65856ec9878267042ffb9cd46590bf4ba2affd9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 May 2017 00:21:09 +0530 Subject: [PATCH] Use Xlib to directly query the xrm system.Only if it fails do we try to shell out to xrd --- kitty/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/kitty/utils.py b/kitty/utils.py index f2db9eab4..2d0b7a8cf 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -50,7 +50,46 @@ def sanitize_title(x): return re.sub(r'\s+', ' ', re.sub(r'[\0-\x19]', '', x)) +@lru_cache() +def load_libx11(): + import ctypes + from ctypes.util import find_library + libx11 = ctypes.CDLL(find_library('X11')) + ans = [] + + def cdef(name, restype, *argtypes): + f = getattr(libx11, name) + if restype is not None: + f.restype = restype + if argtypes: + f.argtypes = argtypes + ans.append(f) + + cdef('XOpenDisplay', ctypes.c_void_p, ctypes.c_char_p) + cdef('XCloseDisplay', ctypes.c_int, ctypes.c_void_p) + cdef('XResourceManagerString', ctypes.c_char_p, ctypes.c_void_p) + return ans + + +def x11_dpi_native(): + XOpenDisplay, XCloseDisplay, XResourceManagerString = load_libx11() + display = XOpenDisplay(None) + if display is None: + raise RuntimeError('Could not connect to the X server') + try: + db = XResourceManagerString(display).decode('utf-8') + for line in db.splitlines(): + if line.startswith('Xft.dpi:'): + return float(line.split()[1]) + finally: + XCloseDisplay(display) + + def x11_dpi(): + try: + return x11_dpi_native() + except Exception: + pass try: raw = subprocess.check_output(['xrdb', '-query']).decode('utf-8') except Exception: