libcvautomation  1.2
cva-input.c
Go to the documentation of this file.
00001 /*
00002  * =====================================================================================
00003  *
00004  *       Filename:  cva-input.c
00005  *
00006  *    Description:  This is an example program to demonstrate the XTest and XInput
00007  *                  functionality in libcvautomation
00008  *
00009  *        Created:  06/26/2012 09:20:20 AM
00010  *       Revision:  none
00011  *       Compiler:  gcc
00012  *
00013  *         Author:  Bradlee Speice (), bspeice.nc@gmail.com
00014  *   Organization:  
00015  *
00016  * =====================================================================================
00017  */
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <unistd.h>
00021 #include <getopt.h>
00022 #include <limits.h>
00023 #include <math.h>
00024 
00025 #include <libcvautomation/libcvautomation.h>
00026 
00027 void usage ();
00028 void checkXTEEnabled (Display *display);
00029 
00030 int main( int argc, char** argv )
00031 {
00032     /* Set up for getopt */
00033     int mouseButton;
00034         mouseButton = 1;
00035     char *separator;
00036         separator = ",";
00037     char *xDisplayLocation;
00038         xDisplayLocation = "";
00039     Display *display;
00040         display = NULL;
00041 
00042     int searchMethod, tolerance;
00043     searchMethod = 0;
00044     tolerance = INT_MAX;
00045 
00046     /* Start getopt */
00047     while (1)
00048     {
00049         static struct option long_options[] =
00050         {
00051                 {"help",        no_argument,        0,  'h'},
00052                 {"usage",       no_argument,        0,  'u'},
00053                 {"version",     no_argument,        0,  'v'},
00054                 {"display",     required_argument,  0,  'd'},
00055                 {"search-method",required_argument, 0,  'm'},
00056                 {"tolerance",   required_argument,  0,  't'},
00057                 {"button",      required_argument,  0,  'b'},
00058                 {"string",      required_argument,  0,  's'},
00059                 {"sane-tolerance", required_argument, 0,'o'},
00060                 {"print-format",required_argument,  0,  'p'},
00061                 /* Other valid values are "optional_argument"
00062                  * and "required_argument" */
00063                 {0, 0, 0, 0}
00064         };
00065 
00066         int option_index = 0;
00067         opterr = 0;
00068 
00069         int c = getopt_long (argc, argv, "hud:m:t:b:cs:", /* Use a single colon for required_argument,
00070                                                            * double colon for optional_argument */
00071                             long_options, &option_index);
00072 
00073         /* We're done with parsing options */
00074         if (c == -1)
00075             break;
00076 
00077         switch (c)
00078         {
00079             case 0:
00080                 break;
00081 
00082             case 'h':
00083                 usage();
00084                 break;
00085 
00086             case 'u':
00087                 usage();
00088                 break;
00089 
00090             case 'v':
00091                 usage();
00092                 break;
00093 
00094             case 'd':
00095                 if (display == NULL)
00096                     display = XOpenDisplay( optarg );
00097                 else
00098                 {
00099                     XCloseDisplay( display );
00100                     XOpenDisplay( optarg );
00101                 }
00102 
00103             case 'm':
00104                 searchMethod = atoi(optarg);
00105                 break;
00106 
00107             case 't':
00108                 tolerance = atoi(optarg);
00109                 break;
00110 
00111             case 'b':
00112                 mouseButton = atoi(optarg);
00113                 break;
00114 
00115             case 's':
00116                 if (display == NULL)
00117                     display = XOpenDisplay( xDisplayLocation );
00118                 cvaPoint returnPoint;
00119                 returnPoint = xte_commandString( display, optarg, mouseButton, searchMethod, tolerance );
00120 
00121                 if (returnPoint.x != -1 && returnPoint.y != -1)
00122                     printf("%s%s%i%s%i\n", optarg, separator, returnPoint.x, separator, returnPoint.y);
00123                 else
00124                     printf("%s\n", optarg);
00125                 break;
00126 
00127             case 'o':
00128                 tolerance = atoi(optarg);
00129                 /* Provide a more sane way to configure tolerance:
00130                  * --sane-tolerance=100 ~= INT_MAX */
00131                 tolerance = pow(1.2397076, tolerance);
00132                 break;
00133     
00134             case '?':
00135                 /* Error routine */
00136                 break;
00137 
00138             default:
00139                 fprintf( stderr, "Unknown option..." );
00140                 exit(0);
00141         };
00142     }
00143 
00144     if ( display != NULL )
00145         XCloseDisplay( display );
00146 
00147     return 0;
00148 }
00149 
00150 /* 
00151  * ===  FUNCTION  ======================================================================
00152  *         Name:  usage
00153  *  Description:  I really shouldn't need to write this
00154  * =====================================================================================
00155  */
00156 void usage ( )
00157 {
00158     fprintf( stderr, "\
00159 Libcvautomation version: %s\n\
00160 cva-input -s <command_string>\n\
00161 \n\
00162 The cva-input program demonstrates the XTest section of libcvautomation.\n\
00163 \n\
00164 Usage: \n\
00165 \n\
00166 \t-h, --help:\t\tDisplay this usage message.\n\
00167 \t-u, --usage:\t\tDisplay this usage message.\n\
00168 \t-d, --display:\t\tSpecify the X display to use.\n\
00169 \t-m, --search-method:\tSpecify a method to search by. See `cva-match --help\'\n\
00170 \t\t\t\tfor more information on this.\n\
00171 \t-t, --tolerance:\tSpecify how strict the match is.\n\
00172 \t-b, --button:\t\tSpecify the mouse button to press (default 1).\n\
00173 \t-c, --center:\t\tInstead of matching the top-left corner of an image,\n\
00174 \t\t\t\tmatch the center of the image.\n\
00175 \t-o, --sane-tolerance:\tSet the tolerance using a scale of 1-100,\n\
00176 \t-s, --string:\t\tCommand string - see below.\n\
00177 \n\
00178 This program works kind of like a mini-language. All options\n\
00179 are parsed left-to-right, and executed right there. Thus, specifying \"--display\"\n\
00180 at different places in the options will cause this program to use the most recent\n\
00181 given display.\n\
00182 Available commands:\n\
00183 \tmouseclick:\tClick the mouse in-place.\n\
00184 \timouseclick:\tClick the mouse at an image's top-left corner.\n\
00185 \ticmouseclick:\tClick the mouse at an image's center.\n\
00186 \tmousexy:\tMove the mouse to the given coordinate.\n\
00187 \tmouserxy:\tMove the mouse by the given x and y values (relative motion).\n\
00188 \tmouseimage:\tMove the mouse to an image's top-left corner.\n\
00189 \tcmouseimage:\tMove the mouse to an image's center.\n\
00190 \tmousedown:\tPush and leave down a mouse button.\n\
00191 \tmouseup:\tRelease a mouse button.\n\
00192 \tmousejiggle:\tJiggle the mouse (helps to activate some widgets).\n\
00193 \tmousescrollu:\tScroll the mouse wheel up.\n\
00194 \tmousescrolld:\tScroll the mouse wheel down.\n\
00195 \tkeyclick:\tClick a keyboard button.\n\
00196 \tkeydown:\tPush and leave down a keyboard button.\n\
00197 \tkeyup:\tRelease a keyboard button.\n\
00198 \tkeystring:\tInput a string of keys to X11.\n\
00199 \n\
00200 If you have any questions, comments, concerns, email <%s>.\n\n", LIBCVAUTOMATION_VERSION, LIBCVAUTOMATION_BUGREPORT );
00201 
00202     exit (0);
00203 
00204 }       /* -----  end of function usage  ----- */
00205 
00206 void checkXTEEnabled ( Display *display )
00207 {
00208     /* Make sure we have the XTest Extensions enabled.
00209      * This is a quick wrapper. */
00210     if (! xte_XTestSupported( display ))
00211     {
00212         printf("The XTest extension is not supported! Aborting...");
00213         exit(255);
00214     }
00215 }
00216 
00217 /* Doxygen Information */
00224 /* The biggest purpose of documenting this code is to trick doxygen into making a man page for it. */
 All Classes Files Functions Defines