Git Product home page Git Product logo

lua-evdev's Introduction

lua-evdev

LuaJIT FFI Bindings for libevdev.

Usage

Initializing a Device

local Device = require("evdev.device")

---@param dev Device
local function print_device(dev)
  local name = dev:name():gsub("\n", " | ")
  print("============", "===")
  print("=     Name: ", name)
  print("=     Phys: ", dev:phys())
  print("=     Uniq: ", dev:uniq())
  print("=  Product: ", dev:product_id())
  print("=   Vendor: ", dev:vendor_id())
  print("=  Bustype: ", dev:bustype())
  print("=  Version: ", dev:version())
  print("============", "===")
end

local dev = Device:new("/dev/input/event7")

print_device(dev)

Reading Events from a Device

local Event = require("evdev.event")
local Device = require("evdev.device")
local libevdev = require("evdev.libevdev")
local input = require("evdev.linux.input")

local enum = libevdev.enum
local input_constant = input.constant

---@param ev Event
local function print_event(ev)
  print(string.format("{'%s', '%s', %d};", ev:type_name(), ev:code_name(), ev:value()))
end

---@param dev Device
---@param initial_state table
local function events(dev, initial_state)
  initial_state = initial_state or {}

  if not initial_state.read_flag then
    initial_state.read_flag = initial_state.read_flag or enum.libevdev_read_flag.NORMAL
  end

  if not initial_state.should_skip then
    initial_state.should_skip = function()
      return false
    end
  end

  ---@return Event
  local function iter(state)
    local rc, ev = 0, Event:new()

    repeat
      rc, ev = dev:next_event(state.read_flag, ev)
      if (rc == enum.libevdev_read_status.SUCCESS) or (rc == enum.libevdev_read_status.SYNC) then
        if not state.should_skip(ev) then
          return ev, state
        end
      end
    until rc ~= enum.libevdev_read_status.SUCCESS and rc ~= enum.libevdev_read_status.SYNC and rc ~= -11

    return nil, state
  end

  return iter, initial_state
end

local dev = Device:new("/dev/input/event7")

local initial_state = {
  ---@param ev Event
  should_skip = function(ev)
    ev:is_type(input_constant.EV_KEY)
  end,
}

for ev in events(dev, initial_state) do
  print_event(ev)
end

Initializing a UInputDevice

local Device = require("evdev.device")
local UIDevice = require("evdev.uinput-device")

---@param uidev UInputDevice
local function print_uinput_device(uidev)
  print("============", "===")
  print("=       FD: ", uidev:fd())
  print("=  SysPath: ", uidev:syspath())
  print("=  DevNode: ", uidev:devnode())
  print("============", "===")
end

local dev = Device:new("/dev/input/event7")

local uidev = UIDevice:new(dev)

print_uinput_device(uidev)

License

Licensed under the MIT License. Check the LICENSE file for details.

lua-evdev's People

Contributors

muniftanjim avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hbalint2100

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.