--- acpi-support-0.140/lib/power-funcs
+++ acpi-support-0.140/lib/power-funcs
@@ -1,33 +1,83 @@
 # a micro library of helper functions for the power scripts
 
-umask 022;
+umask 022
 
-PATH="$PATH:/usr/bin/X11"
-POWERSTATE="/var/lib/acpi-support/powerstate"
+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.
-#         $user: the username of the user who owns the display.
+#         $XUSER: the username of the user who owns the display.
 getXuser() {
-	local display userhome uid
-	if [ -z "$displaynum" ]; then
+	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 | awk 'BEGIN { unix_user = ""; } /^Session/ { unix_user = ""; } /unix-user =/ { gsub(/'\''/,"",$3); unix_user = $3; } /x11-display = '\'$display\''/ { print unix_user; exit (0); }')
+
+		if [ "$uid" ]; then
+			IFS=:
+			set -- $(getent passwd $uid)
+			user=$1
+			unset IFS
+		fi
 	else
-		display=":$displaynum"
+		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
-	uid=$(ck-list-sessions | awk 'BEGIN { unix_user = ""; } /^Session/ { unix_user = ""; } /unix-user =/ { gsub(/'\''/,"",$3); unix_user = $3; } /x11-display = '\'$display\''/ { print unix_user; exit (0); }')
 
-	if [ -n "$uid" ]; then
-		user=$(getent passwd $uid | cut -d: -f1)
-        	userhome=$(getent passwd $user | cut -d: -f6)
-        	export XAUTHORITY=$userhome/.Xauthority
+	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
+				export `grep -z XAUTHORITY /proc/$pid/environ`
+				break
+			fi
+		done
+		if [ -z $XAUTHORITY ]; then
+			eval userhome=~$user
+			export XAUTHORITY=$userhome/.Xauthority
+		fi
 	else
-		user=
-		export XAUTHORITY=""
+		export XAUTHORITY=
 	fi
+	export XUSER=$user
 }
 
 # getXconsole: get the information for the active X console, if any.
@@ -36,45 +86,55 @@
 # output: $XAUTHORITY: the path to the xauth data used for connecting to the
 #                      detected X display.
 #         $DISPLAY: the X display
-#         $user: the username of the user who owns the display.
+#         $XUSER: the username of the user who owns the display.
 getXconsole() {
 	local displaynum
 
 	displaynum=$(ck-list-sessions | awk 'BEGIN { active = 0; } /^Session/ { active = 0; } /active = TRUE/ { active = 1; } active && /x11-display = '\':.+\''/ { gsub(/'\':*'/,"",$3); print $3; exit (0); }')
 
-	if [ -n "$displaynum" ]; then
-		export DISPLAY=":$displaynum"
+	if [ "$displaynum" ]; then
+		export DISPLAY=:$displaynum
 		getXuser
 	fi
 }
 
+# getState side effect: sets global variable STATE
 getState() {
         /usr/bin/on_ac_power
-        if [ "$?" -eq 1 ]; then
-                STATE="BATTERY"
+        if [ $? -eq 1 ]; then
+                STATE=BATTERY
         else
-                STATE="AC"
+                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
-                OLDSTATE=$(<$POWERSTATE) 
+# 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
 }
 
-LAPTOP_MODE='/usr/sbin/laptop_mode'
 HDPARM='/sbin/hdparm -q'
 
-LIDSTATE='/var/lib/acpi-support/lidstate'
+LIDSTATE=/var/lib/acpi-support/lidstate
