File: event.lua

package info (click to toggle)
lua-ljsyscall 0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,732 kB
  • sloc: ansic: 434; sh: 59; makefile: 3
file content (53 lines) | stat: -rw-r--r-- 1,356 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- example of event ioctls

local S = require "syscall"

local EV = S.c.EV
local MSC = S.c.MSC
local KEY = S.c.KEY

local kl = {}
for k, v in pairs(KEY) do kl[v] = k end

local oldassert = assert
local function assert(cond, s)
  collectgarbage("collect") -- force gc, to test for bugs
  return oldassert(cond, tostring(s)) -- annoyingly, assert does not call tostring!
end

local function ev(dev)
  if not dev then dev = "/dev/input/event0" end
  local fd = assert(S.open(dev, "rdonly"))

  local version = assert(S.ioctl(fd, "EVIOCGVERSION"))

  print(string.format("evdev driver version: %d.%d.%d",
    bit.rshift(version, 16), 
    bit.band(bit.rshift(version, 8), 0xff),
    bit.band(version, 0xff)))

  local ev = S.t.input_event()
  while true do
    assert(fd:read(ev))

    if ev.type == EV.MSC then
      if ev.code == MSC.SCAN then
        print("MSC_SCAN: ", string.format("0x%x", ev.value));
      else
        print("MSC: ", ev.code, ev.value);
      end
    elseif ev.type == EV.KEY then
      if ev.value == 1 then print("down", kl[ev.code], ev.code)
      elseif ev.value == 0 then print("up", kl[ev.code], ev.code)
      elseif ev.value == 2 then print("repeat", kl[ev.code], ev.code)
      end
    else
      --print("EVENT TYPE: ", ev.type, "CODE:", ev.code, "VALUE: ", string.format("0x%x", ev.value));
    end
  end
end



ev(arg[1])