DRYer
This commit is contained in:
@@ -71,20 +71,23 @@ def load_libx11():
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def parse_xrdb(raw):
|
||||||
|
q = 'Xft.dpi:\t'
|
||||||
|
for line in raw.decode('utf-8').splitlines():
|
||||||
|
if line.startswith(q):
|
||||||
|
return float(line[len(q):])
|
||||||
|
|
||||||
|
|
||||||
def x11_dpi_native():
|
def x11_dpi_native():
|
||||||
XOpenDisplay, XCloseDisplay, XResourceManagerString = load_libx11()
|
XOpenDisplay, XCloseDisplay, XResourceManagerString = load_libx11()
|
||||||
display = XOpenDisplay(None)
|
display = XOpenDisplay(None)
|
||||||
if display is None:
|
if display is None:
|
||||||
raise RuntimeError('Could not connect to the X server')
|
raise RuntimeError('Could not connect to the X server')
|
||||||
try:
|
try:
|
||||||
db = XResourceManagerString(display).decode('utf-8')
|
raw = XResourceManagerString(display)
|
||||||
q = 'Xft.dpi:\t'
|
return parse_xrdb(raw)
|
||||||
for line in db.splitlines():
|
|
||||||
if line.startswith(q):
|
|
||||||
return float(line[len(q):])
|
|
||||||
finally:
|
finally:
|
||||||
XCloseDisplay(display)
|
XCloseDisplay(display)
|
||||||
raise RuntimeError('Failed to get dpi resource')
|
|
||||||
|
|
||||||
|
|
||||||
def x11_dpi():
|
def x11_dpi():
|
||||||
@@ -93,15 +96,10 @@ def x11_dpi():
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
raw = subprocess.check_output(['xrdb', '-query']).decode('utf-8')
|
raw = subprocess.check_output(['xrdb', '-query'])
|
||||||
|
return parse_xrdb(raw)
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
pass
|
||||||
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():
|
def get_logical_dpi():
|
||||||
|
|||||||
Reference in New Issue
Block a user