forked from Hyperion/HyperionOS
130 lines
3.7 KiB
Plaintext
130 lines
3.7 KiB
Plaintext
--:Minify:--
|
|
-- NeetComputers keyboard driver. The bootloader delivers User-queue key
|
|
-- events as ("keyPressed", 1, code, letter, mods) /
|
|
-- ("keyTyped", 1, letter) / ("keyReleased", 1, code, ...); the ncdaemon
|
|
-- task (30_daemon.kmod) feeds the fifo below.
|
|
local kernel=...
|
|
kernel.nc.keyboards={}
|
|
local kbs=kernel.nc.keyboards
|
|
|
|
local keyboard = {}
|
|
|
|
-- NeetComputers key codes: printable keys are ASCII, the rest follows the
|
|
-- emulator/mod keybind tables (Return=13, arrows 128-131, ...).
|
|
keyboard.keys = {
|
|
back = 8,
|
|
delete = 127,
|
|
down = 131,
|
|
enter = 13,
|
|
home = 158,
|
|
lcontrol = 132,
|
|
left = 128,
|
|
lmenu = 133,
|
|
lshift = 14,
|
|
pageDown = 161,
|
|
pageUp = 160,
|
|
rcontrol = 132,
|
|
right = 129,
|
|
rmenu = 133,
|
|
rshift = 14,
|
|
space = 32,
|
|
tab = 9,
|
|
up = 130,
|
|
["end"] = 159,
|
|
numpadenter = 13,
|
|
}
|
|
kernel.nc.keys=keyboard.keys
|
|
|
|
for i=0, 255 do
|
|
kernel.nc.keys[string.char(i)]=i
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function keyboard.isAltDown(addr)
|
|
return kbs[addr] and kbs[addr].alt or false
|
|
end
|
|
|
|
function keyboard.isControl(char)
|
|
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
|
|
end
|
|
|
|
function keyboard.isControlDown(addr)
|
|
return kbs[addr] and kbs[addr].ctrl or false
|
|
end
|
|
|
|
function keyboard.isKeyDown(addr,charOrCode)
|
|
if not kbs[addr] then return false end
|
|
if type(charOrCode) == "string" then
|
|
return kbs[addr].pressedChars[utf8 and utf8.codepoint(charOrCode) or charOrCode:byte()]
|
|
elseif type(charOrCode) == "number" then
|
|
return kbs[addr].pressedCodes[charOrCode]
|
|
end
|
|
end
|
|
|
|
function keyboard.isShiftDown(addr)
|
|
return kbs[addr] and kbs[addr].shift or false
|
|
end
|
|
kernel.nc.keyboard=keyboard
|
|
|
|
local function serializeBool(bool)
|
|
if bool then
|
|
return "T"
|
|
else
|
|
return "F"
|
|
end
|
|
end
|
|
|
|
kbs["1"]={pressedChars={}, pressedCodes={}, events=kernel.newFifo(),
|
|
ctrl=false, alt=false, shift=false}
|
|
kernel.devfs.data["input"]["keyboard1"]=function(op,mode)
|
|
if op=="type" then
|
|
return "Keyboard"
|
|
elseif op=="open" then
|
|
local fifo=kernel.nc.keyboards["1"].events
|
|
local h = {
|
|
read=function(amount)
|
|
amount = amount or 1
|
|
-- Drain whatever is already queued first.
|
|
local rv=""
|
|
for i=1, amount do
|
|
local event = {fifo.pop()}
|
|
if event[1] then
|
|
rv=rv..event[1]
|
|
else
|
|
break
|
|
end
|
|
end
|
|
if rv~="" then return rv end
|
|
-- Nothing queued: block the task until the ncdaemon
|
|
-- feeds input instead of busy-spinning syscalls.
|
|
local task=kernel.currentTask
|
|
task.status="D"
|
|
task.io=function()
|
|
local event = {fifo.pop()}
|
|
if event[1] then return event[1] end
|
|
end
|
|
return nil
|
|
end,
|
|
write=function(content)
|
|
end,
|
|
gctrl=function()
|
|
local kb=kernel.nc.keyboards["1"]
|
|
return serializeBool(kb.ctrl)..";"..serializeBool(kb.alt)
|
|
end,
|
|
isKeyDown=function(char)
|
|
return keyboard.isKeyDown("1",char)
|
|
end
|
|
}
|
|
if mode=="rw" then
|
|
return h
|
|
elseif mode=="r" then
|
|
h["write"]=nil
|
|
return h
|
|
elseif mode=="w" then
|
|
h["read"]=nil
|
|
return h
|
|
end
|
|
end
|
|
end
|