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