Search code examples
cluaffiluajit

How to implement X11(return colour of a screen pixel) C code for luajit's ffi?


I want to record small section of my screen with luajit.

Haven't found any module for that. And there are barely any documentations/tutorials/examples about luajit's ffi on the web aside from http://luajit.org/ext_ffi.html which doesn't provide any examples of using other C libraries.

I have a C code snippet that works native. How would you implement the C code for luajit's ffi?

Luajit example code:

--ffi part

local screen = {}
for y = 1, 100 do
    for x = 1, 100 do
        local r, g, b = ffi.C.getpixel(x, y)
        table.insert(screen, r)
    end
end

C code snippet:

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main()
{
    XColor c;
    Display *d = XOpenDisplay((char *) NULL);

    int x=1920/2;  // Pixel x 
    int y=1080/2;  // Pixel y

    XImage *image;
    image = XGetImage (d, XRootWindow (d, XDefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
    c.pixel = XGetPixel (image, 0, 0);
    XFree (image);
    XQueryColor (d, XDefaultColormap(d, XDefaultScreen (d)), &c);
    printf("%d %d %d\n", c.red/256, c.green/256, c.blue/256);

    return 0;
}

Solution

  • Basically you only have to copy all the declarations from the headers into the ffi.cdef section and then call these names through a handle to the library. In principle you can translate the C code one to one, with the exception of taking the address of a variable. However, this is documented in the FFI tutorial you linked (http://luajit.org/ext_ffi_tutorial.html#idioms)

                                C code          Lua code
    Functions with outargs      int len = x;    local len = ffi.new("int[1]", x)
    void foo(int *inoutlen);    foo(&len);      foo(len)
                                y = len;        y = len[0]
    

    Here is your C code in LuaJIT. I didn't copy the definitions for Display and XImage because we never access their members and only use pointers to them, so they remain opaque structs.

    local ffi = assert(require("ffi"))
    
    ffi.cdef[[
    // Types from various headers
    typedef struct _Display Display;
    typedef struct _XImage XImage;
    typedef struct {
            unsigned long pixel;
            unsigned short red, green, blue;
            char flags;  /* do_red, do_green, do_blue */
            char pad;
    } XColor; // Xlib.h
    typedef unsigned long XID; // Xdefs.h
    typedef XID Window; // X.h
    typedef XID Drawable; // X.h
    typedef XID Colormap; // X.h
    
    // Functions from Xlib.h
    Display *XOpenDisplay(
        char*       /* display_name */
    );
    int XDefaultScreen(
        Display*            /* display */
    );
    Window XRootWindow(
        Display*            /* display */,
        int                 /* screen_number */
    );
    XImage *XGetImage(
        Display*            /* display */,
        Drawable            /* d */,
        int                 /* x */,
        int                 /* y */,
        unsigned int        /* width */,
        unsigned int        /* height */,
        unsigned long       /* plane_mask */,
        int                 /* format */
    );
    int XFree(
        void*               /* data */
    );
    int XQueryColor(
        Display*            /* display */,
        Colormap            /* colormap */,
        XColor*             /* def_in_out */
    );
    Colormap XDefaultColormap(
        Display*            /* display */,
        int                 /* screen_number */
    );
    // Functions from Xutil.h
    unsigned long XGetPixel(
            XImage *ximage,
            int x, int y);
    ]]
    
    local X11 = assert(ffi.load("X11"))
    
    local AllPlanes = -1 -- Xlib.h: #define AllPlanes ((unsigned long)~0L)
    local XYPixmap = 1 -- X.h: #define XYPixmap 1
    
    local c = ffi.new("XColor[1]")
    local d = X11.XOpenDisplay(ffi.NULL)
    
    local x = 1920 / 2
    local y = 1080 / 2
    
    local image = X11.XGetImage(d, X11.XRootWindow(d, X11.XDefaultScreen(d)), x, y, 1, 1, AllPlanes, XYPixmap)
    c[0].pixel = X11.XGetPixel(image, 0, 0)
    X11.XFree(image)
    X11.XQueryColor(d, X11.XDefaultColormap(d, X11.XDefaultScreen(d)), c)
    print(string.format("%d %d %d", c[0].red/256, c[0].green/256, c[0].blue/256))