diff --git a/include/lupi.h b/include/lupi.h index 0c0a83c..890285e 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -8,10 +8,12 @@ void logn(const char *message); void logi(int message); void logm(const char *message); +void logt(const char *message); #else #define logn(m) #define logi(m) #define logm(m) + #define logt(m) #endif #define pushstuple(state, name, value) lua_pushstring((state), (name)); lua_pushstring((state), (value)); lua_settable((state), -3) diff --git a/src/c/event/event.c b/src/c/event/event.c index bf313e9..5d573a9 100644 --- a/src/c/event/event.c +++ b/src/c/event/event.c @@ -6,19 +6,68 @@ #include #ifdef _WIN32 + #include + #include #define WIN32 #endif +#ifdef WIN32 +#define LOCAL_SOCKETPAIR_AF AF_INET +#else +#define LOCAL_SOCKETPAIR_AF AF_UNIX +#endif + #include #include struct event_base *base; struct event stdinEvent; - int nevt = 0; +#ifdef _WIN32 +struct event winEvent; +evutil_socket_t pfd[2] = {0,0}; + +void pokeWinEvt(char ch) { + send(pfd[1], &ch, 1, 0); +} + +static void handleWinevent(evutil_socket_t fd, short what, void *ptr) { + if(what != EV_READ) return; + + char buf; + int r = recv(fd, &buf, 1, 0); /* TODO: Wide chars? */ + if(r > 0) { + logi(buf); + logn(" < win char"); + lua_State* L = getL(); + + lua_getglobal(L, "pushEvent"); + lua_pushstring(L, "key_down"); + lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */ + lua_pushnumber(L, buf); + lua_pushnumber(L, -1); + lua_pushstring(L, "root"); + lua_call(L, 5, 0); + + lua_getglobal(L, "pushEvent"); + lua_pushstring(L, "key_up"); + lua_pushstring(L, "TODO:SetThisUuid"); + lua_pushnumber(L, buf); + lua_pushnumber(L, -1); + lua_pushstring(L, "root"); + lua_call(L, 5, 0); + + nevt += 2; + } +} +#endif + static void handleStdin(evutil_socket_t fd, short what, void *ptr) { char buf; + + if(what != EV_READ) return; + int r = read(fd, &buf, 1); /* TODO: Wide chars? */ if(r > 0) { lua_State* L = getL(); @@ -43,17 +92,30 @@ static void handleStdin(evutil_socket_t fd, short what, void *ptr) { } } + + void event_prepare() { struct event_config* cfg = event_config_new(); event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME); - base = event_base_new_with_config(cfg); + evutil_make_socket_nonblocking(STDIN_FILENO); event_assign(&stdinEvent, base, STDIN_FILENO, EV_READ, handleStdin, NULL); + +#ifdef _WIN32 + evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pfd); + evutil_make_socket_nonblocking(pfd[0]); + event_assign(&winEvent, base, pfd[0], EV_READ, handleWinevent, NULL); +#endif } static void add_events(struct timeval* timeout) { +#ifndef _WIN32 event_add(&stdinEvent, timeout); +#endif +#ifdef _WIN32 + event_add(&winEvent, timeout); +#endif } diff --git a/src/c/gpu/winapigpu.c b/src/c/gpu/winapigpu.c index 7681f09..1f635b1 100644 --- a/src/c/gpu/winapigpu.c +++ b/src/c/gpu/winapigpu.c @@ -72,13 +72,11 @@ static inline int win_draw_32(int x, int y, int bg, int fg, int chr, int cwd) { } } - +void pokeWinEvt(char ch); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { - logn("win: Msg"); switch(msg) { case WM_PAINT: { - logn("win: PAINT"); PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); screenbmap = CreateBitmap(RES_X, RES_Y, 1, BYPP * 8, (void*) screenbb); @@ -90,12 +88,18 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { EndPaint(hwnd, &ps); } break; + case WM_CHAR: + //case WM_UNICHAR: + //case WM_KEYUP: + //case WM_KEYDOWN: + pokeWinEvt(wParam); + break; case WM_CREATE: logn("win: Create"); screenbb = (uchar*) calloc(RES_X * RES_Y, BYPP); - colbuf = (char *)malloc(2 * CHARSW * CHARSH); - chrbuf = (ushort *)malloc(2 * CHARSW * CHARSH); + colbuf = (char*) malloc(2 * CHARSW * CHARSH); + chrbuf = (ushort*) malloc(2 * CHARSW * CHARSH); break; case WM_CLOSE: diff --git a/src/c/lnative.c b/src/c/lnative.c index f27cfb5..ee4e7ca 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -25,6 +25,8 @@ #define KIOCSOUND 0x4B2F /* start sound generation (0 for off) */ +long logStart = -1; + /* Enable in lupi.h */ #ifdef LOGGING void logn(const char *message) { @@ -69,6 +71,17 @@ void logm(const char *message) { } } +void logt(const char *message) { + struct timeval tp; + gettimeofday(&tp, NULL); + logm("["); + logi(tp.tv_sec - logStart); + logm("+"); + logi(tp.tv_usec); + logm("]"); + logm(message); +} + static int l_log (lua_State *L) { const char* t = lua_tostring(L, 1); logn(t); @@ -79,6 +92,7 @@ static int l_log (lua_State *L) { #define logn(m) #define logi(m) #define logm(m) +#define logt(m) static int l_log (lua_State *L) { return 0; } @@ -86,7 +100,8 @@ static int l_log (lua_State *L) { static int l_sleep (lua_State *L) { unsigned int t = lua_tonumber(L, 1); - usleep(t); + struct timespec st = {.tv_sec = t / 1000000, .tv_nsec = (t % 1000000) * 1000}; + nanosleep(&st, NULL); return 0; } @@ -450,6 +465,10 @@ static int l_debug (lua_State *L) { void luanative_start(lua_State *L) { + struct timeval tp; + gettimeofday(&tp, NULL); + logStart = tp.tv_sec; + struct luaL_Reg nativelib[] = { {"sleep", l_sleep}, {"log", l_log}, diff --git a/src/lua/core/boot.lua b/src/lua/core/boot.lua index f44a011..37175d6 100644 --- a/src/lua/core/boot.lua +++ b/src/lua/core/boot.lua @@ -46,6 +46,9 @@ function boot.boot() bsod(reason) else local crash = false + if native.debug then + native.sleep(500000) + end xpcall(f, function(e) local trace = {} diff --git a/src/lua/core/computer.lua b/src/lua/core/computer.lua index 7d4522d..99554bd 100644 --- a/src/lua/core/computer.lua +++ b/src/lua/core/computer.lua @@ -101,14 +101,14 @@ function computer.signalTransformers.key_down(s, a, ascii, key, user) if key ~= -1 then return s, a, ascii, key, user end - return s, a, asciitr[ascii] or ascii, keymap[ascii] or key, user + return s, a, math.floor(asciitr[ascii] or ascii), keymap[ascii] or key, user end function computer.signalTransformers.key_up(s, a, ascii, key, user) if key ~= -1 then return s, a, ascii, key, user end - return s, a, asciitr[ascii] or ascii, keymap[ascii] or key, user + return s, a, math.floor(asciitr[ascii] or ascii), keymap[ascii] or key, user end ----- diff --git a/src/lua/core/fbgpu.lua b/src/lua/core/fbgpu.lua index 88c2ad9..1fbccea 100644 --- a/src/lua/core/fbgpu.lua +++ b/src/lua/core/fbgpu.lua @@ -117,13 +117,13 @@ function fbgpu.start() y = math.floor(y) if not vertical then local i = 0 - value:gsub(".", function(c) + value:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(c) fb.put(x+i-1, y-1, background, foreground, utf8.codepoint(c)) i = i+1 end) else local i = 0 - value:gsub(".", function(c) + value:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(c) fb.put(x-1, y+i-1, background, foreground, utf8.codepoint(c)) i = i+1 end) @@ -156,11 +156,17 @@ function fbgpu.start() gpu.setForeground(0xFFFFFF) gpu.setBackground(0x000000) + local screenAddr + + function gpu.getScreen() + return screenAddr + end + termutils.init() write("\x1b[?25l") --Disable cursor modules.component.api.register(nil, "gpu", gpu) - modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) deadhooks[#deadhooks + 1] = function() diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index 2ed74d5..31f0f0a 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -107,7 +107,13 @@ function main() if native.debug and native.platform():match("unix") then modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111") end - modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) + if native.platform():match("unix") then + modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) + else + native.fs_mkdir("tmp") + modules.computer.tmp = modules.filesystem.register("tmp/lupi-" .. modules.random.uuid()) + --TODO: cleaning hook or something + end modules.gpudetect.run() diff --git a/src/lua/core/textgpu.lua b/src/lua/core/textgpu.lua index 9e83604..0f4bf09 100644 --- a/src/lua/core/textgpu.lua +++ b/src/lua/core/textgpu.lua @@ -143,7 +143,7 @@ function textgpu.start() else --TODO: Buffers! write("\x1b[" .. y .. ";" .. x .. "H") - value:gsub(".", function(c) + value:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(c) write(c .. "\x1b[D\x1b[B") end) end diff --git a/src/lua/core/winapigpu.lua b/src/lua/core/winapigpu.lua index cc3b735..c709783 100644 --- a/src/lua/core/winapigpu.lua +++ b/src/lua/core/winapigpu.lua @@ -109,13 +109,13 @@ function wingpu.start() y = math.floor(y) if not vertical then local i = 0 - value:gsub(".", function(c) + value:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(c) win.put(x+i-1, y-1, background, foreground, utf8.codepoint(c)) i = i+1 end) else local i = 0 - value:gsub(".", function(c) + value:gsub("([%z\1-\127\194-\244][\128-\191]*)", function(c) win.put(x-1, y+i-1, background, foreground, utf8.codepoint(c)) i = i+1 end) @@ -147,6 +147,11 @@ function wingpu.start() gpu.setForeground(0xFFFFFF) gpu.setBackground(0x000000) + local screenAddr + + function gpu.getScreen() + return screenAddr + end local s, reason = win.open() if not s then @@ -154,7 +159,7 @@ function wingpu.start() end modules.component.api.register(nil, "gpu", gpu) - modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) return s