Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- extern "C" {
- #include <xdo.h>
- }
- enum KEY_CODES {
- NONE = 0,
- CONTROL = 37,
- SHIFT = 50,
- };
- // Mouse position
- struct POS {
- int x;
- int y;
- };
- /*
- * Current time in milliseconds.
- */
- uint64_t millis() {
- struct timespec now;
- clock_gettime(CLOCK_REALTIME, &now);
- return ((uint64_t) now.tv_sec) * 1000 + ((uint64_t) now.tv_nsec) / 1000000;
- }
- void sleep() {
- int ms = (rand() % 20) + 15;
- //printf("sleep(%d);\n", ms);
- usleep(ms * 1000);
- }
- void sleep(int ms) {
- usleep(ms * 1000);
- }
- POS get_mouse_pos(xdo_t* xdo) {
- int ignore_int;
- POS p = {0, 0};
- xdo_get_mouse_location(xdo, &p.x, &p.y, &ignore_int);
- return p;
- }
- void set_mouse_pos(xdo_t* xdo, int x, int y) {
- xdo_move_mouse(xdo, x, y, 0);
- }
- // BUG: Can only get one modifier (random)
- KEY_CODES get_modifier_lazily(xdo_t* xdo) {
- charcodemap_t* keys = nullptr;
- int nkeys = 0;
- xdo_get_active_modifiers(xdo, &keys, &nkeys);
- KEY_CODES rv = KEY_CODES::NONE;
- switch (keys[0].code) {
- case KEY_CODES::SHIFT:
- rv = KEY_CODES::SHIFT;
- break;
- case KEY_CODES::CONTROL:
- rv = KEY_CODES::CONTROL;
- break;
- }
- free(keys);
- return rv;
- }
- void click(
- // you may acquire with / xdo_t* xdo = xdo_new(NULL); /
- xdo_t* xdo,
- // left = 1, right = 3, middle = 2
- int button)
- {
- // Performs mouse click.
- xdo_click_window(xdo, CURRENTWINDOW, button);
- }
- void random_action(xdo_t* xdo) {
- int three = rand() % 4;
- switch (three) {
- case 0:
- //printf("click(xdo, 1);\n");
- click(xdo, 1);
- break;
- case 1:
- //printf("xdo_mouse_up(xdo, CURRENTWINDOW, 1);\n");
- xdo_mouse_up(xdo, CURRENTWINDOW, 1);
- break;
- case 2:
- //printf("xdo_mouse_down(xdo, CURRENTWINDOW, 1);\n");
- xdo_mouse_down(xdo, CURRENTWINDOW, 1);
- break;
- case 3:
- break;
- }
- }
- int main(int argc, char *argv[]) {
- srand((unsigned)time(0));
- // Connect to the display.
- xdo_t* xdo = xdo_new(NULL);
- if (xdo == NULL) {
- // Something went wrong and there seems to be no way of knowing
- // what.
- fprintf(stderr, "Unable to connect to the display.\n");
- return 1;
- }
- while (true) {
- KEY_CODES k = get_modifier_lazily(xdo);
- switch (k) {
- case KEY_CODES::SHIFT:
- return EXIT_SUCCESS;
- case KEY_CODES::CONTROL:
- xdo_move_mouse_relative(xdo, 5, 0);
- sleep();
- random_action(xdo);
- sleep();
- random_action(xdo);
- sleep();
- random_action(xdo);
- sleep();
- xdo_move_mouse_relative(xdo, -10, 0);
- sleep();
- random_action(xdo);
- sleep();
- random_action(xdo);
- sleep();
- random_action(xdo);
- sleep();
- xdo_move_mouse_relative(xdo, 5, 0);
- break;
- }
- sleep(15);
- }
- // Disconnect from the display.
- xdo_free(xdo);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement