No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libcvautomation_funcs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #!/bin/sh
  2. #===============================================================================
  3. # FILE: libcvautomation_funcs
  4. #
  5. # USAGE: . mosaic_testing_funcs
  6. #
  7. # DESCRIPTION:
  8. #
  9. # OPTIONS: ---
  10. # REQUIREMENTS: ---
  11. # BUGS: ---
  12. # NOTES: ---
  13. # AUTHOR: Bradlee Speice, bspeice@uncc.edu
  14. # ORGANIZATION:
  15. # CREATED: 06/19/2012 11:35:20 AM EDT
  16. # REVISION: ---
  17. #===============================================================================
  18. #-------------------------------------------------------------------------------
  19. # This script file is meant to be sourced and then used for driving libcvautomation.
  20. # It provides access to a lot of common macros, like clicking
  21. # an area of the screen based on an image.
  22. #-------------------------------------------------------------------------------
  23. #-------------------------------------------------------------------------------
  24. # NOTE: Due to the way loops work in this script, you may NOT use spaces
  25. # in file names.
  26. #-------------------------------------------------------------------------------
  27. #Make sure all the programs we will need are in the right place
  28. CVAMATCH=`which cva-match` || echo "Could not find the cva-match program..." &>2 #Needed for image recognition
  29. CVAINPUT=`which cva-input` || echo "Could not find the cva-input program..." &>2 #Needed to manipulate mouse and keyboard
  30. INT_MAX=2147483647
  31. #Some program-wide configs
  32. SEARCH_METHOD=0 #CV_TM_SQDIFF
  33. TOLERANCE=2250000
  34. USE_SANE_TOLERANCE=""
  35. #Set to non-null to enable sane tolerance matching
  36. #ex: USE_SANE_TOLERANCE="yes"
  37. USE_CENTER="yes"
  38. #Set to null to disable center-of-image matching
  39. #ex: USE_CENTER=""
  40. USE_WAIT="yes"
  41. #Set to null to disable waiting before an action
  42. #ex: USE_WAIT=""
  43. TIMEOUT=5
  44. #Quick trick to allow for easy redirection of output
  45. #OUTFILE=`mktemp`; echo "Logfile: " $OUTFILE
  46. OUTFILE=/dev/null
  47. #ERRFILE=`mktemp`; echo "Error file: " $ERRFILE
  48. ERRFILE=/dev/null
  49. out ()
  50. {
  51. echo "`date`: $@" >> $OUTFILE
  52. }
  53. err ()
  54. {
  55. echo "`date`: $@" >> $ERRFILE
  56. }
  57. #-------------------------------------------------------------------------------
  58. # Begin testing macros
  59. #-------------------------------------------------------------------------------
  60. #--- FUNCTION ----------------------------------------------------------------
  61. # NAME: mouse_down
  62. # DESCRIPTION: Push the mouse button down, and leave it there.
  63. # PARAMETERS:
  64. # RETURNS:
  65. #-------------------------------------------------------------------------------
  66. mouse_down ()
  67. {
  68. if [ -n "$1" ]; then
  69. eval $CVAINPUT -s 'mousedown "$1"' >> $OUTFILE
  70. else
  71. eval $CVAINPUT -s 'mousedown 1' >> $OUTFILE
  72. fi
  73. return $?
  74. } # ---------- end of function mouse_down ----------
  75. #--- FUNCTION ----------------------------------------------------------------
  76. # NAME: mouse_up
  77. # DESCRIPTION: Release a mouse button
  78. # PARAMETERS:
  79. # RETURNS:
  80. #-------------------------------------------------------------------------------
  81. mouse_up ()
  82. {
  83. if [ -n "$1" ]; then
  84. eval $CVAINPUT -s 'mouseup "$1"' >> $OUTFILE
  85. else
  86. eval $CVAINPUT -s 'mouseup 1' >> $OUTFILE
  87. fi
  88. return $?
  89. } # ---------- end of function mouse_up ----------
  90. #--- FUNCTION ----------------------------------------------------------------
  91. # NAME: mouse_click
  92. # DESCRIPTION: Click the mouse where it is currently located
  93. # PARAMETERS:
  94. # RETURNS:
  95. #-------------------------------------------------------------------------------
  96. mouse_click ()
  97. {
  98. if [ -n "$1" ]; then
  99. eval '$CVAINPUT -s "mouseclick \"$1\"" >> $OUTFILE'
  100. else
  101. eval '$CVAINPUT -s "mouseclick 1" >> $OUTFILE'
  102. fi
  103. return $?
  104. } # ---------- end of function mouse_click ----------
  105. #--- FUNCTION ----------------------------------------------------------------
  106. # NAME: mouse_click_xy
  107. # DESCRIPTION: Click a specific point on the screen
  108. # PARAMETERS: x-location, y-location
  109. # RETURNS:
  110. #-------------------------------------------------------------------------------
  111. mouse_click_xy ()
  112. {
  113. if [ -z "$1" ]; then
  114. out "Did not specify an X location, assuming (0, 0)"
  115. X_LOC=0
  116. Y_LOC=0
  117. else
  118. X_LOC=$1
  119. fi
  120. if [ -z "$2" ]; then
  121. out "Did not specify a Y location, assuming ($1, 0)"
  122. else
  123. Y_LOC=$2
  124. fi
  125. out "mouse_click_xy: " $X_LOC $Y_LOC
  126. if [ -n "$3" ]; then
  127. eval '$CVAINPUT -s "mousexy $X_LOC $Y_LOC" -s "mouseclick $3" >> $OUTFILE'
  128. else
  129. eval '$CVAINPUT -s "mousexy $X_LOC $Y_LOC" -s "mouseclick 1" >> $OUTFILE'
  130. fi
  131. return $?
  132. } # ---------- end of function mouse_click_xy ----------
  133. #--- FUNCTION ----------------------------------------------------------------
  134. # NAME: mouse_click_rxy
  135. # DESCRIPTION: Click a point on the screen relative to where the mouse is at
  136. # PARAMETERS: X increment, Y increment
  137. # RETURNS:
  138. #-------------------------------------------------------------------------------
  139. mouse_click_rxy ()
  140. {
  141. if [ -z "$1" ]; then
  142. out "Did not specify an X increment, assuming (0, 0)"
  143. X_INC=0
  144. Y_INC=0
  145. else
  146. X_INC=$1
  147. fi
  148. if [ -z "$2" ]; then
  149. out "Did not specify a Y increment, assuming ($X_INC, 0)"
  150. else
  151. Y_INC=$2
  152. fi
  153. out "mouse_click_rxy: " $1 $2
  154. if [ -n "$3" ]; then
  155. eval '$CVAINPUT -s "mouserxy $1 $2" -s "mouseclick 1" >> $OUTFILE'
  156. else
  157. eval '$CVAINPUT -s "mouserxy $1 $2" -s "mouseclick 1" >> $OUTFILE'
  158. fi
  159. return $?
  160. } # ---------- end of function mouse_click_rxy ----------
  161. #--- FUNCTION ----------------------------------------------------------------
  162. # NAME: mouse_click_image
  163. # DESCRIPTION: Click a point based on a screenshot
  164. # PARAMETERS: Location of screenshot, optional index to use if multiple
  165. # matches are found
  166. # RETURNS:
  167. #-------------------------------------------------------------------------------
  168. mouse_click_image ()
  169. {
  170. RETURN_CODE=255
  171. COMMAND_LINE=""
  172. if [ -z "$1" ]; then
  173. err "Did not give a base image..."
  174. return 255
  175. fi
  176. out "mouse_click_image: matching $@"
  177. if [ -n "$USE_CENTER" ]; then
  178. center="c"
  179. else
  180. center=""
  181. fi
  182. if [ -n "$USE_SANE_TOLERANCE" ]; then
  183. TOLERANCE_OPTION="-o"
  184. else
  185. TOLERANCE_OPTION="-t"
  186. fi
  187. if [ -z "$TOLERANCE" ]; then
  188. TOLERANCE=$INT_MAX
  189. fi
  190. if [ -n "$USE_WAIT" ]; then
  191. #Alternate between each image until a match is found, or we loop $TIMEOUT times
  192. #This is a bit weird, since we need to lower the TIMEOUT value for the "waitfor"
  193. #function, and then restore it at the end.
  194. OLD_TIMEOUT=$TIMEOUT
  195. TIMEOUT=1
  196. #I don't like this syntax, but 'for loop in {1..$OLD_TIMEOUT}' doesn't work
  197. for loop in `seq 1 $OLD_TIMEOUT`
  198. do
  199. for x in "$@"
  200. do
  201. CVA_RETURN=`waitfor "$x"`
  202. if [ $CVA_RETURN -lt $RETURN_CODE ]; then
  203. RETURN_CODE=$CVA_RETURN
  204. fi
  205. if [ $CVA_RETURN -eq 0 ]; then
  206. #We found the image, click it
  207. TIMEOUT=$OLD_TIMEOUT
  208. eval "$CVAINPUT $TOLERANCE_OPTION $TOLERANCE --search-method $SEARCH_METHOD -s 'i${center}mouseclick \"$x\"' >> $OUTFILE"
  209. return $?
  210. fi
  211. done
  212. done
  213. #We'll get here if we don't find a match
  214. TIMEOUT=$OLD_TIMEOUT
  215. else
  216. eval "$CVAINPUT $TOLERANCE_OPTION $TOLERANCE --search-method $SEARCH_METHOD -s 'i${center}mouseclick \"$x\"' >> $OUTFILE"
  217. CVA_RETURN=$?
  218. if [ $CVA_RETURN -lt $RETURN_CODE ]; then
  219. RETURN_CODE=$CVA_RETURN
  220. fi
  221. fi
  222. return $RETURN_CODE
  223. } # ---------- end of function mouse_click_image ----------
  224. #--- FUNCTION ----------------------------------------------------------------
  225. # NAME: mouse_rightclick_image
  226. # DESCRIPTION: Exactly like click_i, but click the right mouse button
  227. # PARAMETERS:
  228. # RETURNS:
  229. #-------------------------------------------------------------------------------
  230. mouse_rightclick_image ()
  231. {
  232. out "mouse_rightclick_image: $@"
  233. mouse_hover_image "$@" && click 3
  234. } # ---------- end of function mouse_rightclick_image ----------
  235. #--- FUNCTION ----------------------------------------------------------------
  236. # NAME: mouse_doubleclick
  237. # DESCRIPTION: Click the mouse twice
  238. # PARAMETERS:
  239. # RETURNS:
  240. #-------------------------------------------------------------------------------
  241. mouse_doubleclick ()
  242. {
  243. out "mouse_doubleclick: $1"
  244. if [ -n "$1" ]; then
  245. mouse_click "$1" && mouse_click "$1"
  246. else
  247. mouse_click && mouse_click
  248. fi
  249. } # ---------- end of function mouse_doubleclick ----------
  250. #--- FUNCTION ----------------------------------------------------------------
  251. # NAME: mouse_doubleclick_xy
  252. # DESCRIPTION: Click the mouse twice on a specific location
  253. # PARAMETERS:
  254. # RETURNS:
  255. #-------------------------------------------------------------------------------
  256. mouse_doubleclick_xy ()
  257. {
  258. if [ -z "$1" ]; then
  259. err "Did not specify an X location, assuming (0, 0)"
  260. X_LOC=0
  261. Y_LOC=0
  262. else
  263. X_LOC=$1
  264. fi
  265. if [ -z "$2" ]; then
  266. err "Did not specify a Y location, assuming ($1, 0)"
  267. else
  268. Y_LOC=$2
  269. fi
  270. out "mouse_doubleclick_xy $X_LOC $Y_LOC"
  271. if [ -n "$3" ]; then
  272. mouse_click_xy "$X_LOC" "$Y_LOC" "$3" && mouse_click "$3"
  273. else
  274. mouse_click_xy "$X_LOC" "$Y_LOC" && mouse_click
  275. fi
  276. return $?
  277. } # ---------- end of function mouse_doubleclick_xy ----------
  278. #--- FUNCTION ----------------------------------------------------------------
  279. # NAME: mouse_doubleclick_rxy
  280. # DESCRIPTION: Click the mouse twice on a relative location
  281. # PARAMETERS:
  282. # RETURNS:
  283. #-------------------------------------------------------------------------------
  284. mouse_doubleclick_rxy ()
  285. {
  286. if [ -z "$1" ]; then
  287. err "Did not specify an X increment, assuming (0, 0)"
  288. X_INC=0
  289. Y_INC=0
  290. else
  291. X_INC="$1"
  292. fi
  293. if [ -z "$2" ]; then
  294. err "Did not specify a Y increment, assuming ($X_INC, 0)"
  295. else
  296. Y_INC="$2"
  297. fi
  298. out "doubleclickr_xy: $X_INC $Y_INC"
  299. if [ -n "$3" ]; then
  300. mouse_clickr_xy "$X_INC" "$Y_INC" "$3" && mouse_click "$3"
  301. else
  302. mouse_clickr_xy "$1" "$2" && mouse_click
  303. fi
  304. return $?
  305. } # ---------- end of function mouse_doubleclick_rxy ----------
  306. #--- FUNCTION ----------------------------------------------------------------
  307. # NAME: mouse_doubleclick_image
  308. # DESCRIPTION: Double-click an image - moves to image first and then
  309. # executes a double-click to make sure that it is
  310. # compatible with multiple image arguments
  311. # PARAMETERS:
  312. # RETURNS:
  313. #-------------------------------------------------------------------------------
  314. mouse_doubleclick_image ()
  315. {
  316. out "mouse_doubleclick_image: $@"
  317. mouse_hover_image "$@" && mouse_click && mouse_click
  318. } # ---------- end of function mouse_doubleclick_image ----------
  319. #--- FUNCTION ----------------------------------------------------------------
  320. # NAME: image_location
  321. # DESCRIPTION: Get the location of a subimage in root
  322. # PARAMETERS: PNG Filename of sub-image
  323. # RETURNS: "X_LOC,Y_LOC"
  324. #-------------------------------------------------------------------------------
  325. image_location ()
  326. {
  327. COMMAND_LINE=""
  328. if [ -z "$1" ]; then
  329. err "Did not give a base image..."
  330. return 255
  331. fi
  332. #Build the command line arguments
  333. for x in "$@"
  334. do
  335. COMMAND_LINE+="-s \"$x\" "
  336. done
  337. #Find our sub-image
  338. if [ -n "$USE_SANE_TOLERANCE" ]; then
  339. TOLERANCE_OPTION="-o"
  340. else
  341. TOLERANCE_OPTION="-t"
  342. fi
  343. if [ -z "$TOLERANCE" ]; then
  344. TOLERANCE=$INT_MAX
  345. fi
  346. if [ -n "$USE_CENTER" ]; then
  347. CENTER="--center"
  348. else
  349. CENTER=""
  350. fi
  351. MATCH=`$CVAMATCH $TOLERANCE_OPTION $TOLERANCE $CENTER --x-root $COMMAND_LINE`
  352. RETURN_CODE=$?
  353. out "$MATCH"
  354. MATCH=`echo $MATCH | head -n1`
  355. if [ x"$MATCH" == "x" ]; then
  356. err "Could not find a match between \"$@\" and the root window."
  357. return 255
  358. fi
  359. IMAGE_NAME=`echo "$MATCH" | cut -d',' -f1`
  360. X_LOC=`echo "$MATCH" | cut -d',' -f2`
  361. Y_LOC=`echo "$MATCH" | cut -d',' -f3`
  362. out "image_location: \"$MATCH\" " "$X_LOC,$Y_LOC,$IMAGE_NAME"
  363. echo "$X_LOC,$Y_LOC"
  364. return $RETURN_CODE
  365. } # ---------- end of function image_location ----------
  366. #--- FUNCTION ----------------------------------------------------------------
  367. # NAME: mouse_hover_xy
  368. # DESCRIPTION: Move the mouse to location X Y
  369. # PARAMETERS: X_LOC, Y_LOC
  370. # RETURNS:
  371. #-------------------------------------------------------------------------------
  372. mouse_hover_xy ()
  373. {
  374. if [ -z "$1" ]; then
  375. err "Did not provide an X location, assuming 0..."
  376. X_LOC=0
  377. else
  378. X_LOC=$1
  379. fi
  380. if [ -z "$2" ]; then
  381. err "Did not provide a Y location, assuming 0..."
  382. Y_LOC=0
  383. else
  384. Y_LOC=$2
  385. fi
  386. out "mouse_hover_xy: $X_LOC $Y_LOC"
  387. eval '$CVAINPUT -s "mousexy $X_LOC $Y_LOC" >> $OUTFILE'
  388. return $?
  389. } # ---------- end of function mouse_hover_xy ----------
  390. #--- FUNCTION ----------------------------------------------------------------
  391. # NAME: mouse_hover_rxy
  392. # DESCRIPTION: Move the mouse over X and up Y
  393. # PARAMETERS:
  394. # RETURNS:
  395. #-------------------------------------------------------------------------------
  396. mouse_hover_rxy ()
  397. {
  398. if [ -z "$1" ]; then
  399. err "Did not provide an X increment, assuming 0..."
  400. X_INC=0
  401. else
  402. X_INC=$1
  403. fi
  404. if [ -z "$2" ]; then
  405. err "Did not provide a Y increment, assuming 0..."
  406. Y_INC=0
  407. else
  408. Y_INC=$1
  409. fi
  410. out "mouse_hover_rxy: $X_INC $Y_INC"
  411. eval '$CVAINPUT -s "mouserxy $X_INC $Y_INC" >> $OUTFILE'
  412. return $?
  413. } # ---------- end of function mouse_hover_rxy ----------
  414. #--- FUNCTION ----------------------------------------------------------------
  415. # NAME: mouse_hover_image
  416. # DESCRIPTION: Move the mouse to the center of image on root window
  417. # PARAMETERS: PNG File name
  418. # RETURNS:
  419. #-------------------------------------------------------------------------------
  420. mouse_hover_image ()
  421. {
  422. RETURN_CODE=255
  423. COMMAND_LINE=""
  424. if [ -z "$1" ]; then
  425. err "Did not give a base image."
  426. return 255
  427. fi
  428. out "hover_i: $@"
  429. if [ -n "$USE_CENTER" ]; then
  430. center="c"
  431. else
  432. center=""
  433. fi
  434. if [ -n "$USE_SANE_TOLERANCE" ]; then
  435. TOLERANCE_OPTION="-o"
  436. else
  437. TOLERANCE_OPTION="-t"
  438. fi
  439. if [ -z "$TOLERANCE" ]; then
  440. TOLERANCE=$INT_MAX
  441. fi
  442. if [ -n "$USE_WAIT" ]; then
  443. #Alternate between each image until a match is found, or we loop $TIMEOUT times
  444. #This is a bit weird, since we need to lower the TIMEOUT value for the "waitfor"
  445. #function, and then restore it at the end.
  446. OLD_TIMEOUT=$TIMEOUT
  447. TIMEOUT=1
  448. #I don't like this syntax, but 'for loop in {1..$OLD_TIMEOUT}' doesn't work
  449. for loop in `seq 1 $OLD_TIMEOUT`
  450. do
  451. for x in "$@"
  452. do
  453. CVA_RETURN=`waitfor "$x"`
  454. if [ $CVA_RETURN -lt $RETURN_CODE ]; then
  455. RETURN_CODE=$CVA_RETURN
  456. fi
  457. if [ $CVA_RETURN -eq 0 ]; then
  458. #We found the image, click it
  459. eval "$CVAINPUT $TOLERANCE_OPTION $TOLERANCE --search-method $SEARCH_METHOD -s '${center}mouseimage \"$x\"' >> $OUTFILE"
  460. TIMEOUT=$OLD_TIMEOUT
  461. return $?
  462. fi
  463. done
  464. done
  465. #We'll get here if we didn't find a match
  466. TIMEOUT=$OLD_TIMEOUT
  467. else
  468. eval "$CVAINPUT $TOLERANCE_OPTION $TOLERANCE --search-method $SEARCH_METHOD -s '${center}mouseimage \"$x\"' >> $OUTFILE"
  469. CVA_RETURN=$?
  470. if [ $CVA_RETURN -lt $RETURN_CODE ]; then
  471. RETURN_CODE=$CVA_RETURN
  472. fi
  473. fi
  474. return $RETURN_CODE
  475. } # ---------- end of function mouse_hover_image ----------
  476. #--- FUNCTION ----------------------------------------------------------------
  477. # NAME: mouse_jiggle
  478. # DESCRIPTION: Moves the mouse down-right 1 pixel, and then back - necessary
  479. # for activation some widgets
  480. # PARAMETERS:
  481. # RETURNS:
  482. #-------------------------------------------------------------------------------
  483. mouse_jiggle ()
  484. {
  485. out "mouse_jiggle"
  486. eval '$CVAINPUT -s "mousejiggle" >> $OUTFILE'
  487. return $?
  488. } # ---------- end of function mouse_jiggle ----------
  489. #--- FUNCTION ----------------------------------------------------------------
  490. # NAME: mouse_scroll_up
  491. # DESCRIPTION: Scroll the mouse wheel up one
  492. # PARAMETERS:
  493. # RETURNS:
  494. #-------------------------------------------------------------------------------
  495. mouse_scroll_up ()
  496. {
  497. out "mouse_scrollu"
  498. eval '$CVAINPUT -s "mousescrollu" >> $OUTFILE'
  499. return $?
  500. } # ---------- end of function mouse_scroll_up ----------
  501. #--- FUNCTION ----------------------------------------------------------------
  502. # NAME: mouse_scroll_down
  503. # DESCRIPTION: Scroll the mouse wheel down one
  504. # PARAMETERS:
  505. # RETURNS:
  506. #-------------------------------------------------------------------------------
  507. mouse_scroll_down ()
  508. {
  509. out "mouse_scroll_down"
  510. eval '$CVAINPUT -s "mousescrolld" >> $OUTFILE'
  511. return $?
  512. } # ---------- end of function mouse_scroll_down ----------
  513. #--- FUNCTION ----------------------------------------------------------------
  514. # NAME: mouse_drag_n_drop
  515. # DESCRIPTION: Drag one image to another image
  516. # PARAMETERS:
  517. # RETURNS:
  518. #-------------------------------------------------------------------------------
  519. mouse_drag_n_drop ()
  520. {
  521. if [ -z "$1" ]; then
  522. err "mouse_drag_n_drop: Did not give me an image to drag!"
  523. return 255
  524. elif [ -z "$2" ]; then
  525. err "mouse_drag_n_drop: Did not give me an image to drag to!"
  526. return 255
  527. fi
  528. #We have our images to find, hover over the first, mousedown, and then
  529. #hover over the other one, and mouseup.
  530. mouse_hover_image "$1"
  531. if [ $? -ne 0 ]; then
  532. err "mouse_drag_n_drop: Could not find image to drag!"
  533. else
  534. mouse_down 1
  535. mouse_hover_image "$2" || err "mouse_drag_n_drop: Could not find image to drag to!"
  536. mouse_up
  537. fi
  538. } # ---------- end of function mouse_drag_n_drop ----------
  539. #--- FUNCTION ----------------------------------------------------------------
  540. # NAME: key_string
  541. # DESCRIPTION: Simulate pressing keys according to $1
  542. # PARAMETERS: String of text as $1
  543. # RETURNS:
  544. #-------------------------------------------------------------------------------
  545. key_string ()
  546. {
  547. COMMAND_LINE=""
  548. if [ -z "$1" ]; then
  549. err "Did not give me a string to enter..."
  550. return 255
  551. fi
  552. #We use $* instead of $@ because they expand a bit differently -
  553. # "$*" -> "$1 $2 $3"
  554. # "$@" -> "$1" "$2" "$3"
  555. COMMAND_LINE="-s 'keystring $*'"
  556. out "key_str: \"$*\""
  557. eval "$CVAINPUT $COMMAND_LINE >> $OUTFILE"
  558. return $?
  559. } # ---------- end of function key_string ----------
  560. #--- FUNCTION ----------------------------------------------------------------
  561. # NAME: key_down
  562. # DESCRIPTION: Presses down a key - necessary for stuff like Alt-shortcuts
  563. # PARAMETERS:
  564. # RETURNS:
  565. #-------------------------------------------------------------------------------
  566. key_down ()
  567. {
  568. if [ -z "$1" ]; then
  569. err "Did not give me a key to press down..."
  570. return 255
  571. fi
  572. out "key_down: $1"
  573. eval '$CVAINPUT -s "keydown $1" >> $OUTFILE'
  574. return $?
  575. } # ---------- end of function key_down ----------
  576. #--- FUNCTION ----------------------------------------------------------------
  577. # NAME: key_up
  578. # DESCRIPTION: Lifts a key up - every key_down should have a key_up
  579. # PARAMETERS:
  580. # RETURNS:
  581. #-------------------------------------------------------------------------------
  582. key_up ()
  583. {
  584. if [ -z "$1" ]; then
  585. err "Did not give me a key to release..."
  586. fi
  587. out "key_up: $1"
  588. eval "$CVAINPUT -s 'keyup $1' >> $OUTFILE"
  589. return $?
  590. } # ---------- end of function key_up ----------
  591. #--- FUNCTION ----------------------------------------------------------------
  592. # NAME: key_click
  593. # DESCRIPTION: Clicks a single key
  594. # PARAMETERS:
  595. # RETURNS:
  596. #-------------------------------------------------------------------------------
  597. key_click ()
  598. {
  599. if [ -z "$1" ]; then
  600. err "Did not give me a key to press..."
  601. fi
  602. out "key_click: $1"
  603. eval "$CVAINPUT -s 'keyclick $1' >> $OUTFILE"
  604. return $?
  605. } # ---------- end of function key_press ----------
  606. #--- FUNCTION ----------------------------------------------------------------
  607. # NAME: wait_for
  608. # DESCRIPTION: Wait for an image to be displayed
  609. # PARAMETERS:
  610. # RETURNS:
  611. #-------------------------------------------------------------------------------
  612. wait_for ()
  613. {
  614. if [ -z "$1" ]; then
  615. err "Did not give me an image to find..."
  616. fi
  617. out "wait_for: $1"
  618. if [ -n "$USE_SANE_TOLERANCE" ]; then
  619. if [ -z "$TOLERANCE" ]; then
  620. TOLERANCE=$INT_MAX
  621. fi
  622. eval '$CVAINPUT -o $TOLERANCE --search-method $SEARCH_METHOD --timeout $TIMEOUT -s "waitfor \"$1\"" >> $OUTFILE'
  623. else
  624. if [ -z "$TOLERANCE" ]; then
  625. TOLERANCE=$INT_MAX
  626. fi
  627. eval '$CVAINPUT -t $TOLERANCE --search-method $SEARCH_METHOD --timeout $TIMEOUT -s "waitfor \"$1\"" >> $OUTFILE'
  628. fi
  629. RETURN_CODE=$?
  630. echo $RETURN_CODE
  631. #We don't return here, but rather just pass the return code up -
  632. #This makes sure that if the user sets errexit, we don't abort because
  633. #multiple images were used, while still keeping the return code.
  634. } # ---------- end of function waitfor ----------
  635. #--- FUNCTION ----------------------------------------------------------------
  636. # NAME: notify
  637. # DESCRIPTION: Notify the user and pause
  638. # PARAMETERS:
  639. # RETURNS:
  640. #-------------------------------------------------------------------------------
  641. notify ()
  642. {
  643. if [ -z "$1" ]; then
  644. #Not given any text to display, return an error
  645. return 255
  646. fi
  647. if [ -n "$DISPLAY" ]; then
  648. which zenity &> /dev/null
  649. #If zenity isn't found, we don't want to go to a read prompt -
  650. #the user may not be running this from a terminal.
  651. if [ $? -eq 0 ]; then
  652. zenity --info --text="$@"
  653. fi
  654. else
  655. read -p "$@"
  656. fi
  657. } # ---------- end of function notify ----------