#!/bin/bash

#This script runs a nested X11 server. Xephyr has some bizarre input-related
#bugs with certain combinations of guests and hosts (e.g. AIX ignores many keys
#including all alphanumerics when run on the host, and VAX OSes ignore all
#mouse clicks on window decorations when run on the VM under QEMU but not under
#VirtualBox), so this script uses TigerVNC with a Unix domain socket; this seems
#to not suffer from these bugs

xvnc_pid=""
kill_server()
{
	if [ "$xvnc_pid" ]
	then
		kill "$xvnc_pid"
	fi
}

if [ x"$NESTED_X_DISPLAY_RESOLUTION" = x ]
then
	NESTED_X_DISPLAY_RESOLUTION=1280x1024
fi

vnc_socket="/tmp/vnc.$$"

Xtigervnc -SecurityTypes None -geometry "$NESTED_X_DISPLAY_RESOLUTION" -rfbunixpath "$vnc_socket" "$@" >/tmp/xvnc_log.$$ 2>&1 &
xvnc_pid="$!"
trap kill_server HUP INT TERM EXIT

sleep 0.5
while ! xtigervncviewer -RemoteResize=0 "$vnc_socket" >>/tmp/vncviewer.log.$$ 2>&1
do
	if [ ! -e "$vnc_socket" ]
	then
		exit 1
	fi
	sleep 1
done
