Last active
March 7, 2019 04:40
-
-
Save andrewmackrodt/b53943185bbbd804ef4b06cd62503340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
# Description: | |
# startxwin is for use in a WSL environment where an X11 server is required. | |
# The server is provided by Cygwin/X. This script is intended to be added to | |
# the WSL user's ~/.bashrc file to ensure that an X11 server instance is | |
# always running. It does this by calling the same script via cygwin and | |
# checking whether an XWin process is running. The server will be started if | |
# it is not already running. | |
# | |
# Requirements: | |
# - WSL (Ubuntu Bionic 18.04) packages: ubuntu-desktop | |
# - Cygwin packages: xorg-server, xinit | |
# | |
# Installation: | |
# - Copy the script to "/c/cygwin/usr/local/bin/startxwin". | |
# - Add the line ". /c/cygwin/usr/local/bin/startxwin" to ~/.bashrc | |
# Make sure the period is included as the first character of the line. | |
################################################################################ | |
export DISPLAY=:0 | |
# start cygwin X11 server if we're not in a cygwin environment | |
if ! uname | grep -q -i 'Cygwin'; then | |
( cmd.exe /v /c 'set HOME=/tmp && C:\cygwin\bin\bash.exe --login -c /usr/local/bin/startxwin' 2>/tmp/X11.log & ) >/dev/null | |
xdpyinfo -display $DISPLAY >/dev/null | |
return 2>/dev/null || exit 0 # explicit exit return will fail if script not sourced (e.g. debugging) | |
fi | |
# otherwise we're in a cywgin environment | |
log () { | |
echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] ${2:-INFO}: $1" >&2 | |
} | |
start_xwin () { | |
readonly local cmd="/usr/bin/XWin $DISPLAY -listen tcp -multiwindow +iglx -nowgl -noprimary >/dev/null 2>&1" | |
log "Starting XWin ..." | |
log "Command: $cmd" | |
eval "$cmd & echo \$!" | |
log "Process Id: $!" | |
} | |
xwin_pid () { | |
ps x | grep [X]Win | awk '{ print $1 }' | |
} | |
xwin_running () { | |
ps -p $1 >/dev/null | |
} | |
XWIN_PID=$(xwin_pid) | |
if [[ "$XWIN_PID" != "" ]]; then | |
log "XWin detected with PID $XWIN_PID" | |
exit 0 | |
fi | |
log "XWin is not running" | |
XWIN_PID=$(start_xwin) | |
log "Waiting 0.5 seconds ..." | |
sleep 0.5 | |
if ! xwin_running $XWIN_PID; then | |
log "Failed to start XWin" ERROR | |
exit 1 | |
fi | |
log "XWin is running" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment