aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2017-04-19 19:34:05 +0200
committerAlex Auvolat <alex@adnab.me>2017-04-19 19:34:05 +0200
commit59ecab36f634a00cc6e2c4194bf2d5ebc4ec70eb (patch)
tree85daa2af4ea8840c8f7fb9f6e88bc3f76a47fecf /src
parente53a39d9ec28b24ea0d408f1500e987d005cd651 (diff)
downloadkogata-59ecab36f634a00cc6e2c4194bf2d5ebc4ec70eb.tar.gz
kogata-59ecab36f634a00cc6e2c4194bf2d5ebc4ec70eb.zip
First lua app
Diffstat (limited to 'src')
-rw-r--r--src/lib/include/proto/gip.h2
-rw-r--r--src/sysapp/login/main.lua68
-rw-r--r--src/sysapp/test/main.lua4
-rw-r--r--src/sysbin/lx/lxsyslib.c195
-rw-r--r--src/syslua/lx/shell.lua61
-rw-r--r--src/syslua/lx/sysdef.lua31
6 files changed, 341 insertions, 20 deletions
diff --git a/src/lib/include/proto/gip.h b/src/lib/include/proto/gip.h
index 5cf0166..5c56637 100644
--- a/src/lib/include/proto/gip.h
+++ b/src/lib/include/proto/gip.h
@@ -93,8 +93,6 @@
#define GIPN_MOUSE_XY 31 // server: mouse moved at xy ; client: put mouse at xy
#define GIPN_MOUSE_PRESSED 32 // server: button b pressed
#define GIPN_MOUSE_RELEASED 33 // server: button b released
-#define GIPC_LOAD_CURSOR 34 // client: this is graphics for cursor #i
-#define GIPC_SET_CURSOR 35 // client: please use cursor #i (0 = hide cursor)
typedef struct {
uint32_t code; // message type
diff --git a/src/sysapp/login/main.lua b/src/sysapp/login/main.lua
new file mode 100644
index 0000000..d099ec7
--- /dev/null
+++ b/src/sysapp/login/main.lua
@@ -0,0 +1,68 @@
+local sys = require 'lx.sys'
+local sysdef = require 'lx.sysdef'
+
+local mainloop = require 'lx.mainloop'
+local gip = require 'lx.gip'
+local draw = require 'lx.draw'
+
+
+local io = gip.new(sysdef.STD_FD_GIP)
+
+function io:damage(reg)
+ if self.features & gipdef.GIPF_DAMAGE_NOTIF ~= 0 then
+ self:send_buffer_damage(draw.region(0, 0, 256, 256))
+ end
+end
+
+function io:on_initiate(arg)
+ self.flags = arg
+
+ self:async_enumerate_modes(function(modes)
+ sys.dbg_print("Got mode list:\n")
+ for i, v in pairs(modes) do
+ sys.dbg_print(string.format("%d: %dx%d %d\n", i, v.width, v.height, v.bpp))
+ end
+
+ for i, v in pairs(modes) do
+ if v.width == 800 and v.height == 600 and v.bpp == 24 then
+ sys.dbg_print(string.format("Selecting mode %d\n", i))
+ io:send_set_mode(i)
+ return
+ end
+ end
+ end)
+end
+
+function io:on_buffer_info(arg, tok, geom)
+ self.surface = draw.surface_from_fd(sys.use_token(tok))
+ self.geom = geom
+
+ for x = 0, 255 do
+ for y = 0, 255 do
+ self.surface:put(x, y, draw.rgb(x, y, 128))
+ end
+ end
+
+ self:damage(draw.region(0, 0, 256, 256))
+end
+
+function io:async_enumerate_modes(callback)
+ local modelist = {}
+ local function gotmode(cmd, arg)
+ if arg == nil then
+ callback(modelist)
+ else
+ modelist[#modelist + 1] = arg
+ io.on_reply[io:send_query_mode(#modelist)] = gotmode
+ end
+ end
+ io.on_reply[io:send_query_mode(#modelist)] = gotmode
+end
+
+mainloop.add(io)
+
+io:send_reset()
+mainloop.run()
+
+
+
diff --git a/src/sysapp/test/main.lua b/src/sysapp/test/main.lua
new file mode 100644
index 0000000..19e3108
--- /dev/null
+++ b/src/sysapp/test/main.lua
@@ -0,0 +1,4 @@
+print("Hello, world!")
+
+local test = require 'test'
+test.guess(100)
diff --git a/src/sysbin/lx/lxsyslib.c b/src/sysbin/lx/lxsyslib.c
index 5bfec3e..fbafde9 100644
--- a/src/sysbin/lx/lxsyslib.c
+++ b/src/sysbin/lx/lxsyslib.c
@@ -22,6 +22,13 @@
#include "lxlib.h"
+bool lx_checkboolean(lua_State *L, int arg) {
+ if (!lua_isboolean(L, arg)) {
+ luaL_argerror(L, arg, "expected boolean");
+ }
+ return lua_toboolean(L, arg);
+}
+
static void setintfield (lua_State *L, const char *key, int value) {
lua_pushinteger(L, value);
@@ -188,6 +195,172 @@ static int sys_select(lua_State *L) {
return 0;
}
+static int sys_make_channel(lua_State *L) {
+ bool blocking = lx_checkboolean(L, 1);
+ fd_pair_t pair = sc_make_channel(blocking);
+ lua_pushinteger(L, pair.a);
+ lua_pushinteger(L, pair.b);
+ return 2;
+}
+
+static int sys_make_shm(lua_State *L) {
+ int size = luaL_checkinteger(L, 1);
+ int fd = sc_make_shm(size);
+ if (fd) {
+ lua_pushinteger(L, fd);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+static int sys_gen_token(lua_State *L) {
+ int fd = luaL_checkinteger(L, 1);
+ token_t t;
+ if (sc_gen_token(fd, &t)) {
+ lua_pushlstring(L, t.bytes, TOKEN_LENGTH);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+static int sys_use_token(lua_State *L) {
+ const char* tok = luaL_checkstring(L, 1);
+ if (!(luaL_len(L, 1) == TOKEN_LENGTH)) {
+ luaL_argerror(L, 1, "invalid token length");
+ }
+ token_t t;
+ memcpy(t.bytes, tok, TOKEN_LENGTH);
+ fd_t f = sc_use_token(&t);
+ if (f) {
+ lua_pushinteger(L, f);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+static int sys_make_fs(lua_State *L) {
+ const char* name = luaL_checkstring(L, 1);
+ const char* driver = luaL_checkstring(L, 2);
+ int source = luaL_checkinteger(L, 3);
+ const char* options = luaL_checkstring(L, 4);
+ lua_pushboolean(L, sc_make_fs(name, driver, source, options));
+ return 1;
+}
+
+static int sys_fs_add_source(lua_State *L) {
+ const char* fs = luaL_checkstring(L, 1);
+ int source = luaL_checkinteger(L, 2);
+ const char* options = luaL_checkstring(L, 3);
+ lua_pushboolean(L, sc_fs_add_source(fs, source, options));
+ return 1;
+}
+
+static int sys_fs_subfs(lua_State *L) {
+ const char* name = luaL_checkstring(L, 1);
+ const char* orig_fs = luaL_checkstring(L, 2);
+ const char* root = luaL_checkstring(L, 3);
+ int ok_modes = luaL_checkinteger(L, 4);
+ lua_pushboolean(L, sc_fs_subfs(name, orig_fs, root, ok_modes));
+ return 1;
+}
+
+static int sys_fs_remove(lua_State *L) {
+ const char* name = luaL_checkstring(L, 1);
+ sc_fs_remove(name);
+ return 0;
+}
+
+static int sys_new_proc(lua_State *L) {
+ lua_pushinteger(L, sc_new_proc());
+ return 1;
+}
+
+static int sys_bind_fs(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ const char* new_name = luaL_checkstring(L, 2);
+ const char* name = luaL_checkstring(L, 3);
+ lua_pushboolean(L, sc_bind_fs(pid, new_name, name));
+ return 1;
+}
+
+static int sys_bind_subfs(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ const char* new_name = luaL_checkstring(L, 2);
+ const char* fs = luaL_checkstring(L, 3);
+ const char* root = luaL_checkstring(L, 4);
+ int ok_modes = luaL_checkinteger(L, 5);
+ lua_pushboolean(L, sc_bind_subfs(pid, new_name, fs, root, ok_modes));
+ return 1;
+}
+
+static int sys_bind_make_fs(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ const char* new_name = luaL_checkstring(L, 2);
+ const char* driver = luaL_checkstring(L, 3);
+ fd_t source = luaL_checkinteger(L, 4);
+ const char* options = luaL_checkstring(L, 5);
+ lua_pushboolean(L, sc_bind_make_fs(pid, new_name, driver, source, options));
+ return 1;
+}
+
+static int sys_bind_fd(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ int new_fd = luaL_checkinteger(L, 2);
+ int fd = luaL_checkinteger(L, 3);
+ lua_pushboolean(L, sc_bind_fd(pid, new_fd, fd));
+ return 1;
+}
+
+static int sys_proc_exec(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ const char *name = luaL_checkstring(L, 2);
+ lua_pushboolean(L, sc_proc_exec(pid, name));
+ return 1;
+}
+
+static int sys_proc_status(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ proc_status_t s;
+ if (sc_proc_status(pid, &s)) {
+ lua_createtable(L, 0, 3);
+ setintfield(L, "pid", s.pid);
+ setintfield(L, "status",s.status);
+ setintfield(L, "exit_code", s.exit_code);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+static int sys_proc_kill(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ proc_status_t s;
+ if (sc_proc_kill(pid, &s)) {
+ lua_createtable(L, 0, 3);
+ setintfield(L, "pid", s.pid);
+ setintfield(L, "status",s.status);
+ setintfield(L, "exit_code", s.exit_code);
+ } else {
+ lua_pushnil(L);
+ }
+ return 1;
+}
+
+static int sys_proc_wait(lua_State *L) {
+ int pid = luaL_checkinteger(L, 1);
+ bool wait = lx_checkboolean(L, 2);
+ proc_status_t s;
+ sc_proc_wait(pid, wait, &s);
+ lua_createtable(L, 0, 3);
+ setintfield(L, "pid", s.pid);
+ setintfield(L, "status",s.status);
+ setintfield(L, "exit_code", s.exit_code);
+ return 1;
+}
+
static const luaL_Reg syslib[] = {
{"dbg_print", sys_dbg_print},
@@ -204,7 +377,27 @@ static const luaL_Reg syslib[] = {
{"write", sys_write},
{"readdir", sys_readdir},
{"stat_open", sys_stat_open},
-
+ {"ioctl", sys_ioctl},
+ {"fctl", sys_fctl},
+ {"select", sys_select},
+ {"make_channel",sys_make_channel},
+ {"make_shm", sys_make_shm},
+ {"gen_token", sys_gen_token},
+ {"use_token", sys_use_token},
+ {"make_fs", sys_make_fs},
+ {"fs_add_source",sys_fs_add_source},
+ {"fs_subfs", sys_fs_subfs},
+ {"fs_remove", sys_fs_remove},
+ {"new_proc", sys_new_proc},
+ {"bind_fs", sys_bind_fs},
+ {"bind_subfs",sys_bind_subfs},
+ {"bind_make_fs",sys_bind_make_fs},
+ {"bind_fd", sys_bind_fd},
+ {"proc_exec", sys_proc_exec},
+ {"proc_status",sys_proc_status},
+ {"proc_kill", sys_proc_kill},
+ {"proc_wait", sys_proc_wait},
+
{NULL, NULL}
};
diff --git a/src/syslua/lx/shell.lua b/src/syslua/lx/shell.lua
index 62f210f..e736e24 100644
--- a/src/syslua/lx/shell.lua
+++ b/src/syslua/lx/shell.lua
@@ -3,28 +3,29 @@ local sysdef = require 'lx.sysdef'
local _cwd = 'root:/'
+function explode_path(path)
+ local _, _, dr, p = string.find(path, '^(%w+):(.*)$')
+ if not dr or not p then
+ dr, p = nil, path
+ end
+ local pp = string.split(p, '/')
+ if #pp > 1 and pp[#pp] == '' then
+ table.remove(pp)
+ end
+ return dr, pp
+end
+
+function implode_path(dr, p)
+ assert(p[1] == '', 'bad first path component')
+ return dr .. ':' .. table.concat(p, '/')
+end
+
function pathcat(path1, path2)
assert(path1, "invalid argument")
if not path2 then
return path1
end
- function explode_path(path)
- local _, _, dr, p = string.find(path, '^(%w+):(.*)$')
- if not dr or not p then
- dr, p = nil, path
- end
- local pp = string.split(p, '/')
- if #pp > 1 and pp[#pp] == '' then
- table.remove(pp)
- end
- return dr, pp
- end
-
- function implode_path(dr, p)
- assert(p[1] == '', 'bad first path component')
- return dr .. ':' .. table.concat(p, '/')
- end
local dr2, p2 = explode_path(path2)
if dr2 then
@@ -86,3 +87,31 @@ function ls(path)
end
end
+function run(path)
+ path = pathcat(_cwd, path)
+
+ local s = sys.stat(path)
+ if not s then
+ print("not found: " .. path)
+ elseif s.type & sysdef.FT_DIR == 0 then
+ print("not a directory: " .. s)
+ else
+ local mainlua = pathcat(path, 'main.lua')
+ local s2 = sys.stat(mainlua)
+ if not s2 then
+ print("not found: " .. mainlua)
+ else
+ local pid = sys.new_proc()
+ sys.bind_fs(pid, "sys", "sys")
+
+ local _, _, dr, p = string.find(path, '^(%w+):(.*)$')
+ sys.bind_subfs(pid, "app", dr, p, sysdef.FM_READ | sysdef.FM_READDIR)
+ sys.bind_fd(pid, sysdef.STD_FD_TTY_STDIO, sysdef.STD_FD_TTY_STDIO)
+
+ sys.proc_exec(pid, "sys:/bin/lx.bin")
+ local st = sys.proc_wait(pid, true)
+
+ print(st)
+ end
+ end
+end
diff --git a/src/syslua/lx/sysdef.lua b/src/syslua/lx/sysdef.lua
index 7c0d14f..43015e2 100644
--- a/src/syslua/lx/sysdef.lua
+++ b/src/syslua/lx/sysdef.lua
@@ -1,6 +1,7 @@
--- Constant definitions based on common/include/proto/fs.h
return {
+ -- Definitions from proto/fs.h
+
-- FILE TYPES
FT_REGULAR = 0,
FT_DIR = 0x01,
@@ -33,4 +34,32 @@ return {
SEL_READ = 0x01,
SEL_WRITE = 0x02,
SEL_ERROR = 0x04,
+
+
+ -- Definitions from proto/launch.h
+
+ -- STANDARD FILE DESCRIPTORS
+ STD_FD_TTY_STDIO = 1,
+ STD_FD_STDIN = 2,
+ STD_FD_STDOUT = 3,
+ STD_FD_STDERR = 4,
+ STD_FD_GIP = 5,
+ STD_FD_GIOSRV = 10,
+ STD_FD_TTYSRV = 11,
+
+ -- Definitions from proto/fb.h
+
+ -- FRAMEBUFFER TYPES : TODO
+
+ -- FRAMEBUFFER IOCTLs
+ IOCTL_FB_GET_INFO = 1,
+ IOCTL_FBDEV_GET_MODE_INFO = 10,
+ IOCTL_FBDEV_SET_MODE = 11,
+
+ -- Definitions from proto/proc.h
+ PS_LOADING = 1,
+ PS_RUNNING = 2,
+ PS_FINISHED = 3,
+ PS_FAILURE = 4,
+ PS_KILLED = 5,
}