Advertisement
Guest User

Untitled

a guest
Feb 11th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6.  
  7. extern "C" {
  8.     #include <xdo.h>
  9. }
  10.  
  11. enum KEY_CODES {
  12.     NONE = 0,
  13.     CONTROL = 37,
  14.     SHIFT = 50,
  15. };
  16.  
  17. // Mouse position
  18. struct POS {
  19.     int x;
  20.     int y;
  21. };
  22.  
  23. /*
  24.  * Current time in milliseconds.
  25.  */
  26. uint64_t millis() {
  27.     struct timespec now;
  28.     clock_gettime(CLOCK_REALTIME, &now);
  29.     return ((uint64_t) now.tv_sec) * 1000 + ((uint64_t) now.tv_nsec) / 1000000;
  30. }
  31.  
  32. void sleep() {
  33.     int ms = (rand() % 20) + 15;
  34.     //printf("sleep(%d);\n", ms);
  35.     usleep(ms * 1000);
  36. }
  37.  
  38. void sleep(int ms) {
  39.     usleep(ms * 1000);
  40. }
  41.  
  42. POS get_mouse_pos(xdo_t* xdo) {
  43.     int ignore_int;
  44.     POS p = {0, 0};
  45.    
  46.     xdo_get_mouse_location(xdo, &p.x, &p.y, &ignore_int);
  47.    
  48.     return p;
  49. }
  50.  
  51. void set_mouse_pos(xdo_t* xdo, int x, int y) {
  52.     xdo_move_mouse(xdo, x, y, 0);
  53. }
  54.  
  55. // BUG: Can only get one modifier (random)
  56. KEY_CODES get_modifier_lazily(xdo_t* xdo) {
  57.     charcodemap_t* keys = nullptr;
  58.     int nkeys = 0;
  59.     xdo_get_active_modifiers(xdo, &keys, &nkeys);
  60.    
  61.     KEY_CODES rv = KEY_CODES::NONE;
  62.     switch (keys[0].code) {
  63.         case KEY_CODES::SHIFT:
  64.             rv = KEY_CODES::SHIFT;
  65.             break;
  66.            
  67.         case KEY_CODES::CONTROL:
  68.             rv = KEY_CODES::CONTROL;
  69.             break;
  70.     }
  71.     free(keys);
  72.     return rv;
  73. }
  74.  
  75. void click(
  76.     // you may acquire with / xdo_t* xdo = xdo_new(NULL); /
  77.     xdo_t* xdo,
  78.     // left = 1, right = 3, middle = 2
  79.     int button)
  80. {
  81.     // Performs mouse click.
  82.     xdo_click_window(xdo, CURRENTWINDOW, button);
  83. }
  84.  
  85. void random_action(xdo_t* xdo) {
  86.     int three = rand() % 4;
  87.    
  88.     switch (three) {
  89.         case 0:
  90.             //printf("click(xdo, 1);\n");
  91.             click(xdo, 1);
  92.             break;
  93.         case 1:
  94.             //printf("xdo_mouse_up(xdo, CURRENTWINDOW, 1);\n");
  95.             xdo_mouse_up(xdo, CURRENTWINDOW, 1);
  96.             break;
  97.         case 2:
  98.             //printf("xdo_mouse_down(xdo, CURRENTWINDOW, 1);\n");
  99.             xdo_mouse_down(xdo, CURRENTWINDOW, 1);
  100.             break;
  101.         case 3:
  102.             break;
  103.     }
  104. }
  105.  
  106. int main(int argc, char *argv[]) {
  107.     srand((unsigned)time(0));
  108.    
  109.     // Connect to the display.
  110.     xdo_t* xdo = xdo_new(NULL);
  111.  
  112.     if (xdo == NULL) {
  113.         // Something went wrong and there seems to be no way of knowing
  114.         // what.
  115.         fprintf(stderr, "Unable to connect to the display.\n");
  116.         return 1;
  117.     }
  118.  
  119.     while (true) {
  120.         KEY_CODES k = get_modifier_lazily(xdo);
  121.         switch (k) {
  122.             case KEY_CODES::SHIFT:
  123.                 return EXIT_SUCCESS;
  124.                
  125.             case KEY_CODES::CONTROL:
  126.                 xdo_move_mouse_relative(xdo, 5, 0);
  127.                 sleep();
  128.                 random_action(xdo);
  129.                 sleep();
  130.                 random_action(xdo);
  131.                 sleep();
  132.                 random_action(xdo);
  133.                 sleep();
  134.                 xdo_move_mouse_relative(xdo, -10, 0);
  135.                 sleep();
  136.                 random_action(xdo);
  137.                 sleep();
  138.                 random_action(xdo);
  139.                 sleep();
  140.                 random_action(xdo);
  141.                 sleep();
  142.                 xdo_move_mouse_relative(xdo, 5, 0);
  143.                 break;
  144.         }
  145.         sleep(15);
  146.     }
  147.  
  148.     // Disconnect from the display.
  149.     xdo_free(xdo);
  150.  
  151.     return EXIT_SUCCESS;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement