forked from Hyperion/HyperionOS
NC firmware update (thanks spsf). opencomputers now uses mutiple gpus to speed up rendering and now has net drivers. currently working on kernel memory protections to prevent malicious kernel drivers. spm does not work on opencomputers, this is a known issue i am working on fixing it :D sorry for the slow development.
This commit is contained in:
1 parent
fbaafc7cff
commit
ef4b856ee6
89 files changed
+3955
-563
No files matched your search
@@ -1,41 +1,73 @@
|
||||
|
||||
local args = {...}
|
||||
local apis = args[1]
|
||||
local bootdrive = args[2]
|
||||
local bootaddr = args[3]
|
||||
|
||||
local disks = {}
|
||||
local internal = {}
|
||||
|
||||
local function normalize(path)
|
||||
if type(path) ~= "string" then
|
||||
return path
|
||||
end
|
||||
|
||||
-- Preserve root directory.
|
||||
if path == "/" then
|
||||
return "/"
|
||||
end
|
||||
|
||||
-- Remove trailing slashes.
|
||||
path = path:gsub("/+$", "")
|
||||
|
||||
return path == "" and "/" or path
|
||||
end
|
||||
|
||||
local function createDisk(id, hw)
|
||||
local disk = {
|
||||
address = id,
|
||||
isReadOnly = function() return hw.isReadOnly() end
|
||||
isReadOnly = function()
|
||||
return hw.isReadOnly()
|
||||
end
|
||||
}
|
||||
|
||||
function disk:spaceUsed() return hw.spaceUsed() end
|
||||
function disk:spaceTotal() return hw.spaceTotal() end
|
||||
|
||||
function disk:spaceUsed()
|
||||
return hw.spaceUsed()
|
||||
end
|
||||
|
||||
function disk:spaceTotal()
|
||||
return hw.spaceTotal()
|
||||
end
|
||||
|
||||
function disk:list(path)
|
||||
path = normalize(path)
|
||||
|
||||
if not hw.exists(path) or not hw.isDirectory(path) then
|
||||
return nil, "not directory"
|
||||
end
|
||||
local ret={}
|
||||
for i,v in ipairs(hw.list(path)) do
|
||||
if v:sub(-1)=="/" then
|
||||
ret[i]=v:sub(1,#v-1)
|
||||
else ret[i]=v end
|
||||
|
||||
local ret = {}
|
||||
|
||||
for i, v in ipairs(hw.list(path)) do
|
||||
ret[i] = normalize(v)
|
||||
end
|
||||
|
||||
return ret
|
||||
end
|
||||
|
||||
function disk:fileExists(path)
|
||||
path = normalize(path)
|
||||
return hw.exists(path) and not hw.isDirectory(path)
|
||||
end
|
||||
|
||||
function disk:directoryExists(path)
|
||||
path = normalize(path)
|
||||
return hw.exists(path) and hw.isDirectory(path)
|
||||
end
|
||||
|
||||
function disk:type(path)
|
||||
path = normalize(path)
|
||||
|
||||
if not hw.exists(path) then
|
||||
return nil
|
||||
elseif hw.isDirectory(path) then
|
||||
@@ -46,17 +78,29 @@ local function createDisk(id, hw)
|
||||
end
|
||||
|
||||
function disk:makeDirectory(path)
|
||||
hw.makeDirectory(path)
|
||||
path = normalize(path)
|
||||
return hw.makeDirectory(path)
|
||||
end
|
||||
|
||||
function disk:remove(path)
|
||||
if hw.exists(path) then hw.remove(path) end
|
||||
path = normalize(path)
|
||||
|
||||
if hw.exists(path) then
|
||||
return hw.remove(path)
|
||||
end
|
||||
end
|
||||
|
||||
function disk:setLabel(label) hw.setLabel(label) end
|
||||
function disk:getLabel() return hw.getLabel() end
|
||||
function disk:setLabel(label)
|
||||
return hw.setLabel(label)
|
||||
end
|
||||
|
||||
function disk:getLabel()
|
||||
return hw.getLabel()
|
||||
end
|
||||
|
||||
function disk:attributes(path)
|
||||
path = normalize(path)
|
||||
|
||||
return {
|
||||
size = hw.size(path),
|
||||
isDir = hw.isDirectory(path),
|
||||
@@ -67,6 +111,8 @@ local function createDisk(id, hw)
|
||||
end
|
||||
|
||||
function disk:open(path, mode)
|
||||
path = normalize(path)
|
||||
|
||||
local fd = hw.open(path, mode)
|
||||
|
||||
if not fd then
|
||||
@@ -74,43 +120,67 @@ local function createDisk(id, hw)
|
||||
|
||||
if dir and not hw.exists(dir) then
|
||||
local ok, err = hw.makeDirectory(dir)
|
||||
|
||||
if not ok then
|
||||
return nil, err or "could not create parent directory"
|
||||
end
|
||||
end
|
||||
|
||||
fd = hw.open(path, mode)
|
||||
|
||||
if not fd then
|
||||
return nil, "file not found or access denied"
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
read = function(num) return hw.read(fd, num) end,
|
||||
read = function(num)
|
||||
return hw.read(fd, num)
|
||||
end,
|
||||
|
||||
readAll = function()
|
||||
local data = {}
|
||||
|
||||
while true do
|
||||
local chunk = hw.read(fd, math.huge)
|
||||
if not chunk then break end
|
||||
local chunk, err = hw.read(fd, 2048)
|
||||
|
||||
if chunk == nil then
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
break
|
||||
end
|
||||
|
||||
if #chunk == 0 then
|
||||
break
|
||||
end
|
||||
|
||||
data[#data + 1] = chunk
|
||||
end
|
||||
|
||||
return table.concat(data)
|
||||
end,
|
||||
|
||||
write = function(data) return hw.write(fd, data) end,
|
||||
write = function(data)
|
||||
return hw.write(fd, data)
|
||||
end,
|
||||
|
||||
seek = function(whence, offset)
|
||||
return hw.seek(fd, whence, offset)
|
||||
end,
|
||||
|
||||
flush = function()
|
||||
hw.close(fd)
|
||||
fd = hw.open(path, mode)
|
||||
if fd then
|
||||
hw.close(fd)
|
||||
fd = hw.open(path, mode)
|
||||
end
|
||||
end,
|
||||
|
||||
close = function()
|
||||
hw.close(fd)
|
||||
fd = nil
|
||||
if fd then
|
||||
hw.close(fd)
|
||||
fd = nil
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
@@ -122,21 +192,34 @@ internal["$"] = createDisk("$", bootdrive)
|
||||
|
||||
local function refresh()
|
||||
disks = {}
|
||||
|
||||
for addr in apis.component.list("filesystem") do
|
||||
if addr ~= bootaddr then
|
||||
disks[addr] = createDisk(addr, apis.component.proxy(addr))
|
||||
disks[addr] = createDisk(
|
||||
addr,
|
||||
apis.component.proxy(addr)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function iter()
|
||||
refresh()
|
||||
|
||||
local combined = {}
|
||||
|
||||
for id, obj in pairs(internal) do combined[id] = obj end
|
||||
for id, obj in pairs(disks) do combined[id] = obj end
|
||||
for id, obj in pairs(internal) do
|
||||
combined[id] = obj
|
||||
end
|
||||
|
||||
for id, obj in pairs(disks) do
|
||||
combined[id] = obj
|
||||
end
|
||||
|
||||
return pairs(combined)
|
||||
end
|
||||
|
||||
return {refresh = refresh, list = iter}
|
||||
return {
|
||||
refresh = refresh,
|
||||
list = iter
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,442 @@
|
||||
--:Minify:--
|
||||
local kernel = ...
|
||||
|
||||
local component = kernel.oc and kernel.oc.component
|
||||
local internetAddress
|
||||
|
||||
for address in component.list("internet") do
|
||||
internetAddress = address
|
||||
break
|
||||
end
|
||||
|
||||
if not internetAddress then
|
||||
if kernel.log then
|
||||
kernel.log("NET: No Internet component available")
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local internet = component.proxy(internetAddress)
|
||||
local tcpEnabled = false
|
||||
local httpEnabled = false
|
||||
|
||||
if internet.isTcpEnabled then
|
||||
local ok, result = pcall(internet.isTcpEnabled)
|
||||
tcpEnabled = ok and result == true
|
||||
end
|
||||
|
||||
if internet.isHttpEnabled then
|
||||
local ok, result = pcall(internet.isHttpEnabled)
|
||||
httpEnabled = ok and result == true
|
||||
end
|
||||
|
||||
local function closeHandle(handle)
|
||||
if handle and handle.close then
|
||||
pcall(handle.close)
|
||||
end
|
||||
end
|
||||
|
||||
local function parseRawRequest(raw, address)
|
||||
local headerEnd = raw:find("\r\n\r\n", 1, true)
|
||||
|
||||
local headerPart
|
||||
local body = ""
|
||||
|
||||
if headerEnd then
|
||||
headerPart = raw:sub(1, headerEnd - 1)
|
||||
body = raw:sub(headerEnd + 4)
|
||||
else
|
||||
headerPart = raw
|
||||
end
|
||||
|
||||
local lines = {}
|
||||
|
||||
for line in headerPart:gmatch("[^\r\n]+") do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
|
||||
if #lines == 0 then
|
||||
return nil, "EINVAL"
|
||||
end
|
||||
|
||||
local method, path, ver =
|
||||
lines[1]:match("^(%S+)%s+(%S+)%s+(%S+)$")
|
||||
|
||||
if not method then
|
||||
return nil, "EINVAL"
|
||||
end
|
||||
|
||||
if path:sub(1, 1) ~= "/" then
|
||||
return nil, "EINVAL"
|
||||
end
|
||||
|
||||
local headers = {}
|
||||
|
||||
for i = 2, #lines do
|
||||
local k, v = lines[i]:match("^([^:]+):%s*(.*)$")
|
||||
|
||||
if k then
|
||||
headers[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
local url = address .. path
|
||||
|
||||
return {
|
||||
url = url,
|
||||
method = method,
|
||||
headers = headers,
|
||||
body = body
|
||||
}, url
|
||||
end
|
||||
|
||||
local function buildHTTPResponse(req)
|
||||
if req.method ~= "GET" and req.method ~= "POST" then
|
||||
return nil, "EOPNOTSUPP"
|
||||
end
|
||||
|
||||
local postData
|
||||
|
||||
if req.method == "POST" then
|
||||
postData = req.body or ""
|
||||
end
|
||||
|
||||
local ok, response = pcall(
|
||||
internet.request,
|
||||
req.url,
|
||||
postData,
|
||||
req.headers
|
||||
)
|
||||
|
||||
if not ok or not response then
|
||||
return nil, tostring(response or "ECONN")
|
||||
end
|
||||
|
||||
local connected, connectResult = pcall(
|
||||
response.finishConnect
|
||||
)
|
||||
|
||||
if not connected or connectResult == false then
|
||||
closeHandle(response)
|
||||
return nil, tostring(connectResult or "ECONN")
|
||||
end
|
||||
|
||||
local code = 200
|
||||
local msg = "OK"
|
||||
local headers = {}
|
||||
|
||||
if response.response then
|
||||
local responseOK, c, m, h = pcall(response.response)
|
||||
|
||||
if responseOK then
|
||||
code = c or code
|
||||
msg = m or msg
|
||||
headers = h or headers
|
||||
end
|
||||
end
|
||||
|
||||
local chunks = {}
|
||||
|
||||
while true do
|
||||
local readOK, chunk = pcall(response.read, 8192)
|
||||
|
||||
if not readOK then
|
||||
closeHandle(response)
|
||||
return nil, tostring(chunk)
|
||||
end
|
||||
|
||||
if not chunk or #chunk == 0 then
|
||||
break
|
||||
end
|
||||
|
||||
chunks[#chunks + 1] = chunk
|
||||
end
|
||||
|
||||
closeHandle(response)
|
||||
|
||||
local out = {
|
||||
"HTTP/1.1 " .. tostring(code) .. " " .. tostring(msg)
|
||||
}
|
||||
|
||||
for k, v in pairs(headers) do
|
||||
if type(v) == "table" then
|
||||
for i = 1, #v do
|
||||
out[#out + 1] = k .. ": " .. tostring(v[i])
|
||||
end
|
||||
else
|
||||
out[#out + 1] = k .. ": " .. tostring(v)
|
||||
end
|
||||
end
|
||||
|
||||
out[#out + 1] = ""
|
||||
out[#out + 1] = table.concat(chunks)
|
||||
|
||||
return table.concat(out, "\r\n")
|
||||
end
|
||||
|
||||
local function closeFile(socket)
|
||||
if socket.file then
|
||||
pcall(socket.file.close)
|
||||
socket.file = nil
|
||||
end
|
||||
end
|
||||
|
||||
local httpHandler = {}
|
||||
|
||||
function httpHandler.connect(fd, address)
|
||||
local fdo = kernel.currentTask.fd[fd]
|
||||
|
||||
if not fdo then
|
||||
return nil, "EBADF"
|
||||
end
|
||||
|
||||
fdo.socket = fdo.socket or {}
|
||||
|
||||
local socket = fdo.socket
|
||||
|
||||
socket.active = false
|
||||
socket.loaded = false
|
||||
socket.file = nil
|
||||
|
||||
fdo.handle.write = function(raw)
|
||||
local req, url = parseRawRequest(raw, address)
|
||||
|
||||
if not req then
|
||||
return nil, url
|
||||
end
|
||||
|
||||
closeFile(socket)
|
||||
|
||||
socket.active = true
|
||||
socket.loaded = false
|
||||
socket.url = url
|
||||
|
||||
local response, err = buildHTTPResponse(req)
|
||||
|
||||
if not response then
|
||||
socket.active = false
|
||||
socket.error = err
|
||||
return nil, err
|
||||
end
|
||||
|
||||
socket.content = response
|
||||
socket.file = kernel.sfile(response)
|
||||
socket.loaded = true
|
||||
socket.error = nil
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
fdo.handle.read = function(count)
|
||||
if not socket.active then
|
||||
return
|
||||
end
|
||||
|
||||
if socket.error then
|
||||
return nil, socket.error
|
||||
end
|
||||
|
||||
if not socket.file then
|
||||
return
|
||||
end
|
||||
|
||||
return socket.file.read(count)
|
||||
end
|
||||
|
||||
fdo.handle.seek = function(whence, offset)
|
||||
if not socket.active or not socket.file then
|
||||
return
|
||||
end
|
||||
|
||||
return socket.file.seek(whence, offset)
|
||||
end
|
||||
|
||||
fdo.handle.close = function()
|
||||
closeFile(socket)
|
||||
|
||||
socket.active = false
|
||||
socket.loaded = false
|
||||
socket.content = nil
|
||||
socket.url = nil
|
||||
socket.error = nil
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local tcpHandler = {}
|
||||
|
||||
local function parseTCPAddress(address)
|
||||
local host, port
|
||||
|
||||
if address:sub(1, 1) == "[" then
|
||||
host, port = address:match("^%[([^%]]+)%]:(%d+)$")
|
||||
|
||||
if not host then
|
||||
host = address:match("^%[([^%]]+)%]$")
|
||||
end
|
||||
else
|
||||
host, port = address:match("^([^:]+):(%d+)$")
|
||||
|
||||
if not host then
|
||||
host = address
|
||||
end
|
||||
end
|
||||
|
||||
if not host or host == "" then
|
||||
return nil, nil, "EINVAL"
|
||||
end
|
||||
|
||||
if port then
|
||||
port = tonumber(port)
|
||||
|
||||
if not port or port < 1 or port > 65535 then
|
||||
return nil, nil, "EINVAL"
|
||||
end
|
||||
end
|
||||
|
||||
return host, port
|
||||
end
|
||||
|
||||
function tcpHandler.connect(fd, address)
|
||||
if not tcpEnabled then
|
||||
return nil, "EOPNOTSUPP"
|
||||
end
|
||||
|
||||
local fdo = kernel.currentTask.fd[fd]
|
||||
|
||||
if not fdo then
|
||||
return nil, "EBADF"
|
||||
end
|
||||
|
||||
local host, port, err = parseTCPAddress(address)
|
||||
|
||||
if not host then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
fdo.socket = fdo.socket or {}
|
||||
|
||||
local socket = fdo.socket
|
||||
|
||||
if socket.tcp then
|
||||
closeHandle(socket.tcp)
|
||||
end
|
||||
|
||||
socket.tcp = nil
|
||||
socket.active = false
|
||||
socket.error = nil
|
||||
socket.host = host
|
||||
socket.port = port
|
||||
|
||||
local ok, tcp = pcall(
|
||||
internet.connect,
|
||||
host,
|
||||
port
|
||||
)
|
||||
|
||||
if not ok or not tcp then
|
||||
return nil, tostring(tcp or "ECONN")
|
||||
end
|
||||
|
||||
socket.tcp = tcp
|
||||
socket.active = true
|
||||
|
||||
local connected, result = pcall(
|
||||
tcp.finishConnect
|
||||
)
|
||||
|
||||
if not connected or result == false then
|
||||
closeHandle(tcp)
|
||||
|
||||
socket.tcp = nil
|
||||
socket.active = false
|
||||
|
||||
return nil, tostring(result or "ECONN")
|
||||
end
|
||||
|
||||
fdo.handle.write = function(data)
|
||||
if not socket.active or not socket.tcp then
|
||||
return nil, "ENOTCONN"
|
||||
end
|
||||
|
||||
if type(data) ~= "string" then
|
||||
return nil, "EINVAL"
|
||||
end
|
||||
|
||||
local writeOK, written = pcall(
|
||||
socket.tcp.write,
|
||||
data
|
||||
)
|
||||
|
||||
if not writeOK then
|
||||
return nil, tostring(written)
|
||||
end
|
||||
|
||||
return written
|
||||
end
|
||||
|
||||
fdo.handle.read = function(count)
|
||||
if not socket.active or not socket.tcp then
|
||||
return nil, "ENOTCONN"
|
||||
end
|
||||
|
||||
local readOK, data = pcall(
|
||||
socket.tcp.read,
|
||||
count
|
||||
)
|
||||
|
||||
if not readOK then
|
||||
return nil, tostring(data)
|
||||
end
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
fdo.handle.seek = function()
|
||||
return nil, "ESPIPE"
|
||||
end
|
||||
|
||||
fdo.handle.close = function()
|
||||
closeHandle(socket.tcp)
|
||||
|
||||
socket.tcp = nil
|
||||
socket.active = false
|
||||
socket.error = nil
|
||||
socket.host = nil
|
||||
socket.port = nil
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
if httpEnabled then
|
||||
kernel.socket.registerProtocal(
|
||||
"http://",
|
||||
httpHandler
|
||||
)
|
||||
|
||||
kernel.socket.registerProtocal(
|
||||
"https://",
|
||||
httpHandler
|
||||
)
|
||||
|
||||
if kernel.log then
|
||||
kernel.log("NET: HTTP/HTTPS initialized")
|
||||
end
|
||||
end
|
||||
|
||||
if tcpEnabled then
|
||||
kernel.socket.registerProtocal(
|
||||
"tcp://",
|
||||
tcpHandler
|
||||
)
|
||||
|
||||
if kernel.log then
|
||||
kernel.log("NET: TCP initialized")
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,3 @@
|
||||
-- OpenComputers TTY driver
|
||||
|
||||
local kernel = ...
|
||||
local component = kernel.oc.component
|
||||
@@ -26,7 +25,6 @@ local plt = {
|
||||
0x660000, 0x660040, 0x660080, 0x6600C0, 0x6600FF,
|
||||
0x662400, 0x662440, 0x662480, 0x6624C0, 0x6624FF,
|
||||
0x664900, 0x664940, 0x664980, 0x6649C0, 0x6649FF,
|
||||
0x666D00, 0x666D40, 0x666D80, 0x666DC0, 0x666DFF,
|
||||
0x669200, 0x669240, 0x669280, 0x6692C0, 0x6692FF,
|
||||
0x66B600, 0x66B640, 0x66B680, 0x66B6C0, 0x66B6FF,
|
||||
0x66DB00, 0x66DB40, 0x66DB80, 0x66DBC0, 0x66DBFF,
|
||||
@@ -65,116 +63,328 @@ local plt = {
|
||||
0xB4B4B4, 0xC3C3C3, 0xD2D2D2, 0xE1E1E1
|
||||
}
|
||||
|
||||
local gpus,screens = {},{}
|
||||
local gpus = {}
|
||||
local screens = {}
|
||||
|
||||
-- Collect GPUs.
|
||||
for addr, typ in component.list("gpu") do
|
||||
if typ == "gpu" then
|
||||
gpus[#gpus+1]=component.proxy(addr)
|
||||
local gpu = component.proxy(addr)
|
||||
|
||||
gpus[#gpus + 1] = {
|
||||
addr = addr,
|
||||
gpu = gpu,
|
||||
screen = nil,
|
||||
fg = nil,
|
||||
bg = nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
if #gpus>0 then
|
||||
for addr, typ in component.list("screen") do
|
||||
if typ == "screen" then
|
||||
gpus[1].bind(addr)
|
||||
local w,h = gpus[1].maxResolution()
|
||||
gpus[1].setResolution(w,h)
|
||||
screens[addr]={cx=1,cy=1,w=w,h=h,addr=addr,fg=0,bg=0,gpu=nil}
|
||||
-- Collect screens.
|
||||
for addr, typ in component.list("screen") do
|
||||
if typ == "screen" then
|
||||
screens[addr] = {
|
||||
cx = 1,
|
||||
cy = 1,
|
||||
w = 0,
|
||||
h = 0,
|
||||
addr = addr,
|
||||
fg = 0xFFFFFF,
|
||||
bg = 0x000000,
|
||||
gpu = nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
-- Count screens.
|
||||
local scrnum = 0
|
||||
|
||||
for _ in pairs(screens) do
|
||||
scrnum = scrnum + 1
|
||||
end
|
||||
|
||||
-- Find a GPU that can bind to a screen.
|
||||
local function bindGPU(scr)
|
||||
if scr.gpu then
|
||||
local state = scr.gpu
|
||||
|
||||
if state.screen == scr.addr then
|
||||
return state
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1, #gpus do
|
||||
local state = gpus[i]
|
||||
|
||||
-- Avoid unnecessary rebinding.
|
||||
if state.screen == scr.addr then
|
||||
scr.gpu = state
|
||||
return state
|
||||
end
|
||||
|
||||
-- Try binding this GPU.
|
||||
local ok, result = pcall(state.gpu.bind, scr.addr)
|
||||
|
||||
if ok and result ~= false then
|
||||
state.screen = scr.addr
|
||||
state.fg = nil
|
||||
state.bg = nil
|
||||
|
||||
scr.gpu = state
|
||||
|
||||
local w, h = state.gpu.maxResolution()
|
||||
|
||||
scr.w = w
|
||||
scr.h = h
|
||||
|
||||
pcall(state.gpu.setResolution, w, h)
|
||||
|
||||
return state
|
||||
end
|
||||
end
|
||||
|
||||
error("No GPU available for screen " .. tostring(scr.addr))
|
||||
end
|
||||
|
||||
-- Initialize screens.
|
||||
for addr, scr in pairs(screens) do
|
||||
local ok, result = pcall(bindGPU, scr)
|
||||
|
||||
if not ok then
|
||||
kernel.log(
|
||||
"Failed to initialize screen " ..
|
||||
tostring(addr) .. ": " .. tostring(result)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
kernel.log(
|
||||
"initialized " ..
|
||||
tostring(#gpus) ..
|
||||
" gpus and " ..
|
||||
tostring(scrnum) ..
|
||||
" screens"
|
||||
)
|
||||
|
||||
local newid = 0
|
||||
|
||||
-- Get the GPU currently assigned to a screen.
|
||||
local function getGPU(scr)
|
||||
local state = scr.gpu
|
||||
|
||||
if not state then
|
||||
state = bindGPU(scr)
|
||||
end
|
||||
|
||||
if state.screen ~= scr.addr then
|
||||
local ok, result = pcall(state.gpu.bind, scr.addr)
|
||||
|
||||
if not ok or result == false then
|
||||
state.screen = nil
|
||||
state.fg = nil
|
||||
state.bg = nil
|
||||
|
||||
scr.gpu = nil
|
||||
|
||||
state = bindGPU(scr)
|
||||
else
|
||||
state.screen = scr.addr
|
||||
state.fg = nil
|
||||
state.bg = nil
|
||||
end
|
||||
end
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
-- Set colors only when they actually change.
|
||||
local function setColors(state, scr)
|
||||
local gpu = state.gpu
|
||||
|
||||
if state.fg ~= scr.fg then
|
||||
gpu.setForeground(scr.fg)
|
||||
state.fg = scr.fg
|
||||
end
|
||||
|
||||
if state.bg ~= scr.bg then
|
||||
gpu.setBackground(scr.bg)
|
||||
state.bg = scr.bg
|
||||
end
|
||||
end
|
||||
|
||||
-- Scroll terminal.
|
||||
local function scroll(scr, gpu)
|
||||
gpu.copy(
|
||||
1, 2,
|
||||
scr.w, scr.h - 1,
|
||||
0, -1
|
||||
)
|
||||
|
||||
gpu.fill(
|
||||
1, scr.h,
|
||||
scr.w, 1,
|
||||
" "
|
||||
)
|
||||
end
|
||||
|
||||
-- Write terminal output.
|
||||
local function write(text, addr)
|
||||
local scr = screens[addr]
|
||||
|
||||
if not scr then
|
||||
error("NODEV")
|
||||
end
|
||||
|
||||
local state = getGPU(scr)
|
||||
local gpu = state.gpu
|
||||
|
||||
setColors(state, scr)
|
||||
|
||||
for i = 1, #text do
|
||||
local char = text:sub(i, i)
|
||||
|
||||
if char == "\n" then
|
||||
scr.cx = 1
|
||||
scr.cy = scr.cy + 1
|
||||
|
||||
elseif char == "\t" then
|
||||
scr.cx = scr.cx + (4 - ((scr.cx - 1) % 4))
|
||||
|
||||
elseif char == "\b" then
|
||||
if scr.cx > 1 then
|
||||
scr.cx = scr.cx - 1
|
||||
gpu.set(scr.cx, scr.cy, " ")
|
||||
end
|
||||
|
||||
else
|
||||
gpu.set(scr.cx, scr.cy, char)
|
||||
scr.cx = scr.cx + 1
|
||||
end
|
||||
|
||||
if scr.cx > scr.w then
|
||||
scr.cx = 1
|
||||
scr.cy = scr.cy + 1
|
||||
end
|
||||
|
||||
if scr.cy > scr.h then
|
||||
scroll(scr, gpu)
|
||||
scr.cy = scr.cy - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local scrnum = 0
|
||||
for i,v in pairs(screens) do
|
||||
scrnum=scrnum+1
|
||||
end
|
||||
|
||||
kernel.log("initialized "..tostring(#gpus).." gpus and "..tostring(scrnum).." screens")
|
||||
|
||||
local binds,newid={},0
|
||||
local curaddr=""
|
||||
local function scroll(scr, gpu) gpu.copy(1,2,scr.w,scr.h-1,0,-1);gpu.fill(1,scr.h,scr.w,1," ") end
|
||||
local function write(text, addr)
|
||||
local g=gpus[1]
|
||||
local scr=screens[addr]
|
||||
if not scr then error("NODEV") end
|
||||
if curaddr~=addr then g.bind(addr); g.setForeground(scr.fg); g.setBackground(scr.bg) end
|
||||
for i=1,#text do
|
||||
local char=text:sub(i,i)
|
||||
if char=="\n" then scr.cx=1;scr.cy=scr.cy+1
|
||||
elseif char=="\t" then scr.cx=scr.cx+(4-((scr.cx-1)%4))
|
||||
elseif char=="\b" then if scr.cx>1 then scr.cx=scr.cx-1;g.set(scr.cx,scr.cy," ") end
|
||||
else g.set(scr.cx,scr.cy,char);scr.cx=scr.cx+1 end
|
||||
if scr.cx>scr.w then scr.cx=1;scr.cy=scr.cy+1 end
|
||||
if scr.cy>scr.h then scroll(scr, g);scr.cy=scr.cy-1 end
|
||||
end
|
||||
end
|
||||
|
||||
-- Create TTY.
|
||||
local function newtty(addr)
|
||||
newid=newid+1
|
||||
kernel.devfs.data["tty"][tostring(newid)]=function(op, mode)
|
||||
if op=="type" then
|
||||
newid = newid + 1
|
||||
|
||||
kernel.devfs.data["tty"][tostring(newid)] = function(op, mode)
|
||||
if op == "type" then
|
||||
return "Terminal"
|
||||
elseif op=="open" then
|
||||
|
||||
elseif op == "open" then
|
||||
local h = {
|
||||
read=function(amount) end,
|
||||
write=function(content)
|
||||
read = function(amount)
|
||||
end,
|
||||
|
||||
write = function(content)
|
||||
write(content, addr)
|
||||
end,
|
||||
size=function()
|
||||
return screens[addr].w, screens[addr].h
|
||||
|
||||
size = function()
|
||||
local scr = screens[addr]
|
||||
return scr.w, scr.h
|
||||
end,
|
||||
clear=function()
|
||||
local g=gpus[1]
|
||||
local scr=screens[addr]
|
||||
if addr~=curaddr then g.bind(addr) end
|
||||
g.fill(1,1,scr.w,scr.h," ");
|
||||
scr.cx,scr.cy=1,1
|
||||
|
||||
clear = function()
|
||||
local scr = screens[addr]
|
||||
local state = getGPU(scr)
|
||||
local gpu = state.gpu
|
||||
|
||||
setColors(state, scr)
|
||||
|
||||
gpu.fill(
|
||||
1, 1,
|
||||
scr.w, scr.h,
|
||||
" "
|
||||
)
|
||||
|
||||
scr.cx = 1
|
||||
scr.cy = 1
|
||||
end,
|
||||
gpos=function()
|
||||
return screens[addr].cx, screens[addr].cy
|
||||
|
||||
gpos = function()
|
||||
local scr = screens[addr]
|
||||
return scr.cx, scr.cy
|
||||
end,
|
||||
spos=function(x,y)
|
||||
if not tonumber(x) or not tonumber(y) then error("Invailid arg") end
|
||||
screens[addr].cx, screens[addr].cy = x,y
|
||||
|
||||
spos = function(x, y)
|
||||
if not tonumber(x) or not tonumber(y) then
|
||||
error("Invalid arg")
|
||||
end
|
||||
|
||||
local scr = screens[addr]
|
||||
|
||||
scr.cx = x
|
||||
scr.cy = y
|
||||
end,
|
||||
sfgc=function(c)
|
||||
screens[addr].fg=c
|
||||
|
||||
sfgc = function(c)
|
||||
local scr = screens[addr]
|
||||
|
||||
if scr.fg ~= c then
|
||||
scr.fg = c
|
||||
end
|
||||
end,
|
||||
sbgc=function(c)
|
||||
screens[addr].bg=c
|
||||
|
||||
sbgc = function(c)
|
||||
local scr = screens[addr]
|
||||
|
||||
if scr.bg ~= c then
|
||||
scr.bg = c
|
||||
end
|
||||
end,
|
||||
gfgc=function()
|
||||
|
||||
gfgc = function()
|
||||
return screens[addr].fg
|
||||
end,
|
||||
gbgc=function()
|
||||
|
||||
gbgc = function()
|
||||
return screens[addr].bg
|
||||
end,
|
||||
isvirt=function()
|
||||
|
||||
isvirt = function()
|
||||
return false
|
||||
end,
|
||||
gplt=function()
|
||||
|
||||
gplt = function()
|
||||
return plt
|
||||
end,
|
||||
gfx=function()
|
||||
|
||||
gfx = function()
|
||||
return false
|
||||
end
|
||||
}
|
||||
if mode=="rw" then
|
||||
|
||||
if mode == "rw" then
|
||||
return h
|
||||
elseif mode=="r" then
|
||||
h["write"]=nil
|
||||
|
||||
elseif mode == "r" then
|
||||
h.write = nil
|
||||
return h
|
||||
elseif mode=="w" then
|
||||
h["read"]=nil
|
||||
|
||||
elseif mode == "w" then
|
||||
h.read = nil
|
||||
return h
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if #gpus>=1 and scrnum>=1 then
|
||||
for addr,obj in pairs(screens) do
|
||||
newtty(addr)
|
||||
if #gpus >= 1 and scrnum >= 1 then
|
||||
for addr, scr in pairs(screens) do
|
||||
if scr.gpu then
|
||||
newtty(addr)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,19 +7,19 @@ local kbidx=0
|
||||
local keyboard = {}
|
||||
|
||||
keyboard.keys = {
|
||||
back = 0x0E, -- backspace
|
||||
back = 0x0E,
|
||||
delete = 0xD3,
|
||||
down = 0xD0,
|
||||
enter = 0x1C,
|
||||
home = 0xC7,
|
||||
lcontrol = 0x1D,
|
||||
left = 0xCB,
|
||||
lmenu = 0x38, -- left Alt
|
||||
lmenu = 0x38,
|
||||
lshift = 0x2A,
|
||||
pageDown = 0xD1,
|
||||
rcontrol = 0x9D,
|
||||
right = 0xCD,
|
||||
rmenu = 0xB8, -- right Alt
|
||||
rmenu = 0xB8,
|
||||
rshift = 0x36,
|
||||
space = 0x39,
|
||||
tab = 0x0F,
|
||||
@@ -33,8 +33,6 @@ for i=0, 255 do
|
||||
kernel.oc.keys[string.char(i)]=i
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
function keyboard.isAltDown(addr)
|
||||
return kbs[addr].pressedCodes[keyboard.keys.lmenu] or kbs[addr].pressedCodes[keyboard.keys.rmenu]
|
||||
end
|
||||
@@ -78,15 +76,24 @@ for addr, typ in kernel.oc.component.list("keyboard") do
|
||||
elseif op=="open" then
|
||||
local h = {
|
||||
read=function(amount)
|
||||
amount = amount or 1
|
||||
local rv=""
|
||||
for i=1, amount or 1 do
|
||||
for i=1, amount do
|
||||
local event = {kernel.oc.keyboards[addr].events.pop()}
|
||||
if event[1] then
|
||||
rv=rv..event[1]
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
if rv=="" then rv=nil end
|
||||
return rv
|
||||
if rv~="" then return rv end
|
||||
local task=kernel.currentTask
|
||||
task.status="D"
|
||||
task.io=function()
|
||||
local event = {kernel.oc.keyboards[addr].events.pop()}
|
||||
if event[1] then return event[1] end
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
write=function(content)
|
||||
end,
|
||||
|
||||
@@ -27,8 +27,6 @@ kernel.processes.ocdaemon = function()
|
||||
[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",
|
||||
|
||||
@@ -10,7 +10,6 @@ local lambda_var = 0.02
|
||||
local k_min = 0.5
|
||||
local k_max = 0.5
|
||||
local B = 0.01
|
||||
local nextyield=0
|
||||
|
||||
function kernel.main()
|
||||
kernel.log("Starting main loop...")
|
||||
@@ -18,6 +17,9 @@ function kernel.main()
|
||||
local stopLog=5
|
||||
local logTasks=100
|
||||
|
||||
local burstSteps = 0
|
||||
local maxBurstSteps = 64
|
||||
|
||||
while not kernel.exitMain do
|
||||
if kernel.config.logTasks then
|
||||
if logTasks<0 then
|
||||
@@ -121,6 +123,11 @@ function kernel.main()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
burstSteps = burstSteps + N
|
||||
if N == 0 or burstSteps >= maxBurstSteps then
|
||||
burstSteps = 0
|
||||
kernel.EFI:yield()
|
||||
end
|
||||
|
||||
@@ -144,4 +151,4 @@ function kernel.main()
|
||||
kernel.saveLog()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user