You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.6 KiB
145 lines
3.6 KiB
#define _POSIX_C_SOURCE 200809l
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <math.h>
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include "api/api.h"
|
|
#include "renderer.h"
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
// TODO: configuration here is required
|
|
#define FONT_14PT_SIZE_MM 3
|
|
|
|
SDL_Window *window;
|
|
|
|
static double get_scale(SDL_DisplayMode *dm)
|
|
{
|
|
float dpi, s;
|
|
SDL_GetDisplayDPI(0, NULL, &dpi, NULL);
|
|
// TODO: get the current window's screen not the primary
|
|
printf("w: %d, h: %d\n", dm->w, dm->h);
|
|
s = (1.0/dpi)*25.4; // size of a point
|
|
// as per standard 14 point should be 3mm
|
|
return fmax(1.0, floor((FONT_14PT_SIZE_MM/(14*s))*10)/10.0);
|
|
}
|
|
|
|
static void get_exe_filename(char *buf, int sz)
|
|
{
|
|
#ifdef __linux__
|
|
char path[512];
|
|
sprintf(path, "/proc/%d/exe", getpid());
|
|
int len = readlink(path, buf, sz - 1);
|
|
buf[len] = '\0';
|
|
#elif defined(_PREFIX)
|
|
strncpy(buf, _PREFIX, sz);
|
|
#else
|
|
strncpy(buf, "/usr/local/share/lite/", sz);
|
|
#endif
|
|
}
|
|
|
|
static void init_window_icon(void)
|
|
{
|
|
// FIXME: include inside function!?
|
|
#include "../icon.inl"
|
|
(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);
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
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
|
|
#if SDL_VERSION_ATLEAST(2, 0, 5)
|
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
|
#endif
|
|
|
|
SDL_DisplayMode dm;
|
|
SDL_GetDesktopDisplayMode(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);
|
|
int wd = SDL_GetWindowDisplayIndex(window);
|
|
printf("%d \n", wd);
|
|
SDL_GetDesktopDisplayMode(wd, &dm);
|
|
init_window_icon();
|
|
ren_init(window);
|
|
|
|
lua_State *L = luaL_newstate();
|
|
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");
|
|
|
|
lua_pushstring(L, "1.11");
|
|
lua_setglobal(L, "VERSION");
|
|
|
|
lua_pushstring(L, SDL_GetPlatform());
|
|
lua_setglobal(L, "PLATFORM");
|
|
|
|
double scale = get_scale(&dm);
|
|
printf("%f\n", scale);
|
|
lua_pushnumber(L, scale);
|
|
lua_setglobal(L, "SCALE");
|
|
|
|
char exename[2048];
|
|
get_exe_filename(exename, sizeof(exename));
|
|
lua_pushstring(L, exename);
|
|
lua_setglobal(L, "EXEFILE");
|
|
|
|
(void) luaL_dostring(L,
|
|
"local core\n"
|
|
"xpcall(function()\n"
|
|
" SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or SCALE\n"
|
|
" USERDIR = os.getenv(\"HOME\") .. '/.config/lite'\n"
|
|
" TEMPDIR = os.getenv(\"LITE_CACHE\") or '/tmp'\n"
|
|
" PATHSEP = package.config:sub(1, 1)\n"
|
|
" EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n"
|
|
" package.path = EXEDIR .. '/data/?.lua;' .. package.path\n"
|
|
" package.path = EXEDIR .. '/data/?/init.lua;' .. package.path\n"
|
|
// load user config file
|
|
" package.path = USERDIR .. '/?.lua;' .. package.path\n"
|
|
" package.path = USERDIR .. '/?/init.lua;' .. package.path\n"
|
|
" core = require('core')\n"
|
|
" core.init()\n"
|
|
" core.run()\n"
|
|
"end, function(err)\n"
|
|
" print('Error: ' .. tostring(err))\n"
|
|
" print(debug.traceback(nil, 2))\n"
|
|
" if core and core.on_error then\n"
|
|
" pcall(core.on_error, err)\n"
|
|
" end\n"
|
|
" os.exit(1)\n"
|
|
"end)");
|
|
|
|
|
|
lua_close(L);
|
|
SDL_DestroyWindow(window);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|