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