RE: [ANN] LJIT2libevdev - LuaJIT binding to libevdev

  • From: William Adams <william_a_adams@xxxxxxxxxxx>
  • To: "luajit@xxxxxxxxxxxxx" <luajit@xxxxxxxxxxxxx>
  • Date: Tue, 8 Sep 2015 15:10:24 +0000



----------------------------------------
Date: Tue, 8 Sep 2015 16:40:48 +1000
Subject: Re: [ANN] LJIT2libevdev - LuaJIT binding to libevdev
From: quae@xxxxxxxxxxxxxxx
To: luajit@xxxxxxxxxxxxx

There's already quite a good evdev library that's not luajit specific
(i.e. doesn't use FFI).
https://github.com/Tangent128/lua-evdev

What does yours do that the existing one doesn't?


Good question. There's also this one:
http://github.com/dafrito/luacxx/
Which covers Qt5 and all the rest of the input and graphics stuff. So, why
bother?

1) With the exception of TINN, I don't really have any compiled code in my
luajit projects. I love that beauty of luajit.
2) API consistency. The one listed above, while very good, might not have
the same API objectives that a number of other bindings might have, therefore
if you use several of them, you get a lumpy experience. The way I typically
do my bindings, if you are a 'C' programmer, you can program using a C style.
If you're more of a lua programmer, then you can utilize a more 'wrapped'
lua style. Both are useful.


This code sample is almost verbatim from the libevdev wiki page:
http://www.freedesktop.org/wiki/Software/libevdev/
But, it's lua of course, using the LJIT2libevdev binding that I've created. 
It's as [un]safe as the typical C version would be, but can only get better
with some "Wrapped in LuaJIT goodness" applied.

package.path = package.path..";../?.lua"
local ffi = require("ffi")
local bit = require("bit")
local bor, band = bit.bor, bit.band;
local libevdev = require("libevdev_ffi")(_G);
local input = require("linux_input")(_G);
local setup = require("test_setup")();



local fd = open("/dev/input/event5", bor(O_RDONLY,O_NONBLOCK));
local dev = ffi.new("struct libevdev *[1]")
local rc = libevdev_new_from_fd(fd, dev);
if (rc < 0) then
         printf("Failed to init libevdev (%s)\n", strerror(-rc));
         error(1);
end
dev = dev[0];

printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
printf("Input device ID: bus %#x vendor %#x product %#x\n",
        libevdev_get_id_bustype(dev),
        libevdev_get_id_vendor(dev),
        libevdev_get_id_product(dev));

if (libevdev_has_event_type(dev, EV_REL)==0 or libevdev_has_event_code(dev,
EV_KEY, BTN_LEFT)==0) then
         printf("This device does not look like a mouse\n");
         error(1);
end

repeat
    local ev = ffi.new("struct input_event");
    rc = libevdev_next_event(dev, ffi.C.LIBEVDEV_READ_FLAG_NORMAL, ev);
    if (rc == 0) then
        printf("Event: %s %s %d\n",
            libevdev_event_type_get_name(ev.type),
            libevdev_event_code_get_name(ev.type, ev.code),
            ev.value);
    end
until (rc ~= 1 and rc ~= 0 and rc ~= -EAGAIN);





Other related posts: