No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cva-input.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: cva-input.c
  5. *
  6. * Description: This is an example program to demonstrate the XTest and XInput
  7. * functionality in libcvautomation
  8. *
  9. * Created: 06/26/2012 09:20:20 AM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: Bradlee Speice (), bspeice.nc@gmail.com
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <getopt.h>
  22. #include <limits.h>
  23. #include <math.h>
  24. #include <libcvautomation/libcvautomation.h>
  25. void usage ();
  26. void checkXTEEnabled (Display *display);
  27. int main( int argc, char** argv )
  28. {
  29. /* Set up for getopt */
  30. int mouseButton;
  31. mouseButton = 1;
  32. char *separator;
  33. separator = ",";
  34. char *xDisplayLocation;
  35. xDisplayLocation = "";
  36. Display *display;
  37. display = NULL;
  38. int searchMethod, tolerance, timeout;
  39. searchMethod = 0;
  40. tolerance = INT_MAX;
  41. timeout = 5;
  42. int returnCode = 1;
  43. /* Start getopt */
  44. while (1)
  45. {
  46. static struct option long_options[] =
  47. {
  48. {"help", no_argument, 0, 'h'},
  49. {"usage", no_argument, 0, 'u'},
  50. {"version", no_argument, 0, 'v'},
  51. {"display", required_argument, 0, 'd'},
  52. {"search-method",required_argument, 0, 'm'},
  53. {"tolerance", required_argument, 0, 't'},
  54. {"button", required_argument, 0, 'b'},
  55. {"string", required_argument, 0, 's'},
  56. {"sane-tolerance", required_argument, 0,'o'},
  57. {"print-format",required_argument, 0, 'p'},
  58. {"timeout", required_argument, 0, 'i'},
  59. /* Other valid values are "optional_argument"
  60. * and "required_argument" */
  61. {0, 0, 0, 0}
  62. };
  63. int option_index = 0;
  64. opterr = 0;
  65. int c = getopt_long (argc, argv, "hud:m:t:b:cs:", /* Use a single colon for required_argument,
  66. * double colon for optional_argument */
  67. long_options, &option_index);
  68. /* We're done with parsing options */
  69. if (c == -1)
  70. break;
  71. switch (c)
  72. {
  73. case 0:
  74. break;
  75. case 'h':
  76. usage();
  77. break;
  78. case 'u':
  79. usage();
  80. break;
  81. case 'v':
  82. usage();
  83. break;
  84. case 'd':
  85. if (display == NULL)
  86. display = XOpenDisplay( optarg );
  87. else
  88. {
  89. XCloseDisplay( display );
  90. XOpenDisplay( optarg );
  91. }
  92. case 'm':
  93. searchMethod = atoi(optarg);
  94. break;
  95. case 't':
  96. tolerance = atoi(optarg);
  97. break;
  98. case 'b':
  99. mouseButton = atoi(optarg);
  100. break;
  101. case 's':
  102. if (display == NULL)
  103. display = XOpenDisplay( xDisplayLocation );
  104. cvaPoint returnPoint;
  105. returnPoint = xte_commandString( display, optarg, mouseButton, searchMethod, tolerance, timeout );
  106. if (returnPoint.x == -2 && returnPoint.y == -2)
  107. {
  108. /* Not an error, just that the command didn't use returnPoint */
  109. printf("%s\n", optarg);
  110. returnCode = 0;
  111. }
  112. else if (returnPoint.x != -1 && returnPoint.y != -1)
  113. {
  114. printf("%s%s%i%s%i\n", optarg, separator, returnPoint.x, separator, returnPoint.y);
  115. returnCode = 0;
  116. }
  117. break;
  118. case 'o':
  119. tolerance = atoi(optarg);
  120. /* Provide a more sane way to configure tolerance:
  121. * --sane-tolerance=100 ~= INT_MAX */
  122. tolerance = pow(1.2397076, tolerance);
  123. break;
  124. case 'i':
  125. timeout = atoi(optarg);
  126. break;
  127. case '?':
  128. /* Error routine */
  129. break;
  130. default:
  131. fprintf( stderr, "Unknown option..." );
  132. exit(0);
  133. };
  134. }
  135. if ( display != NULL )
  136. XCloseDisplay( display );
  137. return returnCode;
  138. }
  139. /*
  140. * === FUNCTION ======================================================================
  141. * Name: usage
  142. * Description: I really shouldn't need to write this
  143. * =====================================================================================
  144. */
  145. void usage ( )
  146. {
  147. fprintf( stderr, "\
  148. Libcvautomation version: %s\n\
  149. cva-input -s <command_string>\n\
  150. \n\
  151. The cva-input program demonstrates the XTest section of libcvautomation.\n\
  152. The return code is 1 if there are no commands given, or if all commands fail.\n\
  153. It is 0 otherwise.\n\
  154. \n\
  155. Usage: \n\
  156. \n\
  157. \t-h, --help:\t\tDisplay this usage message.\n\
  158. \t-u, --usage:\t\tDisplay this usage message.\n\
  159. \t-d, --display:\t\tSpecify the X display to use.\n\
  160. \t-m, --search-method:\tSpecify a method to search by. See `cva-match --help\'\n\
  161. \t\t\t\tfor more information on this.\n\
  162. \t-t, --tolerance:\tSpecify how strict the match is.\n\
  163. \t-b, --button:\t\tSpecify the mouse button to press (default 1).\n\
  164. \t-c, --center:\t\tInstead of matching the top-left corner of an image,\n\
  165. \t\t\t\tmatch the center of the image.\n\
  166. \t-o, --sane-tolerance:\tSet the tolerance using a scale of 1-100,\n\
  167. \t-i, --timeout:\t\tSpecify the timeout to use when using the 'waitfor' function\n\
  168. \t-s, --string:\t\tCommand string - see below.\n\
  169. \t\t\t\tPlease make sure to use the '-t' or '-o' options when using this.\n\
  170. \n\
  171. This program works kind of like a mini-language. All options\n\
  172. are parsed left-to-right, and executed right there. Thus, specifying \"--display\"\n\
  173. at different places in the options will cause this program to use the most recent\n\
  174. given display.\n\
  175. Available commands:\n\
  176. \tmouseclick:\tClick the mouse in-place.\n\
  177. \timouseclick:\tClick the mouse at an image's top-left corner.\n\
  178. \ticmouseclick:\tClick the mouse at an image's center.\n\
  179. \tmousexy:\tMove the mouse to the given coordinate.\n\
  180. \tmouserxy:\tMove the mouse by the given x and y values (relative motion).\n\
  181. \tmouseimage:\tMove the mouse to an image's top-left corner.\n\
  182. \tcmouseimage:\tMove the mouse to an image's center.\n\
  183. \tmousedown:\tPush and leave down a mouse button.\n\
  184. \tmouseup:\tRelease a mouse button.\n\
  185. \tmousejiggle:\tJiggle the mouse (helps to activate some widgets).\n\
  186. \tmousescrollu:\tScroll the mouse wheel up.\n\
  187. \tmousescrolld:\tScroll the mouse wheel down.\n\
  188. \tmouselocation:\tReturn the location of the mouse.\n\
  189. \tkeyclick:\tClick a keyboard button.\n\
  190. \tkeydown:\tPush and leave down a keyboard button.\n\
  191. \tkeyup:\tRelease a keyboard button.\n\
  192. \tkeystring:\tInput a string of keys to X11.\n\
  193. \twaitfor:\tWait for an image to be displayed.\n\
  194. \n\
  195. If you have any questions, comments, concerns, email <%s>.\n\n", LIBCVAUTOMATION_VERSION, LIBCVAUTOMATION_BUGREPORT );
  196. exit (0);
  197. } /* ----- end of function usage ----- */
  198. void checkXTEEnabled ( Display *display )
  199. {
  200. /* Make sure we have the XTest Extensions enabled.
  201. * This is a quick wrapper. */
  202. if (! xte_XTestSupported( display ))
  203. {
  204. printf("The XTest extension is not supported! Aborting...");
  205. exit(255);
  206. }
  207. }
  208. /* Doxygen Information */
  209. /** \file cva-input.c
  210. * \brief The cva-input program to demonstrate Libcvautomation's XTest functionality
  211. * \author Bradlee Speice <bspeice@uncc.edu>
  212. */
  213. /* The biggest purpose of documenting this code is to trick doxygen into making a man page for it. */
  214. /** \page cva-input
  215. *
  216. * \author Bradlee Speice <bspeice@uncc.edu>
  217. * \section usage Usage:
  218. * This program works kind of like a mini-language. All options are parsed left-to-right, and executed right there. Thus, specifying "--display" at different places in the options will cause this program to use the most recent given display.
  219. * The return code is 1 if there are no commands given, or if all commands fail. It is 0 otherwise.
  220. * \section example Example Usage:
  221. * Click the mouse:
  222. *
  223. * cva-input -s 'click 1'
  224. *
  225. * Press key 'a':
  226. *
  227. * cva-input -s 'keyclick a'
  228. *
  229. * \section options Full Options:
  230. *
  231. * -h, --help: Display this usage message.
  232. *
  233. * -u, --usage: Display this usage message.
  234. *
  235. * -d, --display: Specify the X display to use.
  236. *
  237. * -m, --search-method: Specify a method to search by. See <tt>cva-match --help</tt> for more information on this.
  238. *
  239. * -t, --tolerance: Specify how strict the match is.
  240. *
  241. * -b, --button: Specify the mouse button to press (default 1).
  242. *
  243. * -c, --center: Instead of matching the top-left corner of an image, match the center of the image.
  244. *
  245. * -o, --sane-tolerance: Set the tolerance using a scale of 1-100, rather than INT_MIN to INT_MAX (100 ~= INT_MAX)
  246. *
  247. * -s, --string: Command string - see below.
  248. *
  249. * -i, --timeout: Specify the timeout to use when using the 'waitfor' command
  250. *
  251. *
  252. * \section commands Full Command List:
  253. * \c mouseclick: Click the mouse in-place.
  254. *
  255. * \c imouseclick: Click the mouse at an image's top-left corner.
  256. *
  257. * \c icmouseclick: Click the mouse at an image's center.
  258. *
  259. * \c mousexy: Move the mouse to the given coordinate.
  260. *
  261. * \c mouserxy: Move the mouse by the given x and y values (relative motion).
  262. *
  263. * \c mouseimage: Move the mouse to an image's top-left corner.
  264. *
  265. * \c cmouseimage: Move the mouse to an image's center.
  266. *
  267. * \c mousedown: Push and leave down a mouse button.
  268. *
  269. * \c mouseup: Release a mouse button.
  270. *
  271. * \c mousejiggle: Jiggle the mouse (helps to activate some widgets).
  272. *
  273. * \c mousescrollu: Scroll the mouse wheel up
  274. *
  275. * \c mousescrolld: Scroll the mouse wheel down
  276. *
  277. * \c mouselocation: Return the current location of the mouse
  278. *
  279. * \c keyclick: Click a keyboard button.
  280. *
  281. * \c keydown: Push and leave down a keyboard button.
  282. *
  283. * \c keyup: Release a keyboard button.
  284. *
  285. * \c keystring: Input a string of keys to X11.
  286. *
  287. * \c waitfor: Wait for an image to be displayed
  288. *
  289. * \section contact Contact Information:
  290. * Questions? Comments? Concerns? Suggestions? Send all feedback to Bradlee Speice at <bspeice@uncc.edu>
  291. */