wip tool: maskfiles
[hband-tools.git] / xwin-tools / xdpy-attach
blob1ceec35697d32f282aa958fc639b4719eccfaed6
1 #!/bin/bash
3 # source up this script to setup new DISPLAY
4 # environent in the current session
6 _xdpy_attach_find_display()
8 declare -A confirm_xdpy_pid
10 local signal
11 for signal in INT QUIT TERM
13 trap "kill -SIG$signal -- \${!confirm_xdpy_pid[@]} 2>/dev/null" $signal
14 done
15 trap "kill -SIGINT -- \${!confirm_xdpy_pid[@]} 2>/dev/null" EXIT
17 for dpy in `xdpys`
19 echo "Found Display - $dpy" >&2
20 DISPLAY=$dpy zenity --question --text="Is this the right Display?\n$dpy" </dev/null >/dev/null 2>&1 &
21 confirm_xdpy_pid[$!]=$dpy
22 done
23 unset dpy
25 local check_pid
26 echo "Waiting for user to response ..." >&2
28 while [ ${#confirm_xdpy_pid[@]} -gt 0 ]
30 for check_pid in "${!confirm_xdpy_pid[@]}"
32 if ! kill -0 "$check_pid" 2>/dev/null
33 then
34 local dpy=${confirm_xdpy_pid[$check_pid]}
35 unset confirm_xdpy_pid[$check_pid]
37 wait $check_pid
38 if [ $? = 0 ]
39 then
40 echo "$dpy"
41 break 2
44 done
45 # Ctrl-C is most likely got by sleep, if so, exit from the loop.
46 sleep 0.4 || break
47 done
48 unset check_pid
50 kill -SIGINT -- ${!confirm_xdpy_pid[@]} 2>/dev/null
51 unset confirm_xdpy_pid
54 _xdpy_attach_apply()
56 if _xdpy_attach_is_shell_source
57 then
58 export $1=$2
59 else
60 echo export $1=$2
64 _xdpy_attach_is_shell_source()
66 [ "$0" != "$BASH_SOURCE" ]
69 _xdpy_attach_screen_setenv()
71 if [ -n "$STY" ]
72 then
73 echo "Setting Gnu/Screen env $1" >&2
74 screen -S "$STY" -X setenv "$1" "$2"
79 echo "current DISPLAY=$DISPLAY" >&2
80 dpy=`_xdpy_attach_find_display`
81 if [ -n "$dpy" ]
82 then
83 echo "Setting DISPLAY to '$dpy'" >&2
84 _xdpy_attach_apply DISPLAY "$dpy"
86 _xdpy_attach_screen_setenv DISPLAY "$dpy"
88 dbus=`DISPLAY="$dpy" xprop -root DBUS_SESSION_BUS_ADDRESS | cut -s -f2- -d= | cut -f2 -d'"'`
89 dbus=${dbus// /}
90 if [ -n "$dbus" ]
91 then
92 echo "Setting DBUS_SESSION_BUS_ADDRESS to '$dbus'" >&2
93 _xdpy_attach_apply DBUS_SESSION_BUS_ADDRESS "$dbus"
95 _xdpy_attach_screen_setenv DBUS_SESSION_BUS_ADDRESS "$dbus"
98 if [ -n "$1" ]
99 then
100 env DISPLAY="$dpy" ${dbus:+DBUS_SESSION_BUS_ADDRESS="$dbus"} "$@"
103 unset dbus
105 unset dpy