From 3135e82b798b03d2424cc105f203988908d1cb3c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 May 2019 13:29:58 -0400 Subject: X11: Better DPI handling --- .../user/X11@.target.wants/fix-dpi@.service | 1 + .../user/X11@.target.wants/xresources-dpi@.service | 1 - .config/systemd/user/fix-dpi@.service | 15 ++++++ .config/systemd/user/xresources-dpi@.service | 15 ------ .local/bin/fix-dpi | 53 ++++++++++++++++++++++ .local/bin/xrdb-set-dpi | 3 -- 6 files changed, 69 insertions(+), 19 deletions(-) create mode 120000 .config/systemd/user/X11@.target.wants/fix-dpi@.service delete mode 120000 .config/systemd/user/X11@.target.wants/xresources-dpi@.service create mode 100644 .config/systemd/user/fix-dpi@.service delete mode 100644 .config/systemd/user/xresources-dpi@.service create mode 100755 .local/bin/fix-dpi delete mode 100755 .local/bin/xrdb-set-dpi diff --git a/.config/systemd/user/X11@.target.wants/fix-dpi@.service b/.config/systemd/user/X11@.target.wants/fix-dpi@.service new file mode 120000 index 0000000..af1d134 --- /dev/null +++ b/.config/systemd/user/X11@.target.wants/fix-dpi@.service @@ -0,0 +1 @@ +../fix-dpi@.service \ No newline at end of file diff --git a/.config/systemd/user/X11@.target.wants/xresources-dpi@.service b/.config/systemd/user/X11@.target.wants/xresources-dpi@.service deleted file mode 120000 index f3b5a4c..0000000 --- a/.config/systemd/user/X11@.target.wants/xresources-dpi@.service +++ /dev/null @@ -1 +0,0 @@ -../xresources-dpi@.service \ No newline at end of file diff --git a/.config/systemd/user/fix-dpi@.service b/.config/systemd/user/fix-dpi@.service new file mode 100644 index 0000000..d248cf2 --- /dev/null +++ b/.config/systemd/user/fix-dpi@.service @@ -0,0 +1,15 @@ +[Unit] +Description=Rectify all DPI settings on X display %I +Documentation=man:xrdb(1) +Before=X11@%i.target +Requisite=X11@%i.target + +[Service] +Environment=DISPLAY=%I + +Type=oneshot +ExecStart=/usr/bin/env flock %t/x11-xrdb@%I -c fix-dpi +SyslogIdentifier=fix-dpi + +[Install] +WantedBy=X11@%i.target diff --git a/.config/systemd/user/xresources-dpi@.service b/.config/systemd/user/xresources-dpi@.service deleted file mode 100644 index c9d23cc..0000000 --- a/.config/systemd/user/xresources-dpi@.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=Set the X Resources DataBase DPI to match the display DPI on X display %I -Documentation=man:xrdb(1) -Before=X11@%i.target -Requisite=X11@%i.target - -[Service] -Environment=DISPLAY=%I - -Type=oneshot -ExecStart=/usr/bin/env flock %t/x11-xrdb@%I -c xrdb-set-dpi -SyslogIdentifier=xrdb-set-dpi - -[Install] -WantedBy=X11@%i.target diff --git a/.local/bin/fix-dpi b/.local/bin/fix-dpi new file mode 100755 index 0000000..90019c2 --- /dev/null +++ b/.local/bin/fix-dpi @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# Copyright 2019 Luke Shumaker + +# env-var: MAX_DPI: integer +# env-var: DRY_RUN: empty/non-empty + +( # Phase 1: Probe outputs + export LC_ALL=C + xrandr | sed -rn -e 's@(.*) connected( .*)? ([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)( .*)? ([0-9]+)mm x ([0-9]+)mm( .*)?@\1 \3 \4 \8 \9@p' -e 's@^ ([0-9]+)x([0-9]+)i? .*\*.*@ \1 \2@p' | sed '/^\S/{ N; s/\n//; }' + # 1 2 3 4 5 6 7 8 9 10 1 2 + # `-outputName | `-fb_xpx `-fb_ypx `-fb_xoff `-fb_yoff `- hw_xmm `- hw_ymm | `-hw_xpx `-hw_ypx + # `-discard `- discard `- discard +) | ( # Phase 2: Translate that to a set of actions to perform + max_xdpi=${MAX_DPI:-96} + max_ydpi=${MAX_DPI:-96} + + dpi=$(xrdb -query|sed -n 's/^Xft\.dpi:\s*//p') + xdpi=${dpi%%x*} + ydpi=${dpi#*x} + if (( xdpi > max_xdpi )); then + max_xdpi=$xdpi + fi + if (( ydpi > max_ydpi )); then + max_ydpi=$ydpi + fi + + declare -A outputs + while read -r output fb_xpx fb_ypx hw_xmm hw_ymm hw_xpx hw_ypx; do + xdpi=$(bc <<<"($hw_xpx*25.4)/$hw_xmm") + ydpi=$(bc <<<"($hw_ypx*25.4)/$hw_ymm") + if (( xdpi > max_xdpi )); then + max_xdpi=$xdpi + fi + if (( ydpi > max_ydpi )); then + max_ydpi=$ydpi + fi + outputs["$output"]="$xdpi $ydpi" + done + echo 'xrandr \' + printf ' --dpi %q \\\n' "${max_xdpi}x${max_ydpi}" + for output in "${!outputs[@]}"; do + read -r xdpi ydpi <<<"${outputs[$output]}" + printf ' --output %q --scale %q \\\n' "$output" "$(bc <<<"scale=5; $max_xdpi/$xdpi")x$(bc <<<"scale=5; $max_ydpi/$ydpi")" + done + echo + printf "xrdb -merge <<<'Xft.dpi: %s'\n" "${max_xdpi}x${max_ydpi}" +) | ( # Phase 3: Apply those actions + if [[ -z "$DRY_RUN" ]]; then + bash -v + else + cat + fi +) diff --git a/.local/bin/xrdb-set-dpi b/.local/bin/xrdb-set-dpi deleted file mode 100755 index a87627e..0000000 --- a/.local/bin/xrdb-set-dpi +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -dpi=$(LC_ALL=C xdpyinfo|sed -rn 's/^\s*resolution:\s*(.*) dots per inch$/\1/p') -xrdb -merge <<<"Xft.dpi: ${dpi}" -- cgit v1.1-4-g5e80