Skip to content

Instantly share code, notes, and snippets.

@3v1n0
Last active March 25, 2019 20:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 3v1n0/772d48481cffc5b8ca1527d6dcc62f38 to your computer and use it in GitHub Desktop.
Save 3v1n0/772d48481cffc5b8ca1527d6dcc62f38 to your computer and use it in GitHub Desktop.
Xrandr Scaling script, with auto panning when scaling down
#!/bin/bash
export LANG=C
output=$1;
scale=$2;
if [ -n "$output" ] && ! (xrandr --listmonitors | grep -qw "$output"); then
echo "Invalid output: '$output'";
exit 1;
fi
modes=$(xrandr | sed -n "/$output/,//{/$output/{p;n};/^[^ ]\+/{q};p}");
selected_mode=$(echo "$modes" | grep '[0-9].\*'| cut -f4 -d' ');
x=$(echo "$selected_mode" | cut -f1 -dx);
y=$(echo "$selected_mode" | cut -f2 -dx);
if [ -z "$scale" ] || ( [ -n "${scale//[0-9]/}" ] && [ "${scale//[0-9]/}" != '.' ] ); then
echo "Invalid scale input: '$scale'";
exit 1;
fi
scale=$(echo "scale=4; 1.0 / $scale" | bc);
scaled_x=$(printf "%0.0f" $(echo "scale=4; $x * $scale" | bc));
scaled_y=$(printf "%0.0f" $(echo "scale=4; $y * $scale" | bc));
if [ $(printf "%0.0f" $scale) == "1" ]; then
panning=0x0;
else
panning=${scaled_x}x${scaled_y}
fi
echo "Current res is ${x}x${y}, scaling at ${scaled_x}x${scaled_y}";
xrandr --output $output --scale ${scale}x${scale} --panning $panning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment