From a4bc8986ff82d6c2ce0bd315bf283f3be534fdce Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Fri, 5 Mar 2021 10:33:50 +0100 Subject: [PATCH] Implement fatal error message box Used when lite-xl fails at startup --- data/core/init.lua | 2 +- src/api/system.c | 25 +++++++++++++++++++++++++ src/main.c | 9 +++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 6eefacae..da24c930 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -406,7 +406,7 @@ function core.init() end project_dir_abs = system.absolute_path(".") if not core.set_project_dir(project_dir_abs) then - print("internal error: cannot set project directory to cwd") + system.show_fatal_error("Lite XL internal error", "cannot set project directory to cwd") os.exit(1) end end diff --git a/src/api/system.c b/src/api/system.c index 3f43dca0..56880616 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -258,6 +258,30 @@ static int f_show_confirm_dialog(lua_State *L) { } +static int f_show_fatal_error(lua_State *L) { + const char *title = luaL_checkstring(L, 1); + const char *msg = luaL_checkstring(L, 2); + +#ifdef _WIN32 + MessageBox(0, msg, title, MB_OK | MB_ICONERROR); + +#else + SDL_MessageBoxButtonData buttons[] = { + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Ok" }, + }; + SDL_MessageBoxData data = { + .title = title, + .message = msg, + .numbuttons = 1, + .buttons = buttons, + }; + int buttonid; + SDL_ShowMessageBox(&data, &buttonid); +#endif + return 0; +} + + static int f_chdir(lua_State *L) { const char *path = luaL_checkstring(L, 1); int err = chdir(path); @@ -441,6 +465,7 @@ static const luaL_Reg lib[] = { { "set_window_size", f_set_window_size }, { "window_has_focus", f_window_has_focus }, { "show_confirm_dialog", f_show_confirm_dialog }, + { "show_fatal_error", f_show_fatal_error }, { "chdir", f_chdir }, { "mkdir", f_mkdir }, { "list_dir", f_list_dir }, diff --git a/src/main.c b/src/main.c index 848c43f6..ed02c11b 100644 --- a/src/main.c +++ b/src/main.c @@ -134,8 +134,13 @@ init_lua: " core.init()\n" " core.run()\n" "end, function(err)\n" - " print('Error: ' .. tostring(err))\n" - " print(debug.traceback(nil, 2))\n" + " system.show_fatal_error('Lite XL start error', '" + "Fatal error: cannot locate the data directory.\\n" + "Please verify that the data folder is available.')\n" + " local fp = io.open((exedir and exedir .. '/' or '') .. 'error.txt', 'wb')\n" + " fp:write('Error: ' .. tostring(err) .. '\\n')\n" + " fp:write(debug.traceback(nil, 2) .. '\\n')\n" + " fp:close()\n" " if core and core.on_error then\n" " pcall(core.on_error, err)\n" " end\n"