]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - contrib/pm-utils/40autorandr
Fixes in 40autorandr; thanks to @rnav for spotting the issues
[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=32
10
11 find_user() {
12         # Determine user owning the display session from $1
13         D="$1"
14
15         # Prefer loginctl over all others, see bug #39
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
18                 # by SO user intelfx
19                 user="$(
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
22                                 case "$property" in
23                                         Active)
24                                                 if [ "$value" != "yes" ]; then
25                                                         continue
26                                                 fi
27                                                 ;;
28                                         Display)
29                                                 if [ -n "$value" -a "$value" = "$D" ]; then
30                                                         echo $user
31                                                 fi
32                                                 ;;
33                                 esac
34                         done
35                 done
36                 )"
37                 if [ -n "$user" ]; then
38                         echo $user
39                         return 0
40                 fi
41         fi
42
43         # Prefer w to who, see bug #39
44         if [ -x "`which w`" ]; then
45                 user="`w -h | awk -vD="$D" '$2 ~ ":"D"(.[0-9])?$" || $3 ~ ":"D"(.[0-9])?$" {print $1}' | head -n1`"
46
47                 if [ -z "$user" ];      then
48                         # This fallback checks if there is exactly one user (except
49                         # root) logged in an interactive session and assumes the
50                         # session belongs to him. See bug #39.
51                         user="`w -hu | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
52                 fi
53         else
54                 user="`who --all | awk -vD="$D" '$3 ~ ":"D"(.[0-9])?$" {print $1}' | head -1`"
55
56                 if [ -z "$user" ];      then
57                         # Same fallback as above; see bug #39.
58                         user="`who -u | awk '/^\w+/ && $1 !~ "root" { users[$1]=$1; } ENDFILE { if(asort(users) == 1) for(u in users) print users[u]; }'`"
59                 fi
60         fi
61
62         if [ -n "$user" ]; then
63                 echo $user
64                 return 0
65         fi
66
67         # If none of the above worked, check if there is only one user owning
68         # processes in $DISPLAY except root
69         #
70         # This code should be optimized somehow, but keep in mind that not all
71         # systems symlink sh -> bash!
72         OUTPUT="$(
73                 for p in /proc/*; do
74                         [ -d $p ] || continue
75                         d="$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' $p/environ 2>/dev/null)"
76                         if [ "$d" = "$D" ]; then
77                                 stat -c %U $p
78                         fi
79                 done | sort | uniq | grep -v root | nl | head -n1
80         )"
81         count=$(echo $OUTPUT | awk '{print $1}')
82         user=$(echo $OUTPUT | awk '{print $2}')
83
84         if [ "$count" -eq 1 ]; then
85                 echo $user
86                 return 0
87         fi
88
89         return 1
90 }
91
92 detect_display()
93 {
94         for X in /tmp/.X11-unix/X*; do
95                 D="${X##/tmp/.X11-unix/X}"
96
97                 user="$(find_user ":$D")"
98
99                 if [ x"$user" != x"" ]; then
100                         logger "autorandr: Changing display configuration for display :$D, user '$user'"
101                         export DISPLAY=":$D"
102                         /bin/su -c "${AUTORANDR}" "$user"
103                 fi
104         done
105 }
106
107 if grep -q systemd /proc/1/comm && [ "$2" = "udev" ]; then
108     exec /bin/systemctl start autorandr-resume.service
109 fi
110
111 case "$1" in
112         thaw|resume)
113                 detect_display
114                 ;;
115 esac