3 # 40autorandr: Change autorandr profile on thaw/resume
4 exec > /var/log/autorandr.log 2>&1
6 AUTORANDR="autorandr -c --default default"
8 # Work around #44: Long user names in w
9 export PROCPS_USERLEN=32
12 # Determine user owning the display session from $1
15 # Prefer loginctl over all others, see bug #49
16 if [ -x "`which loginctl`" ]; then
17 # Based on http://unix.stackexchange.com/questions/203844/how-to-find-out-the-current-active-xserver-display-number/204498
20 loginctl list-sessions --no-legend | while read id uid user seat; do
21 loginctl show-session -p Display -p Active "$id" | while IFS='=' read property value; do
24 if [ "$value" != "yes" ]; then
29 if [ -n "$value" -a "$value" = "$D" ]; then
38 if [ -n "$user" ]; then
44 # Prefer w to who, see bug #39
45 if [ -x "`which w`" ]; then
46 user="`w -h | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" || $3 ~ ":"D"(.[0-9])?$" {print $1}' | head -n1`"
48 if [ -z "$user" ]; then
49 # This fallback checks if there is exactly one user (except
50 # root) logged in an interactive session and assumes the
51 # session belongs to him. See bug #39.
52 user="`w -hu | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
55 user="`who --all | awk -vD="$D" '$3 ~ ":"D"(.[0-9])?$" {print $1}' | head -1`"
57 if [ -z "$user" ]; then
58 # Same fallback as above; see bug #39.
59 user="`who -u | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
64 if [ -n "$user" ]; then
69 # If none of the above worked, check if there is only one user owning
70 # processes in $DISPLAY except root
72 # This code should be optimized somehow, but keep in mind that not all
73 # systems symlink sh -> bash!
77 d="$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' $p/environ 2>/dev/null)"
78 if [ "$d" = "$D" ]; then
81 done | sort | uniq | grep -v root | nl | head -n1
83 count=$(echo $OUTPUT | awk '{print $1}')
84 user=$(echo $OUTPUT | awk '{print $2}')
86 if [ "$count" -eq 1 ]; then
96 for X in /tmp/.X11-unix/X*; do
97 D="${X##/tmp/.X11-unix/X}"
99 user="$(find_user "$D")"
101 if [ x"$user" != x"" ]; then
102 logger "autorandr: Changing display configuration for display :$D, user '$user'"
104 /bin/su -c "${AUTORANDR}" "$user"
109 if grep -q systemd /proc/1/comm && [ "$2" = "udev" ]; then
110 exec /bin/systemctl start autorandr-resume.service