Change behavior of cva-input to rely on getopt for what to do

This commit is contained in:
Bradlee Speice 2012-06-27 10:28:42 -04:00
parent b48f3e636c
commit deff35b221

View File

@ -25,6 +25,7 @@
#include <libcvautomation/libcvautomation.h> #include <libcvautomation/libcvautomation.h>
void usage (); void usage ();
void checkXTEEnabled ();
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
@ -67,6 +68,7 @@ int main( int argc, char** argv )
{"mover-mousey",required_argument, 0, 's'}, {"mover-mousey",required_argument, 0, 's'},
{"movei-mouse", required_argument, 0, 'i'}, {"movei-mouse", required_argument, 0, 'i'},
{"keypress", required_argument, 0, 'k'}, {"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'},
@ -79,7 +81,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, "hux:y:r:s:i:k:", /* Use a single colon for required_argument, int c = getopt_long (argc, argv, "hux:y:r:s:i:k:l::", /* 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);
@ -101,41 +103,76 @@ int main( int argc, char** argv )
break; break;
case 'd': case 'd':
xDisplayLocation = optarg; if (display == NULL)
display = XOpenDisplay( optarg );
else
{
XCloseDisplay( display );
XOpenDisplay( optarg );
}
case 'x': case 'x':
if ( display == NULL ) if ( display == NULL )
display = XOpenDisplay( xDisplayLocation ); display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled( display );
currentLocation = xte_pointerLocation( display ); currentLocation = xte_pointerLocation( display );
mouseXMovement = atoi(optarg) - currentLocation.x; xte_hoverMouseXY( display, atoi(optarg), currentLocation.y );
break; break;
case 'y': case 'y':
if ( display == NULL ) if ( display == NULL )
display = XOpenDisplay( xDisplayLocation ); display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled();
currentLocation = xte_pointerLocation( display ); currentLocation = xte_pointerLocation( display );
mouseYMovement = atoi(optarg) - currentLocation.y; xte_hoverMouseXY( display, currentLocation.x, atoi(optarg) );
break; break;
case 'r': case 'r':
mouseXMovement = atoi(optarg); if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled();
currentLocation = xte_pointerLocation( display );
xte_hoverMouseRXY( display, atoi(optarg), 0 );
break; break;
case 's': case 's':
mouseYMovement = atoi(optarg); if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled();
currentLocation = xte_pointerLocation( display );
xte_hoverMouseRXY( display, 0, atoi(optarg) );
break; break;
case 'i': case 'i':
useMouseImage = True; if ( display == NULL )
mouseImage = optarg; 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; break;
case 'k': case 'k':
keypress = optarg; if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled();
xte_clickKey( display, optarg );
break; break;
case 'm': case 'm':
@ -154,6 +191,14 @@ int main( int argc, char** argv )
useCenter = True; useCenter = True;
break; break;
case 'l':
if ( display == NULL )
display = XOpenDisplay( xDisplayLocation );
checkXTEEnabled();
xte_clickMouse( display, mouseButton );
case '?': case '?':
/* Error routine */ /* Error routine */
break; break;
@ -164,40 +209,8 @@ int main( int argc, char** argv )
}; };
} }
/* If we haven't opened our display yet, do that now. if ( display != NULL )
* Note that we will only ever open one display due to XCloseDisplay( display );
* 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;
} }
@ -230,16 +243,30 @@ Usage: \n\
\t\t\t\tBy default, the program will click the top-left corner of the image.\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\t\t\tUse the \"-c\" switch to change this.\n\
\t-k, --keypress:\t\tSpecify a key to press.\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\
\n\ \n\
This program works kind of like a mini-language using getopt. All options\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\
given display.\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);
} /* ----- end of function usage ----- */ } /* ----- end of function usage ----- */
void checkXTEEnabled ( Display *display )
{
/* Make sure we have the XTest Extensions enabled.
* This is a quick wrapper. */
if (! xte_XTestSupported( display ))
{
printf("The XTest extension is not supported! Aborting...");
exit(255);
}
}