Add timeout to wait for program window to appear

Also add error helper function and fix option validation.

Addresses #62. Prevents tdrop from spiking CPU forever if a program fails to
start.
master
Fox Kiester 5 years ago
parent f4f0f34644
commit 9c75b30212
No known key found for this signature in database
GPG Key ID: 316E205D6017DBFF

42
tdrop

@ -57,6 +57,9 @@ options:
window a dropdown (e.g. '$ tdrop --clear current') window a dropdown (e.g. '$ tdrop --clear current')
--no-cancel don't cancel auto-showing (default is to prevent this when --no-cancel don't cancel auto-showing (default is to prevent this when
manually toggling a window after it is auto-hidden) manually toggling a window after it is auto-hidden)
--timeout set the timeout (in seconds) that tdrop will wait for a window
to appear before giving up in case the program fails to start
(default: 10)
--help print help --help print help
See man page for more details. See man page for more details.
@ -68,6 +71,11 @@ See man page for more details.
fi fi
} }
error() {
echo >&2 "$@" | tee -a "$MUTDROP_PATH"/log
exit 1
}
# * Default Options and Option Parsing # * Default Options and Option Parsing
# xdotool can take percentages; cannot take decimal percentages though # xdotool can take percentages; cannot take decimal percentages though
width="100%" width="100%"
@ -102,6 +110,7 @@ wm=
user_set_wm=false user_set_wm=false
class= class=
name= name=
timeout=10
while getopts :h:w:x:y:s:n:c:C:l:L:p:P:u:U:d:S:i:f:-:amt opt while getopts :h:w:x:y:s:n:c:C:l:L:p:P:u:U:d:S:i:f:-:amt opt
do do
case $opt in case $opt in
@ -165,6 +174,7 @@ do
name) name=$OPTARG;; name) name=$OPTARG;;
clear) clearwid=true;; clear) clearwid=true;;
no-cancel) cancel_auto_show=false;; no-cancel) cancel_auto_show=false;;
timeout) timeout=$OPTARG;;
help) print_help;; help) print_help;;
*) print_help illegal_opt;; *) print_help illegal_opt;;
esac;; esac;;
@ -178,31 +188,24 @@ if [[ -z $program_flags ]]; then
program_flags=("${@:2}") program_flags=("${@:2}")
fi fi
# check that the program is not a path and does not contain flags if [[ -z $program ]]; then
if [[ ! $program =~ ^(current|auto_hide|auto_show|toggle_auto_hide|hide_all)$ ]] && \ error "Program to run is required as a positional argument." \
! type "$program" &> /dev/null; then "For help use -h or --help or see the manpage."
echo >&2 "The program should be in PATH and not contain flags." | \
tee -a "$MUTDROP_PATH"/log
exit 1
fi fi
if [[ -z $program ]]; then # check that the program is in PATH
echo >&2 "Program to run is required as a positional argument." \ if [[ ! $program =~ ^(current|auto_hide|auto_show|toggle_auto_hide|hide_all)$ ]] && \
"For help use -h or --help or see the manpage." | \ ! type "$program" &> /dev/null; then
tee -a "$MUTDROP_PATH"/log error "The program should be in PATH."
exit 1
fi fi
# validate options that require number values # validate options that require number values
if [[ ! $height$width$xoff$yoff =~ ^[0-9%-]*$ ]]; then if [[ ! $height$width$xoff$yoff =~ ^[0-9%-]*$ ]]; then
echo >&2 "The -h, -w, -x, and -y values must be numbers (or percentages)." | \ error "The -h, -w, -x, and -y values must be numbers (or percentages)."
tee -a "$MUTDROP_PATH"/log
exit 1
fi fi
if [[ -n $dec_fix ]] && [[ ! $dec_fix =~ ^-?[0-9]+x-?[0-9]+$ ]]; then if [[ -n $dec_fix ]] && [[ ! $dec_fix =~ ^-?[0-9]+x-?[0-9]+$ ]]; then
echo >&2 "The decoration fix value must have form 'num'x'num'." \ error "The decoration fix value must have form 'num'x'num'." \
"The numbers can be negative or zero." | tee -a "$MUTDROP_PATH"/log "The numbers can be negative or zero."
exit 1
fi fi
# non-user-settable global vars # non-user-settable global vars
@ -569,6 +572,7 @@ create_win_return_wid() {
pid=$! pid=$!
fi fi
visible_wid=false visible_wid=false
counter=0
while : ; do while : ; do
if [[ $program == gnome-terminal ]]; then if [[ $program == gnome-terminal ]]; then
# only 1 pid; changes at some point after first run # only 1 pid; changes at some point after first run
@ -598,6 +602,10 @@ create_win_return_wid() {
if $visible_wid; then if $visible_wid; then
break break
fi fi
((counter=counter+1))
if [[ $counter -gt $((timeout * 100)) ]]; then
error "Exceeded timeout of $timeout seconds waiting for program."
fi
sleep 0.01 sleep 0.01
done done
# workaround for urxvt tabbed plugin using -embed # workaround for urxvt tabbed plugin using -embed

@ -109,6 +109,8 @@ Used to clear a saved window id for the given program or 'current' instead of cr
.TP .TP
\fB --no-cancel\fR \fB --no-cancel\fR
Specifies that manually re-showing an auto-hidden window with tdrop should not cancel an auto_show. Takes no argument. See the examples. Specifies that manually re-showing an auto-hidden window with tdrop should not cancel an auto_show. Takes no argument. See the examples.
\fB --timeout\fR
Specifies the timeout in to wait for a window to appear when starting a program before giving up. This prevents a tdrop process from sticking around forever if a program fails to start. (default: 10)
.TP .TP
\fB --help\fR \fB --help\fR
Print basic help information. Takes no argument. Print basic help information. Takes no argument.

Loading…
Cancel
Save