]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - contrib/pm-utils/40autorandr
Workaround to fix #39
[deb_pkgs/autorandr.git] / contrib / pm-utils / 40autorandr
index 11291778c6b32dc28a479b562a0a2cd13bfef525..8a66f3971f84d408e322e8ef0c692e0f892c8d43 100755 (executable)
@@ -7,11 +7,33 @@ AUTORANDR="autorandr -c --default default"
 
 detect_display()
 {
+       # Wait for the system to recognize the changed state, see bug #30
+       # Note the ampersand below, this runs asynchroneously.
+       sleep 5
+
        for X in /tmp/.X11-unix/X*; do
                D="${X##/tmp/.X11-unix/X}"
-               user=`who | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" {print $1}' | head -1`
+               # Prefer w to who, see bug #39
+               if [ -x "`which w`" ]; then
+                       user="`w -h | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" || $3 ~ ":"D"(.[0-9])?$" {print $1}' | head -n1`"
+
+                       if [ -z "$user" ];      then
+                               # This fallback checks if there is exactly one user (except
+                               # root) logged in an interactive session and assumes the
+                               # session belongs to him. See bug #39.
+                               user="`w -hu | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
+                       fi
+               else
+                       user="`who --all | awk -vD="$D" '$3 ~ ":"D"(.[0-9])?$" {print $1}' | head -1`"
+
+                       if [ -z "$user" ];      then
+                               # Same fallback as above; see bug #39.
+                               user="`who -u | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
+                       fi
+               fi
+
                if [ x"$user" != x"" ]; then
-                       logger "autorandr: Changing display configuration for user '$user'"
+                       logger "autorandr: Changing display configuration for display :$D, user '$user'"
                        export DISPLAY=":$D"
                        /bin/su -c "${AUTORANDR}" "$user"
                fi
@@ -20,6 +42,6 @@ detect_display()
 
 case "$1" in
        thaw|resume)
-               detect_display
+               detect_display &
                ;;
 esac