Skip to content

Commit af3450d

Browse files
committed
Some code reorder for faster startup
1 parent bf5bcd6 commit af3450d

File tree

1 file changed

+85
-85
lines changed

1 file changed

+85
-85
lines changed

runx

+85-85
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ finish() { # Clean up and terminate
7676
unset -f error warning note verbose
7777
unset -f rmcr getwslpath escapestring convertpath
7878
unset -f check_display check_displayport cookiebaker makecookie strlenhex
79-
unset -f check_host check_dependency check_dependencies setup_cookie generate_xcommand
79+
unset -f check_host check_dependency check_xserver setup_cookie generate_xcommand
8080
unset -f cleanup
8181
unset -f declare_variables parse_options main
8282

@@ -122,6 +122,14 @@ verbose() { # Show verbose message (--verbose)
122122

123123
#### general routines
124124

125+
check_dependency() { # Check for single command
126+
[ "${1:-}" ] || return 1
127+
command -v "${1:-}" >/dev/null || {
128+
note "Command not found: ${1:-}"
129+
return 1
130+
}
131+
return 0
132+
}
125133
rmcr() { # Remove carriage return to translate DOS/Windows newlines into UNIX newlines.
126134
# Convert stdin if $1 is empty. Otherwise convert file $1.
127135
case "${1:-}" in
@@ -291,9 +299,6 @@ strlenhex() { # print byte length of string $1 as hex value
291299
}
292300

293301
#### setup
294-
check_displayport() { # return 0 if display number $1 is in use
295-
echo "quit" | env LC_ALL=C telnet "$Hostip" "$((6000+${1:-}))" 2>&1 | grep -q "Connected"
296-
}
297302
check_display() { # find unused display number
298303
local Displaynumber
299304
for Displaynumber in $(seq 999 | shuf); do
@@ -304,6 +309,9 @@ check_display() { # find unused display number
304309
done
305310
return 1
306311
}
312+
check_displayport() { # return 0 if display number $1 is in use
313+
echo "quit" | env LC_ALL=C telnet "$Hostip" "$((6000+${1:-}))" 2>&1 | grep -q "Connected"
314+
}
307315
check_host() { # Check host environment
308316

309317
# Check for MS Windows subsystem
@@ -313,6 +321,11 @@ check_host() { # Check host environment
313321
}
314322
uname -r | grep -q Microsoft && Winsubsystem="WSL1"
315323
uname -r | grep -q microsoft && Winsubsystem="WSL2"
324+
case "$Winsubsystem" in
325+
WSL1|WSL2|CYGWIN|MSYS2) ;;
326+
*) error "runx is designed to run on MS Windows in WSL, Cygwin or MSYS2.
327+
Did not detect WSL, Cygwin or MSYS2." ;;
328+
esac
316329

317330
[ "$0" = "$BASH_SOURCE" ] && Sourced="no" || Sourced="yes"
318331
verbose "Script is being sourced yes/no: $Sourced"
@@ -349,23 +362,9 @@ check_host() { # Check host environment
349362

350363
return 0
351364
}
352-
check_dependency() { # Check for single command
353-
[ "${1:-}" ] || return 1
354-
command -v "${1:-}" >/dev/null || {
355-
note "Command not found: ${1:-}"
356-
return 1
357-
}
358-
return 0
359-
}
360-
check_dependencies() { # Check all dependencies
365+
check_xserver() { # Check all dependencies
361366
local Line
362367

363-
# subsystem
364-
case "$Winsubsystem" in
365-
WSL1|WSL2|CYGWIN|MSYS2) ;;
366-
*) error "runx is designed to run on MS Windows in WSL, Cygwin or MSYS2.
367-
Did not detect WSL, Cygwin or MSYS2." ;; ###
368-
esac
369368
[ "$Winsubsystem" = "MSYS2" ] && [ "$Xserver" = "xwin" ] && {
370369
note "runx in MSYS2 does not support --xwin.
371370
Fallback: Enabling option --vcxsrv."
@@ -408,22 +407,12 @@ check_dependencies() { # Check all dependencies
408407
https://sourceforge.net/projects/vcxsrv
409408
or install Cygwin with packages xinit and xauth to provide XWin:
410409
https://www.cygwin.com/" ;;
411-
MSYS2) error "No X server found for MSYS2.
410+
MSYS2) error "No X server found for MSYS2.
412411
Please install X server VcXsrv:
413412
https://sourceforge.net/projects/vcxsrv" ;;
414413
esac
415414
}
416-
417-
# Windows commands
418-
for Line in cmd.exe ipconfig.exe powershell.exe tasklist.exe taskkill.exe; do
419-
check_dependency "$Line" || error "Did not find Windows command: $Line"
420-
done
421-
422-
# Linux commands
423-
for Line in telnet shuf; do
424-
check_dependency "$Line" || error "Did not find Linux command: $Line
425-
Please install $Line"
426-
done
415+
return 0
427416
}
428417
cookiebaker() { # create an X cookie without xauth
429418
# $1 DISPLAY
@@ -652,11 +641,69 @@ parse_options() {
652641
shift
653642
done
654643
}
644+
start_xserver() {
645+
check_xserver
646+
generate_xcommand
647+
[ "$Xauthentication" = "yes" ] && setup_cookie
648+
649+
case "$Verbose" in
650+
yes) Xcommand="$Xcommand 1>&2" ;;
651+
no) Xcommand="$Xcommand >/dev/null 2>&1" ;;
652+
esac
653+
654+
# Run X in background?
655+
{ [ "$Hostcommand" ] || [ "$Sourced" = "yes" ] ; } && Xcommand="$Xcommand & Xserverpid=\$!"
656+
657+
# Store Windows PID list
658+
case $Winsubsystem in
659+
WSL1|WSL2) Tasklistold="$(tasklist.exe | rmcr | grep -i ${Xserver}.exe | awk '{print $2}')" ;;
660+
esac
661+
662+
# Run X server
663+
eval $Xcommand
664+
665+
# Find Windows PID of X server
666+
case $Winsubsystem in
667+
WSL1|WSL2)
668+
Tasklistnew="$(tasklist.exe | rmcr | grep -i ${Xserver}.exe | awk '{print $2}')"
669+
Xserverwinpid="$(echo "$Tasklistold
670+
$Tasklistnew" | sort | uniq -u | sed '/^$/d')"
671+
verbose "Windows PID of X server: $Xserverwinpid"
672+
;;
673+
esac
674+
675+
# Check for successfull startup
676+
[ "$Xserverpid" ] && {
677+
verbose "Linux PID of X server: $Xserverpid
678+
$(ps -p "$Xserverpid")"
679+
case "$Xserver" in
680+
xwin)
681+
for Waiting in 1 2 3 4 5 6 7 8 9 10; do
682+
[ -e "$(dirname "$Xwinexe")/../../tmp/.X11-unix/X$Newdisplaynumber" ] && Xready="yes"
683+
[ -e "$(dirname "$Xwinexe")/../tmp/.X11-unix/X$Newdisplaynumber" ] && Xready="yes"
684+
sleep 1
685+
[ "$Xready" ] && break
686+
verbose "Waiting since ${Waiting}s for XWin to be ready."
687+
done
688+
[ "$Xready" ] && verbose "X server XWin ready after ${Waiting}s." || error "X server XWin not ready after ${Waiting}s."
689+
;;
690+
vcxsrv) sleep 1 ;;
691+
esac
692+
}
693+
}
655694
main() {
656695
local Waiting Tasklistold Tasklistnew Xalreadyrunning
657696

658-
Xclientcookie="$(convertpath subsystem "~/runx_Xauthority")"
659-
Xservercookie="$(convertpath subsystem "$(MSYS2_ARG_CONV_EXCL='*' cmd.exe /C "echo %userprofile%")" | rmcr)/runx_Xauthority"
697+
# Dependency checks
698+
# # Windows commands ### check disabled for faster startup
699+
# for Line in cmd.exe ipconfig.exe powershell.exe tasklist.exe taskkill.exe; do
700+
# check_dependency "$Line" || error "Did not find Windows command: $Line"
701+
# done
702+
# Linux commands
703+
for Line in telnet shuf; do
704+
check_dependency "$Line" || error "Did not find Linux command: $Line
705+
Please install $Line"
706+
done
660707

661708
Xalreadyrunning="no"
662709
[ -n "$Newdisplaynumber" ] && {
@@ -666,7 +713,6 @@ main() {
666713
Xalreadyrunning="yes"
667714
}
668715
}
669-
670716
[ -z "$Newdisplaynumber" ] && {
671717
Newdisplaynumber="$(check_display)" || error "runx: Did not find a free display number."
672718
}
@@ -680,6 +726,8 @@ main() {
680726
export DISPLAY=$Hostip:$Newdisplaynumber
681727
case "$Xauthentication" in
682728
yes)
729+
Xclientcookie="$(convertpath subsystem "~/runx_Xauthority")"
730+
Xservercookie="$(convertpath subsystem "$(MSYS2_ARG_CONV_EXCL='*' cmd.exe /C "echo %userprofile%")" | rmcr)/runx_Xauthority"
683731
export XAUTHORITY="$Xclientcookie"
684732
verbose "DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
685733
note "If you get application error messages like 'Cannot open display'
@@ -699,57 +747,10 @@ main() {
699747
;;
700748
esac
701749

702-
[ "$Xalreadyrunning" = "no" ] && {
703-
[ "$Xauthentication" = "yes" ] && setup_cookie
704-
generate_xcommand
705-
706-
case "$Verbose" in
707-
yes) Xcommand="$Xcommand 1>&2" ;;
708-
no) Xcommand="$Xcommand >/dev/null 2>&1" ;;
709-
esac
710-
711-
# Run X in background?
712-
{ [ "$Hostcommand" ] || [ "$Sourced" = "yes" ] ; } && Xcommand="$Xcommand & Xserverpid=\$!"
713-
714-
# Store Windows PID list
715-
case $Winsubsystem in
716-
WSL1|WSL2) Tasklistold="$(tasklist.exe | rmcr | grep -i ${Xserver}.exe | awk '{print $2}')" ;;
717-
esac
718-
719-
# Run X server
720-
eval $Xcommand
721-
722-
# Find Windows PID of X server
723-
case $Winsubsystem in
724-
WSL1|WSL2)
725-
Tasklistnew="$(tasklist.exe | rmcr | grep -i ${Xserver}.exe | awk '{print $2}')"
726-
Xserverwinpid="$(echo "$Tasklistold
727-
$Tasklistnew" | sort | uniq -u | sed '/^$/d')"
728-
verbose "Windows PID of X server: $Xserverwinpid"
729-
;;
730-
esac
731-
732-
# Check for successfull startup
733-
[ "$Xserverpid" ] && {
734-
verbose "Linux PID of X server: $Xserverpid
735-
$(ps -p "$Xserverpid")"
736-
case "$Xserver" in
737-
xwin)
738-
for Waiting in 1 2 3 4 5 6 7 8 9 10; do
739-
[ -e "$(dirname "$Xwinexe")/../../tmp/.X11-unix/X$Newdisplaynumber" ] && Xready="yes"
740-
[ -e "$(dirname "$Xwinexe")/../tmp/.X11-unix/X$Newdisplaynumber" ] && Xready="yes"
741-
sleep 1
742-
[ "$Xready" ] && break
743-
verbose "Waiting since ${Waiting}s for XWin to be ready."
744-
done
745-
[ "$Xready" ] && verbose "X server XWin ready after ${Waiting}s." || error "X server XWin not ready after ${Waiting}s."
746-
;;
747-
vcxsrv) sleep 1 ;;
748-
esac
749-
}
750-
}
750+
# run X
751+
[ "$Xalreadyrunning" = "no" ] && start_xserver
751752

752-
# Run host command
753+
# run host command
753754
[ -z "$Exitcode" ] && [ "$Hostcommand" ] && {
754755
verbose "Executing command:
755756
$Hostcommand"
@@ -761,7 +762,6 @@ trap finish EXIT
761762
declare_variables
762763
check_host
763764
parse_options "$@"
764-
check_dependencies
765765
[ "$Cleanup" = "yes" ] && cleanup && Exitcode="${Exitcode:-0}"
766766
[ -z "$Exitcode" ] && main
767767
finish "${Exitcode:-0}"

0 commit comments

Comments
 (0)