forked from Hyperion/HyperionOS
107 lines
3.4 KiB
Plaintext
107 lines
3.4 KiB
Plaintext
--:Minify:--
|
|
-- NeetComputers input daemon. Consumes kernel machine events produced by
|
|
-- the bootloader event pump and feeds /dev/input/keyboard1. Ctrl+C sends
|
|
-- SIGINT to all tasks, mirroring the OC/CCT daemons.
|
|
local kernel=...
|
|
local NCTRACEBACK = debug.traceback
|
|
local keys=kernel.nc.keys
|
|
kernel.nc.eventhooks=kernel.nc.eventhooks or {}
|
|
|
|
kernel.processes.ncdaemon = function()
|
|
|
|
local timeout = false
|
|
|
|
kernel.log("NC daemon started")
|
|
|
|
local function isCtrl(mods) return (math.floor((tonumber(mods) or 0) / 2) % 2) == 1 end
|
|
|
|
local specialKeyMap = {
|
|
[keys.up] = "\27[A",
|
|
[keys.down] = "\27[B",
|
|
[keys.right] = "\27[C",
|
|
[keys.left] = "\27[D",
|
|
[keys.home] = "\27[H",
|
|
[keys["end"]] = "\27[F",
|
|
[keys.pageUp] = "\27[5~",
|
|
[keys.pageDown] = "\27[6~",
|
|
[keys.delete] = "\27[3~",
|
|
[keys.enter] = "\n",
|
|
[keys.back] = "\b",
|
|
[keys.tab] = "\t",
|
|
}
|
|
|
|
while true do
|
|
|
|
local event = {kernel.EFI.getMachineEvent()}
|
|
|
|
if event[1] then
|
|
|
|
local eventType = event[1]
|
|
local kb = kernel.nc.keyboards["1"]
|
|
|
|
if eventType == "keyPressed" then
|
|
local code, letter, mods = event[3], event[4], event[5]
|
|
kb.pressedCodes[code] = true
|
|
if type(letter) == "string" and #letter == 1 then
|
|
kb.pressedChars[letter:byte()] = true
|
|
end
|
|
if isCtrl(mods) then
|
|
kb.ctrl = true
|
|
local ch = nil
|
|
if type(letter) == "string" and #letter == 1 then
|
|
ch = letter:lower():byte()
|
|
elseif type(code) == "number" and code >= 64 and code <= 95 then
|
|
ch = code
|
|
end
|
|
if ch and ch >= 97 and ch <= 122 then
|
|
local byte = ch - 96
|
|
if byte == 3 then
|
|
for _, task in ipairs(syscall.getTasks()) do
|
|
syscall.sigsend(task, 1)
|
|
end
|
|
else
|
|
kb.events.push(string.char(byte))
|
|
end
|
|
end
|
|
else
|
|
local special = specialKeyMap[code]
|
|
if special then
|
|
kb.events.push(special)
|
|
end
|
|
end
|
|
|
|
elseif eventType == "keyReleased" then
|
|
local code = event[3]
|
|
kb.pressedCodes[code] = nil
|
|
if type(event[4]) == "string" and #event[4] == 1 then
|
|
kb.pressedChars[event[4]:byte()] = nil
|
|
end
|
|
if not isCtrl(event[5]) then kb.ctrl = false end
|
|
|
|
elseif eventType == "keyTyped" then
|
|
if event[3] then kb.events.push(event[3]) end
|
|
|
|
else
|
|
if kernel.nc.eventhooks[eventType] then
|
|
local ok,err = xpcall(kernel.nc.eventhooks[eventType], NCTRACEBACK, table.unpack(event))
|
|
if not ok then
|
|
kernel.log("Error on eventhook "..eventType.."\n"..err)
|
|
end
|
|
end
|
|
end
|
|
|
|
timeout = false
|
|
|
|
else
|
|
timeout = true
|
|
end
|
|
|
|
if timeout then
|
|
sleep(0.05)
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
kernel.log("NC daemon queued for execution")
|