Skip to content

Instantly share code, notes, and snippets.

@polaco1782
Created July 19, 2023 18:03
Show Gist options
  • Save polaco1782/4941463202af21c242dfa1a943735e83 to your computer and use it in GitHub Desktop.
Save polaco1782/4941463202af21c242dfa1a943735e83 to your computer and use it in GitHub Desktop.
mouse moving
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
Window findWindow(Display *display, Window window, const char *name) {
Window root, parent, *children;
unsigned int numChildren;
char *windowName;
if (XFetchName(display, window, &windowName) > 0 && windowName != NULL) {
if (strcmp(windowName, name) == 0) {
XFree(windowName);
return window;
}
XFree(windowName);
}
if (XQueryTree(display, window, &root, &parent, &children, &numChildren) == 0) {
return 0;
}
for (unsigned int i = 0; i < numChildren; i++) {
Window found = findWindow(display, children[i], name);
if (found != 0) {
XFree(children);
return found;
}
}
XFree(children);
return 0;
}
Window findXfreerdpWindow() {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Unable to open X display\n");
return 0;
}
Window rootWindow = DefaultRootWindow(display);
Window xfreerdpWindow = findWindow(display, rootWindow, "FreeRDP: 172.16.15.2:2021");
XCloseDisplay(display);
return xfreerdpWindow;
}
void sendMouseMovementEvent(Display *display, Window window, int x, int y) {
XEvent event;
memset(&event, 0, sizeof(event));
event.type = MotionNotify;
event.xmotion.display = display;
event.xmotion.window = window;
event.xmotion.root = DefaultRootWindow(display);
event.xmotion.subwindow = window;
event.xmotion.time = CurrentTime;
event.xmotion.x = x;
event.xmotion.y = y;
event.xmotion.state = 0;
event.xmotion.is_hint = NotifyNormal;
XSendEvent(display, window, True, PointerMotionMask, &event);
XFlush(display);
}
void sendKeyPress(Display *display, Window window, KeyCode keyCode) {
XEvent event;
memset(&event, 0, sizeof(event));
event.type = KeyPress;
event.xkey.display = display;
event.xkey.window = window;
event.xkey.root = DefaultRootWindow(display);
event.xkey.subwindow = window;
event.xkey.time = CurrentTime;
event.xkey.keycode = keyCode;
event.xkey.same_screen = True;
XSendEvent(display, window, True, KeyPressMask, &event);
XFlush(display);
event.type = KeyRelease;
event.xkey.time = CurrentTime;
XSendEvent(display, window, True, KeyReleaseMask, &event);
XFlush(display);
}
int main() {
Window xfreerdpWindow = findXfreerdpWindow();
if (xfreerdpWindow == 0) {
printf("xfreerdp window not found\n");
return 1;
}
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Unable to open X display\n");
return 1;
}
int rootX, rootY, winX, winY;
Window root, child;
unsigned int mask;
for(;;)
{
// Get the current mouse position
XQueryPointer(display, DefaultRootWindow(display), &root, &child, &rootX, &rootY, &winX, &winY, &mask);
// Add a random offset to the coordinates
srand(time(NULL));
int offsetX = rand() % 10;
int offsetY = rand() % 10;
int newX = winX + offsetX;
int newY = winY + offsetY;
// Send the new mouse position to the window
sendMouseMovementEvent(display, xfreerdpWindow, newX, newY);
usleep(100);
// Send mouse position back to the window
sendMouseMovementEvent(display, xfreerdpWindow, winX, winY);
sleep(60);
}
XCloseDisplay(display);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment