Fix Electron/Chromium double-scaling via balanced XSETTINGS and unsetting GDK_SCALE
This commit is contained in:
1 parent
cf1544eb0c
commit
d0ef9c66ef
11 files changed
+205
-39
No files matched your search
@@ -53,6 +53,16 @@ class XClientMessageEvent(ctypes.Structure):
|
||||
]
|
||||
|
||||
|
||||
class XButtonEvent(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int),
|
||||
("display", Display_p), ("window", Window), ("root", Window), ("subwindow", Window),
|
||||
("time", ctypes.c_ulong), ("x", ctypes.c_int), ("y", ctypes.c_int),
|
||||
("x_root", ctypes.c_int), ("y_root", ctypes.c_int),
|
||||
("state", ctypes.c_uint), ("button", ctypes.c_uint), ("same_screen", ctypes.c_int),
|
||||
]
|
||||
|
||||
|
||||
class XWMHints(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("flags", ctypes.c_long), ("input", ctypes.c_int), ("initial_state", ctypes.c_int),
|
||||
@@ -622,6 +632,144 @@ def test_lockscreen_overlaps_systray():
|
||||
terminate(xvfb)
|
||||
|
||||
|
||||
def test_gtk_xsettings_no_double_scale():
|
||||
disp = pick_display()
|
||||
with tempfile.TemporaryDirectory() as home:
|
||||
env = dict(os.environ)
|
||||
env["DISPLAY"] = disp
|
||||
env["HOME"] = home
|
||||
env["XDG_CONFIG_HOME"] = os.path.join(home, ".config")
|
||||
env["XDG_DATA_HOME"] = os.path.join(home, ".local", "share")
|
||||
env["ANODE_PREFIX"] = ROOT
|
||||
env["ANODE_NO_SIGNAL"] = "1"
|
||||
env["ANODE_NO_DBUS"] = "1"
|
||||
env["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "")
|
||||
env["PATH"] = (
|
||||
os.path.join(BUILD, "dwm") + ":" +
|
||||
os.path.join(ROOT, "scripts") + ":" +
|
||||
env.get("PATH", "")
|
||||
)
|
||||
|
||||
xvfb = subprocess.Popen(
|
||||
["Xvfb", disp, "-noreset", "-screen", "0", "1920x1200x24", "-nolisten", "tcp"],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
dpy = None
|
||||
xsd = None
|
||||
dwm = None
|
||||
try:
|
||||
dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode()))
|
||||
assert dpy, f"Failed to open Xvfb {disp}"
|
||||
|
||||
gtk3_probe = (
|
||||
"import json\n"
|
||||
"try:\n"
|
||||
" import gi; gi.require_version('Gtk', '3.0'); from gi.repository import Gtk, Gdk\n"
|
||||
"except Exception:\n"
|
||||
" print('SKIP'); raise SystemExit(0)\n"
|
||||
"Gtk.init([])\n"
|
||||
"s = Gtk.Settings.get_default()\n"
|
||||
"d = Gdk.Display.get_default()\n"
|
||||
"m = d.get_primary_monitor() or d.get_monitor(0)\n"
|
||||
"print(json.dumps({'xft_dpi': s.get_property('gtk-xft-dpi'), 'mon_sc': m.get_scale_factor()}))\n"
|
||||
)
|
||||
gtk4_probe = (
|
||||
"import json\n"
|
||||
"try:\n"
|
||||
" import gi; gi.require_version('Gtk', '4.0'); from gi.repository import Gtk, Gdk\n"
|
||||
"except Exception:\n"
|
||||
" print('SKIP'); raise SystemExit(0)\n"
|
||||
"Gtk.init()\n"
|
||||
"s = Gtk.Settings.get_default()\n"
|
||||
"d = Gdk.Display.get_default()\n"
|
||||
"m = d.get_monitors().get_item(0)\n"
|
||||
"print(json.dumps({'xft_dpi': s.get_property('gtk-xft-dpi'), 'mon_sc': m.get_scale_factor()}))\n"
|
||||
)
|
||||
|
||||
import json
|
||||
has_xset = bool(shutil.which("xsettingsd"))
|
||||
if has_xset:
|
||||
os.makedirs(os.path.join(home, ".config", "xsettingsd"), exist_ok=True)
|
||||
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "1.50", "--touch=0"], env=env, check=True)
|
||||
xsd = subprocess.Popen(
|
||||
["xsettingsd", "-c", os.path.join(home, ".config", "xsettingsd", "xsettingsd.conf")],
|
||||
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
time.sleep(0.25)
|
||||
|
||||
for sc_str, exp_wsf, exp_dpi in [("1.50", 1, 144), ("1.75", 2, 168), ("2.00", 2, 192)]:
|
||||
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), sc_str, "--touch=0"], env=env, check=True)
|
||||
if xsd:
|
||||
xsd.send_signal(signal.SIGHUP)
|
||||
time.sleep(0.2)
|
||||
|
||||
dirty_env = dict(env)
|
||||
dirty_env["GDK_SCALE"] = "2"
|
||||
dirty_env["GDK_DPI_SCALE"] = "0.5"
|
||||
scale_env = os.path.join(home, ".config", "anode", "scale.env")
|
||||
|
||||
for label, probe_code in [("gtk3", gtk3_probe), ("gtk4", gtk4_probe)]:
|
||||
res = subprocess.run(
|
||||
["/bin/sh", "-c", '. "$1" && exec "$2" -c "$3"', "sh", scale_env, sys.executable, probe_code],
|
||||
env=dirty_env, capture_output=True, text=True, check=True,
|
||||
)
|
||||
line = [l for l in res.stdout.splitlines() if l.strip()][-1].strip()
|
||||
if line == "SKIP":
|
||||
continue
|
||||
info = json.loads(line)
|
||||
wsf_used = exp_wsf if has_xset else 1
|
||||
exp_xft = int((exp_dpi * 1024) / wsf_used + 0.5)
|
||||
assert info["mon_sc"] == wsf_used, f"{label} @ {sc_str}: {info} expected mon_sc={wsf_used}"
|
||||
assert abs(info["xft_dpi"] - exp_xft) <= 1024, f"{label} @ {sc_str}: {info} expected xft_dpi~{exp_xft}"
|
||||
eff_dpr = (info["xft_dpi"] / 1024.0 / 96.0) * info["mon_sc"]
|
||||
assert abs(eff_dpr - float(sc_str)) <= 0.02, f"{label} @ {sc_str} double-scaled: eff_dpr={eff_dpr} ({info})"
|
||||
|
||||
fakebin = os.path.join(home, "fakebin")
|
||||
os.makedirs(fakebin, exist_ok=True)
|
||||
child_env_path = os.path.join(home, "child_env.txt")
|
||||
launcher_stub = os.path.join(fakebin, "anode-launcher")
|
||||
with open(launcher_stub, "w", encoding="utf-8") as f:
|
||||
f.write("#!/bin/sh\nenv > \"$HOME/child_env.tmp\" && mv -f \"$HOME/child_env.tmp\" \"$HOME/child_env.txt\"\n")
|
||||
os.chmod(launcher_stub, 0o755)
|
||||
|
||||
dirty_dwm_env = dict(env)
|
||||
dirty_dwm_env["PATH"] = fakebin + ":" + dirty_dwm_env.get("PATH", "")
|
||||
dirty_dwm_env["GDK_SCALE"] = "2"
|
||||
dirty_dwm_env["GDK_DPI_SCALE"] = "0.5"
|
||||
dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=dirty_dwm_env)
|
||||
tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0)
|
||||
assert wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom)), "DWM failed to start on Xvfb"
|
||||
|
||||
root = X11.XDefaultRootWindow(dpy)
|
||||
bar_win = wait_until(lambda: next((w for w in query_children(dpy, root) if "anode-wm" in get_wm_class(dpy, w)), None))
|
||||
assert bar_win, "DWM barwin not found"
|
||||
bev = XButtonEvent()
|
||||
bev.type = 4 # ButtonPress
|
||||
bev.display = dpy
|
||||
bev.window = bar_win
|
||||
bev.root = root
|
||||
bev.x = 2
|
||||
bev.y = 2
|
||||
bev.x_root = 2
|
||||
bev.y_root = 2
|
||||
bev.state = 0
|
||||
bev.button = 1
|
||||
bev.same_screen = 1
|
||||
X11.XSendEvent(dpy, bar_win, 0, 1 << 2, ctypes.byref(bev))
|
||||
X11.XSync(dpy, 0)
|
||||
assert wait_until(lambda: os.path.isfile(child_env_path)), "DWM did not spawn launcher on bar click"
|
||||
with open(child_env_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||
child_lines = [line.strip() for line in f.read().splitlines() if line.strip()]
|
||||
assert "ANODE_SCALE=2.00" in child_lines, child_lines
|
||||
assert not any(line.startswith(("GDK_SCALE=", "GDK_DPI_SCALE=")) for line in child_lines), child_lines
|
||||
finally:
|
||||
terminate(dwm)
|
||||
terminate(xsd)
|
||||
if dpy:
|
||||
X11.XCloseDisplay(dpy)
|
||||
terminate(xvfb)
|
||||
|
||||
|
||||
def main():
|
||||
if not shutil.which("Xvfb"):
|
||||
print("test_x11_xvfb: Xvfb not installed (skipping)")
|
||||
@@ -638,6 +786,8 @@ def main():
|
||||
print("test_x11_xvfb: St.font override & Dunst scale=1 OK")
|
||||
test_lockscreen_overlaps_systray()
|
||||
print("test_x11_xvfb: lockscreen overlaps systray OK")
|
||||
test_gtk_xsettings_no_double_scale()
|
||||
print("test_x11_xvfb: GTK/XSETTINGS non-double-scaling OK")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user