| 1 |
# a micro library of helper functions for the power scripts |
| 2 |
|
| 3 |
umask 022 |
| 4 |
|
| 5 |
PATH=$PATH:/usr/bin/X11 |
| 6 |
POWERSTATE=/var/lib/acpi-support/powerstate |
| 7 |
|
| 8 |
# getXuser: get the user for the specified X display, or if none is |
| 9 |
# specified, then the user for whatever X display we find. |
| 10 |
# input: $displaynum: the X display to query |
| 11 |
# output: $XAUTHORITY: the path to the xauth data used for connecting to the |
| 12 |
# detected X display. |
| 13 |
# $XUSER: the username of the user who owns the display. |
| 14 |
getXuser() { |
| 15 |
local plist display uid user startx pid userhome IFS |
| 16 |
|
| 17 |
if [ "$displaynum" ]; then |
| 18 |
display=":$displaynum" |
| 19 |
else |
| 20 |
display=.+ |
| 21 |
fi |
| 22 |
|
| 23 |
user= |
| 24 |
if [ -x /usr/bin/ck-list-sessions ]; then |
| 25 |
uid=$(ck-list-sessions 2>/dev/null| awk 'BEGIN { unix_user = ""; } /^Session/ { unix_user = ""; } /unix-user =/ { gsub(/'\''/,"",$3); unix_user = $3; } /x11-display = '\'$display\''/ { print unix_user; exit (0); } /x11-display = '\'$display.0\''/ { print unix_user; exit (0);}') |
| 26 |
|
| 27 |
if [ "$uid" ]; then |
| 28 |
IFS=: |
| 29 |
set -- $(getent passwd $uid) |
| 30 |
user=$1 |
| 31 |
unset IFS |
| 32 |
fi |
| 33 |
fi |
| 34 |
|
| 35 |
if [ -z "$user" ]; then |
| 36 |
plist=$(pinky -fw) || pwf_error "pinky lost" |
| 37 |
while read l; do |
| 38 |
set -- $l |
| 39 |
eval lastpp=\$$# |
| 40 |
for ds in $2 $lastpp; do |
| 41 |
case $ds in |
| 42 |
$display) |
| 43 |
user=$1 |
| 44 |
break |
| 45 |
;; |
| 46 |
esac |
| 47 |
done |
| 48 |
[ -z "$user" ] || break |
| 49 |
for ds in $2 $lastpp; do |
| 50 |
case $ds in |
| 51 |
$display.0) |
| 52 |
user=$1 |
| 53 |
break |
| 54 |
;; |
| 55 |
esac |
| 56 |
done |
| 57 |
[ -z "$user" ] || break |
| 58 |
done <<-EOF |
| 59 |
$plist |
| 60 |
EOF |
| 61 |
fi |
| 62 |
|
| 63 |
if [ -z "$user" ]; then |
| 64 |
startx=$(pgrep -n startx || :) |
| 65 |
[ -z "$startx" ] || user=$(ps -o user --no-headers $startx || :) |
| 66 |
fi |
| 67 |
|
| 68 |
if [ x"$user" != x ]; then |
| 69 |
for pid in `ps -U "$user" -o pid=`; do |
| 70 |
if grep -z -q XAUTHORITY /proc/$pid/environ; then |
| 71 |
XAUTHORITY="$(grep -z XAUTHORITY /proc/$pid/environ | cut -d= -f2-)" |
| 72 |
export XAUTHORITY |
| 73 |
break |
| 74 |
fi |
| 75 |
done |
| 76 |
if [ -z "$XAUTHORITY" ]; then |
| 77 |
userhome="$(getent passwd "$user" | cut -d: -f6)" |
| 78 |
XAUTHORITY="$userhome/.Xauthority" |
| 79 |
export XAUTHORITY |
| 80 |
fi |
| 81 |
else |
| 82 |
export XAUTHORITY= |
| 83 |
fi |
| 84 |
XUSER="$user" |
| 85 |
export XUSER |
| 86 |
} |
| 87 |
|
| 88 |
# getXconsole: get the information for the active X console, if any. |
| 89 |
# calls getXuser to get information related to the logged-in user. |
| 90 |
# input: none |
| 91 |
# output: $XAUTHORITY: the path to the xauth data used for connecting to the |
| 92 |
# detected X display. |
| 93 |
# $DISPLAY: the X display |
| 94 |
# $XUSER: the username of the user who owns the display. |
| 95 |
getXconsole() { |
| 96 |
local displaynum |
| 97 |
|
| 98 |
if [ -x /usr/bin/ck-list-sessions ]; then |
| 99 |
displaynum=$(ck-list-sessions 2>/dev/null| awk 'BEGIN { active = 0; } /^Session/ { active = 0; } /active = TRUE/ { active = 1; } active && /x11-display = '\':.+\''/ { gsub(/'\':*'/,"",$3); print $3; exit (0); }') |
| 100 |
fi |
| 101 |
|
| 102 |
if [ -z "$displaynum" ]; then |
| 103 |
console=`fgconsole`; |
| 104 |
displaynum=`ps t tty$console | sed -n -re 's,.*/Xorg .*:([0-9]+).*,\1,p'` |
| 105 |
fi |
| 106 |
|
| 107 |
if [ "$displaynum" ]; then |
| 108 |
DISPLAY=":$displaynum" |
| 109 |
export DISPLAY |
| 110 |
getXuser |
| 111 |
fi |
| 112 |
} |
| 113 |
|
| 114 |
# getState side effect: sets global variable STATE |
| 115 |
getState() { |
| 116 |
/usr/bin/on_ac_power |
| 117 |
if [ $? -eq 1 ]; then |
| 118 |
STATE=BATTERY |
| 119 |
else |
| 120 |
STATE=AC |
| 121 |
fi |
| 122 |
} |
| 123 |
|
| 124 |
#check our state has actually changed |
| 125 |
checkStateChanged() { |
| 126 |
local OLDSTATE |
| 127 |
|
| 128 |
# do we have our state stashed? |
| 129 |
# XXX: if vaiable STATE is unset, set it to the null string |
| 130 |
: ${STATE=${STATE:-}} |
| 131 |
# XXX: make sure variable STATE has a non-null value |
| 132 |
[ "$STATE" ] || getState |
| 133 |
if [ -f "$POWERSTATE" ]; then |
| 134 |
# XXX: unset or empty variable STATE and an existing but empty POWERSTATE file |
| 135 |
# and the function will probably have undesired side effects |
| 136 |
read OLDSTATE <$POWERSTATE || : |
| 137 |
if [ "$STATE" = "$OLDSTATE" ]; then |
| 138 |
exit 0 |
| 139 |
else |
| 140 |
#stash the new state |
| 141 |
# XXX: unset or empty variable STATE, will create an empty POWERSTATE file |
| 142 |
echo "$STATE" > $POWERSTATE |
| 143 |
fi |
| 144 |
else |
| 145 |
#we need to stash the new state |
| 146 |
# XXX: unset or empty variable STATE, will create an empty POWERSTATE file |
| 147 |
echo "$STATE" > $POWERSTATE |
| 148 |
fi |
| 149 |
} |
| 150 |
|
| 151 |
HDPARM='/sbin/hdparm -q' |
| 152 |
|
| 153 |
LIDSTATE=/var/lib/acpi-support/lidstate |