lite-xl/src/main.c

194 lines
5.0 KiB
C
Raw Normal View History

2019-12-28 12:16:32 +01:00
#include <stdio.h>
#include <stdlib.h>
2020-06-29 15:24:08 +02:00
#include <SDL.h>
2019-12-28 12:16:32 +01:00
#include "api/api.h"
#include "rencache.h"
2019-12-28 12:16:32 +01:00
#include "renderer.h"
2019-12-28 12:16:32 +01:00
#ifdef _WIN32
#include <windows.h>
#elif __linux__ || __FreeBSD__
#include <unistd.h>
#include <signal.h>
#elif __APPLE__
#include <mach-o/dyld.h>
2019-12-28 12:16:32 +01:00
#endif
SDL_Window *window;
static double get_scale(void) {
#ifndef __APPLE__
float dpi;
if (SDL_GetDisplayDPI(0, NULL, &dpi, NULL) == 0)
return dpi / 96.0;
#endif
return 1.0;
}
2020-05-17 10:36:46 +02:00
static void get_exe_filename(char *buf, int sz) {
#if _WIN32
int len = GetModuleFileName(NULL, buf, sz - 1);
buf[len] = '\0';
#elif __linux__
2021-10-12 09:06:37 +02:00
char path[] = "/proc/self/exe";
int len = readlink(path, buf, sz - 1);
buf[len] = '\0';
#elif __APPLE__
/* use realpath to resolve a symlink if the process was launched from one.
** This happens when Homebrew installs a cack and creates a symlink in
** /usr/loca/bin for launching the executable from the command line. */
unsigned size = sz;
char exepath[size];
_NSGetExecutablePath(exepath, &size);
realpath(exepath, buf);
#else
2020-05-17 10:36:46 +02:00
strcpy(buf, "./lite");
#endif
}
2020-05-06 20:49:34 +02:00
static void init_window_icon(void) {
#if !defined(_WIN32) && !defined(__APPLE__)
2021-06-20 17:37:08 +02:00
#include "../resources/icons/icon.inl"
2020-05-06 20:49:34 +02:00
(void) icon_rgba_len; /* unused */
SDL_Surface *surf = SDL_CreateRGBSurfaceFrom(
icon_rgba, 64, 64,
32, 64 * 4,
0x000000ff,
0x0000ff00,
0x00ff0000,
0xff000000);
SDL_SetWindowIcon(window, surf);
SDL_FreeSurface(surf);
#endif
}
#ifdef _WIN32
#define LITE_OS_HOME "USERPROFILE"
#define LITE_PATHSEP_PATTERN "\\\\"
#define LITE_NONPATHSEP_PATTERN "[^\\\\]+"
#else
#define LITE_OS_HOME "HOME"
#define LITE_PATHSEP_PATTERN "/"
#define LITE_NONPATHSEP_PATTERN "[^/]+"
#endif
2021-04-19 09:52:00 +02:00
#ifdef __APPLE__
void enable_momentum_scroll();
#ifdef MACOS_USE_BUNDLE
void set_macos_bundle_resources(lua_State *L);
#endif
2021-04-19 09:52:00 +02:00
#endif
2019-12-28 12:16:32 +01:00
int main(int argc, char **argv) {
#ifdef _WIN32
HINSTANCE lib = LoadLibrary("user32.dll");
int (*SetProcessDPIAware)() = (void*) GetProcAddress(lib, "SetProcessDPIAware");
SetProcessDPIAware();
#else
signal(SIGPIPE, SIG_IGN);
2019-12-28 12:16:32 +01:00
#endif
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_EnableScreenSaver();
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
atexit(SDL_Quit);
#ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* Available since 2.0.8 */
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
#endif
2019-12-28 12:16:32 +01:00
#if SDL_VERSION_ATLEAST(2, 0, 5)
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
#endif
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(0, &dm);
window = SDL_CreateWindow(
"", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w * 0.8, dm.h * 0.8,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
2020-05-06 20:49:34 +02:00
init_window_icon();
2019-12-28 12:16:32 +01:00
ren_init(window);
lua_State *L;
init_lua:
L = luaL_newstate();
2019-12-28 12:16:32 +01:00
luaL_openlibs(L);
api_load_libs(L);
lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i + 1);
}
lua_setglobal(L, "ARGS");
2020-04-23 21:03:14 +02:00
lua_pushstring(L, SDL_GetPlatform());
lua_setglobal(L, "PLATFORM");
2019-12-28 12:16:32 +01:00
lua_pushnumber(L, get_scale());
lua_setglobal(L, "SCALE");
2019-12-28 12:16:32 +01:00
2020-05-17 10:36:46 +02:00
char exename[2048];
get_exe_filename(exename, sizeof(exename));
lua_pushstring(L, exename);
lua_setglobal(L, "EXEFILE");
#ifdef __APPLE__
enable_momentum_scroll();
#ifdef MACOS_USE_BUNDLE
set_macos_bundle_resources(L);
#endif
#endif
const char *init_lite_code = \
2019-12-28 12:16:32 +01:00
"local core\n"
"xpcall(function()\n"
" HOME = os.getenv('" LITE_OS_HOME "')\n"
" local exedir = EXEFILE:match('^(.*)" LITE_PATHSEP_PATTERN LITE_NONPATHSEP_PATTERN "$')\n"
" local prefix = exedir:match('^(.*)" LITE_PATHSEP_PATTERN "bin$')\n"
" dofile((MACOS_RESOURCES or (prefix and prefix .. '/share/lite-xl' or exedir .. '/data')) .. '/core/start.lua')\n"
" core = require(os.getenv('LITE_XL_RUNTIME') or 'core')\n"
2019-12-28 12:16:32 +01:00
" core.init()\n"
" core.run()\n"
"end, function(err)\n"
" local error_dir\n"
" io.stdout:write('Error: '..tostring(err)..'\\n')\n"
" io.stdout:write(debug.traceback(nil, 4)..'\\n')\n"
2019-12-28 12:16:32 +01:00
" if core and core.on_error then\n"
" error_dir=USERDIR\n"
2019-12-28 12:16:32 +01:00
" pcall(core.on_error, err)\n"
" else\n"
" error_dir=system.absolute_path('.')\n"
" local fp = io.open('error.txt', 'wb')\n"
" fp:write('Error: ' .. tostring(err) .. '\\n')\n"
" fp:write(debug.traceback(nil, 4)..'\\n')\n"
" fp:close()\n"
2019-12-28 12:16:32 +01:00
" end\n"
" system.show_fatal_error('Lite XL internal error',\n"
" 'An internal error occurred in a critical part of the application.\\n\\n'..\n"
" 'Please verify the file \\\"error.txt\\\" in the directory '..error_dir)\n"
2019-12-28 12:16:32 +01:00
" os.exit(1)\n"
"end)\n"
"return core and core.restart_request\n";
2019-12-28 12:16:32 +01:00
if (luaL_loadstring(L, init_lite_code)) {
fprintf(stderr, "internal error when starting the application\n");
exit(1);
}
lua_pcall(L, 0, 1, 0);
if (lua_toboolean(L, -1)) {
lua_close(L);
rencache_invalidate();
goto init_lua;
}
2019-12-28 12:16:32 +01:00
lua_close(L);
ren_free_window_resources();
2019-12-28 12:16:32 +01:00
return EXIT_SUCCESS;
}