From 69c508729e775de1faa93a8f4cca3bf2e904714d Mon Sep 17 00:00:00 2001 From: Rowan Easter-Robinson Date: Sun, 18 Apr 2021 15:24:36 +0100 Subject: [PATCH] Added LaunchBrowser API (#153) You can now add LaunchBrowser() to .init.lua which will ask redbean to open your website in the local desktop browser when the server is launched. --- tool/net/redbean.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 6b00f5eca..8d91e634c 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -1461,6 +1461,24 @@ static bool IsHiddenPath(const char *s) { return false; } +static void LaunchBrowser() { + char openbrowsercommand[255]; + char *prog; + if (IsWindows()) { + prog = "explorer"; + } else if (IsXnu()) { + prog = "open"; + } else { + prog = "xdg-open"; + } + struct in_addr addr = serveraddr.sin_addr; + if (addr.s_addr == INADDR_ANY) addr.s_addr = htonl(INADDR_LOOPBACK); + snprintf(openbrowsercommand, sizeof(openbrowsercommand), "%s http://%s:%d", + prog, inet_ntoa(addr), ntohs(serveraddr.sin_port)); + DEBUGF("Opening browser with command %s\n", openbrowsercommand); + system(openbrowsercommand); +} + static int LuaServeAsset(lua_State *L) { size_t pathlen; struct Asset *a; @@ -1915,6 +1933,11 @@ static int LuaGetZipPaths(lua_State *L) { return 1; } +static int LuaLaunchBrowser(lua_State *L) { + LaunchBrowser(); + return 1; +} + static void LuaRun(const char *path) { struct Asset *a; const char *code; @@ -1954,6 +1977,7 @@ static const luaL_Reg kLuaFuncs[] = { {"GetUri", LuaGetUri}, // {"GetVersion", LuaGetVersion}, // {"GetZipPaths", LuaGetZipPaths}, // + {"LaunchBrowser", LuaLaunchBrowser}, // {"HasParam", LuaHasParam}, // {"HidePath", LuaHidePath}, // {"LoadAsset", LuaLoadAsset}, //