Answer the question
In order to leave comments, you need to log in
Find error in code (C++ X11)
I will not upload the entire file, but only what does not work normally (works but does not work)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
Display *display;
Drawable drawable;
void init() {
display = XOpenDisplay(NULL);
drawable = RootWindow(display, DefaultScreen(display));
}
int getScreenWidth() {
Screen *screen = DefaultScreenOfDisplay(display);
return screen->width;
}
int getScreenHeight() {
Screen *screen = DefaultScreenOfDisplay(display);
return screen->height;
}
bool findColor(int &varX, int &varY,
int startX, int startY,
int endX, int endY,
int startRed, int endRed, int startGreen, int endGreen, int startBlue, int endBlue) {
XImage *image;
int x, y;
XColor color;
image = XGetImage(display, drawable, startX, startY, endX - startX, endY - startY, AllPlanes, XYPixmap);
for(x = startX; x <= endX; x++) {
for(y = startY; y <= endY; y++) {
color.pixel = XGetPixel(image, x - startX, y - startY);
XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &color);
if(color.red / 256 >= startRed && color.red / 256 <= endRed &&
color.green / 256 >= startGreen && color.green / 256 <= endGreen &&
color.blue / 256 >= startBlue && color.blue / 256 <= endBlue) {
XFree(image);
varX = x;
varY = y;
return true;
}
}
}
XFree(image);
return false;
}
void cursorMove(int x, int y) {
Window root_window = XRootWindow(display, 0);
XSelectInput(display, root_window, KeyReleaseMask);
XWarpPointer(display, None, root_window, 0, 0, 0, 0, x, y);
XFlush(display);
}
int main(int argc, char *argv[]) {
init();
int width = getScreenWidth() - 1,
height = getScreenHeight() - 1;
int x, y;
if(findColor(x, y, 0, 0, width, height, 0, 0, 0, 10, 0, 100)) {
printf("Found at %d:%d\n", x, y);
cursorMove(x, y);
}
return 0;
}
int width = getScreenWidth() - 1,
height = getScreenHeight() - 1;
Answer the question
In order to leave comments, you need to log in
And everything works fine for me, 1600x900.
Maybe you don't have enough memory and XGetImage returns NULL?
In addition, XGetImage returns a rectangle that is 1 pixel smaller than the screen size, both in x and y, and you call XGetPixel according to the full width and height of the screen.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question