Skip to content

Instantly share code, notes, and snippets.

@mortie
Last active February 16, 2024 21:44
Show Gist options
  • Save mortie/37751e6bf4fbd408473c20c138370328 to your computer and use it in GitHub Desktop.
Save mortie/37751e6bf4fbd408473c20c138370328 to your computer and use it in GitHub Desktop.
gamerandr: Display mode switching automation for GNOME Wayland
#!/bin/sh
# This program uses gnome-randr (https://github.com/maxwellainatchi/gnome-randr-rust)
# to change display configurations for the duration of a process.
# It's intended to use with games if you're normally using the computer with
# DPI scaling enabled.
# Usage: gamerandr <config> [command...]
# It can be used with Steam by changing the launch options of a game to:
# gamerandr <config> %command%
# For example, I use 'gamerandr fps %command%' for Counter-Strike 2,
# and 'gamerandr cinematic %command%' for Baldur's Gate 3.
# These options select what config to return back to when the process exits.
[email protected]
STD_SCALE=1.5
CONF="$1"
shift
# These are the available configs.
if [ "$CONF" = cinematic ]; then
[email protected]
SCALE=1
elif [ "$CONF" = fps ]; then
[email protected]
SCALE=1
elif [ "$CONF" = std ]; then
MODE="$STD_MODE"
SCALE="$STD_SCALE"
else
echo "Unknown config: '$CONF'" >&2
exit 1
fi
PRIMARY="$(gnome-randr query --summary \
| grep 'primary: yes' -A2 \
| tail -n 1 \
| awk '{print $1}')"
if [ -z "$PRIMARY" ]; then
echo "Failed to find primary monitor!" >&1
exit 1
fi
reset() {
if [ "$MODE" != "$STD_MODE" ] || [ "$SCALE" != "$STD_SCALE" ]; then
echo ' === Reset config...' >&2
exec gnome-randr modify --mode "$STD_MODE" --scale "$STD_SCALE" "$PRIMARY"
fi
}
trap reset exit TERM INT
echo ' === Set config...' >&2
gnome-randr modify --mode "$MODE" --scale "$SCALE" "$PRIMARY"
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment