Add functionality to input a command string

This commit is contained in:
Bradlee Speice 2012-06-28 12:12:30 -04:00
parent 2c41ec3bca
commit 9dc1d24926
4 changed files with 290 additions and 171 deletions

View File

@ -30,25 +30,16 @@ void checkXTEEnabled ();
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
/* Set up for getopt */ /* Set up for getopt */
int mouseXMovement, mouseYMovement;
/* Safe to say that nobody will ever need to move INT_MAX
* pixels. INT_MAX is used to signify an uninitialized value. */
mouseXMovement = mouseYMovement = INT_MAX;
cvaPoint currentLocation;
char *mouseImage;
mouseImage = NULL;
Bool useMouseImage; Bool useMouseImage;
useMouseImage = False; useMouseImage = False;
int mouseButton; int mouseButton;
mouseButton = 1; mouseButton = 1;
char *keypress; char *separator;
keypress = NULL; separator = ",";
char *xDisplayLocation; char *xDisplayLocation;
xDisplayLocation = ""; xDisplayLocation = "";
Display *display; Display *display;
display = NULL; display = NULL;
Bool useCenter;
useCenter = False;
int searchMethod, tolerance; int searchMethod, tolerance;
searchMethod = 0; searchMethod = 0;
@ -62,21 +53,11 @@ int main( int argc, char** argv )
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"usage", no_argument, 0, 'u'}, {"usage", no_argument, 0, 'u'},
{"display", required_argument, 0, 'd'}, {"display", required_argument, 0, 'd'},
{"move-mousex", required_argument, 0, 'x'},
{"move-mousey", required_argument, 0, 'y'},
{"mover-mousex",required_argument, 0, 'r'},
{"mover-mousey",required_argument, 0, 's'},
{"movei-mouse", required_argument, 0, 'i'},
{"keypress", required_argument, 0, 'k'},
{"click", optional_argument, 0, 'l'},
{"search-method",required_argument, 0, 'm'}, {"search-method",required_argument, 0, 'm'},
{"tolerance", required_argument, 0, 't'}, {"tolerance", required_argument, 0, 't'},
{"button", required_argument, 0, 'b'}, {"button", required_argument, 0, 'b'},
{"center", no_argument, 0, 'c'}, {"string", required_argument, 0, 's'},
{"jiggle", no_argument, 0, 'j'}, {"print-format",required_argument, 0, 'p'},
{"string", required_argument, 0, 'n'},
{"keydown", required_argument, 0, 'o'},
{"keyup", required_argument, 0, 'p'},
/* Other valid values are "optional_argument" /* Other valid values are "optional_argument"
* and "required_argument" */ * and "required_argument" */
{0, 0, 0, 0} {0, 0, 0, 0}
@ -85,8 +66,8 @@ int main( int argc, char** argv )
int option_index = 0; int option_index = 0;
opterr = 0; opterr = 0;
int c = getopt_long (argc, argv, "hux:y:r:s:i:k:l::", /* Use a single colon for required_argument, int c = getopt_long (argc, argv, "hud:m:t:b:cs:", /* Use a single colon for required_argument,
* double colon for optional_argument */ * double colon for optional_argument */
long_options, &option_index); long_options, &option_index);
/* We're done with parsing options */ /* We're done with parsing options */
@ -115,70 +96,6 @@ int main( int argc, char** argv )
XOpenDisplay( optarg ); XOpenDisplay( optarg );
} }
case 'x':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
currentLocation = xte_pointerLocation( display );
xte_hoverMouseXY( display, atoi(optarg), currentLocation.y );
break;
case 'y':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
currentLocation = xte_pointerLocation( display );
xte_hoverMouseXY( display, currentLocation.x, atoi(optarg) );
break;
case 'r':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
currentLocation = xte_pointerLocation( display );
xte_hoverMouseRXY( display, atoi(optarg), 0 );
break;
case 's':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
currentLocation = xte_pointerLocation( display );
xte_hoverMouseRXY( display, 0, atoi(optarg) );
break;
case 'i':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
if (useCenter)
xte_clickMouseImage_location_center( display, optarg, mouseButton, searchMethod, tolerance );
else
xte_clickMouseImage_location( display, optarg, mouseButton, searchMethod, tolerance );
break;
case 'k':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_clickKey( display, optarg );
break;
case 'm': case 'm':
searchMethod = atoi(optarg); searchMethod = atoi(optarg);
break; break;
@ -191,50 +108,18 @@ int main( int argc, char** argv )
mouseButton = atoi(optarg); mouseButton = atoi(optarg);
break; break;
case 'c': case 's':
useCenter = True; if (display == NULL)
display = XOpenDisplay( xDisplayLocation );
cvaPoint returnPoint;
returnPoint = xte_commandString( display, optarg, mouseButton, searchMethod, tolerance );
if (returnPoint.x != -1 && returnPoint.y != -1)
printf("%s%s%i%s%i\n", optarg, separator, returnPoint.x, separator, returnPoint.y);
else
printf("%s\n", optarg);
break; break;
case 'l':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_clickMouse( display, mouseButton );
case 'j':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_mouseJiggle( display );
case 'n':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_clickKeyStr ( display, optarg );
case 'o':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_keyDown( display, optarg );
case 'p':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
xte_keyUp( display, optarg );
case '?': case '?':
/* Error routine */ /* Error routine */
break; break;
@ -271,25 +156,34 @@ Usage: \n\
\t-h, --help:\t\tDisplay this usage message.\n\ \t-h, --help:\t\tDisplay this usage message.\n\
\t-u, --usage:\t\tDisplay this usage message.\n\ \t-u, --usage:\t\tDisplay this usage message.\n\
\t-d, --display:\t\tSpecify the X display to use.\n\ \t-d, --display:\t\tSpecify the X display to use.\n\
\t-x, --move-mousex:\tSpecify the end location of the mouse (x-coordinate).\n\
\t-y, --move-mousey:\tSpecify the end location of the mouse (y-coordinate).\n\
\t-r, --mover-mousex:\tSpecify the distance in the X plane to move the mouse.\n\
\t-s, --mover-mousey:\tSpecify the distance in the Y plane to move the mouse.\n\
\t-i, --movei-mouse:\tSpecify an image to click.\n\
\t\t\t\tBy default, the program will click the top-left corner of the image.\n\
\t\t\t\tUse the \"-c\" switch to change this.\n\
\t-k, --keypress:\t\tSpecify a key to press.\n\
\t-m, --search-method:\tSpecify a method to search by. See `cva-match --help\'\n\ \t-m, --search-method:\tSpecify a method to search by. See `cva-match --help\'\n\
\t\t\t\tfor more information on this.\n\ \t\t\t\tfor more information on this.\n\
\t-t, --tolerance:\tSpecify how strict the match is.\n\ \t-t, --tolerance:\tSpecify how strict the match is.\n\
\t-b, --button:\t\tSpecify the mouse button to press (default 1).\n\ \t-b, --button:\t\tSpecify the mouse button to press (default 1).\n\
\t-c, --center:\t\tInstead of matching the top-left corner of an image,\n\ \t-c, --center:\t\tInstead of matching the top-left corner of an image,\n\
\t\t\t\tmatch the center of the image.\n\ \t\t\t\tmatch the center of the image.\n\
\t-s, --string:\t\tCommand string - see below.\n\
\n\ \n\
This program works kind of like a mini-language using getopt. All options\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\ are parsed left-to-right, and executed right there. Thus, specifying \"--display\"\n\
at different places in the options will cause this program to use the most recent\n\ at different places in the options will cause this program to use the most recent\n\
given display.\n\ given display.\n\
Available commands:\n\
\tmouseclick:\tClick the mouse in-place.\n\
\timouseclick:\tClick the mouse at an image's top-left corner.\n\
\ticmouseclick:\tClick the mouse at an image's center.\n\
\tmousexy:\tMove the mouse to the given coordinate.\n\
\tmouserxy:\tMove the mouse by the given x and y values (relative motion).\n\
\tmouseimage:\tMove the mouse to an image's top-left corner.\n\
\tcmouseimage:\tMove the mouse to an image's center.\n\
\tmousedown:\tPush and leave down a mouse button.\n\
\tmouseup:\tRelease a mouse button.\n\
\tmousejiggle:\tJiggle the mouse (helps to activate some widgets).\n\
\tkeyclick:\tClick a keyboard button.\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\
\n\
If you have any questions, comments, concerns, email <bspeice@uncc.edu>.\n\n" ); If you have any questions, comments, concerns, email <bspeice@uncc.edu>.\n\n" );
exit (0); exit (0);

View File

@ -23,7 +23,7 @@
/* Make sure that the XTest extension is supported. /* Make sure that the XTest extension is supported.
* If it's not, return 0 (false) */ * If it's not, return 0 (false) */
int xte_XTestSupported ( Display *displayLocation ); Bool xte_XTestSupported ( Display *displayLocation );
/* Get the current location of the pointer */ /* Get the current location of the pointer */
cvaPoint xte_pointerLocation ( Display *displayLocation ); cvaPoint xte_pointerLocation ( Display *displayLocation );
@ -38,18 +38,18 @@ void xte_clickMouseXY ( Display *displayLocation, int xLocation, int yLocation,
void xte_clickMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement, int mouseButton ); void xte_clickMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement, int mouseButton );
/* Click the mouse at the top-left corner of an image on the specified display */ /* 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 );
/* Click the mouse at the top-left corner of an image on the specified display /* Click the mouse at the top-left corner of an image on the specified display
* where the subImage is a file location */ * 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 );
/* Click the mouse at the center of an image on the specified display */ /* 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 );
/* Click the mouse at the center of an image on the specified display /* Click the mouse at the center of an image on the specified display
* where the subImage is a file location */ * 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 );
/* Move the mouse to a location and leave it there */ /* Move the mouse to a location and leave it there */
void xte_hoverMouseXY ( Display *displayLocation, int xLocation, int yLocation ); void xte_hoverMouseXY ( Display *displayLocation, int xLocation, int yLocation );
@ -59,19 +59,19 @@ void xte_hoverMouseRXY ( Display *displayLocation, int xIncrement, int yIncremen
/* Move the mouse to a location at the top-left corner of an image on the specified display /* Move the mouse to a location at the top-left corner of an image on the specified display
* but don't click the mouse */ * 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 );
/* Move the mouse to a location at the top-left corner of an image from file on the specified display /* Move the mouse to a location at the top-left corner of an image from file on the specified display
* but don't click the mouse */ * 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 );
/* Move the mouse to a location at the center of an image on the specified display /* Move the mouse to a location at the center of an image on the specified display
* but don't click the mouse */ * 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 );
/* Move the mouse to a location at the center of an image from file on the specified display /* Move the mouse to a location at the center of an image from file on the specified display
* but don't click the mouse */ * 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 );
/* Push a mouse button down, but don't release it */ /* Push a mouse button down, but don't release it */
void xte_mouseDown ( Display *displayLocation, int mouseButton ); void xte_mouseDown ( Display *displayLocation, int mouseButton );
@ -87,7 +87,7 @@ void xte_mouseJiggle ( Display *displayLocation );
void xte_clickKey ( Display *displayLocation, char *key ); void xte_clickKey ( Display *displayLocation, char *key );
/* Press and release keys in a string */ /* Press and release keys in a string */
void xte_clickKeyStr ( Display *displayLocation, const char *string ); void xte_clickKeyStr ( Display *displayLocation, char *string );
/* Press a key down */ /* Press a key down */
void xte_keyDown ( Display *displayLocation, char *key ); void xte_keyDown ( Display *displayLocation, char *key );
@ -95,4 +95,7 @@ void xte_keyDown ( Display *displayLocation, char *key );
/* Release a key */ /* Release a key */
void xte_keyUp ( Display *displayLocation, char *key ); void xte_keyUp ( Display *displayLocation, char *key );
/* Use one of the functions by command name */
cvaPoint xte_commandString ( Display *displayLocation, char *command, int mouseButton, int searchMethod, int tolerance );
#endif /* LIBCVAUTOMATION_XTEST_H */ #endif /* LIBCVAUTOMATION_XTEST_H */

View File

@ -20,6 +20,7 @@
/* C includes */ /* C includes */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#ifdef USEOPENCV2 #ifdef USEOPENCV2
/* OpenCV2 includes - some filenames are different */ /* OpenCV2 includes - some filenames are different */

View File

@ -18,6 +18,9 @@
#include <libcvautomation/libcvautomation-xtest.h> #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, /* 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 * but I've found in testing that events don't get done correctly unless
* we do. I've included the XFlush() calls. */ * we do. I've included the XFlush() calls. */
@ -29,7 +32,7 @@
* If it's not, return 0 (false) * If it's not, return 0 (false)
* ===================================================================================== * =====================================================================================
*/ */
int xte_XTestSupported ( Display *displayLocation ) Bool xte_XTestSupported ( Display *displayLocation )
{ {
int event_basep, error_basep, majorp, minorp; int event_basep, error_basep, majorp, minorp;
return XTestQueryExtension( displayLocation, &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 * 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 - /* This one is a bit more in-depth -
* Find where the image is at * 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 ); resultPoint = matchSubImage_X11 ( displayLocation, subImage, searchMethod, tolerance );
cvaPoint result;
result.x = resultPoint.x;
result.y = resultPoint.y;
if (resultPoint.x == -1 && resultPoint.y == -1) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -151,6 +158,8 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation ); XFlush( displayLocation );
return result;
} }
/* /*
@ -160,15 +169,19 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
* where the subImage is a file location * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -183,6 +196,8 @@ void xte_clickMouseImage_location ( Display *displayLocation, const char *fileNa
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation ); 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 * 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 - /* This one is a bit more in-depth -
* Find where the image is at * 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 ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -219,6 +238,8 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation ); XFlush( displayLocation );
return result;
} }
/* /*
@ -228,15 +249,19 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
* where the subImage is a file location * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -251,6 +276,8 @@ void xte_clickMouseImage_location_center ( Display *displayLocation, const char
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation ); 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 * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance ); resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance );
cvaPoint result;
result.x = resultPoint.x;
result.y = resultPoint.y;
if (resultPoint.x == -1 && resultPoint.y == -1) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -313,6 +344,8 @@ void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int sea
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation ); 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 * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -341,6 +378,8 @@ void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileNa
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation ); 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 * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -369,6 +412,8 @@ void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage,
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation ); 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 * 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; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance ); 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) if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */ /* Match not found */
return; return result;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -397,6 +446,8 @@ void xte_hoverMouseImage_location_center ( Display *displayLocation, const char
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation ); XFlush( displayLocation );
return result;
} }
/* /*
@ -470,10 +521,11 @@ void xte_clickKey ( Display *displayLocation, char *key )
* Description: Press and release keys in a string * 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 ); 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;
}