-
Notifications
You must be signed in to change notification settings - Fork 0
/
click.sh
executable file
·443 lines (407 loc) · 11.7 KB
/
click.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/sh
# Maintained at: [email protected]:dareni/shellscripts.git
# Automate a periodic mouse click on a window.
#sleep 6; xdotool click --repeat 1000 1
#To get the window id use xdotool selectwindow
#Use xev -id winid for mouse location. Test with xdotool mousemove.
CMD=$1
DELAY=$2
FILENAME=$3
DUP=$4
COUNT_LIMIT=$5
if [ \( -z "$DELAY" \) -o \( "$CMD" = "IMAGE" -a -z "$FILENAME" \) ]; then
if [ ! \( "$CMD" = "FARM" -o "$CMD" = "SAND" -o "$CMD" = "MESE" -o \
"$CMD" = "CLICKER" -o "$CMD" = "POINTS" -o "$CMD" = "AL" \) ]; then
echo
echo Usage:
echo click.sh MULTI secDelay
echo secDelay may be a float for a portion of a second.
echo click.sh CLICK secDelay
echo secDelay - integer for seconds before each click.
echo Note: secDelay is added to a random generated fraction to
echo for the interval between each click.
echo click.sh IMAGE secDelay fileName dup count
echo secDelay - seconds before each image
echo filename - name for new images
echo dup - 2/1 images per shot
echo count - number of shots
return
fi
fi
getWindowCoordinates() {
# Prompt for a window click.
# Return
# - window id : F_WINID
# - mouseclick window relative X coordinate : F_X
# - mouseclick window relative Y coordinate : F_Y
F_WINID=`xdotool selectwindow`
#echo winid:$F_WINID
#get root relative mouseclick coordinates
F_MOUSELOC=`xdotool getMouselocation`
#echo $F_MOUSELOC
F_WINCOORD=`xwininfo -id $F_WINID -stats |grep geometry|sed -e 's/^[[:space:]]*-geometry //' #| cut -d+ -f2,3`
F_ROOT=`xwininfo -root -stats|grep geometry |sed -e 's/^[[:space:]]*//' |cut -d" " -f2|tr 'x+-' ' '`
F_ROOTW=`echo $F_ROOT|cut -d" " -f1`
F_ROOTH=`echo $F_ROOT|cut -d" " -f2`
F_WINSTATS=`xwininfo -id $F_WINID -stats |awk '{ if ($0 ~ /Width/){ width=$2 } \
if ($0 ~ /Height/){ height=$2 }
if ($0 ~ /Corners/){ corner=$2 }
} END{print corner" "width" "height}'`
#echo winstats:$F_WINSTATS
#xwininfo -id $F_WINID -stats |grep geometry
#xwininfo -id $F_WINID -stats |grep geometry|cut -d"+" -f2,3
F_WINCOORD=`echo $F_WINSTATS |cut -d" " -f1`
#echo wincoord:$F_WINCOORD
F_PLUS=`expr match "$F_WINCOORD" '.*+'`
F_MINUS=`expr match "$F_WINCOORD" '.*-'`
F_WINCOORD=`echo $F_WINCOORD |tr 'x+-' ' '`
F_WINXLOC=`echo $F_WINCOORD |cut -d" " -f1`
F_WINYLOC=`echo $F_WINCOORD |cut -d" " -f2`
#echo wincoord:\'$F_WINCOORD\' winx:$F_WINXLOC winy:$F_WINYLOC
F_WWIDTH=`echo $F_WINSTATS|cut -d" " -f2`
F_WHEIGHT=`echo $F_WINSTATS|cut -d" " -f3`
if [ $F_PLUS -eq 0 ]; then
F_WINXLOC=$(($F_ROOTW-$F_WINXLOC-$F_WWIDTH))
F_WINYLOC=$(($F_ROOTH-$F_WINYLOC-$F_WHEIGHT))
elif [ $F_MINUS -eq 0 ]; then
:
else
#-+ or +-
if [ $F_MINUS -gt $F_PLUS ]; then
F_WINYLOC=$(($F_ROOTH-$F_WINYLOC-$F_WHEIGHT))
else
F_WINXLOC=$(($F_ROOTW-$F_WINXLOC-$F_WWIDTH))
fi
fi
F_MOUSEX=`echo $F_MOUSELOC|cut -d" " -f1|cut -d: -f2`
F_MOUSEY=`echo $F_MOUSELOC|cut -d" " -f2|cut -d: -f2`
#echo winx:$F_WINXLOC winy:$F_WINYLOC mox:$F_MOUSEX moy:$F_MOUSEY
#Calculate the window relative mouse coordinates.
F_X=$(($F_MOUSEX-$F_WINXLOC))
F_Y=$(($F_MOUSEY-$F_WINYLOC))
#echo $F_WINXLOC $F_WINYLOC $F_MOUSEX $F_MOUSEY $X $Y
}
getch() {
pid_parent=`ps -h -q "$$" -o ppid `
key_binder=`ps -h -q $pid_parent -o cmd |grep -c key_binder`
if [ 1 = "$key_binder" ]; then
#do not read for a char when triggered from keybinder
return
fi
#read a single char from stdin
old=$(stty -g)
#time 1 = 100ms timeout
stty raw min 0 time 1
printf '%s' $(dd bs=1 count=1 2>/dev/null)
stty $old
}
getCommand() {
XEV_WINID=$1
xev -id $XEV_WINID -event keyboard | awk ' \
BEGIN{CTRL=0;PRESS=0;COMMAND=""}
{ if ($0 ~ /KeyPress/) {PRESS=1}
if ($0 ~ /keycode 37/ && PRESS == 1) {CTRL=1} #Left ALT
if ($0 ~ /KeyRelease/) {PRESS=0; CTRL=0; COMMAND=""}
if ($0 ~ /keycode 59/) {COMMAND="START"} #Comma
if ($0 ~ /keycode 60/) {COMMAND="STOP"} #Fullstop
if ($0 ~ /keycode 61/) {COMMAND="QUIT"} #Forward Slash
if (CTRL==1 && length(COMMAND) > 0) {
print COMMAND
CTRL=0
PRESS=0
COMMAND=""
fflush()
}
}'
}
getRandomDelay() {
GRD_DELAY=$1
if [ 0 -ne "$GRD_DELAY" ]; then
GRD_DELAY=$GRD_DELAY.`head /dev/urandom |cksum |cut -f1 -d " " |cut -c5-8`
else
GRD_DELAY=0
fi
}
getRandom5() {
a=`head -1 /dev/urandom |cut -b1`
a=`printf "%03d" "'$a" |cut -b3`
echo "(5-$a)/2" |bc
}
#Generate a random number between 5 and -4.
getRandom10() {
a=`head -1 /dev/urandom |cut -b1`
a=`printf "%03d" "'$a" |cut -b3`
echo $((5-$a))
}
#Generate a random number between 0 and 63
getRandom50() {
a=`head -1 /dev/urandom |cut -b1`
a=`printf "%03i" "'$a" |cut -b1-3`
echo $a/4 |bc
}
doMouseClick() {
DMC_WINID=$1
DMC_X=$2
DMC_Y=$3
PREVWIN=`xdotool getmouselocation`
PREVX=`echo $PREVWIN|cut -f1 -d " " |cut -f2 -d:`
PREVY=`echo $PREVWIN|cut -f2 -d " " |cut -f2 -d:`
PREVWINID=`echo $PREVWIN|cut -f4 -d " " |cut -f2 -d:`
xdotool mousemove --window $DMC_WINID $DMC_X $DMC_Y click 1
xdotool windowfocus $PREVWINID mousemove $PREVX $PREVY
}
doSimpleClick() {
DMC_WINID=$1
DMC_X=$2
DMC_Y=$3
xdotool mousemove --window $DMC_WINID $DMC_X $DMC_Y click 1
}
doMouseDClick() {
DMC_WINID=$1
DMC_X=$2
DMC_Y=$3
REPEAT=1
if [ -n "$4" ]; then
REPEAT=$4
fi
PREVWIN=`xdotool getmouselocation`
PREVX=`echo $PREVWIN|cut -f1 -d " " |cut -f2 -d:`
PREVY=`echo $PREVWIN|cut -f2 -d " " |cut -f2 -d:`
PREVWINID=`echo $PREVWIN|cut -f4 -d " " |cut -f2 -d:`
xdotool mousemove --window $DMC_WINID $DMC_X $DMC_Y click --repeat $REPEAT 1
xdotool windowfocus $PREVWINID mousemove $PREVX $PREVY
}
if [ "$CMD" = "CLICK" ]; then
echo Click on the window ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo Press {q} to quit ...
while [ -z `getch` ];
do
doMouseClick $WINID $X $Y
getRandomDelay $DELAY
SLEEP=$GRD_DELAY
sleep $SLEEP
done
elif [ "$CMD" = "MULTI" ]; then
echo Click on the window ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo Press 'q' to quit ...
while [ -z `getch` ];
do
doMouseClick $WINID $X $Y
sleep $DELAY
done
elif [ "$CMD" = "IMAGE" ]; then
if [ -z "$COUNT_LIMIT" ]; then
COUNT_LIMIT=0
fi
if [ -z "$DUP" ]; then
DUP=1
fi
mkdir "$FILENAME"
COUNTER=$FILENAME/count
touch $COUNTER
COUNT=`cat $COUNTER`
COUNT=$(($COUNT+1))
echo Click the top left corner of the image ...
F_WINID=`xdotool selectwindow`
F_MOUSELOC=`xdotool getMouselocation`
TOP_LEFT_X=`echo $F_MOUSELOC|cut -d" " -f1|cut -d: -f2`
TOP_LEFT_Y=`echo $F_MOUSELOC|cut -d" " -f2|cut -d: -f2`
echo Click the bottom right corner of the image ...
F_WINID=`xdotool selectwindow`
F_MOUSELOC=`xdotool getMouselocation`
BOTTOM_RIGHT_X=`echo $F_MOUSELOC|cut -d" " -f1|cut -d: -f2`
BOTTOM_RIGHT_Y=`echo $F_MOUSELOC|cut -d" " -f2|cut -d: -f2`
WIDTH=$(($BOTTOM_RIGHT_X-$TOP_LEFT_X))
HEIGHT=$(($BOTTOM_RIGHT_Y-$TOP_LEFT_Y))
if [ "$DUP" = 2 ]; then
echo Click the top left corner of the second image ...
F_WINID=`xdotool selectwindow`
F_MOUSELOC=`xdotool getMouselocation`
TOP_LEFT_X2=`echo $F_MOUSELOC|cut -d" " -f1|cut -d: -f2`
TOP_LEFT_Y2=`echo $F_MOUSELOC|cut -d" " -f2|cut -d: -f2`
echo Click the bottom right corner of the second image ...
F_WINID=`xdotool selectwindow`
F_MOUSELOC=`xdotool getMouselocation`
BOTTOM_RIGHT_X2=`echo $F_MOUSELOC|cut -d" " -f1|cut -d: -f2`
BOTTOM_RIGHT_Y2=`echo $F_MOUSELOC|cut -d" " -f2|cut -d: -f2`
WIDTH2=$(($BOTTOM_RIGHT_X-$TOP_LEFT_X))
HEIGHT2=$(($BOTTOM_RIGHT_Y-$TOP_LEFT_Y))
fi
import -window root -quality 100 -crop ${WIDTH}x${HEIGHT}+${TOP_LEFT_X}+${TOP_LEFT_Y} $FILENAME/$COUNT.jpg
echo $COUNT > $COUNTER
echo -n "$COUNT "
if [ "$DUP" = 2 ]; then
COUNT=$(($COUNT+1))
import -window root -quality 100 -crop ${WIDTH}x${HEIGHT}+${TOP_LEFT_X2}+${TOP_LEFT_Y2} $FILENAME/$COUNT.jpg
echo $COUNT > $COUNTER
echo -n "$COUNT "
fi
echo Click the next button ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo pc: $COUNT $COUNT_LIMIT
while [ -z `getch` ] && [ $COUNT -lt "$COUNT_LIMIT" ];
do
COUNT=$(($COUNT+1))
doMouseClick $WINID $X $Y
getRandomDelay $DELAY
SLEEP=$GRD_DELAY
sleep $SLEEP
import -window root -quality 100 -crop ${WIDTH}x${HEIGHT}+${TOP_LEFT_X}+${TOP_LEFT_Y} $FILENAME/$COUNT.jpg
echo $COUNT > $COUNTER
echo -n "$COUNT "
if [ "$DUP" = 2 ]; then
COUNT=$(($COUNT+1))
import -window root -quality 100 -crop ${WIDTH2}x${HEIGHT2}+${TOP_LEFT_X2}+${TOP_LEFT_Y2} $FILENAME/$COUNT.jpg
echo $COUNT > $COUNTER
echo -n "$COUNT "
fi
done
elif [ "$CMD" = "FARM" ]; then
echo Click on the window ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo Press 'q' to quit ...
while [ -z `getch` ];
do
doMouseDClick $WINID $X $Y 2
sleep $DELAY
xdotool keydown w
xdotool keyup w
done
elif [ "$CMD" = "SAND" ]; then
echo Click on the window ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo Press 'q' to quit ...
while [ -z `getch` ];
do
xdotool mousedown 1
sleep 2.5
xdotool mouseup 1
sleep 1
xdotool click 3
sleep 1
done
elif [ "$CMD" = "MESE" ]; then
echo Click point 1 ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo Press 'q' to quit ...
while [ -z `getch` ];
do
#Eat mushroom
xdotool key 1
xdotool click 1
xdotool mousedown 1
sleep .5
xdotool mouseup 1
#Place coral
xdotool key 2
sleep .5
xdotool click 3
#Mine coral with bronze pick
xdotool key 3
sleep .5
xdotool mousedown 1
sleep .75
xdotool mouseup 1
done
elif [ "$CMD" = "AL" ]; then
read -p "Add delay(sec)?" DELAY
echo Click point c ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
doSimpleClick $WINID $X $Y
while [ -z `getch` ];
do
sleep .5
CWIN=`xdotool getwindowfocus`
if [ $CWIN -ne $WINID ]; then
return
fi
sleep .5
CWIN=`xdotool getwindowfocus`
if [ $CWIN -ne $WINID ]; then
return
fi
x_adj=$(($X+`getRandom5`))
y_adj=$(($Y+`getRandom5`))
doSimpleClick $WINID $x_adj $y_adj
sleep $((DELAY+`getRandom50`))
done
return
elif [ "$CMD" = "POINTS" ]; then
echo start
. array.env
read -p "Number of points? " POINT_COUNT
for c in `jot - 1 ${POINT_COUNT:=1} 1`
do
echo Click point $c ...
getWindowCoordinates
set_avar winid $c $F_WINID
set_avar x $c $F_X
set_avar y $c $F_Y
doMouseClick `get_avar winid $c` `get_avar x $c` `get_avar y $c`
read -p "Add delay?" DELAY
set_avar delay $c ${DELAY:=0}
echo `get_avar x $c` `get_avar y $c` `get_avar delay $c`
done;
echo Press 'q' to quit ...
while [ -z `getch` ];
do
for c in `jot - 1 ${POINT_COUNT:=1} 1`
do
doMouseClick `get_avar winid $c` `get_avar x $c` `get_avar y $c`
sleep `get_avar delay $c`
done
done
return
elif [ "$CMD" = "CLICKER" ]; then
echo Click on the window ...
getWindowCoordinates
WINID=$F_WINID
X=$F_X
Y=$F_Y
echo ctrl-, start
echo ctrl-. stop
echo ctrl-/ quit
CMD=""
xev -id $WINID
exit
getCommand $WINID | \
while read KEY; do
if [ "$KEY" == "START" ]; then
echo "START"
CMD=$KEY
elif [ "$KEY" == "STOP" ]; then
echo "STOP"
CMD=$KEY
elif [ "$KEY" == "QUIT" ]; then
echo "QUIT"
kill -15 `ps |grep "xev" | cut -d" " -f 1`
exit;
fi
if [ "$CMD" == "START" ]; then
doMouseClick $WINID $X $Y
fi
done;
fi