Files

51 lines
2.0 KiB
C

// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
#include "commands_native.h"
#include <X11/extensions/XTest.h>
int cthulhu_ocr_click(unsigned long foreground, int x, int y, int root_width, int root_height) {
Display *display = XOpenDisplay(NULL);
if (!display) return 0;
Window root = DefaultRootWindow(display), query_root, child;
XWindowAttributes attributes;
int pointer_x, pointer_y, window_x, window_y;
unsigned int mask;
int event_base, error_base, major, minor;
int ok = XGetWindowAttributes(display, root, &attributes) &&
attributes.width == root_width && attributes.height == root_height &&
x >= 0 && x < root_width && y >= 0 && y < root_height &&
XTestQueryExtension(display, &event_base, &error_base, &major, &minor) &&
XQueryPointer(display, root, &query_root, &child, &pointer_x, &pointer_y,
&window_x, &window_y, &mask) &&
read_foreground(display).window == foreground;
int moved = 0;
if (ok) {
cthulhu_x11_begin_read(display);
moved = XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
ok = moved;
XSync(display, False);
if (cthulhu_x11_end_read(display)) ok = 0;
}
if (ok && read_foreground(display).window == foreground) {
cthulhu_x11_begin_read(display);
int pressed = XTestFakeButtonEvent(display, 1, True, CurrentTime);
XSync(display, False);
int released = !pressed || XTestFakeButtonEvent(display, 1, False, CurrentTime);
XTestFakeMotionEvent(display, -1, pointer_x, pointer_y, CurrentTime);
XSync(display, False);
int native_error = cthulhu_x11_end_read(display);
ok = pressed && released && !native_error;
} else {
ok = 0;
if (moved) {
cthulhu_x11_begin_read(display);
XTestFakeMotionEvent(display, -1, pointer_x, pointer_y, CurrentTime);
cthulhu_x11_end_read(display);
}
}
XCloseDisplay(display);
return ok;
}