Search code examples
cwindowsvisual-studioxlibunistd.h

Libraries "X11/Xlib.h", "X11/Xutil.h", "unistd.h" for Windows


I've done a C program that takes the RGB values (0-255) of a pixel of the screen knowing its position (x,y). It works in Linux, but when I try to compile it in Visual Studio (Windows), crashes because libraries X11/Xlib.h, X11/Xutil.h, unistd.h doesn't exist.

This is the code:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

void getimage(XColor c, Display* d, int* rgb, int x, int y) {
    XImage* image;
    image = XGetImage(d, RootWindow(d, DefaultScreen(d)),
        x, y, 1, 1, AllPlanes, XYPixmap);
    c.pixel = XGetPixel(image, 0, 0);
    XFree(image);
    XQueryColor(d, DefaultColormap(d, DefaultScreen(d)), &c);
    rgb[0] = c.red / 256;
    rgb[1] = c.green / 256;
    rgb[2] = c.blue / 256;
}

int main () {
    int rgb[3], x, y;
    XColor c;
    Display* d = XOpenDisplay((char*)NULL);
    getimage(c, d, rgb, x, y);
}

How can I implement this to run in Windows?


Solution

  • You've written an X11 program. X is the consensus standard for graphical displays on UNIX-like systems other than OS X (but it is also freely available for OS X).

    Windows uses a completely different graphical display API. It is conceivable that you could find an X implementation that runs on Windows, but Microsoft certainly doesn't provide one. Since your program is quite short, your best bet is probably to rewrite it using the Windows API.

    As for unistd.h (and time.h), I don't see anything in your program that depends on it. If you weren't going to rewrite the program, you could resolve that part of your problem simply by removing the offending #include statement.