|
2 | 2 |
|
3 | 3 | LuaJIT FFI Bindings for [`libevdev`](https://gitlab.freedesktop.org/libevdev/libevdev). |
4 | 4 |
|
| 5 | +## Usage |
| 6 | + |
| 7 | +### Initializing a Device |
| 8 | + |
| 9 | +```lua |
| 10 | +local Device = require("evdev.device") |
| 11 | + |
| 12 | +---@param dev Device |
| 13 | +local function print_device(dev) |
| 14 | + local name = dev:name():gsub("\n", " | ") |
| 15 | + print("============", "===") |
| 16 | + print("= Pathname: ", dev.pathname) |
| 17 | + print("= Name: ", name) |
| 18 | + print("= Phys: ", dev:phys()) |
| 19 | + print("= Uniq: ", dev:uniq()) |
| 20 | + print("= Product: ", dev:id_product()) |
| 21 | + print("= Vendor: ", dev:id_vendor()) |
| 22 | + print("= Bustype: ", dev:id_bustype()) |
| 23 | + print("= Version: ", dev:id_version()) |
| 24 | + print("============", "===") |
| 25 | +end |
| 26 | + |
| 27 | +local dev = Device:new("/dev/input/event7") |
| 28 | + |
| 29 | +print_device(dev) |
| 30 | +``` |
| 31 | + |
| 32 | +### Reading Events from a Device |
| 33 | + |
| 34 | +```lua |
| 35 | +local Event = require("evdev.event") |
| 36 | +local Device = require("evdev.device") |
| 37 | +local libevdev = require("evdev.libevdev") |
| 38 | +local input = require("evdev.linux.input") |
| 39 | + |
| 40 | +local enum = libevdev.enum |
| 41 | +local input_constant = input.constant |
| 42 | + |
| 43 | +---@param ev Event |
| 44 | +local function print_event(ev) |
| 45 | + print(string.format("{'%s', '%s', %d};", ev:type_name(), ev:code_name(), ev:value())) |
| 46 | +end |
| 47 | + |
| 48 | +---@param dev Device |
| 49 | +---@param initial_state table |
| 50 | +local function events(dev, initial_state) |
| 51 | + initial_state = initial_state or {} |
| 52 | + |
| 53 | + if not initial_state.read_flag then |
| 54 | + initial_state.read_flag = initial_state.read_flag or enum.libevdev_read_flag.NORMAL |
| 55 | + end |
| 56 | + |
| 57 | + if not initial_state.should_skip then |
| 58 | + initial_state.should_skip = function() |
| 59 | + return false |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + local function iter(state) |
| 64 | + local rc, ev = 0, Event:new() |
| 65 | + |
| 66 | + repeat |
| 67 | + rc, ev = dev:next_event(state.read_flag, ev) |
| 68 | + if (rc == enum.libevdev_read_status.SUCCESS) or (rc == enum.libevdev_read_status.SYNC) then |
| 69 | + if not state.should_skip(ev) then |
| 70 | + return ev, state |
| 71 | + end |
| 72 | + end |
| 73 | + until rc ~= enum.libevdev_read_status.SUCCESS and rc ~= enum.libevdev_read_status.SYNC and rc ~= -11 |
| 74 | + |
| 75 | + return nil, state |
| 76 | + end |
| 77 | + |
| 78 | + return iter, initial_state |
| 79 | +end |
| 80 | + |
| 81 | +local dev = Device:new("/dev/input/event7") |
| 82 | + |
| 83 | +local initial_state = { |
| 84 | + ---@param ev Event |
| 85 | + should_skip = function(ev) |
| 86 | + ev:is_type(input_constant.EV_KEY) |
| 87 | + end, |
| 88 | +} |
| 89 | + |
| 90 | +for ev in events(dev, initial_state) do |
| 91 | + print_event(ev) |
| 92 | +end |
| 93 | +``` |
| 94 | + |
5 | 95 | ## License |
6 | 96 |
|
7 | 97 | Licensed under the MIT License. Check the [LICENSE](./LICENSE) file for details. |
0 commit comments