]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blobdiff - contrib/pm-utils/40autorandr
Add ability to remove profile
[deb_pkgs/autorandr.git] / contrib / pm-utils / 40autorandr
index 264ba5f671b15baf06e8059cbfe5bb216cfbc4ae..f44739a609e2aa032ed868c584a773d48851c234 100755 (executable)
@@ -5,12 +5,11 @@ exec > /var/log/autorandr.log 2>&1
 
 AUTORANDR="autorandr -c --default default"
 
+# Work around #44: Long user names in w
+export PROCPS_USERLEN=32
+
 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}"
                # Prefer w to who, see bug #39
@@ -18,23 +17,34 @@ detect_display()
                        user="`w -h | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" || $3 ~ ":"D"(.[0-9])?$" {print $1}' | head -n1`"
 
                        if [ -z "$user" ];      then
-                               # Try xinit as a fallback, see bug #39
-                               user="`w -h | awk -vD="$D" '$7 ~ "xinit" && $0 ~ ":"D"(.[0-9])?" {print $1}' | head -n1`"
+                               # 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
        done
 }
 
+if grep -q systemd $(readlink -f $(awk '{print $1}' /proc/1/cmdline)) && [ "$2" = "udev" ]; then
+    exec /bin/systemctl start autorandr-resume.service
+fi
+
 case "$1" in
        thaw|resume)
-               detect_display &
+               detect_display
                ;;
 esac