Advertisement
vertexSymphony

Untitled

Jun 26th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. /*
  2.  *  Ejemplo super simple de cómo floodear con clicks a alguna aplicacion
  3.  *  Por ejemplos esos juegos de quien clickea más rápido.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10.  
  11. #include <X11/Xlib.h>
  12. #include <X11/Xutil.h>
  13.  
  14. #define nullptr 0
  15.  
  16.  
  17. /* Simplemente para llevarnos mejor con -ansi */
  18. extern char *optarg;
  19. extern int optind, opterr, optopt;
  20.  
  21.  
  22. int main(int argc, char* argv[])
  23. {
  24.  
  25.     Display* targetDisplay = nullptr; /* Por Ahora no abrimos display, chequeemos cosas primero */
  26.     XEvent windowEvent;
  27.     Window targetWindow = 0;
  28.      
  29.     int clickButton = 1; /* Izquierdo, por default */
  30.     int clickCount = 1, clickInterval = 0; /* 1 Click, default */
  31.     int clickX =-1, clickY=-1;
  32.    
  33.     int showUsage = argc<3?1:0;
  34.    
  35.     int cmdOpt = 0;
  36.    
  37.     /* Mas feo que unos meros ifs, pero mas estandar y estricto*/
  38.    
  39.     do{
  40.      
  41.       /* TODO: Agregar soporte para X e Y*/
  42.       cmdOpt = getopt(argc,argv,":w:b:c:t:h");
  43.      
  44.       switch(cmdOpt)
  45.       {
  46.    
  47.     /* Window ID [Obligatorio]*/
  48.     case 'w':
  49.         targetWindow = atoi(optarg);
  50.       break;
  51.    
  52.     /* Numero de boton [Opcional]*/
  53.     case 'b':
  54.         clickButton = atoi(optarg);  
  55.       break;
  56.      
  57.     /* Numero de clicks [opcional] */
  58.     case 'c':
  59.         clickCount = atoi(optarg);   
  60.       break;
  61.      
  62.     /* Intervalo de tiempo entre click y click [opcional] */
  63.     case 't':
  64.         clickInterval = atoi(optarg);    
  65.       break;
  66.    
  67.     /* Ayuda */
  68.     case 'h':
  69.         showUsage = 1;   
  70.       break;
  71.      
  72.      /* Manejo de unexpected's*/
  73.     case ':':
  74.         fprintf(stderr,"Option -%c requires an argument ! \r\n",optopt);
  75.         showUsage = 1;
  76.       break;
  77.      
  78.     case '?':
  79.         fprintf(stderr,"Unrecognized option -%c, call without arguments for help on usage\r\n",optopt );   
  80.         showUsage = 1;
  81.       break;
  82.      
  83.       }
  84.      
  85.     } while ( cmdOpt != -1 && !showUsage);
  86.    
  87.    
  88.     /* Mostramos el uso del programa? bueno, mostrar y salir */
  89.     if (showUsage)
  90.     {
  91.         fprintf(stderr,"Usage : %s -w ID [-b Button -c TotalNumClicks -t WaitTimeInmsBetweenClicks] \r\n", argv[0]);
  92.         exit(EXIT_FAILURE);
  93.     }
  94.    
  95.     /* Pase lo que pase, no podemos tener un ID 0, significa que no se leyo el argumento ... el resto puede estar bien con defaut */
  96.     if (targetWindow == 0 )
  97.     {
  98.         fprintf(stderr,"The Window ID should be different than 0\r\n");
  99.         exit(EXIT_FAILURE);
  100.     }
  101.  
  102.  
  103.     /* Ahora si podemos abrir el display */
  104.     targetDisplay = XOpenDisplay(nullptr);
  105.  
  106.     /* Ok, no hay acceso ? terminamos la ejecucion aca mismo */
  107.     if (targetDisplay == nullptr)
  108.     {
  109.         fprintf(stderr,"XOpenDisplay : Error al abrir el Display !\r\n");
  110.         exit(EXIT_FAILURE);
  111.     }
  112.  
  113.     /* Estructura de evento a 0 */
  114.     memset(&windowEvent, 0, sizeof(windowEvent));
  115.  
  116.     /* Se describe solito esto ...*/
  117.     windowEvent.xbutton.button = clickButton;
  118.     windowEvent.xbutton.same_screen = True;
  119.  
  120.     /* Pido estado del puntero .. */
  121.     XQueryPointer(
  122.         targetDisplay, targetWindow,
  123.         &windowEvent.xbutton.root,
  124.         &windowEvent.xbutton.window,
  125.         &windowEvent.xbutton.x_root,
  126.         &windowEvent.xbutton.y_root,
  127.         &windowEvent.xbutton.x,
  128.         &windowEvent.xbutton.y,
  129.         &windowEvent.xbutton.state
  130.     );
  131.  
  132.     /*
  133.      * Creo que esto se describe solo ...
  134.      *TODO: IMHO deberia agregar una funcion XWindowExists() para verificar si targetWindow es un ID legitimo revisando la lista de ventanas
  135.      */
  136.  
  137.     while (clickCount--)
  138.     {
  139.         windowEvent.type = ButtonPress;
  140.         XSendEvent(targetDisplay, targetWindow, True, 0xfff, &windowEvent);
  141.         windowEvent.type = ButtonRelease;
  142.         XSendEvent(targetDisplay, targetWindow, True, 0xfff, &windowEvent);
  143.         XFlush(targetDisplay);
  144.  
  145.         usleep(clickInterval*1000);
  146.     }
  147.  
  148.     XCloseDisplay(targetDisplay);
  149.  
  150.     return EXIT_SUCCESS;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement