aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.SRCINFO17
-rw-r--r--.gitignore3
-rw-r--r--50-auto-xrandr.rules1
-rw-r--r--LICENSE7
-rw-r--r--PKGBUILD21
-rw-r--r--README.md53
-rwxr-xr-xauto-xrandr84
-rw-r--r--auto-xrandr.168
8 files changed, 254 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO
new file mode 100644
index 000000000000..4451e91ed15a
--- /dev/null
+++ b/.SRCINFO
@@ -0,0 +1,17 @@
+pkgbase = auto-xrandr
+ pkgdesc = Automatically configure xrandr when a display is connected or disconnected
+ pkgver = 1.0
+ pkgrel = 1
+ arch = any
+ license = MIT
+ source = auto-xrandr
+ source = 50-auto-xrandr.rules
+ source = auto-xrandr.1
+ source = LICENSE
+ md5sums = 8318174020e291f92200699b7d2eae87
+ md5sums = 1fe793614e16d43d7ac23ff49194779f
+ md5sums = 6150fd40022bcacc38a92114c7756ad5
+ md5sums = e0dd463a730f70e341bfd18a4e6d2b3d
+
+pkgname = auto-xrandr
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000000..6741c5aced80
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+pkg/
+src/
+*.pkg.tar.zst \ No newline at end of file
diff --git a/50-auto-xrandr.rules b/50-auto-xrandr.rules
new file mode 100644
index 000000000000..2fbd62ecb677
--- /dev/null
+++ b/50-auto-xrandr.rules
@@ -0,0 +1 @@
+SUBSYSTEM=="drm", ACTION=="change", ENV{DISPLAY}=":0", RUN+="/usr/bin/auto-xrandr"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 000000000000..574137a84f36
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2021 Adrian Sinclair
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/PKGBUILD b/PKGBUILD
new file mode 100644
index 000000000000..3c81a167c37f
--- /dev/null
+++ b/PKGBUILD
@@ -0,0 +1,21 @@
+pkgname=auto-xrandr
+pkgver=1.0
+pkgrel=1
+pkgdesc="Automatically configure xrandr when a display is connected or disconnected"
+arch=('any')
+license=('MIT')
+source=("auto-xrandr"
+ "50-auto-xrandr.rules"
+ "auto-xrandr.1"
+ "LICENSE")
+md5sums=('8318174020e291f92200699b7d2eae87'
+ '1fe793614e16d43d7ac23ff49194779f'
+ '6150fd40022bcacc38a92114c7756ad5'
+ 'e0dd463a730f70e341bfd18a4e6d2b3d')
+
+package() {
+ install -Dm755 -t "$pkgdir/usr/bin" "$srcdir/auto-xrandr"
+ install -Dm644 -t "$pkgdir/usr/lib/udev/rules.d" "$srcdir/50-auto-xrandr.rules"
+ install -Dm644 -t "$pkgdir/usr/share/man/man1" "$srcdir/auto-xrandr.1"
+ install -Dm644 -t "$pkgdir/usr/share/licenses/auto-xrandr" "$srcdir/LICENSE"
+}
diff --git a/README.md b/README.md
new file mode 100644
index 000000000000..272af7f47d10
--- /dev/null
+++ b/README.md
@@ -0,0 +1,53 @@
+# auto-xrandr
+
+auto-xrandr is a small tool that assists in creating xrandr configurations.
+
+Its intended use it to be run automatically when a display is connected or disconnected, via its included udev rule.
+It can also be run manually at the command line.
+
+When run with no arguments, auto-xrandr will execute `$XDG_CONFIG_HOME/auto-xrandr/configure-outputs`, passing in some information about connected displays over stdin.
+The `configure-outputs` script is run as the user who owns it, in a new login shell, inheriting the `DISPLAY` environment variable from auto-xrandr.
+Its input consists of lines in the format `<xrandr_name> <product_id> <serial_no>`.
+For example:
+
+```
+eDP-1 1234 0
+DP-4 56789 7654321
+DP-3-2 56789 8765432
+```
+
+It is intended that the `configure-outputs` script will use the product ID and serial number to identify the display independent of how it is connected, and then configure it using xrandr.
+An example `configure-outputs` script:
+
+```bash
+#!/bin/sh
+while read -r output product serial; do
+ case "$product:$serial" in
+ 56789:8765432)
+ xrandr --output "$output" --scale 1.5x1.5 --pos 0x0 --primary
+ ;;
+ 56789:7654321)
+ xrandr --output "$output" --scale 1.5x1.5 --pos 5760x0
+ ;;
+ 1234:0)
+ xrandr --output "$output" --scale 2x2 --pos 3840x3240
+ ;;
+ esac
+done
+```
+
+When auto-xrandr is run with the `--list` flag, it will write the display information to stdout instead of calling `configure-outputs`.
+
+## Limitations
+
+auto-xrandr assumes that only one user has an active Xorg session.
+If more than one user is running X, it will arbitrarily pick one of their `configure-outputs` scripts to execute.
+
+When invoked via udev, auto-xrandr assumes `DISPLAY=:0`.
+When invoked manually, it correctly inherits the `DISPLAY` variable.
+
+## Requirements
+
+auto-xrandr has no dependencies other than xrandr and a POSIX compliant runtime.
+
+Udev is required for automatic detection of changes to connected displays. \ No newline at end of file
diff --git a/auto-xrandr b/auto-xrandr
new file mode 100755
index 000000000000..ea39b42b0300
--- /dev/null
+++ b/auto-xrandr
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+list_mode=false
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -l|--list)
+ list_mode=true
+ ;;
+ *)
+ printf 'USAGE: %s [-l|--list]\n' "$0" >&2
+ exit 1
+ ;;
+ esac
+ shift
+done
+
+escape() {
+ printf '%q ' "$@"
+ printf '\n'
+}
+
+read -r _Xorg xorg_user <<<"$(ps -eo comm,user | grep Xorg)"
+if ! $list_mode && [ -e "$xorg_user" ]; then
+ printf '[%s] could not find owner of X session' "$0" >&2
+ exit 1
+fi
+
+[ "$USER" != "$xorg_user" ] && exec su --login "$xorg_user" -c "DISPLAY=$(escape "$DISPLAY") $(escape "$0" "$@")"
+
+# per the EDID standard: http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf
+
+big_to_little_endian() {
+ little_endian=""
+ while read -rn2 byte; do
+ little_endian="${byte}${little_endian}"
+ done <<<"$1"
+ printf '%s\n' "$little_endian"
+}
+
+hex_to_dec() {
+ printf '%d\n' "0x${1}"
+}
+
+model_number() {
+ hex_to_dec "$(big_to_little_endian "${1:20:4}")"
+}
+
+serial_number() {
+ hex_to_dec "$(big_to_little_endian "${1:24:8}")"
+}
+
+state=start
+{ xrandr --prop | grep -E '^[A-Za-z0-9-]+ connected|^[[:space:]]*[0-9a-f]+$'; printf 'EOF\n'; } | while read -r line; do
+ while true; do
+ case $state in
+ start)
+ if printf '%s\n' "$line" | grep -E '^[A-Za-z0-9-]+ connected' >/dev/null; then
+ state=edid
+ edid=""
+ output="$(printf '%s\n' "$line" | cut -f1 -d' ')"
+ fi
+ ;;
+ edid)
+ if printf '%s\n' "$line" | grep -vE '^[[:space:]]*[0-9a-f]+$' >/dev/null; then
+ printf '%s %s %s\n' "$output" "$(model_number "$edid")" "$(serial_number "$edid")"
+ state=start
+ continue
+ fi
+ edid="${edid}${line}"
+ ;;
+ esac
+ break
+ done
+done | {
+ if $list_mode; then
+ cat
+ else
+ if ! [ -x "${XDG_CONFIG_HOME:-$HOME/.config}/auto-xrandr/configure-outputs" ]; then
+ printf '[%s] user %s does not have an auto-xrandr configuration\n' "$0" "$xorg_user" >&2
+ exit 1
+ fi
+ "${XDG_CONFIG_HOME:-$HOME/.config}/auto-xrandr/configure-outputs"
+ fi
+}
diff --git a/auto-xrandr.1 b/auto-xrandr.1
new file mode 100644
index 000000000000..f9bfbb325349
--- /dev/null
+++ b/auto-xrandr.1
@@ -0,0 +1,68 @@
+.\" man page for auto-xrandr
+.TH AUTO-XRANDR 1 "auto-xrandr 1.0" "Adrian Sinclair" "auto-xrandr Manual"
+.SH NAME
+auto\-xrandr \- automatically configure displays
+.SH SYNOPSIS
+.B auto\-xrandr
+[\fB\-l\fR]
+.SH DESCRIPTION
+.B auto-xrandr
+is a small tool used to help with configuring display outputs using xrandr.
+It is intended to be run automatically by a udev rule, but can also be run manually.
+.P
+auto\-xrandr executes \`$XDG_CONFIG_HOME/auto\-xrandr/configure\-outputs,\' passing information about connected displays over STDIN.
+This information consists of lines in the format \`<xrandr_name> <product_id> <serial_number>.\'
+.SH OPTIONS
+.TP
+.BR \-l ", " \-\-list
+Write connected display information to STDOUT instead of calling the user's configuration script.
+.SH EXAMPLES
+Example output of auto\-xrandr \-\-list:
+.P
+.in 14
+eDP\-1 1234 0
+.br
+DP\-4 56789 7654321
+.br
+DP\-3\-2 56789 8765432
+.P
+Example configure-outputs script:
+.P
+.in 14
+#!/bin/sh
+.br
+while read \-r output product serial; do
+.br
+.in 18
+case "$product:$serial" in
+.br
+56789:8765432)
+.br
+.in 22
+xrandr \-\-output "$output" \-\-scale 1.5x1.5 \-\-pos 0x0 \-\-primary
+.br
+;;
+.br
+.in 18
+56789:7654321)
+.br
+.in 22
+xrandr \-\-output "$output" \-\-scale 1.5x1.5 \-\-pos 5760x0
+.br
+;;
+.br
+.in 18
+1234:0)
+.br
+.in 22
+xrandr \-\-output "$output" \-\-scale 2x2 \-\-pos 3840x3240
+.br
+;;
+.br
+.in 18
+esac
+.br
+.in 14
+done
+.SH SEE ALSO
+Xrandr(1) \ No newline at end of file