7

I wrote a set-up script for my dual screen set-up some while ago, for when X went bonkers and I needed to restore sanity, but I just found a spare monitor and worked out I can use DP from a USB-C port I didn't know I had.

It suddenly occurred to me that xrandr might be able to create its own command-line for the current set-up, rather than me having to fiddle about writing it myself, but I can't find any options for that.

Is there a utility for this? Is it a stupid idea?

  • I recommend letting autorandr handle the xrandr or arandr details behind the scenes for you. Example here: https://askubuntu.com/a/1130337/8 It's much simpler, just be sure to disable/remove the prior solutions so they don't clash with each other. – mechanical_meat Nov 21 '21 at 21:23

3 Answers3

5

I've had the same problem before which led me to create a function in ~/.bashrc called xreset:

xreset () {

    xrandr --output HDMI-0  --mode 1920x1080 --pos 0x0       --rotate normal \
           --output eDP-1-1 --mode 1920x1080 --pos 3840x2160 --rotate normal \
           --output DP-1-1  --mode 3840x2160 --pos 1920x0    --rotate normal

} # xreset

After you get your monitors arranged by position and resolution, grab the current setup with:

$ xrandr | grep " connected"

HDMI-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 1107mm x 623mm
eDP-1-1 connected primary 1920x1080+3840+2160 (normal left inverted right x axis y axis) 382mm x 215mm
DP-1-1 connected 3840x2160+1920+0 (normal left inverted right x axis y axis) 1600mm x 900mm

All the information is there for you to create your own xreset function.

4

Tut. Sometimes asking the question gets you different search terms.

Arandr is a graphical editor that allows you to move things around, but most pertinently it will save/export xrandr command-lines as scripts:

xrandr \
   --output HDMI-2 --mode 3840x2160 --pos 0x48 --rotate normal \
   --output HDMI-1 --off \
   --output DP-1 --mode 3840x2160 --pos 5760x0 --rotate left \
   --output eDP-1 --primary --mode 1920x1080 --pos 3840x1528 --rotate normal \
   --output DP-2 --off

I don't need the 'off' outputs, but they're harmless.

0

I don't know such software but here's a quick script that does the logical counterpart of iptables -S, that is generate commands that result in current config:

#!/bin/bash
# Emit current X configuration as xrandr commands (similar to iptables -S)
# (does not currently support custom modelines or axis or panning)
# Copyright 2003 Mikko Rantalainen <mikko.rantalainen@iki.fi>
# License: MIT

function create_flags() { port="$1" shift status="$1" shift if [ "$status" = "disconnected" ]; then printf "%s" "--output '$port' --off" return fi if [ "$status" != "connected" ]; then echo "Error: port=$port: unknown status=$status" 1>&2 exit 1 fi

PRIMARY=&quot;&quot;
if [ &quot;$1&quot; = &quot;primary&quot; ]; then
    PRIMARY=&quot;yes&quot;
    shift
fi

mode=&quot;$1&quot;
shift

printf &quot;%s\n&quot; &quot;$mode&quot; | sed 's/+/ /; s/+/x/' | while read mode position
do
    printf &quot;%s&quot; &quot;--output '$port' --mode '$mode' --pos '$position'&quot;
done

if [ &quot;$PRIMARY&quot; = &quot;yes&quot; ]; then
    printf &quot;%s&quot; &quot; --primary&quot;
fi

rotation=&quot;$1&quot;
if [ &quot;$rotation&quot; = &quot;left&quot; ]; then
    printf &quot; --rotation left&quot;
    shift
elif [ &quot;$rotation&quot; = &quot;right&quot; ]; then
    printf &quot; --rotation right&quot;
    shift
elif [ &quot;$rotation&quot; = &quot;inverted&quot; ]; then
    printf &quot; --rotation inverted&quot;
    shift
fi

return

}

printf "xrandr" LC_ALL=C xrandr -q | grep connected | while read line do test "$1" = "--debug" && printf "\nProcessing line [%s]\n" "$line" 1>&2 printf " %s" "$(create_flags $line)" done printf "\n"

Save that as a text file, give it executable bit and run it to emit the current X screen configuration as xrandr command. Pass a command line argument --debug to show how each line is interpreted.

I wish xrandr -S emitted this by default. Parsing textual output is always error-prone if the output is not designed to be parsed in the future, too. This doesn't try to preserve the selected refresh rate because output syntax of the query is that problematic. Instead, the xrandr will default to highest supported refresh rate for each GPU and monitor combination.

Note that connector / output names may change so the emitted commands cannot be used if xrandr renames the outputs after reboot. See https://www.baeldung.com/linux/primary-monitor-x-wayland for details.