summaryrefslogtreecommitdiffstats
path: root/abs/core/linhes-scripts/xwin_find.sh
blob: 35309ddf68a7a4fa34eb9c70ab5bddec97ca4b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
#
# xwin_find [-v|-q] [timeout] window_name_regex
#
# Look for a window of the windows full name given by a awk regular
# expression, and print the windows xwindow ID.
#
# If a timeout is given (in seconds)continue to look for the windows ID
# for this amount of time before returning.  (EG default a single search)
#
# If no such window is found output nothing, just exit
#
# OPTIONS
#    -v    verbose, print the full matching xwininfo line on stderr
#    -q    do not print windows ID on stdout
#
####
# Anthony Thyssen    September 2005
#
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
Usage() {
  echo >&2 "$PROGNAME:" "$@"
  sed >&2 -n '/^###/q; s/^#$/# /; 3s/^#/# Usage:/;  3,$s/^# //p;' \
          "$PROGDIR/$PROGNAME"
  exit 10;
}

timeout=0

while [  $# -gt 0 ]; do
  case "$1" in
  [0-9]*) timeout=`date +%s`
          timeout=`expr $timeout + $1 + 1` || Usage
          ;;
  -q)     QUIET=true ;;   # don't print the final window ID, just status
  -v)     VERBOSE=true ;; # output the full xwininfo line on stderr

  --)     shift; break ;;    # end of user options
  -*)     Usage "Unknown option \"$1\"" ;;
  *)      break ;;           # end of user options
  esac
  shift   # next option
done

[ $# -lt 1 ] && Usage "Missing window search regex"
[ $# -gt 1 ] && Usage "Too many arguments."


find_win() {
  # nice added to let it give way to starting processes
  if [ "$VERBOSE" ]; then
    line=`nice xwininfo -root -tree | awk '/"'"$1"'":/ {print; exit}'`
    echo >&2 $line    # VERBOSE - xwininfo output
    echo "$line" |  sed 's/ .*//'
  else
    nice xwininfo -root -tree | awk '/"'"$1"'":/ {print $1; exit}'
  fi
}

while :; do
  id=`find_win "$1"`
  if [ "$id" ]; then
    [ -z "$QUIET" ] && echo $id  # the window ID found
    exit 0;
  fi
  [ `date +%s` -ge $timeout ] && break
done

exit 1  # window was not found