termgpu fixes

This commit is contained in:
Łukasz Magiera 2016-02-09 17:42:11 +01:00
parent 12bbefc034
commit 7b486671a8
2 changed files with 52 additions and 30 deletions

View File

@ -9,6 +9,8 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
struct termios old, new;
static void handle_winch(int sig){ static void handle_winch(int sig){
signal(SIGWINCH, SIG_IGN); signal(SIGWINCH, SIG_IGN);
@ -24,11 +26,19 @@ static int l_get_term_sz (lua_State *L) {
return 2; return 2;
} }
static int l_term_restore (lua_State *L) {
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &old);
return 0;
}
static int l_term_init (lua_State *L) {
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new);
return 0;
}
void termutils_start(lua_State *L) { void termutils_start(lua_State *L) {
signal(SIGWINCH, handle_winch); signal(SIGWINCH, handle_winch);
struct termios old, new;
if (tcgetattr (STDOUT_FILENO, &old) != 0) if (tcgetattr (STDOUT_FILENO, &old) != 0)
return; return;
new = old; new = old;
@ -40,13 +50,10 @@ void termutils_start(lua_State *L) {
new.c_cflag &= ~(CSIZE | PARENB); new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8; new.c_cflag |= CS8;
if (tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new) != 0)
return;
lua_createtable (L, 0, 1); lua_createtable (L, 0, 1);
pushctuple(L, "getSize", l_get_term_sz); pushctuple(L, "getSize", l_get_term_sz);
pushctuple(L, "init", l_term_init);
pushctuple(L, "restore", l_term_restore);
lua_setglobal(L, "termutils"); lua_setglobal(L, "termutils");
} }

View File

@ -44,11 +44,14 @@ local function insertString(main, sub, at)
checkArg(1, main, "string") checkArg(1, main, "string")
checkArg(2, sub, "string") checkArg(2, sub, "string")
checkArg(3, at, "number") checkArg(3, at, "number")
return usub(main, 1, at - 1) .. sub .. usub(main, at + utf8.len(sub))
return usub(main, 1, at - 1)
.. sub .. usub(main, at + (utf8.len(sub) or 0))
end end
function textgpu.start() function textgpu.start()
usub = modules.sandbox.unicode.sub usub = modules.sandbox.unicode.sub
local _height = 0
local gpu = {} local gpu = {}
function gpu.bind() return false, "This is static bound gpu" end function gpu.bind() return false, "This is static bound gpu" end
function gpu.setBackground(color, isPaletteIndex) function gpu.setBackground(color, isPaletteIndex)
@ -157,9 +160,15 @@ function textgpu.start()
local btbuf = {} local btbuf = {}
local ftbuf = {} local ftbuf = {}
for i=1, h do for i=1, h do
ttbuf[i] = tbuffer[y + i - 1] and tbuffer[y + i - 1]:sub(x, x + w - 1) or (" "):rep(w) if i + y - 2 <= h and i + y > 1 then
btbuf[i] = bbuffer[y + i - 1] and bbuffer[y + i - 1]:sub(x, x + w - 1) or background:rep(w) ttbuf[i] = tbuffer[y + i - 1] and usub(tbuffer[y + i - 1], x, x + w - 1) or (" "):rep(w)
ftbuf[i] = fbuffer[y + i - 1] and fbuffer[y + i - 1]:sub(x, x + w - 1) or foreground:rep(w) btbuf[i] = bbuffer[y + i - 1] and bbuffer[y + i - 1]:sub(x, x + w - 1) or background:rep(w)
ftbuf[i] = fbuffer[y + i - 1] and fbuffer[y + i - 1]:sub(x, x + w - 1) or foreground:rep(w)
else
ttbuf[i] = (" "):rep(w)
btbuf[i] = background:rep(w)
ftbuf[i] = foreground:rep(w)
end
end end
local bg = background local bg = background
local fg = foreground local fg = foreground
@ -179,14 +188,14 @@ function textgpu.start()
if lwrite then if lwrite then
local wx = (tx + linex)|0 local wx = (tx + linex)|0
local wy = (ty + y + i - 1)|0 local wy = (ty + y + i - 1)|0
if tbuffer[wy] then tbuffer[wy] = tbuffer[wy] or (" "):rep(utf8.len(line))
write("\x1b[4" .. bg .. "m") write("\x1b[4" .. bg .. "m")
write("\x1b[3" .. fg .. "m") write("\x1b[3" .. fg .. "m")
write("\x1b[" .. wy .. ";" .. wx .. "H" .. line) write("\x1b[" .. wy .. ";" .. wx .. "H" .. line)
tbuffer[wy] = insertString(tbuffer[wy], line, wx) tbuffer[wy] = insertString(tbuffer[wy], line, wx)
bbuffer[wy] = insertString(bbuffer[wy], bg:rep(utf8.len(line)), wx) bbuffer[wy] = insertString(bbuffer[wy], bg:rep(utf8.len(line)), wx)
fbuffer[wy] = insertString(fbuffer[wy], fg:rep(utf8.len(line)), wx) fbuffer[wy] = insertString(fbuffer[wy], fg:rep(utf8.len(line)), wx)
end
bg = btbuf[i]:sub(j,j) bg = btbuf[i]:sub(j,j)
fg = ftbuf[i]:sub(j,j) fg = ftbuf[i]:sub(j,j)
line = nil line = nil
@ -194,19 +203,19 @@ function textgpu.start()
lwrite = false lwrite = false
end end
if not line then linex = j end if not line then linex = j end
line = (line or "") .. ttbuf[i]:sub(j,j) line = (line or "") .. usub(ttbuf[i], j,j)
end end
if line then if line then
local wx = (tx + linex)|0 local wx = (tx + linex)|0
local wy = (ty + y + i - 1)|0 local wy = (ty + y + i - 1)|0
if tbuffer[wy] then tbuffer[wy] = tbuffer[wy] or (" "):rep(utf8.len(line))
write("\x1b[4" .. bg .. "m") write("\x1b[4" .. bg .. "m")
write("\x1b[3" .. fg .. "m") write("\x1b[3" .. fg .. "m")
write("\x1b[" .. wy .. ";" .. wx .. "H" .. line) write("\x1b[" .. wy .. ";" .. wx .. "H" .. line)
tbuffer[wy] = insertString(tbuffer[wy], line, wx) tbuffer[wy] = insertString(tbuffer[wy], line, wx)
bbuffer[wy] = insertString(bbuffer[wy], bg:rep(utf8.len(line)), wx) bbuffer[wy] = insertString(bbuffer[wy], bg:rep(utf8.len(line)), wx)
fbuffer[wy] = insertString(fbuffer[wy], fg:rep(utf8.len(line)), wx) fbuffer[wy] = insertString(fbuffer[wy], fg:rep(utf8.len(line)), wx)
end
line = nil line = nil
linex = nil linex = nil
lwrite = false lwrite = false
@ -223,9 +232,11 @@ function textgpu.start()
checkArg(3, w, "number") checkArg(3, w, "number")
checkArg(4, h, "number") checkArg(4, h, "number")
checkArg(5, ch, "string") checkArg(5, ch, "string")
ch = ch:sub(1, 1):rep(math.floor(w)) ch = usub(ch, 1, 1):rep(math.floor(w))
for i=1, h do for i=1, h do
gpu.set(x, y + i - 1, ch) if i + y - 1 <= h and i + y > 1 then
gpu.set(x, y + i - 1, ch)
end
end end
return true return true
end end
@ -236,8 +247,10 @@ function textgpu.start()
return screenAddr return screenAddr
end end
termutils.init()
write("\x1b[?25l") --Disable cursor write("\x1b[?25l") --Disable cursor
local w, h = gpu.getResolution() local w, h = gpu.getResolution()
_height = h
prepareBuffers(w, h) prepareBuffers(w, h)
gpu.setForeground(0xFFFFFF) gpu.setForeground(0xFFFFFF)
gpu.setBackground(0x000000) gpu.setBackground(0x000000)
@ -247,7 +260,9 @@ function textgpu.start()
modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) modules.component.api.register("TODO:SetThisUuid", "keyboard", {})
deadhooks[#deadhooks + 1] = function() deadhooks[#deadhooks + 1] = function()
write("\x1b[?25h") --Enable cursor on quit write("\x1b[?25h\x1b[" .. ((h-1)|0) .. ";1H") --Enable cursor on quit
io.flush()
termutils.restore()
end end
end end