-
Notifications
You must be signed in to change notification settings - Fork 3
/
Keyboard.lua
133 lines (99 loc) · 2.76 KB
/
Keyboard.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
128
129
130
131
132
local ffi = require "ffi"
local bit = require "bit"
local band = bit.band
local lshift = bit.lshift
local S = require "syscall"
local UI = require "input"
local Keyboard = {}
local Keyboard_mt = {
__index = Keyboard,
}
Keyboard.new = function(handlers, devicename)
devicename = devicename or "/dev/input/event0";
-- Create Actual Device Handle
local devicefd, err = S.open(devicename, S.c.O.RDWR);
if not devicefd then
return false, err
end
local obj = handlers or {}
obj.DeviceDescriptor = devicefd;
obj.AlertHandle = devicefd;
obj.WhichAlerts = S.c.POLL.RDNORM;
setmetatable(obj, Keyboard_mt)
return obj
end
Keyboard.OnAlert = function(self, loop, fd, events)
--print("Keyboard.OnAlert: ", fd, events);
-- Read the keyboard device
local event = input_event();
local bytesread, err = S.read(fd, event, ffi.sizeof(event));
if not bytesread then
return false, err
end
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 and self.OnKeyDown then
self:OnKeyDown(event.code);
elseif event.value == 0 and self.OnKeyUp then
self:OnKeyUp(event.code);
elseif event.value == 2 and self.OnKeyRepeat then
self:OnKeyRepeat(event.code, 1);
end
else
--print("EVENT TYPE: ", UI.EventTypes[event.type][2], "CODE:",event.code, "VALUE: ", string.format("0x%x",event.value));
end
end
--[[
Get the current state of all the keys
What is returned is a bitfield with KEY_MAX entries
You can find out the state of any key by checking to
see if the appropriate bit is set.
--]]
Keyboard.GetKeys = function(self)
-- Maybe this buffer should be allocated
-- only once, and simply zeroed for each call
local key_b = ffi.new("unsigned char [?]",KEY_MAX/8 + 1);
success, err = S.ioctl(self.AlertHandle:getfd(), EVIOCGKEY(ffi.sizeof(key_b)), key_b);
if not success then
return false, err
end
return key_b;
end
Keyboard.IsKeyPressed = function(self, keycode)
local keys, err = self:GetKeys()
if not keys then
return false, err
end
return test_bit(keycode, keys);
end
--[[
Return the state of all the LEDs on the keyboard
--]]
Keyboard.GetLEDs = function(self)
end
Keyboard.SetLED = function(self, whichled, state)
local event = input_event();
event.type = EV_LED;
event.code = whichled;
if state then
event.value = 1;
else
event.value = 0;
end
return self.AlertHandle:write(event, ffi.sizeof(event));
end
Keyboard.SetCapsLock = function(self, state)
self:SetLED(LED_CAPSL, state)
end
Keyboard.SetNumLock = function(self, state)
self:SetLED(LED_NUML, state)
end
Keyboard.SetScrollLock = function(self, state)
self:SetLED(LED_SCROLLL, state)
end
return Keyboard;