Flesh out the cva-input program, and add calls to XFlush()

This commit is contained in:
Bradlee Speice 2012-06-26 16:20:44 -04:00
parent 8c1ab45c26
commit b48f3e636c
3 changed files with 223 additions and 7 deletions

View File

@ -18,15 +18,41 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <limits.h>
#include <libcvautomation/libcvautomation.h>
void usage (); void usage ();
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
/* 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;
useMouseImage = False;
int mouseButton;
mouseButton = 1;
char *keypress;
keypress = NULL;
char *xDisplayLocation;
xDisplayLocation = "";
Display *display;
display = NULL;
Bool useCenter;
useCenter = False;
int searchMethod, tolerance;
searchMethod = 0;
tolerance = INT_MAX;
/* Start getopt */ /* Start getopt */
while (1) while (1)
{ {
@ -34,6 +60,17 @@ 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'},
{"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'},
{"search-method",required_argument, 0, 'm'},
{"tolerance", required_argument, 0, 't'},
{"button", required_argument, 0, 'b'},
{"center", no_argument, 0, 'c'},
/* 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}
@ -42,7 +79,7 @@ int main( int argc, char** argv )
int option_index = 0; int option_index = 0;
opterr = 0; opterr = 0;
int c = getopt_long (argc, argv, "hu", /* Use a single colon for required_argument, int c = getopt_long (argc, argv, "hux:y:r:s:i:k:", /* 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);
@ -63,6 +100,60 @@ int main( int argc, char** argv )
usage(); usage();
break; break;
case 'd':
xDisplayLocation = optarg;
case 'x':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
currentLocation = xte_pointerLocation( display );
mouseXMovement = atoi(optarg) - currentLocation.x;
break;
case 'y':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
currentLocation = xte_pointerLocation( display );
mouseYMovement = atoi(optarg) - currentLocation.y;
break;
case 'r':
mouseXMovement = atoi(optarg);
break;
case 's':
mouseYMovement = atoi(optarg);
break;
case 'i':
useMouseImage = True;
mouseImage = optarg;
break;
case 'k':
keypress = optarg;
break;
case 'm':
searchMethod = atoi(optarg);
break;
case 't':
tolerance = atoi(optarg);
break;
case 'b':
mouseButton = atoi(optarg);
break;
case 'c':
useCenter = True;
break;
case '?': case '?':
/* Error routine */ /* Error routine */
break; break;
@ -73,6 +164,41 @@ int main( int argc, char** argv )
}; };
} }
/* If we haven't opened our display yet, do that now.
* Note that we will only ever open one display due to
* checks implemented in the optarg section. */
if (display == NULL)
display = XOpenDisplay( xDisplayLocation );
if (! xte_XTestSupported( display ))
{
printf("The XTest extension is not supported! Aborting...");
exit(255);
}
if (useMouseImage)
{
if (useCenter)
xte_clickMouseImage_location( display, mouseImage, mouseButton, searchMethod, tolerance );
else
xte_clickMouseImage_location_center( display, mouseImage, mouseButton, searchMethod, tolerance );
}
else if (keypress != NULL)
xte_clickKey( display, keypress );
else
{
if (mouseXMovement == INT_MAX)
mouseXMovement = 0;
if (mouseYMovement == INT_MAX)
mouseYMovement = 0;
printf("%i, %i\n", mouseXMovement, mouseYMovement);
xte_clickMouseRXY( display, mouseXMovement, mouseYMovement, mouseButton );
}
return 0; return 0;
} }
@ -85,16 +211,33 @@ int main( int argc, char** argv )
void usage ( ) void usage ( )
{ {
fprintf( stderr, "\n\ fprintf( stderr, "\n\
<program_name> version <program_version>\n\ cva-input -i <image_file>\n\
cva-input -x <x_location> -y <y_location>\n\
cva-input -k <key>\n\
\n\ \n\
Put your usage or help text here.\n\ The cva-input program demonstrates the XTest section of libcvautomation.\n\
\n\ \n\
Usage: \n\ Usage: \n\
\n\ \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-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\t\t\tfor more information on this.\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-c, --center:\t\tInstead of matching the top-left corner of an image,\n\
\t\t\t\tmatch the center of the image.\n\
\n\ \n\
If you have any questions, comments, concerns, email somebody else.\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 ); int 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 );

View File

@ -18,6 +18,10 @@
#include <libcvautomation/libcvautomation-xtest.h> #include <libcvautomation/libcvautomation-xtest.h>
/* 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. */
/* /*
* === FUNCTION ====================================================================== * === FUNCTION ======================================================================
* Name: xte_xTestSupported * Name: xte_xTestSupported
@ -71,6 +75,8 @@ void xte_clickMouse ( Display *displayLocation, int mouseButton )
{ {
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -92,6 +98,8 @@ void xte_clickMouseXY ( Display *displayLocation, int xLocation, int yLocation,
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -105,6 +113,8 @@ void xte_clickMouseRXY ( Display *displayLocation, int xIncrement, int yIncremen
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -124,6 +134,10 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
resultPoint = matchSubImage_X11 ( displayLocation, subImage, searchMethod, tolerance ); resultPoint = matchSubImage_X11 ( displayLocation, subImage, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -135,6 +149,8 @@ void xte_clickMouseImage ( Display *displayLocation, IplImage *subImage, int mou
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -150,6 +166,10 @@ void xte_clickMouseImage_location ( Display *displayLocation, const char *fileNa
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance ); resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -161,6 +181,8 @@ void xte_clickMouseImage_location ( Display *displayLocation, const char *fileNa
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -180,6 +202,10 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
resultPoint = matchSubImage_X11_center ( displayLocation, subImage, searchMethod, tolerance ); resultPoint = matchSubImage_X11_center ( displayLocation, subImage, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -192,6 +218,7 @@ void xte_clickMouseImage_center ( Display *displayLocation, IplImage *subImage,
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -207,6 +234,10 @@ void xte_clickMouseImage_location_center ( Display *displayLocation, const char
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance ); resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -218,6 +249,8 @@ void xte_clickMouseImage_location_center ( Display *displayLocation, const char
XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, movementX, movementY, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 0, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -236,6 +269,8 @@ void xte_hoverMouseXY ( Display *displayLocation, int xLocation, int yLocation )
yIncrement = yLocation - pointerLocation.y; yIncrement = yLocation - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -248,6 +283,8 @@ void xte_hoverMouseXY ( Display *displayLocation, int xLocation, int yLocation )
void xte_hoverMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement ) void xte_hoverMouseRXY ( Display *displayLocation, int xIncrement, int yIncrement )
{ {
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -262,6 +299,10 @@ void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int sea
CvPoint resultPoint; CvPoint resultPoint;
resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance ); resultPoint = matchSubImage_X11( displayLocation, subImage, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -270,6 +311,8 @@ void xte_hoverMouseImage ( Display *displayLocation, IplImage *subImage, int sea
yIncrement = resultPoint.y - pointerLocation.y; yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -284,6 +327,10 @@ void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileNa
CvPoint resultPoint; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance ); resultPoint = matchSubImage_X11_location( displayLocation, fileName, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -292,6 +339,8 @@ void xte_hoverMouseImage_location ( Display *displayLocation, const char *fileNa
yIncrement = resultPoint.y - pointerLocation.y; yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -306,6 +355,10 @@ void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage,
CvPoint resultPoint; CvPoint resultPoint;
resultPoint = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance ); resultPoint = matchSubImage_X11_center( displayLocation, subImage, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -314,6 +367,8 @@ void xte_hoverMouseImage_center ( Display *displayLocation, IplImage *subImage,
yIncrement = resultPoint.y - pointerLocation.y; yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -328,6 +383,10 @@ void xte_hoverMouseImage_location_center ( Display *displayLocation, const char
CvPoint resultPoint; CvPoint resultPoint;
resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance ); resultPoint = matchSubImage_X11_location_center( displayLocation, fileName, searchMethod, tolerance );
if (resultPoint.x == -1 && resultPoint.y == -1)
/* Match not found */
return;
cvaPoint pointerLocation; cvaPoint pointerLocation;
pointerLocation = xte_pointerLocation( displayLocation ); pointerLocation = xte_pointerLocation( displayLocation );
@ -336,6 +395,8 @@ void xte_hoverMouseImage_location_center ( Display *displayLocation, const char
yIncrement = resultPoint.y - pointerLocation.y; yIncrement = resultPoint.y - pointerLocation.y;
XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, xIncrement, yIncrement, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -347,6 +408,8 @@ void xte_hoverMouseImage_location_center ( Display *displayLocation, const char
void xte_mouseDown ( Display *displayLocation, int mouseButton ) void xte_mouseDown ( Display *displayLocation, int mouseButton )
{ {
XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, 1, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -358,6 +421,8 @@ void xte_mouseDown ( Display *displayLocation, int mouseButton )
void xte_mouseUp ( Display *displayLocation, int mouseButton ) void xte_mouseUp ( Display *displayLocation, int mouseButton )
{ {
XTestFakeButtonEvent( displayLocation, mouseButton, False, CurrentTime ); XTestFakeButtonEvent( displayLocation, mouseButton, False, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -370,6 +435,8 @@ void xte_mouseJiggle ( Display *displayLocation )
{ {
XTestFakeRelativeMotionEvent( displayLocation, 1, 1, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, 1, 1, CurrentTime );
XTestFakeRelativeMotionEvent( displayLocation, -1, -1, CurrentTime ); XTestFakeRelativeMotionEvent( displayLocation, -1, -1, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -393,6 +460,8 @@ void xte_clickKey ( Display *displayLocation, char *key )
XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime ); XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime );
XTestFakeKeyEvent( displayLocation, kc, False, CurrentTime ); XTestFakeKeyEvent( displayLocation, kc, False, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -425,6 +494,8 @@ void xte_keyDown ( Display *displayLocation, char *key )
kc = XKeysymToKeycode( displayLocation, ks ); kc = XKeysymToKeycode( displayLocation, ks );
XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime ); XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime );
XFlush( displayLocation );
} }
/* /*
@ -445,4 +516,6 @@ void xte_keyUp ( Display *displayLocation, char *key )
kc = XKeysymToKeycode( displayLocation, ks ); kc = XKeysymToKeycode( displayLocation, ks );
XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime ); XTestFakeKeyEvent( displayLocation, kc, True, CurrentTime );
XFlush( displayLocation );
} }