Last active
December 10, 2019 23:18
-
-
Save tronje/48122a035afd1efe9fb0c04ed5338a56 to your computer and use it in GitHub Desktop.
C program to click the mouse n times in X11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gcc -o click click.c -lX11 -lXtst -O2 -flto -march=native -mtune=native -DDELAY_SECONDS=0 -DDELAY_NSECONDS=35000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <time.h> | |
#include <X11/Xlib.h> | |
#include <X11/extensions/XTest.h> | |
#ifndef DELAY_SECONDS | |
#define DELAY_SECONDS 0 | |
#endif | |
#ifndef DELAY_NSECONDS | |
#define DELAY_NSECONDS 25000000; | |
#endif | |
int main(int argc, char ** argv) { | |
if (argc != 2) { | |
puts("Invalid arguments!"); | |
return 1; | |
} | |
long signed_clicks = atol(argv[1]); | |
if (signed_clicks < 1) { | |
puts("Invalid number of clicks!"); | |
return 1; | |
} | |
// 1st argument is number of clicks to perform | |
unsigned long clicks = strtoul(argv[1], NULL, 10); | |
// get X display | |
Display *display = XOpenDisplay(NULL); | |
// timespec struct to hold the time to sleep | |
// between clicks | |
struct timespec sleeptime; | |
// time between clicks in seconds... | |
sleeptime.tv_sec = DELAY_SECONDS; | |
// ... and nanoseconds. | |
sleeptime.tv_nsec = DELAY_NSECONDS; | |
// click loop | |
for (unsigned long i = 0; i < clicks; i++) { | |
// click left mouse | |
XTestFakeButtonEvent(display, 1, true, 0); | |
XFlush(display); | |
// release left mouse | |
XTestFakeButtonEvent(display, 1, false, 0); | |
XFlush(display); | |
// sleep; super-fast clicking is usually not desirable | |
nanosleep(&sleeptime, NULL); | |
unsigned long ratio = 100 * i / clicks; | |
// print a progress star for every 5 percent done | |
printf("\r[%.*s", (ratio / 5), "********************"); | |
// print a space for every 5% missing | |
printf("%.*s] ", (20 - ratio / 5), " "); | |
// print percentage of total clicks | |
printf("%3d%% ", ratio); | |
// flush stdout so progress is actually shown in real time | |
fflush(stdout); | |
} | |
// hard-coded 100% | |
printf("\r[********************] 100%%!\n"); | |
// close the display | |
XCloseDisplay(display); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment