Commit 28c725c3 authored by hydrargyrum's avatar hydrargyrum

xephyr-run-cmd: run a Xephyr server and run a command in it (like xvfb-run)

parent b5c51e7d
......@@ -38,6 +38,7 @@ This repository hosts various small personal tools.
* univisible: tweak Unicode combinations and visualize them
* vhd: visual hex dump, splitting at newlines, not fixed-width lines
* wakeonwan: wake remote machines with Wake-on-WAN
* xephyr-run-cmd: run a Xephyr server and run a command in it (like xvfb-run)
For more info, see also: http://indigo.re
......
#!/usr/bin/env python3
# license: Do What The Fuck You Want To Public License version 2 [http://www.wtfpl.net/]
# Similar to xfvb-run but for Xephyr
# Start a Xephyr server and run a command in it
# sample usage: xephyr-run-cmd -screen 1024x768 -- xterm
import os
from subprocess import Popen
import sys
import time
def usage():
print(
f'usage: {sys.argv[0]} [XEPHYR ARGS...] -- CMD [ARGS...]\n\n'
'Run Xephyr with optional arguments XEPHYR ARGS and run CMD '
'in the Xephyr display\n\n'
f'sample usage: {sys.argv[0]} -screen 1024x768 -- xterm\n\n'
'Similar to xfvb-run but for Xephyr',
file=sys.stderr,
)
sys.exit(os.EX_USAGE)
# parse arguments
args = sys.argv[1:]
try:
pos = args.index('--')
except ValueError:
usage()
xargs = args[:pos]
cmd = args[pos + 1:]
if not cmd:
usage()
if xargs and xargs[0].startswith(':'):
newdisp = xargs[0]
else:
newdisp = ':1'
#
sproc = None
cproc = None
try:
sproc = Popen(['Xephyr', newdisp, *xargs])
time.sleep(3) # give Xephyr some time to start
if sproc.poll() is not None:
sys.exit(f'Xephyr exited with code {sproc.poll()}')
os.environ['DISPLAY'] = newdisp
cproc = Popen(cmd)
sys.exit(cproc.wait())
except KeyboardInterrupt:
pass
finally:
if cproc:
cproc.kill()
if sproc:
sproc.kill()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment