Skip to content

Instantly share code, notes, and snippets.

@jeffhung
Last active September 5, 2018 01:09
Show Gist options
  • Save jeffhung/0a2506a77ee37c7e5a03b2bec5627098 to your computer and use it in GitHub Desktop.
Save jeffhung/0a2506a77ee37c7e5a03b2bec5627098 to your computer and use it in GitHub Desktop.
xapp.sh

xapp.sh

Select X display in dialog to pick where to render your X application.

You can add these lines to your ~/.login file to switch to this behavior:

if ( $?DISPLAY  ) then
	xapp.sh --register "$DISPLAY";
	setenv DISPLAY_ORI	"$DISPLAY"
	unsetenv DISPLAY;
endif
#!/bin/sh -f
# ---------------------------------------------------------------------------
# Copyright (c) 2007, Jeff Hung
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------
# $Date: 2006-09-12 12:37:52 +0800 (星期二, 12 九月 2006) $
# $Rev: 3 $
# $Author: jeffhung $
# ----------------------------------------------------------------------------
__exe_name__=`basename $0`;
__revision__=`echo '$Rev: 506 $' | cut -d' ' -f2`;
__rev_date__=`echo '$Date: 2007-06-26 11:10:35 +0800 (星期二, 26 六月 2007) $' | cut -d' ' -f2`;
XDISPLAYS="$HOME/.xdisplays";
xapp_usage()
{
echo >&2 "\
Usage: $__exe_name__ [ <option> ... ] [ <cmd> ... ]
Run <cmd> or initiate X session in one of registered X display. If multiple
X displays registered, will raise a dialog(1) for user to choose. List of
registered X displays will be saved at ~/.xdisplays.
Options:
-r,--register <disp> Register display <disp>.
-u,--unregister <disp> Unregister display <disp>.
-h,--help Show this help message.
Revision: r$__revision__ ($__rev_date__)";
}
xapp_msg_exit()
{
local ex=$1; shift;
echo >&2 "\
Usage: $__exe_name__ [ <option> ... ] [ <cmd> ... ]
Type '$__exe_name__ --help' for help messages.
";
while [ $# -gt 0 ]; do
echo >&2 "ERROR: $1"; shift;
done;
exit $ex;
}
xapp_save_displays()
{
rm -f "$XDISPLAYS";
xdisplays="`set | grep ^XDISPLAY | grep -v ^XDISPLAYS | sed -e 's/=.*$//' | xargs echo`";
local xd;
for xd in $xdisplays; do
eval xdv="$"$xd;
# echo "$xd='$xdv';";
echo "$xd='$xdv';" \
>> "$XDISPLAYS" \
;
done;
}
xapp_register_display()
{
local disp="$1"; shift;
local remotehost="$1"; shift;
local timestamp="`date "+%Y-%m-%d %H:%M:%S"`";
if [ -r "$XDISPLAYS" ]; then
. "$XDISPLAYS"; # load ~/.xdisplays
fi;
local xdisplays="`set | grep ^XDISPLAY | grep -v ^XDISPLAYS | sed -e 's/=.*$//' | xargs echo`";
local i=1;
while true; do
local xdisplay_i="XDISPLAY$i";
echo "$xdisplays" | grep "$xdisplay_i" > /dev/null;
if [ $? -ne 0 ]; then
eval "XDISPLAY$i"="\"[$disp] @ $remotehost ($timestamp)\";";
break; # while
fi;
i=`expr $i + 1`;
done;
xapp_save_displays;
}
xapp_unregister_display()
{
local disp="$1"; shift;
if [ ! -r "$XDISPLAYS" ]; then
xapp_msg_exit 2 'No display registerred.';
fi;
. "$XDISPLAYS"; # load ~/.xdisplays
local i=1;
xdisplays="`set | grep ^XDISPLAY | grep -v ^XDISPLAYS | sed -e 's/=.*$//' | xargs echo`";
local xd;
local xdisp;
for xd in $xdisplays; do
echo "xd: $xd";
eval xdv="$"$xd;
xdisp=`echo "$xdv" | grep '\[.*\]' | sed -e 's/^.*\[//' -e 's/\].*\$//'`;
echo "xdisp: [$xdisp]";
echo "disp: [$disp]";
if [ "x$xdisp" = "x$disp" ]; then
unset `echo "$xd"`;
fi;
done;
xapp_save_displays;
}
xapp_select_display()
{
local result_file="$1"; shift;
if [ ! -r "$XDISPLAYS" ]; then
xapp_msg_exit 2 'No display registerred.';
fi;
. "$XDISPLAYS"; # load ~/.xdisplays
xdisplays="`set | grep ^XDISPLAY | grep -v ^XDISPLAYS | sed -e 's/=.*$//' | xargs echo`";
local dialog_menu_items="dialog --clear --title 'X Display Selector' --menu 'Select X display to run X application:' 24 76 16";
# local dialog_menu_items='';
local xd;
for xd in $xdisplays; do
eval xdv="$"$xd;
dialog_menu_items="$dialog_menu_items $xd '$xdv'";
done;
dialog_menu_items="$dialog_menu_items XCONSOLE 'X Server in console'";
# echo "$dialog_menu_items";
# exit;
local dialog_result=`mktemp /tmp/xapp.XXXXXX`;
eval $dialog_menu_items 2> "$dialog_result";
# dialog --clear \
# --title 'X Display Selector' \
# --menu \
# 'Select X display to run X application:' \
# 24 76 16 \
# ${dialog_menu_items} \
# 2> "$dialog_result" \
# ;
#exit;
local dialog_status="$?";
case "$dialog_status" in
0)
cat "$dialog_result" \
| tr -d '\n' \
| sed -e 's/^\[//' -e 's/\].*$//' \
> "$result_file" \
;
;;
1|255) # Cancel|ESC pressed
xapp_msg_exit 0 'Action cancelled.';
;;
2) # HELP '$choice' chosen
xapp_msg_exit 0;
;;
*)
xapp_msg_exit 2 "Unexpected dialog exit code: $dialog_status";
;;
esac;
rm -f "$dialog_result";
}
xapp_run_x_app()
{
local disp="$1"; shift;
env DISPLAY=$xdisp $@;
}
xapp_run_cmd()
{
local xsel_result=`mktemp /tmp/xapp.XXXXXX`;
xapp_select_display "$xsel_result";
local disp=`cat "$xsel_result"`;
# echo "XDISPLAY: $disp";
rm -f "$xsel_result";
local xdisp;
if [ "x$disp" = 'xXCONSOLE' ]; then
xdisp=':0.0';
else
eval xdisp="$""$disp";
xdisp=`echo "$xdisp" | sed -e 's/^.*\[//' -e 's/\].*$//'`;
fi;
# echo "xdisp: $xdisp";
unset WINDOW; # clear WINDOW env var that comes from screen.
xapp_run_x_app "$xdisp" $@ &
# env DISPLAY=$xdisp $@;
}
xapp_run_x_session()
{
local disp="$1"; shift;
cd ~;
env DISPLAY=$xdisp $HOME/.xinitrc;
# clear x
env DISPLAY=$xdisp xsetroot -def;
}
xapp_init_x_session()
{
local xsel_result=`mktemp /tmp/xapp.XXXXXX`;
xapp_select_display "$xsel_result";
local disp=`cat "$xsel_result"`;
# echo "XDISPLAY: $disp";
rm -f "$xsel_result";
local xdisp;
if [ "x$disp" = 'xXCONSOLE' ]; then
xdisp=':0.0';
else
eval xdisp="$""$disp";
xdisp=`echo "$xdisp" | sed -e 's/^.*\[//' -e 's/\].*$//'`;
fi;
# echo "xdisp: $xdisp";
unset WINDOW; # clear WINDOW env var that comes from screen.
xapp_run_x_session "$xdisp" &
}
xapp_main()
{
local arg;
local disp;
local cmd;
while [ $# -gt 0 ]; do
arg="$1"; shift;
case "$arg" in
-h|--help)
xapp_usage;
exit;
;;
-r|--register)
if [ $# -gt 0 ]; then
disp="$1"; shift;
xapp_register_display "$disp" "$REMOTEHOST";
exit;
else
xapp_msg_exit 1 'Missing <disp>.';
fi;
;;
-u|--unregister)
if [ $# -gt 0 ]; then
disp="$1"; shift;
xapp_unregister_display "$disp";
exit;
else
xapp_msg_exit 1 'Missing <disp>.';
fi;
;;
-*)
xapp_msg_exit 1 "Unknown option: $arg";
;;
*)
cmd="$arg";
xapp_run_cmd "$cmd" $@;
exit $?;
;;
esac;
done;
xapp_init_x_session; # since not exited
}
xapp_main $@;
exit $?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment