getXenv.inc: Do not work on current console
[shutils.git] / X / getXenv.inc
1 # Extracted from /etc/acpi/powerbtn.sh and modified
2 # shuber@sthu.org
3
4
5 # Taken from Debian's 2.0.4-1 diff file. This version handles KDE4.
6 # Power Button event handler.
7 # Checks to see if gnome or KDE are already handling the power button.
8 # If not, initiates a plain shutdown.
9
10 # getXuser gets the X user belonging to the display in $displaynum.
11 # If you want the foreground X user, use getXconsole!
12 # Input:
13 # displaynum - X display number
14 # Output:
15 # XUSER - the name of the user
16 # XAUTHORITY - full pathname of the user's .Xauthority file
17 getXuser() {
18 user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
19 if [ x"$user" = x"" ]; then
20 startx=`pgrep -n startx`
21 if [ x"$startx" != x"" ]; then
22 user=`ps -o user --no-headers $startx`
23 fi
24 fi
25 if [ x"$user" != x"" ]; then
26 userhome=`getent passwd $user | cut -d: -f6`
27 export XAUTHORITY=$userhome/.Xauthority
28 else
29 export XAUTHORITY=""
30 fi
31 export XUSER=$user
32 }
33
34 # Gets the X display number for the active virtual terminal.
35 # Output:
36 # DISPLAY - the X display number
37 # See getXuser()'s output.
38 getXconsole() {
39 pid=$(pgrep -x X | head -n 1)
40 if [ x"$pid" != x"" ]; then
41 displaynum=`ps -p "$pid" -o args | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
42 if [ x"$displaynum" != x"" ]; then
43 export DISPLAY=":$displaynum"
44 getXuser
45 fi
46 fi
47 }