Customized fork of github.com/rxi/lite
at main 3.3 kB view raw
1#include <stdio.h> 2#include <stdlib.h> 3#include <SDL3/SDL.h> 4#include "api/api.h" 5#include "renderer.h" 6 7#ifdef _WIN32 8# include <windows.h> 9#elif __linux__ 10# include <unistd.h> 11#elif __APPLE__ 12# include <mach-o/dyld.h> 13#endif 14 15 16SDL_Window *window; 17 18 19static double get_scale(void) 20{ 21 return (double)SDL_min(SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()), 1.0); 22} 23 24static void get_exe_filename(char *buf, int sz) 25{ 26# if _WIN32 27 int len = GetModuleFileName(NULL, buf, sz - 1); 28 buf[len] = '\0'; 29# elif __linux__ 30 char path[512]; 31 sprintf(path, "/proc/%d/exe", getpid()); 32 int len = readlink(path, buf, sz - 1); 33 buf[len] = '\0'; 34# elif __APPLE__ 35 unsigned size = sz; 36 _NSGetExecutablePath(buf, &size); 37# else 38 strcpy(buf, "./lite"); 39# endif 40} 41 42 43static void init_window_icon(void) 44{ 45# ifndef _WIN32 46# include "../icon.inl" 47 (void) icon_rgba_len; /* unused */ 48 SDL_Surface *surf = SDL_CreateSurfaceFrom(64, 64, SDL_PIXELFORMAT_ABGR8888, icon_rgba, 64 * 4); 49 SDL_SetWindowIcon(window, surf); 50 SDL_DestroySurface(surf); 51# endif 52} 53 54 55int main(int argc, char **argv) 56{ 57# ifdef _WIN32 58 HINSTANCE lib = LoadLibrary("user32.dll"); 59 int (*SetProcessDPIAware)() = (void *)GetProcAddress(lib, "SetProcessDPIAware"); 60 SetProcessDPIAware(); 61# endif 62 63 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); 64 SDL_EnableScreenSaver(); 65 SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, true); 66 atexit(SDL_Quit); 67 68 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"); 69 SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); 70 71 const SDL_DisplayMode *dm = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay()); 72 window = SDL_CreateWindow("", (int)(dm->w * 0.8), (int)(dm->h * 0.8), SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_HIDDEN); 73 if (!window) 74 { 75 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create window: %s\n", SDL_GetError()); 76 return EXIT_FAILURE; 77 } 78 init_window_icon(); 79 ren_init(window); 80 81 if (!SDL_StartTextInput(window)) 82 { 83# ifndef _WIN32 84 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "ERROR: failed SDL_StartTextInput() - text entry may not work"); 85# endif 86 } 87 88 89 lua_State *L = luaL_newstate(); 90 luaL_openlibs(L); 91 api_load_libs(L); 92 93 lua_newtable(L); 94 for (int i = 0 ; i < argc ; i++) 95 { 96 lua_pushstring(L, argv[i]); 97 lua_rawseti(L, -2, i + 1); 98 } 99 lua_setglobal(L, "ARGS"); 100 101 lua_pushstring(L, "1.11"); 102 lua_setglobal(L, "VERSION"); 103 104 lua_pushstring(L, SDL_GetPlatform()); 105 lua_setglobal(L, "PLATFORM"); 106 107 lua_pushnumber(L, get_scale()); 108 lua_setglobal(L, "SCALE"); 109 110 char exename[2048]; 111 get_exe_filename(exename, sizeof(exename)); 112 lua_pushstring(L, exename); 113 lua_setglobal(L, "EXEFILE"); 114 115 (void) luaL_dostring(L, 116 "local core\n" 117 "xpcall(function()\n" 118 " SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or SCALE\n" 119 " PATHSEP = package.config:sub(1, 1)\n" 120 " EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n" 121 " package.path = EXEDIR .. '/data/?.lua;' .. package.path\n" 122 " package.path = EXEDIR .. '/data/?/init.lua;' .. package.path\n" 123 " core = require('core')\n" 124 " core.init()\n" 125 " core.run()\n" 126 "end, function(err)\n" 127 " print('Error: ' .. tostring(err))\n" 128 " print(debug.traceback(nil, 2))\n" 129 " if core and core.on_error then\n" 130 " pcall(core.on_error, err)\n" 131 " end\n" 132 " os.exit(1)\n" 133 "end)"); 134 135 136 lua_close(L); 137 SDL_DestroyWindow(window); 138 139 return EXIT_SUCCESS; 140}