12 changed files with 266 additions and 18 deletions
@ -0,0 +1,34 @@ |
|||
#include "lupi.h" |
|||
#include <lua.h> |
|||
#include <lualib.h> |
|||
#include <lauxlib.h> |
|||
#include <string.h> |
|||
#include <signal.h> |
|||
#include <sys/ioctl.h> |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
|
|||
static void handle_winch(int sig){ |
|||
signal(SIGWINCH, SIG_IGN); |
|||
|
|||
//FIXME: Prerelease: Implement
|
|||
signal(SIGWINCH, handle_winch); |
|||
} |
|||
|
|||
static int l_get_term_sz (lua_State *L) { |
|||
struct winsize w; |
|||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); |
|||
lua_pushnumber(L, w.ws_col); |
|||
lua_pushnumber(L, w.ws_row); |
|||
return 2; |
|||
} |
|||
|
|||
|
|||
void termutils_start(lua_State *L) { |
|||
signal(SIGWINCH, handle_winch); |
|||
|
|||
lua_createtable (L, 0, 1); |
|||
pushctuple(L, "getSize", l_get_term_sz); |
|||
|
|||
lua_setglobal(L, "termutils"); |
|||
} |
@ -1,7 +1,13 @@ |
|||
local computer = {} |
|||
local api = {} |
|||
computer.api = api |
|||
|
|||
function computer.prepare( ... ) |
|||
|
|||
end |
|||
|
|||
function api.pushSignal(...) |
|||
--FIXME: ASAP: Implement |
|||
end |
|||
|
|||
return computer |
@ -0,0 +1,121 @@ |
|||
local textgpu = {} |
|||
|
|||
local mapping = { |
|||
["0"] = 0x000000, |
|||
["1"] = 0xFF0000, |
|||
["2"] = 0x00FF00, |
|||
["3"] = 0xFFFF00, |
|||
["4"] = 0x0000FF, |
|||
["5"] = 0xFF00FF, |
|||
["6"] = 0x00FFFF, |
|||
["7"] = 0xFFFFFF, |
|||
} |
|||
|
|||
local background = "0" |
|||
local foreground = "0" |
|||
|
|||
function textgpu.start() |
|||
local gpu = {} |
|||
function gpu.bind() return false, "This is static bound gpu" end |
|||
function gpu.getScreen() return "n/a" end |
|||
function gpu.setBackground(color, isPaletteIndex) |
|||
checkArg(1, color, "number") |
|||
checkArg(2, isPaletteIndex, "boolean", "nil") |
|||
if isPaletteIndex then |
|||
return --TODO: Maybe? |
|||
end |
|||
background = modules.color.nearest(color, mapping) |
|||
io.write("\x1b[4" .. background .. "m") |
|||
end |
|||
function gpu.setForeground(color, isPaletteIndex) |
|||
checkArg(1, color, "number") |
|||
checkArg(2, isPaletteIndex, "boolean", "nil") |
|||
if isPaletteIndex then |
|||
return --TODO: Maybe? |
|||
end |
|||
background = modules.color.nearest(color, mapping) |
|||
io.write("\x1b[3" .. background .. "m") |
|||
end |
|||
function gpu.getBackground() |
|||
return mapping[background], false |
|||
end |
|||
function gpu.getForeground() |
|||
return mapping[foreground], false |
|||
end |
|||
function gpu.getPaletteColor() |
|||
return nil |
|||
end |
|||
function gpu.setPaletteColor() |
|||
return nil |
|||
end |
|||
function gpu.maxDepth() |
|||
return 3 |
|||
end |
|||
function gpu.setDepth() |
|||
return false |
|||
end |
|||
function gpu.getDepth() |
|||
return 3 |
|||
end |
|||
function gpu.maxResolution() |
|||
return termutils.getSize() |
|||
end |
|||
function gpu.getResolution() |
|||
return termutils.getSize() |
|||
end |
|||
function gpu.setResolution(w, h) |
|||
checkArg(1, w, "number") |
|||
checkArg(2, h, "number") |
|||
return false, "Non resizeable gpu" |
|||
end |
|||
function gpu.get(x, y) |
|||
checkArg(1, x, "number") |
|||
checkArg(2, y, "number") |
|||
--FIXME: ASAP: Implement |
|||
return " " |
|||
end |
|||
function gpu.set(x, y, value, vertical) |
|||
checkArg(1, x, "number") |
|||
checkArg(2, y, "number") |
|||
checkArg(3, value, "string") |
|||
checkArg(4, vertical, "boolean", "nil") |
|||
if not vertical then |
|||
io.write("\x1b[" .. y .. ";" .. x .. "H" .. value) |
|||
else |
|||
io.write("\x1b[" .. y .. ";" .. x .. "H") |
|||
value:gsub(".", function(c) |
|||
io.write(c .. "\x1b[D\x1b[B") |
|||
end) |
|||
end |
|||
return true |
|||
end |
|||
function gpu.copy(x, y, w, h, tx, ty) |
|||
checkArg(1, x, "number") |
|||
checkArg(2, y, "number") |
|||
checkArg(3, w, "number") |
|||
checkArg(4, h, "number") |
|||
checkArg(5, tx, "number") |
|||
checkArg(6, ty, "number") |
|||
--FIXME: ASAP: Implement |
|||
return false |
|||
end |
|||
function gpu.fill(x, y, w, h, ch) |
|||
checkArg(1, x, "number") |
|||
checkArg(2, y, "number") |
|||
checkArg(3, w, "number") |
|||
checkArg(4, h, "number") |
|||
checkArg(5, ch, "string") |
|||
ch = ch:sub(1, 1):rep(w) |
|||
for i=1, h do |
|||
gpu.set(x, y + i - 1, ch) |
|||
end |
|||
return true |
|||
end |
|||
|
|||
gpu.setForeground(0xFFFFFF) |
|||
gpu.setBackground(0x000000) |
|||
|
|||
modules.component.api.register(nil, "gpu", gpu) |
|||
end |
|||
|
|||
return textgpu |
@ -0,0 +1,68 @@ |
|||
local color = {} |
|||
|
|||
function color.rgbToHsv(r, g, b) |
|||
local h, s, v |
|||
local min, max, delta |
|||
min = math.min(r, g, b) |
|||
max = math.max(r, g, b) |
|||
v = max |
|||
delta = max - min |
|||
if delta < 0.00001 then |
|||
return 0, 0, v |
|||
end |
|||
if max ~= 0 then |
|||
s = delta / max |
|||
else |
|||
s = 0 |
|||
h = -1 |
|||
return h, s, v |
|||
end |
|||
|
|||
if r == max then |
|||
h = (g - b) / delta |
|||
elseif g == max then |
|||
h = 2 + (b - r) / delta |
|||
else |
|||
h = 4 + (r - g) / delta |
|||
end |
|||
|
|||
h = h * 60 |
|||
if h < 0 then h = h + 360 end |
|||
return h, s, v |
|||
end |
|||
|
|||
function color.hsvToRgb(h, s, v) |
|||
local i, f, p, q, t |
|||
if s == 0 then |
|||
return v, v, v |
|||
end |
|||
h = h / 60 |
|||
i = math.floor(h) |
|||
f = h - i |
|||
p = v * (1 - s) |
|||
q = v * (1 - s * f) |
|||
t = v * (1 - s * (1 - f)) |
|||
if i == 0 then return v, t, p end |
|||
if i == 1 then return q, v, p end |
|||
if i == 2 then return p, v, t end |
|||
if i == 3 then return p, q, v end |
|||
if i == 4 then return t, p, v end |
|||
return v, p, q |
|||
end |
|||
|
|||
function color.nearest(to, colors) |
|||
local lowest = math.huge |
|||
local lowestk = nil |
|||
local th, ts, tv = color.rgbToHsv((to & 0xFF0000) >> 16, (to & 0xFF00) >> 8, to & 0xFF) |
|||
for k, col in pairs(colors) do |
|||
local h, s, v = color.rgbToHsv((col & 0xFF0000) >> 16, (col & 0xFF00) >> 8, col & 0xFF) |
|||
local d = math.abs(h - th) + math.abs(s - ts) + math.abs(v - tv) |
|||
if d < lowest then |
|||
lowest = d |
|||
lowestk = k |
|||
end |
|||
end |
|||
return lowestk |
|||
end |
|||
|
|||
return color |
Loading…
issues.context.reference_issue