diff --git a/include/luares.h b/include/luares.h index e8061b9..cf85f09 100644 --- a/include/luares.h +++ b/include/luares.h @@ -8,6 +8,7 @@ extern char lua_eeprom[]; extern char lua_fbgpu[]; extern char lua_filesystem[]; extern char lua_gpio[]; +extern char lua_gpudetect[]; extern char lua_init[]; extern char lua_internet[]; extern char lua_sandbox[]; diff --git a/include/lupi.h b/include/lupi.h index 4e0296b..7461cd7 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -20,7 +20,7 @@ typedef unsigned short ushort; lua_State* getL(); -void run_init(); +void run_init(int argc, char **argv); void lupi_init(); void luanative_start(lua_State *L); void fb_start(lua_State *L); diff --git a/include/res.h b/include/res.h index 9fe7e3a..a72bbd1 100644 --- a/include/res.h +++ b/include/res.h @@ -1,5 +1,6 @@ #ifndef RES_H #define RES_H extern char res_eepromDefault[]; +extern char res_help[]; extern char res_unifont[]; #endif diff --git a/resources/help b/resources/help new file mode 100644 index 0000000..31a1bcc --- /dev/null +++ b/resources/help @@ -0,0 +1,5 @@ +Usage: lupi [options] + + -h, --help This help + -f, --fb Force framebuffer rendering + -t, --text Force text mode rendering diff --git a/src/c/lnative.c b/src/c/lnative.c index 5f81e1d..f27cfb5 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -429,6 +429,19 @@ static int l_platform (lua_State *L) { /* returns platform identifiers separated return 1; } +static int l_isinit (lua_State *L) { +#ifndef _WIN32 + lua_pushboolean(L, 0); +#else + if(getpid() == 1) { + lua_pushboolean(L, 1); + } else { + lua_pushboolean(L, 0); + } +#endif + return 1; +} + #ifdef DEBUG static int l_debug (lua_State *L) { return 0; @@ -464,6 +477,7 @@ void luanative_start(lua_State *L) { {"freeMemory", l_freeMemory}, {"pull", l_pull}, {"platform", l_platform}, + {"isinit", l_isinit}, #ifdef DEBUG {"debug", l_debug}, #endif diff --git a/src/c/main.c b/src/c/main.c index e16878a..2a4433f 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,9 +1,9 @@ #include #include "lupi.h" -int main (void) { +int main (int argc, char **argv) { puts("LuPI L0 INIT"); lupi_init(); - run_init(); + run_init(argc, argv); return 0; } diff --git a/src/c/modules.c b/src/c/modules.c index 4175c12..82613e6 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -7,25 +7,27 @@ #include void setup_modules(lua_State *L) { - lua_createtable (L, 0, 1); + lua_createtable (L, 0, 32); - pushstuple(L, "boot", lua_boot); - pushstuple(L, "color", lua_util_color); - pushstuple(L, "component", lua_component); - pushstuple(L, "computer", lua_computer); - pushstuple(L, "debug", lua_debug); - pushstuple(L, "eeprom", lua_eeprom); - pushstuple(L, "filesystem", lua_filesystem); - pushstuple(L, "gpio", lua_gpio); - pushstuple(L, "internet", lua_internet); - pushstuple(L, "sandbox", lua_sandbox); - pushstuple(L, "textgpu", lua_textgpu); - pushstuple(L, "fbgpu", lua_fbgpu); - pushstuple(L, "color", lua_util_color); - pushstuple(L, "random", lua_util_random); - pushstuple(L, "buffer", lua_util_buffer); + pushstuple(L, "boot", lua_boot); + pushstuple(L, "color", lua_util_color); + pushstuple(L, "component", lua_component); + pushstuple(L, "computer", lua_computer); + pushstuple(L, "debug", lua_debug); + pushstuple(L, "eeprom", lua_eeprom); + pushstuple(L, "filesystem", lua_filesystem); + pushstuple(L, "gpio", lua_gpio); + pushstuple(L, "gpudetect", lua_gpudetect); + pushstuple(L, "internet", lua_internet); + pushstuple(L, "sandbox", lua_sandbox); + pushstuple(L, "textgpu", lua_textgpu); + pushstuple(L, "fbgpu", lua_fbgpu); + pushstuple(L, "color", lua_util_color); + pushstuple(L, "random", lua_util_random); + pushstuple(L, "buffer", lua_util_buffer); - pushstuple(L, "eepromDefault", res_eepromDefault); + pushstuple(L, "eepromDefault", res_eepromDefault); + pushstuple(L, "help", res_help); lua_setglobal(L, "moduleCode"); } diff --git a/src/c/run.c b/src/c/run.c index 8510a89..0510b17 100644 --- a/src/c/run.c +++ b/src/c/run.c @@ -20,7 +20,7 @@ lua_State* getL() { return L; } -void run_init() { +void run_init(int argc, char **argv) { L = luaL_newstate(); luaL_openlibs (L); @@ -36,6 +36,9 @@ void run_init() { fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1)); exit(1); } - lua_call(L, 0, 0); + for(int i = 0; i < argc; i++) { + lua_pushstring(L, argv[i]); + } + lua_call(L, argc, 0); lua_close(L); } diff --git a/src/lua/core/gpudetect.lua b/src/lua/core/gpudetect.lua new file mode 100644 index 0000000..7136973 --- /dev/null +++ b/src/lua/core/gpudetect.lua @@ -0,0 +1,37 @@ +local gpudetect = {} + +local function tryText() + loadModule("textgpu") + local textgpuAddr, tgfail = modules.textgpu.start() + if not textgpuAddr then + lprint("Couldn't initialize text gpu: " .. tostring(tgfail)) + return false + end + return true +end + +local function tryFb() + if framebuffer.isReady() then + loadModule("fbgpu") + modules.fbgpu.start() + return true + end + return false +end + +function gpudetect.run() + local s = false + if hasOpt("-t", "--text") then + s = tryText() + return + end + if hasOpt("-f", "--fb") or native.isinit() then + s = tryFb() + end + if not s then + lprint("Falling back to text gpu") + s = tryText() + end +end + +return gpudetect diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index dcecf2e..2ed74d5 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -15,6 +15,27 @@ function checkArg(n, have, ...) end ------------------------------------------------------------------------------- + +local argv = {...} +function hasOpt(o, n, ...) + for _, v in pairs(argv) do + if v == o then + return true + end + end + if n then + return hasOpt(n, ...) + end + return false +end + +if hasOpt("-h", "--help") then + print(moduleCode.help) + os.exit(0) +end + +------------------------------------------------------------------------------- + math.randomseed(native.uptime())--TODO: Make it better? lprint = function (...) @@ -26,7 +47,10 @@ lprint("LuPI L1 INIT") modules = {} deadhooks = {} -local function loadModule(name) +function loadModule(name) + if modules[name] then + return + end lprint("LuPI L1 INIT > Load module > " .. name) io.flush() --TODO: PRERELEASE: Module sandboxing, preferably secure-ish @@ -63,11 +87,7 @@ function main() loadModule("eeprom") loadModule("gpio") - if framebuffer.isReady() then - loadModule("fbgpu") - else - loadModule("textgpu") - end + loadModule("gpudetect") loadModule("filesystem") loadModule("internet") @@ -89,14 +109,7 @@ function main() end modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) - if framebuffer.isReady() then - modules.fbgpu.start() - else - local textgpuAddr, tgfail = modules.textgpu.start() - if not textgpuAddr then - lprint("Couldn't initialize text gpu: " .. tostring(tgfail)) - end - end + modules.gpudetect.run() if native.debug then modules.debug.hook()