mirror of
https://github.com/bspeice/libcvautomation
synced 2024-12-04 13:58:11 -05:00
Add a function to wait for an image to be displayed
Clean up some loose ends with the waitfor function Finish tying up loose ends, build is clean now
This commit is contained in:
parent
756807d787
commit
b37f802d38
@ -39,9 +39,10 @@ int main( int argc, char** argv )
|
||||
Display *display;
|
||||
display = NULL;
|
||||
|
||||
int searchMethod, tolerance;
|
||||
int searchMethod, tolerance, timeout;
|
||||
searchMethod = 0;
|
||||
tolerance = INT_MAX;
|
||||
timeout = 5;
|
||||
|
||||
int returnCode = 1;
|
||||
|
||||
@ -60,6 +61,7 @@ int main( int argc, char** argv )
|
||||
{"string", required_argument, 0, 's'},
|
||||
{"sane-tolerance", required_argument, 0,'o'},
|
||||
{"print-format",required_argument, 0, 'p'},
|
||||
{"timeout", required_argument, 0, 'i'},
|
||||
/* Other valid values are "optional_argument"
|
||||
* and "required_argument" */
|
||||
{0, 0, 0, 0}
|
||||
@ -118,7 +120,7 @@ int main( int argc, char** argv )
|
||||
if (display == NULL)
|
||||
display = XOpenDisplay( xDisplayLocation );
|
||||
cvaPoint returnPoint;
|
||||
returnPoint = xte_commandString( display, optarg, mouseButton, searchMethod, tolerance );
|
||||
returnPoint = xte_commandString( display, optarg, mouseButton, searchMethod, tolerance, timeout );
|
||||
|
||||
if (returnPoint.x != -1 && returnPoint.y != -1)
|
||||
{
|
||||
@ -141,6 +143,10 @@ int main( int argc, char** argv )
|
||||
* --sane-tolerance=100 ~= INT_MAX */
|
||||
tolerance = pow(1.2397076, tolerance);
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
timeout = atoi(optarg);
|
||||
break;
|
||||
|
||||
case '?':
|
||||
/* Error routine */
|
||||
@ -187,6 +193,7 @@ Usage: \n\
|
||||
\t\t\t\tmatch the center of the image.\n\
|
||||
\t-o, --sane-tolerance:\tSet the tolerance using a scale of 1-100,\n\
|
||||
\t-s, --string:\t\tCommand string - see below.\n\
|
||||
\t-i, --timeout:\t\tSpecify the timeout to use when using the 'waitfor' function\n\
|
||||
\n\
|
||||
This program works kind of like a mini-language. All options\n\
|
||||
are parsed left-to-right, and executed right there. Thus, specifying \"--display\"\n\
|
||||
@ -209,6 +216,7 @@ Available commands:\n\
|
||||
\tkeydown:\tPush and leave down a keyboard button.\n\
|
||||
\tkeyup:\tRelease a keyboard button.\n\
|
||||
\tkeystring:\tInput a string of keys to X11.\n\
|
||||
\twaitfor:\tWait for an image to be displayed.\n\
|
||||
\n\
|
||||
If you have any questions, comments, concerns, email <%s>.\n\n", LIBCVAUTOMATION_VERSION, LIBCVAUTOMATION_BUGREPORT );
|
||||
|
||||
@ -271,6 +279,8 @@ void checkXTEEnabled ( Display *display )
|
||||
*
|
||||
* -s, --string: Command string - see below.
|
||||
*
|
||||
* -i, --timeout: Specify the timeout to use when using the 'waitfor' command
|
||||
*
|
||||
*
|
||||
* \section commands Full Command List:
|
||||
* \c mouseclick: Click the mouse in-place.
|
||||
@ -305,6 +315,8 @@ void checkXTEEnabled ( Display *display )
|
||||
*
|
||||
* \c keystring: Input a string of keys to X11.
|
||||
*
|
||||
* \c waitfor: Wait for an image to be displayed
|
||||
*
|
||||
* \section contact Contact Information:
|
||||
* Questions? Comments? Concerns? Suggestions? Send all feedback to Bradlee Speice at <bspeice@uncc.edu>
|
||||
*/
|
||||
|
@ -103,8 +103,14 @@ void xte_keyDown ( Display *displayLocation, char *key );
|
||||
/* Release a key */
|
||||
void xte_keyUp ( Display *displayLocation, char *key );
|
||||
|
||||
/* Wait for an image to show up on screen */
|
||||
cvaPoint xte_waitForImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance, int timeout );
|
||||
|
||||
/* Wait for an image from file to show up on screen */
|
||||
cvaPoint xte_waitForImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance, int timeout );
|
||||
|
||||
/* Use one of the functions by command name */
|
||||
cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance );
|
||||
cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance, int timeout );
|
||||
|
||||
#endif /* LIBCVAUTOMATION_XTEST_H */
|
||||
|
||||
@ -436,6 +442,10 @@ Release a keyboard key. This can be a key like \c 'a', \c 'b', or something fanc
|
||||
\code 'keystring <string>' \endcode
|
||||
Input a string of text to the X11 server. For example, inputting 'Hello, world!" will act as if you typed 'Hello, world!' from the keyboard.
|
||||
\warning Unlike \ref keydown, \ref keyup, and \ref keyclick, this function can not handle special keys like 'space'.
|
||||
|
||||
\section waitfor Wait for Image
|
||||
\code 'waitfor <image_name>' \endcode
|
||||
Wait for an image to show up on screen. For example, this can be used to make sure a button exists before clicking it.
|
||||
*/
|
||||
|
||||
/** \def IS_CMD (x, y)
|
||||
@ -649,8 +659,29 @@ Input a string of text to the X11 server. For example, inputting 'Hello, world!"
|
||||
* \param key The key to click as a string
|
||||
* \see \ref xtest_key_strings
|
||||
*/
|
||||
/** \fn cvaPoint xte_waitForImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance, int timeout );
|
||||
* \brief Wait for an image to show up on screen
|
||||
* \details This method allows you to search for an image on screen and wait for it to show up - this way you can make sure an image exists, and then respond to it. Also makes error checking easy.
|
||||
* \param displayLocation The Display of which to search for an image
|
||||
* \param subImage The sub image to search for
|
||||
* \param searchMethod The search method to use when searching for \c subImage
|
||||
* \param tolerance The tolerance to use when searching for \c tolerance
|
||||
* \param timeout The time (in seconds) to search for the image
|
||||
* \see \ref libcvautomation_search_methods
|
||||
*/
|
||||
|
||||
/** \fn cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance );
|
||||
/** \fn cvaPoint xte_waitForImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance, int timeout );
|
||||
* \brief Wait for an image from file to show up on screen
|
||||
* \details This method allows you to search for an image on screen and wait for it to show up - this way you can make sure an image exists, and then respond to it. Also makes error checking easy.
|
||||
* \param displayLocation The Display of which to search for an image
|
||||
* \param fileName The file to load an image from before searching
|
||||
* \param searchMethod The search method to use when searching for \c subImage
|
||||
* \param tolerance The tolerance to use when searching for \c tolerance
|
||||
* \param timeout The time (in seconds) to search for the image
|
||||
* \see \ref libcvautomation_search_methods
|
||||
*/
|
||||
|
||||
/** \fn cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance, int timeout );
|
||||
* \brief Execute a command where the command is coming from a string
|
||||
* \details This function allows you to input a command to libcvautomation from a string. For example, to click a mouse button, you would use the \c command 'mouseclick'. Please note that some <tt>command</tt>s may need arguments to the string, and some may use function arguments. See \ref xtest_command_strings for a full list of command and arguments
|
||||
* \param displayLocation The Display of which to operate on
|
||||
@ -658,6 +689,7 @@ Input a string of text to the X11 server. For example, inputting 'Hello, world!"
|
||||
* \param mouseButton The mouse button to click if it is needed by the command being executed
|
||||
* \param searchMethod The search method to use if it is needed by the command being executed
|
||||
* \param tolerance The tolerance to use if it is needed by the command being executed
|
||||
* \param timeout The time in seconds to wait for an image to be displayed when using the \c waitfor command.
|
||||
* \see \ref xtest_command_strings
|
||||
* \see \ref libcvautomation_search_methods
|
||||
*/
|
||||
|
@ -23,6 +23,7 @@
|
||||
/* C includes */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Autoconf logic to select the correct OpenCV version */
|
||||
@cv_headers@
|
||||
|
@ -763,6 +763,61 @@ void xte_keyUp ( Display *displayLocation, char *key )
|
||||
XFlush( displayLocation );
|
||||
}
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: xte_waitForImage
|
||||
* Description: Wait for an image to show up on screen
|
||||
* =====================================================================================
|
||||
*/
|
||||
cvaPoint xte_waitForImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance, int timeout )
|
||||
{
|
||||
cvaPoint resultPoint;
|
||||
resultPoint.x = resultPoint.y = -1;
|
||||
|
||||
int localTime = 0;
|
||||
while ( localTime < timeout )
|
||||
{
|
||||
resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
if ( resultPoint.x != -1 && resultPoint.y != -1 )
|
||||
return resultPoint;
|
||||
|
||||
sleep( 1 );
|
||||
localTime++;
|
||||
}
|
||||
|
||||
/* Return error, we couldn't find the image */
|
||||
return resultPoint;
|
||||
} /* ----- end of function xte_waitForImage ----- */
|
||||
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: xte_waitForImage_location
|
||||
* Description: Wait for an image from file to show up on screen
|
||||
* =====================================================================================
|
||||
*/
|
||||
cvaPoint xte_waitForImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance, int timeout )
|
||||
{
|
||||
cvaPoint resultPoint;
|
||||
resultPoint.x = resultPoint.y = -1;
|
||||
|
||||
int localTime = 0;
|
||||
while ( localTime < timeout )
|
||||
{
|
||||
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
if ( resultPoint.x != -1 && resultPoint.y != -1 )
|
||||
return resultPoint;
|
||||
|
||||
sleep( 1 );
|
||||
localTime++;
|
||||
}
|
||||
|
||||
/* Return error, we couldn't find the image */
|
||||
return resultPoint;
|
||||
} /* ----- end of function xte_waitForImage_location ----- */
|
||||
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
@ -770,7 +825,7 @@ void xte_keyUp ( Display *displayLocation, char *key )
|
||||
* Description: Use one of the functions by command name
|
||||
* =====================================================================================
|
||||
*/
|
||||
cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance )
|
||||
cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance, int timeout )
|
||||
{
|
||||
/* Alright, this function bears a bit of talking about.
|
||||
* What happens is that I test here for the command, and pass it off.
|
||||
@ -778,7 +833,7 @@ cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int
|
||||
* the original can be found at: http://hoopajoo.net/projects/xautomation.html
|
||||
* Most of the code for parsing is the same, just easier to do it that way. */
|
||||
|
||||
/* Note that most of the functions don't need mouseButton, searchMethod, or tolerance,
|
||||
/* Note that most of the functions don't need mouseButton, searchMethod, etc.
|
||||
* but they're here to make sure that they're available if needed. */
|
||||
|
||||
/* The returns also bear a bit of instruction.
|
||||
@ -960,6 +1015,16 @@ cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int
|
||||
|
||||
resultPoint.x = resultPoint.y = -2;
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "waitfor" ))
|
||||
{
|
||||
char *fileName;
|
||||
fileName = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "waitfor %s", fileName );
|
||||
|
||||
resultPoint = xte_waitForImage_location( displayLocation, fileName, searchMethod, tolerance, timeout );
|
||||
|
||||
free(fileName);
|
||||
}
|
||||
|
||||
/* Note that we will return (-1,-1) implicitly
|
||||
* if we don't recognize the argument */
|
||||
|
Loading…
Reference in New Issue
Block a user