Moving X scripts to dedicated dir
[shutils.git] / X / getXenv.inc
diff --git a/X/getXenv.inc b/X/getXenv.inc
new file mode 100644 (file)
index 0000000..4b1c7e3
--- /dev/null
@@ -0,0 +1,44 @@
+# Extracted from /etc/acpi/powerbtn.sh
+
+
+# Taken from Debian's 2.0.4-1 diff file.  This version handles KDE4.
+# Power Button event handler.
+# Checks to see if gnome or KDE are already handling the power button.
+# If not, initiates a plain shutdown.
+
+# getXuser gets the X user belonging to the display in $displaynum.
+# If you want the foreground X user, use getXconsole!
+# Input:
+#   displaynum - X display number
+# Output: 
+#   XUSER - the name of the user
+#   XAUTHORITY - full pathname of the user's .Xauthority file
+getXuser() {
+        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
+        if [ x"$user" = x"" ]; then
+                startx=`pgrep -n startx`
+                if [ x"$startx" != x"" ]; then
+                        user=`ps -o user --no-headers $startx`
+                fi
+        fi
+        if [ x"$user" != x"" ]; then
+                userhome=`getent passwd $user | cut -d: -f6`
+                export XAUTHORITY=$userhome/.Xauthority
+        else
+                export XAUTHORITY=""
+        fi
+        export XUSER=$user
+}
+
+# Gets the X display number for the active virtual terminal.
+# Output:
+#   DISPLAY - the X display number
+#   See getXuser()'s output.
+getXconsole() {
+        console=`fgconsole`;
+        displaynum=`ps t tty$console | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
+        if [ x"$displaynum" != x"" ]; then
+                export DISPLAY=":$displaynum"
+                getXuser
+        fi
+}