autorandr/autorandr

329 lines
8.4 KiB
Plaintext
Raw Normal View History

2010-10-26 14:33:34 +00:00
#!/bin/sh
#
2012-06-28 20:25:33 +00:00
# Automatically select a display configuration based on connected devices
2010-10-26 14:33:34 +00:00
#
# Stefan Tomanek <stefan.tomanek@wertarbyte.de>
#
# How to use:
#
2010-12-10 01:01:21 +00:00
# Save your current display configuration and setup with:
# $ autorandr --save mobile
2010-10-26 14:33:34 +00:00
#
2010-12-10 01:01:21 +00:00
# Connect an additional display, configure your setup and save it:
# $ autorandr --save docked
2010-10-26 14:33:34 +00:00
#
# Now autorandr can detect which hardware setup is active:
2010-12-10 01:01:21 +00:00
# $ autorandr
# mobile
# docked (detected)
2010-10-26 14:33:34 +00:00
#
# To automatically reload your setup, just append --change to the command line
#
# To manually load a profile, you can use the --load <profile> option.
#
# autorandr tries to avoid reloading an identical configuration. To force the
2010-12-10 01:01:21 +00:00
# (re)configuration, apply --force.
#
2010-10-26 14:33:34 +00:00
# To prevent a profile from being loaded, place a script call "block" in its
# directory. The script is evaluated before the screen setup is inspected, and
# in case of it returning a value of 0 the profile is skipped. This can be used
# to query the status of a docking station you are about to leave.
#
# If no suitable profile can be identified, the current configuration is kept.
# To change this behaviour and switch to a fallback configuration, specify
# --default <profile>
#
# Another script called "postswitch "can be placed in the directory
2010-12-10 01:01:21 +00:00
# ~/.autorandr as well as in all profile directories: The scripts are executed
# after a mode switch has taken place and can notify window managers or other
# applications about it.
#
#
2012-06-28 20:25:33 +00:00
# While the script uses xrandr by default, calling it by the name "autodisper"
2010-12-10 01:01:21 +00:00
# or "auto-disper" forces it to use the "disper" utility, which is useful for
# controlling nvidia chipsets. The formats for fingerprinting the current setup
# and saving/loading the current configuration are adjusted accordingly.
2010-10-26 14:33:34 +00:00
XRANDR=/usr/bin/xrandr
2010-12-10 01:01:21 +00:00
DISPER=/usr/bin/disper
2012-02-12 22:10:54 +00:00
XDPYINFO=/usr/bin/xdpyinfo
2010-10-26 14:33:34 +00:00
PROFILES=~/.autorandr/
2010-11-08 21:19:18 +00:00
CONFIG=~/.autorandr.conf
2010-10-26 14:33:34 +00:00
CHANGE_PROFILE=0
FORCE_LOAD=0
2010-10-26 14:33:34 +00:00
DEFAULT_PROFILE=""
SAVE_PROFILE=""
FP_METHODS="setup_fp_sysfs_edid setup_fp_xrandr_edid"
2010-12-10 01:01:21 +00:00
CURRENT_CFG_METHOD="current_cfg_xrandr"
LOAD_METHOD="load_cfg_xrandr"
SCRIPTNAME="$(basename $0)"
# when called as autodisper/auto-disper, we assume different defaults
if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then
2011-01-16 16:13:41 +00:00
echo "Assuming disper defaults..." >&2
2010-12-10 01:01:21 +00:00
FP_METHODS="setup_fp_disper"
CURRENT_CFG_METHOD="current_cfg_disper"
LOAD_METHOD="load_cfg_disper"
fi
2011-01-16 16:13:41 +00:00
if [ -f $CONFIG ]; then
echo "Loading configuration from '$CONFIG'" >&2
. $CONFIG
fi
2010-11-08 21:19:18 +00:00
2010-11-08 20:46:03 +00:00
setup_fp_xrandr_edid() {
2010-10-26 14:33:34 +00:00
$XRANDR -q --verbose | awk '
2010-11-08 20:46:03 +00:00
/^[^ ]+ (dis)?connected / { DEV=$1; }
2010-10-26 14:33:34 +00:00
$1 ~ /^[a-f0-9]+$/ { ID[DEV] = ID[DEV] $1 }
END { for (X in ID) { print X " " ID[X]; } }'
}
2010-11-08 20:46:03 +00:00
setup_fp_sysfs_edid() {
2010-11-09 14:16:46 +00:00
# xrandr triggers the reloading of EDID data
$XRANDR -q > /dev/null
2010-11-08 20:46:03 +00:00
# hash the EDIDs of all _connected_ devices
for P in /sys/class/drm/card*-*/; do
# nothing found
[ ! -d "$P" ] && continue
2010-11-08 20:46:03 +00:00
if grep -q "^connected$" < "${P}status"; then
echo -n "$(basename "$P") "
md5sum ${P}edid | awk '{print $1}'
fi
done
}
2010-12-10 01:01:21 +00:00
setup_fp_disper() {
$DISPER -l | grep '^display '
}
2010-11-08 20:46:03 +00:00
setup_fp() {
local FP="";
for M in $FP_METHODS; do
2010-11-08 20:46:03 +00:00
FP="$($M)"
2011-01-16 16:13:41 +00:00
if [ -n "$FP" ]; then
break
fi
2010-11-08 20:46:03 +00:00
done
if [ -z "$FP" ]; then
echo "Unable to fingerprint display configuration" >&2
return
fi
echo "$FP"
2010-10-27 19:56:08 +00:00
}
2010-12-10 01:01:21 +00:00
current_cfg_xrandr() {
2012-02-12 22:10:54 +00:00
local PRIMARY_SETUP="";
if [ -x "$XDPYINFO" ]; then
PRIMARY_SETUP="$($XDPYINFO -ext XINERAMA | awk '/^ head #0:/ {printf $3 $5}')"
fi
$XRANDR -q | awk -v primary_setup="${PRIMARY_SETUP}" '
2011-04-11 19:59:08 +00:00
# display is connected and has a mode
/^[^ ]+ connected [^(]/ {
2010-10-26 14:33:34 +00:00
print "output "$1;
if ($3 == "primary") {
print $3
split($4, A, "+")
$4=$5
}
else {
split($3, A, "+");
if (A[1] A[2] "," A[3] == primary_setup)
print "primary";
}
if (($4 == "left") || ($4 == "right")) {
split(A[1], B, "x");
A[1] = B[2]"x"B[1];
print "rotate "$4;
}
else {
print "rotate normal";
}
2010-10-26 14:33:34 +00:00
print "mode "A[1];
print "pos "A[2]"x"A[3];
2011-04-11 19:59:08 +00:00
next;
}
# disconnected or disabled displays
/^[^ ]+ (dis)?connected / ||
/^[^ ]+ unknown connection / {
2011-04-11 19:59:08 +00:00
print "output "$1;
print "off";
next;
2010-10-26 14:33:34 +00:00
}'
}
2010-12-10 01:01:21 +00:00
current_cfg_disper() {
$DISPER -p
}
current_cfg() {
$CURRENT_CFG_METHOD;
2010-12-10 01:01:21 +00:00
}
2010-10-26 14:33:34 +00:00
blocked() {
2010-11-08 20:48:25 +00:00
local PROFILE="$1"
[ ! -x "$PROFILES/$PROFILE/block" ] && return 1
2010-10-26 14:33:34 +00:00
2010-11-08 20:48:25 +00:00
"$PROFILES/$PROFILE/block" "$PROFILE"
2010-10-26 14:33:34 +00:00
}
config_equal() {
local PROFILE="$1"
if [ "$(cat "$PROFILES/$PROFILE/config")" = "$(current_cfg)" ]; then
echo "Config already loaded"
return 0
else
return 1
fi
}
2010-12-10 01:01:21 +00:00
load_cfg_xrandr() {
# sed 1: Prefix arguments with "--"
# sed 2: Merge arguments into one line per output
# sed 3: Merge into two lines, all --off outputs in the first one
sed 's/^/--/' "$1" | sed -e '
:START
/\n--output/{P;D}
s/\n/ /
N;bSTART' | sed -e '
### First line
/ --off/{
G
# Merge if next line contains --off
s/\n\([^\n]* --off\)/ \1/
h
$!d;b
}
### Last line
H;x
# Merge if previous line contains --mode
s/\(--mode [^\n]*\)\n/\1 /
h
$!d' | xargs -L 1 $XRANDR
2010-12-10 01:01:21 +00:00
}
load_cfg_disper() {
$DISPER -i < "$1"
2010-12-10 01:01:21 +00:00
}
2010-10-26 14:33:34 +00:00
load() {
2010-11-08 20:48:25 +00:00
local PROFILE="$1"
2010-12-10 01:01:21 +00:00
local CONF="$PROFILES/$PROFILE/config"
if [ -e "$CONF" ] ; then
2013-12-30 21:18:32 +00:00
[ -x "$PROFILES/preswitch" ] && \
"$PROFILES/preswitch" "$PROFILE"
[ -x "$PROFILES/$PROFILE/preswitch" ] && \
"$PROFILES/$PROFILE/preswitch" "$PROFILE"
2010-11-08 20:48:25 +00:00
echo " -> loading profile $PROFILE"
2010-12-10 01:01:21 +00:00
$LOAD_METHOD "$CONF"
2010-11-08 20:48:25 +00:00
[ -x "$PROFILES/$PROFILE/postswitch" ] && \
"$PROFILES/$PROFILE/postswitch" "$PROFILE"
[ -x "$PROFILES/postswitch" ] && \
"$PROFILES/postswitch" "$PROFILE"
fi
2010-10-26 14:33:34 +00:00
}
2010-11-08 21:15:44 +00:00
help() {
cat <<EOH
2010-12-10 01:01:21 +00:00
Usage: $SCRIPTNAME [options]
2010-11-08 21:15:44 +00:00
-h, --help get this small help
-c, --change reload current setup
-s, --save <profile> save your current setup to profile <profile>
-l, --load <profile> load profile <profile>
-d, --default <profile> make profile <profile> the default profile
2010-11-09 22:31:06 +00:00
--force force (re)loading of a profile
--fingerprint fingerprint your current hardware setup
2013-01-06 17:38:35 +00:00
--config dump your current xrandr setup
2010-11-08 21:15:44 +00:00
To prevent a profile from being loaded, place a script call "block" in its
directory. The script is evaluated before the screen setup is inspected, and
in case of it returning a value of 0 the profile is skipped. This can be used
to query the status of a docking station you are about to leave.
If no suitable profile can be identified, the current configuration is kept.
To change this behaviour and switch to a fallback configuration, specify
2010-11-09 22:31:06 +00:00
--default <profile>.
2010-11-08 21:15:44 +00:00
Another script called "postswitch "can be placed in the directory
2010-12-10 01:01:21 +00:00
~/.autorandr as well as in any profile directories: The scripts are executed
2010-11-09 22:31:06 +00:00
after a mode switch has taken place and can notify window managers.
2010-11-08 21:15:44 +00:00
2010-12-10 01:01:21 +00:00
When called by the name "autodisper" or "auto-disper", the script uses "disper"
instead of "xrandr" to detect, configure and save the display configuration.
2010-11-08 21:15:44 +00:00
EOH
exit
}
2010-10-26 14:33:34 +00:00
# process parameters
2013-01-06 17:38:35 +00:00
OPTS=$(getopt -n autorandr -o s:l:d:cfh --long change,default:,save:,load:,force,fingerprint,config,help -- "$@")
2010-10-26 14:33:34 +00:00
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while true; do
2010-11-08 20:48:25 +00:00
case "$1" in
-c|--change) CHANGE_PROFILE=1; shift ;;
-d|--default) DEFAULT_PROFILE="$2"; shift 2 ;;
-s|--save) SAVE_PROFILE="$2"; shift 2 ;;
-l|--load) LOAD_PROFILE="$2"; shift 2 ;;
-h|--help) help ;;
--force) FORCE_LOAD=1; shift ;;
2010-11-08 20:48:25 +00:00
--fingerprint) setup_fp; exit 0;;
2013-01-06 17:38:35 +00:00
--config) current_cfg; exit 0;;
2010-11-08 20:48:25 +00:00
--) shift; break ;;
*) echo "Error: $1"; exit 1;;
esac
2010-10-26 14:33:34 +00:00
done
2010-11-08 20:46:03 +00:00
CURRENT_SETUP="$(setup_fp)"
2010-10-26 14:33:34 +00:00
if [ -n "$SAVE_PROFILE" ]; then
2010-11-08 20:48:25 +00:00
echo "Saving current configuration as profile '${SAVE_PROFILE}'"
mkdir -p "$PROFILES/$SAVE_PROFILE"
echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup"
$CURRENT_CFG_METHOD > "$PROFILES/$SAVE_PROFILE/config"
2010-11-08 20:48:25 +00:00
exit 0
2010-10-26 14:33:34 +00:00
fi
if [ -n "$LOAD_PROFILE" ]; then
CHANGE_PROFILE=1 FORCE_LOAD=1 load "$LOAD_PROFILE"
2010-11-08 20:48:25 +00:00
exit $?
2010-10-26 14:33:34 +00:00
fi
for SETUP_FILE in $PROFILES/*/setup; do
2010-11-08 20:48:25 +00:00
if ! [ -e $SETUP_FILE ]; then
break
fi
PROFILE="$(basename $(dirname "$SETUP_FILE"))"
echo -n "$PROFILE"
if blocked "$PROFILE"; then
echo " (blocked)"
continue
fi
FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")"
if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then
echo " (detected)"
if [ "$CHANGE_PROFILE" -eq 1 ]; then
if [ "$FORCE_LOAD" -eq 1 ] || ! config_equal "$PROFILE"; then
load "$PROFILE"
fi
fi
2010-11-08 20:48:25 +00:00
# found the profile, exit with success
exit 0
else
echo ""
fi
2010-10-26 14:33:34 +00:00
done
# we did not find the profile, load default
if [ -n "$DEFAULT_PROFILE" ]; then
2010-11-08 20:48:25 +00:00
echo "No suitable profile detected, falling back to $DEFAULT_PROFILE"
load "$DEFAULT_PROFILE"
2010-10-26 14:33:34 +00:00
fi
exit 1