mirror of
https://github.com/bspeice/libcvautomation
synced 2025-07-02 14:26:48 -04:00
Add functionality to input a command string
This commit is contained in:
@ -18,6 +18,9 @@
|
||||
|
||||
#include <libcvautomation/libcvautomation-xtest.h>
|
||||
|
||||
#define IS_CMD( x, y ) strncmp( x, y, strlen( y ) ) == 0
|
||||
#define COMMAND_STR_LEN 512
|
||||
|
||||
/* Note: The XLib documentation says that we shouldn't need to XFlush,
|
||||
* but I've found in testing that events don't get done correctly unless
|
||||
* we do. I've included the XFlush() calls. */
|
||||
@ -29,7 +32,7 @@
|
||||
* If it's not, return 0 (false)
|
||||
* =====================================================================================
|
||||
*/
|
||||
int xte_XTestSupported ( Display *displayLocation )
|
||||
Bool xte_XTestSupported ( Display *displayLocation )
|
||||
{
|
||||
int event_basep, error_basep, majorp, minorp;
|
||||
return XTestQueryExtension( displayLocation, &event_basep, &error_basep, &majorp, &minorp );
|
||||
@ -123,7 +126,7 @@ void xte_clickMouseRXY ( Display *displayLocation, int xIncrement, int yIncremen
|
||||
* Description: Click the mouse at the top-left corner of an image on the specified display
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
|
||||
cvaPoint xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
|
||||
{
|
||||
/* This one is a bit more in-depth -
|
||||
* Find where the image is at
|
||||
@ -134,9 +137,13 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
|
||||
|
||||
resultPoint = matchSubImage_X11 ( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -151,6 +158,8 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
|
||||
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -160,15 +169,19 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
|
||||
* where the subImage is a file location
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_clickMouseImage_location ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
|
||||
cvaPoint xte_clickMouseImage_location ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
|
||||
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -183,6 +196,8 @@ void xte_clickMouseImage_location ( Display *displayLocation, const char *fileNa
|
||||
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -191,7 +206,7 @@ void xte_clickMouseImage_location ( Display *displayLocation, const char *fileNa
|
||||
* Description: Click the mouse at the center of an image on the specified display
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
|
||||
cvaPoint xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage, int mouseButton, int searchMethod, int tolerance )
|
||||
{
|
||||
/* This one is a bit more in-depth -
|
||||
* Find where the image is at
|
||||
@ -202,9 +217,13 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
|
||||
|
||||
resultPoint = matchSubImage_X11_center ( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -219,6 +238,8 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
|
||||
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -228,15 +249,19 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
|
||||
* where the subImage is a file location
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_clickMouseImage_location_center ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
|
||||
cvaPoint xte_clickMouseImage_location_center ( Display *displayLocation, const char *fileName, int mouseButton, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
|
||||
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -251,6 +276,8 @@ void xte_clickMouseImage_location_center ( Display *displayLocation, const char
|
||||
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -294,14 +321,18 @@ void xte_hoverMouseRXY ( Display *displayLocation, int xIncrement, int yIncremen
|
||||
* the specified display but don't click the mouse
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
|
||||
cvaPoint xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -313,6 +344,8 @@ void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int sea
|
||||
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -322,14 +355,18 @@ void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int sea
|
||||
* file on the specified display but don't click the mouse
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
|
||||
cvaPoint xte_hoverMouseImage_location ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -341,6 +378,8 @@ void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileNa
|
||||
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -350,14 +389,18 @@ void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileNa
|
||||
* specified display but don't click the mouse
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
|
||||
cvaPoint xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
resultPoint = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -369,6 +412,8 @@ void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage,
|
||||
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -378,14 +423,18 @@ void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage,
|
||||
* the specified display but don't click the mouse
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_hoverMouseImage_location_center ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
|
||||
cvaPoint xte_hoverMouseImage_location_center ( Display *displayLocation, const char *fileName, int searchMethod, int tolerance )
|
||||
{
|
||||
CvPoint resultPoint;
|
||||
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
cvaPoint result;
|
||||
result.x = resultPoint.x;
|
||||
result.y = resultPoint.y;
|
||||
|
||||
if (resultPoint.x == -1 && resultPoint.y == -1)
|
||||
/* Match not found */
|
||||
return;
|
||||
return result;
|
||||
|
||||
cvaPoint pointerLocation;
|
||||
pointerLocation = xte_pointerLocation( displayLocation );
|
||||
@ -397,6 +446,8 @@ void xte_hoverMouseImage_location_center ( Display *displayLocation, const char
|
||||
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
|
||||
|
||||
XFlush( displayLocation );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -470,10 +521,11 @@ void xte_clickKey ( Display *displayLocation, char *key )
|
||||
* Description: Press and release keys in a string
|
||||
* =====================================================================================
|
||||
*/
|
||||
void xte_clickKeyStr ( Display *displayLocation, const char *string )
|
||||
void xte_clickKeyStr ( Display *displayLocation, char *string )
|
||||
{
|
||||
/* TODO: Code the function to convert a string to key presses */
|
||||
|
||||
/* TODO: Write the code to implement a function that allows you to enter a string
|
||||
* at a time to X, rather than a single character.*/
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -519,3 +571,172 @@ void xte_keyUp ( Display *displayLocation, char *key )
|
||||
|
||||
XFlush( displayLocation );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* === FUNCTION ======================================================================
|
||||
* Name: xte_commandString
|
||||
* Description: Use one of the functions by command name
|
||||
* =====================================================================================
|
||||
*/
|
||||
cvaPoint xte_commandString ( Display *displayLocation, char *commandString, int mouseButton, int searchMethod, int tolerance )
|
||||
{
|
||||
/* Alright, this function bears a bit of talking about.
|
||||
* The way it works is that you give the **command two parameters:
|
||||
* [0]: The command
|
||||
* [1]: The command argument
|
||||
* And what happens is that I test here for the command, and pass it off.
|
||||
* This functionality was inspired by xte from xautomation,
|
||||
* 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,
|
||||
* but they're here to make sure that they're available if needed. */
|
||||
|
||||
/* Perform basic sanity checking */
|
||||
if (commandString == NULL)
|
||||
return;
|
||||
|
||||
/* And now we sanitize the input */
|
||||
char *s_commandString;
|
||||
s_commandString = (char*)malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
memset(s_commandString, '\0', COMMAND_STR_LEN * sizeof(char));
|
||||
|
||||
strncpy( s_commandString, commandString, COMMAND_STR_LEN - 1 );
|
||||
|
||||
/* And now to test for all of our options */
|
||||
if (IS_CMD( s_commandString, "mouseclick" ))
|
||||
{
|
||||
int mouseButton;
|
||||
sscanf( s_commandString, "mouseclick %i", &mouseButton );
|
||||
|
||||
xte_clickMouse( displayLocation, mouseButton );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mousexy" ))
|
||||
{
|
||||
int xLocation, yLocation;
|
||||
sscanf( s_commandString, "mousexy %i %i", &xLocation, &yLocation );
|
||||
|
||||
xte_hoverMouseXY( displayLocation, xLocation, yLocation );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mouserxy" ))
|
||||
{
|
||||
int xIncrement, yIncrement;
|
||||
sscanf( s_commandString, "mouserxy %i %i", &xIncrement, &yIncrement );
|
||||
|
||||
xte_hoverMouseRXY( displayLocation, xIncrement, yIncrement );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mouseimage" ))
|
||||
{
|
||||
char *fileName;
|
||||
fileName = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "mouseimage %s", fileName );
|
||||
|
||||
cvaPoint resultPoint;
|
||||
resultPoint = xte_hoverMouseImage_location( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
free(fileName);
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "cmouseimage" ))
|
||||
{
|
||||
char *fileName;
|
||||
fileName = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "cmouseimage %s", fileName );
|
||||
|
||||
xte_hoverMouseImage_location_center( displayLocation, fileName, searchMethod, tolerance );
|
||||
|
||||
free(fileName);
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "imouseclick" ))
|
||||
{
|
||||
char *fileName;
|
||||
fileName = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "imouseclick %s", fileName );
|
||||
|
||||
cvaPoint resultPoint;
|
||||
resultPoint = xte_clickMouseImage_location( displayLocation, fileName, mouseButton, searchMethod, tolerance );
|
||||
|
||||
free(fileName);
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "icmouseclick" ))
|
||||
{
|
||||
char *fileName;
|
||||
fileName = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "icmouseclick %s", fileName );
|
||||
|
||||
cvaPoint resultPoint;
|
||||
resultPoint = xte_clickMouseImage_location_center( displayLocation, fileName, mouseButton, searchMethod, tolerance );
|
||||
|
||||
free(fileName);
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mousedown" ))
|
||||
{
|
||||
int mouseButton;
|
||||
sscanf( s_commandString, "mousedown %i", &mouseButton );
|
||||
|
||||
xte_mouseDown( displayLocation, mouseButton );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mouseup" ))
|
||||
{
|
||||
int mouseButton;
|
||||
sscanf( s_commandString, "mouseup %i", &mouseButton );
|
||||
|
||||
xte_mouseUp( displayLocation, mouseButton );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "mousejiggle" ))
|
||||
{
|
||||
xte_mouseJiggle( displayLocation );
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "keyclick" ))
|
||||
{
|
||||
char *key;
|
||||
key = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "keyclick %s", key );
|
||||
|
||||
xte_clickKey( displayLocation, key );
|
||||
|
||||
free(key);
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "keydown" ))
|
||||
{
|
||||
char *key;
|
||||
key = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "keydown %s", key );
|
||||
|
||||
xte_keyDown( displayLocation, key );
|
||||
|
||||
free(key);
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "keyup" ))
|
||||
{
|
||||
char *key;
|
||||
key = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "keyup %s", key );
|
||||
|
||||
xte_keyUp( displayLocation, key );
|
||||
|
||||
free(key);
|
||||
}
|
||||
else if (IS_CMD( s_commandString, "keystring" ))
|
||||
{
|
||||
char *keyString;
|
||||
keyString = malloc(COMMAND_STR_LEN * sizeof(char));
|
||||
sscanf( s_commandString, "keystring %s", keyString );
|
||||
|
||||
xte_clickKeyStr( displayLocation, keyString );
|
||||
|
||||
free(keyString);
|
||||
}
|
||||
|
||||
cvaPoint resultPoint;
|
||||
resultPoint.x = -1;
|
||||
resultPoint.y = -1;
|
||||
|
||||
return resultPoint;
|
||||
}
|
||||
|
Reference in New Issue
Block a user