/
test_libev.lua
127 lines (93 loc) · 2.51 KB
/
test_libev.lua
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
local ffi = require "ffi"
local ev = require "ev_utils"
local S = require "syscall"
local UI = require "input"
--print("EV Version: ", ev.version());
local loop = ev.ev_loop(); -- ev.Loop();
assert(loop, "event loop not created");
--[[
Callback functions
--]]
function OnTimeout(loop, ...)
print("OnTimeout: ", loop:iteration());
end
function OnIdle(loop, ...)
print("Idling");
end
--[[
Event type:
EV_KEY
EV_MSC
value:
0 == keyup
1 == keydown
--]]
function OnKey(loop, w, revents)
local event = input_event();
local bytesread = S.read(w.fd, event, ffi.sizeof(event));
if event.type == EV_MSC then
if event.code == MSC_SCAN then
--print("MSC_SCAN: ", string.format("0x%x",event.value));
else
--print("MSC: ", event.code, event.value);
end
elseif event.type == EV_KEY then
if event.value == 1 then
print("KEYDOWN: ", event.code);
elseif event.value == 0 then
print("KEYUP: ", event.code);
if event.code == KEY_ESC then
loop:halt();
return false;
end
elseif event.value == 2 then
print("KEYREP: ", event.code);
end
else
--print("EVENT TYPE: ", UI.EventTypes[event.type][2], "CODE:",event.code, "VALUE: ", string.format("0x%x",event.value));
end
end
function OnTTY(loop, w, revents)
local bufflen = 10;
local buff = ffi.new("char[?]", bufflen);
local bytesread = S.read(w.fd, buff, bufflen);
print("TTY: ", ffi.string(buff, bytesread));
end
function OnMouse(loop, w, revents)
--print("OnMouse: ", w, revents);
local event = input_event();
local bytesread = S.read(w.fd, event, ffi.sizeof(event));
print("MOUSE: ", event.type, event.code, event.value);
end
--[[
Create Observers
--]]
local idler = ev.ev_idle(OnIdle);
--idler:start(loop, true);
local timer = ev.ev_timer(OnTimeout, 1, 2)
--timer:start(loop, true);
-- Keyboard Tracking
local fd = S.open("/dev/input/event0", "O_RDONLY");
local kfd = fd:getfd();
print("FD: ", fd, kfd);
local iowatcher = ev.ev_io(OnKey, kfd, ffi.C.EV_READ);
iowatcher:start(loop, true);
function watchtty(filename)
local tty, err = S.open("/dev/tty0", "O_RDONLY");
if not tty then
print("TTY ERROR: ", err);
return false
end
local ttyfd = tty:getfd();
print("TTY FD: ", ttyfd);
local ttywatcher = ev.ev_io(OnTTY, ttyfd, ffi.C.EV_READ);
ttywatcher:start(loop, true);
end
-- Mouse Tracking
local fd = S.open("/dev/input/event1", "O_RDONLY");
local mfd = fd:getfd();
local mousewatcher = ev.ev_io(OnMouse, mfd, ffi.C.EV_READ);
--mousewatcher:start(loop, true);
-- Run the loop
print(loop:run());
print("Loop Running");