]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - contrib/pm-utils/40autorandr
Workaround to fix #39
[deb_pkgs/autorandr.git] / contrib / pm-utils / 40autorandr
1 #!/bin/sh
2 #
3 # 40autorandr: Change autorandr profile on thaw/resume
4 exec > /var/log/autorandr.log 2>&1
5
6 AUTORANDR="autorandr -c --default default"
7
8 detect_display()
9 {
10         # Wait for the system to recognize the changed state, see bug #30
11         # Note the ampersand below, this runs asynchroneously.
12         sleep 5
13
14         for X in /tmp/.X11-unix/X*; do
15                 D="${X##/tmp/.X11-unix/X}"
16                 # Prefer w to who, see bug #39
17                 if [ -x "`which w`" ]; then
18                         user="`w -h | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" || $3 ~ ":"D"(.[0-9])?$" {print $1}' | head -n1`"
19
20                         if [ -z "$user" ];      then
21                                 # This fallback checks if there is exactly one user (except
22                                 # root) logged in an interactive session and assumes the
23                                 # session belongs to him. See bug #39.
24                                 user="`w -hu | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
25                         fi
26                 else
27                         user="`who --all | awk -vD="$D" '$3 ~ ":"D"(.[0-9])?$" {print $1}' | head -1`"
28
29                         if [ -z "$user" ];      then
30                                 # Same fallback as above; see bug #39.
31                                 user="`who -u | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
32                         fi
33                 fi
34
35                 if [ x"$user" != x"" ]; then
36                         logger "autorandr: Changing display configuration for display :$D, user '$user'"
37                         export DISPLAY=":$D"
38                         /bin/su -c "${AUTORANDR}" "$user"
39                 fi
40         done
41 }
42
43 case "$1" in
44         thaw|resume)
45                 detect_display &
46                 ;;
47 esac