T
T
Tolik2014-05-08 17:12:08
linux
Tolik, 2014-05-08 17:12:08

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;
}

In main(), I just have all sorts of chips for tests
. The problem is that if I scan a small area (for example, half the screen, a horizontal or vertical strip), then everything is fine. Finds a pixel or does not find it. But if you scan on a large area (for example, my whole screen is 1366 * 768), then it crashes with an addressing error
. I also have a simplified function, where not a range of colors, but a specific color. But it doesn't change anything and there is the same picture
PS I know that pixel numbering starts from zero, that's why I have
int width = getScreenWidth() - 1,
      height = getScreenHeight() - 1;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-05-08
@Diel

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 question

Ask a Question

731 491 924 answers to any question