# a micro library of helper functions for the power scripts
umask 022
PATH=$PATH:/usr/bin/X11
POWERSTATE=/var/lib/acpi-support/powerstate
# getXuser: get the user for the specified X display, or if none is
# specified, then the user for whatever X display we find.
# input: $displaynum: the X display to query
# output: $XAUTHORITY: the path to the xauth data used for connecting to the
# detected X display.
# $XUSER: the username of the user who owns the display.
getXuser() {
local plist display uid user startx pid userhome IFS
if [ "$displaynum" ]; then
display=":$displaynum"
else
display=.+
fi
user=
if [ -x /usr/bin/ck-list-sessions ]; then
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);}')
if [ "$uid" ]; then
IFS=:
set -- $(getent passwd $uid)
user=$1
unset IFS
fi
fi
if [ -z "$user" ]; then
plist=$(pinky -fw) || pwf_error "pinky lost"
while read l; do
set -- $l
eval lastpp=\$$#
for ds in $2 $lastpp; do
case $ds in
$display)
user=$1
break
;;
esac
done
[ -z "$user" ] || break
for ds in $2 $lastpp; do
case $ds in
$display.0)
user=$1
break
;;
esac
done
[ -z "$user" ] || break
done <<-EOF
$plist
EOF
fi
if [ -z "$user" ]; then
startx=$(pgrep -n startx || :)
[ -z "$startx" ] || user=$(ps -o user --no-headers $startx || :)
fi
if [ x"$user" != x ]; then
for pid in `ps -U "$user" -o pid=`; do
if grep -z -q XAUTHORITY /proc/$pid/environ; then
XAUTHORITY="$(grep -z XAUTHORITY /proc/$pid/environ | cut -d= -f2-)"
export XAUTHORITY
break
fi
done
if [ -z "$XAUTHORITY" ]; then
userhome="$(getent passwd "$user" | cut -d: -f6)"
XAUTHORITY="$userhome/.Xauthority"
export XAUTHORITY
fi
else
export XAUTHORITY=
fi
XUSER="$user"
export XUSER
}
# getXconsole: get the information for the active X console, if any.
# calls getXuser to get information related to the logged-in user.
# input: none
# output: $XAUTHORITY: the path to the xauth data used for connecting to the
# detected X display.
# $DISPLAY: the X display
# $XUSER: the username of the user who owns the display.
getXconsole() {
local displaynum
if [ -x /usr/bin/ck-list-sessions ]; then
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); }')
fi
if [ -z "$displaynum" ]; then
console=`fgconsole`;
displaynum=`ps t tty$console | sed -n -re 's,.*/Xorg .*:([0-9]+).*,\1,p'`
fi
if [ "$displaynum" ]; then
DISPLAY=":$displaynum"
export DISPLAY
getXuser
fi
}
# getState side effect: sets global variable STATE
getState() {
/usr/bin/on_ac_power
if [ $? -eq 1 ]; then
STATE=BATTERY
else
STATE=AC
fi
}
#check our state has actually changed
checkStateChanged() {
local OLDSTATE
# do we have our state stashed?
# XXX: if vaiable STATE is unset, set it to the null string
: ${STATE=${STATE:-}}
# XXX: make sure variable STATE has a non-null value
[ "$STATE" ] || getState
if [ -f "$POWERSTATE" ]; then
# XXX: unset or empty variable STATE and an existing but empty POWERSTATE file
# and the function will probably have undesired side effects
read OLDSTATE <$POWERSTATE || :
if [ "$STATE" = "$OLDSTATE" ]; then
exit 0
else
#stash the new state
# XXX: unset or empty variable STATE, will create an empty POWERSTATE file
echo "$STATE" > $POWERSTATE
fi
else
#we need to stash the new state
# XXX: unset or empty variable STATE, will create an empty POWERSTATE file
echo "$STATE" > $POWERSTATE
fi
}
HDPARM='/sbin/hdparm -q'
LIDSTATE=/var/lib/acpi-support/lidstate