aboutsummaryrefslogtreecommitdiff
path: root/robot/robot.sh
blob: 6d51ea8f05cfbc36463debeaf7b2e2d0f98e8a6b (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
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash

set -e

## call find_bot to init this
window_id=

## This is the small sleep between key presses. Change for debugging
## when you are not sure what is happening
echo arg is $1
if [ -z $1 ]
then
    small_sleep=0.1
else
    small_sleep=$1
fi

big_sleep=1

emacs_process_pid=

function sleep_small {
    sleep $small_sleep
}

function sleep_big {
    sleep $big_sleep
}

function send_answer {
    retn
    sleep_small
    key 5
    sleep_small
}

function launch_emacs {
    echo Launching $EMACS -Q -l $* &

    $EMACS -Q -l $* &
    emacs_process_pid=$!
}

function kill_emacs {
    key alt+x
    command kill-emacs
    retn
}

function xdo {
    xdotool $1 --window $window_id $2 || {
        echo "Failed command: $1, $2";exit 1
    }
}
function key {
    xdo key $1
}

function command {
    xdo type $1
}

function retn {
    key Return
}

function wait_emacs {
    wait $emacs_process_pid || exit 1
}

function find_bot {
    for i in `seq 1 40`;
    do
        window_id=`xdotool search --onlyvisible --name "emacs-bot" 2> /dev/null`
        if [ -z "$window_id" ]
        then
            sleep 0.1
        else
            echo "Found Emacs-Bot on $i-th poll"
            return
        fi
    done
    echo "Could not find window ID for Emacs-bot"
    exit 1
}