Files
fastfetch/src/common/init.c
T

268 lines
6.6 KiB
C
Raw Normal View History

2021-04-09 14:07:05 +02:00
#include "fastfetch.h"
2025-04-14 10:28:01 +08:00
#include "common/init.h"
2022-07-30 23:30:46 +02:00
#include "common/parsing.h"
#include "common/thread.h"
2022-09-06 15:42:37 +02:00
#include "detection/displayserver/displayserver.h"
#include "detection/terminaltheme/terminaltheme.h"
2022-12-06 17:03:31 +08:00
#include "util/textModifier.h"
#include "logo/logo.h"
2021-04-09 14:07:05 +02:00
2022-06-18 14:25:31 +02:00
#include <stdlib.h>
2021-04-09 14:07:05 +02:00
#include <unistd.h>
2024-06-07 10:00:30 +08:00
#include <locale.h>
2022-10-13 18:12:54 +08:00
#ifdef _WIN32
2023-03-24 23:21:39 +08:00
#include <windows.h>
#include "util/windows/unicode.h"
2022-10-13 18:12:54 +08:00
#else
#include <signal.h>
#endif
2021-04-09 14:07:05 +02:00
FFinstance instance; // Global singleton
2021-04-09 14:07:05 +02:00
static void initState(FFstate* state)
{
2022-04-06 14:02:05 +02:00
state->logoWidth = 0;
2022-04-02 14:35:43 +02:00
state->logoHeight = 0;
state->keysHeight = 0;
state->terminalLightTheme = false;
state->titleFqdn = false;
2023-01-14 16:38:34 +01:00
2023-01-15 15:57:26 +01:00
ffPlatformInit(&state->platform);
2023-06-11 00:51:29 +08:00
state->configDoc = NULL;
2023-08-29 11:13:55 +08:00
state->resultDoc = NULL;
state->dynamicInterval = 0;
{
// don't enable bright color if the terminal is in light mode
FFTerminalThemeResult result;
if (ffDetectTerminalTheme(&result, true /* forceEnv for performance */) && !result.bg.dark)
state->terminalLightTheme = true;
}
2021-04-09 14:07:05 +02:00
}
static void defaultConfig(void)
2021-04-09 14:07:05 +02:00
{
2023-10-25 14:43:46 +08:00
ffOptionsInitLogo(&instance.config.logo);
ffOptionsInitGeneral(&instance.config.general);
2023-10-26 10:26:15 +08:00
ffOptionsInitDisplay(&instance.config.display);
2021-04-09 14:07:05 +02:00
}
void ffInitInstance(void)
2021-04-09 14:07:05 +02:00
{
2025-12-02 15:37:30 +08:00
#ifdef _WIN32
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?source=recommendat>
2023-01-15 15:57:26 +01:00
setlocale(LC_ALL, ".UTF8");
2024-06-07 10:00:30 +08:00
#else
// Never use `setlocale(LC_ALL, "")`
setlocale(LC_TIME, "");
2023-01-15 15:57:26 +01:00
#endif
initState(&instance.state);
defaultConfig();
2021-04-09 14:07:05 +02:00
}
2022-06-06 14:02:21 +02:00
static volatile bool ffDisableLinewrap = true;
static volatile bool ffHideCursor = true;
static void resetConsole(void)
2021-10-23 15:48:24 +02:00
{
2022-06-06 14:02:21 +02:00
if(ffDisableLinewrap)
2021-10-23 15:48:24 +02:00
fputs("\033[?7h", stdout);
2022-06-06 14:02:21 +02:00
if(ffHideCursor)
2021-10-23 15:48:24 +02:00
fputs("\033[?25h", stdout);
2023-06-13 11:08:49 +08:00
#if defined(_WIN32)
fflush(stdout);
#endif
if(instance.state.dynamicInterval > 0)
fputs("\033[?1049l", stdout); // Disable alternate buffer
2021-10-23 15:48:24 +02:00
}
2022-10-13 18:12:54 +08:00
#ifdef _WIN32
BOOL WINAPI consoleHandler(FF_MAYBE_UNUSED DWORD signal)
2022-10-13 18:12:54 +08:00
{
resetConsole();
exit(0);
2022-10-13 18:12:54 +08:00
}
#else
static void exitSignalHandler(FF_MAYBE_UNUSED int signal)
2021-10-23 15:48:24 +02:00
{
2022-06-06 14:02:21 +02:00
resetConsole();
2021-10-23 15:48:24 +02:00
exit(0);
}
2022-10-13 18:12:54 +08:00
#endif
2021-10-23 15:48:24 +02:00
void ffStart(void)
2021-06-17 13:49:25 +02:00
{
2023-10-26 10:26:15 +08:00
ffDisableLinewrap = instance.config.display.disableLinewrap && !instance.config.display.pipe && !instance.state.resultDoc;
ffHideCursor = instance.config.display.hideCursor && !instance.config.display.pipe && !instance.state.resultDoc;
2021-10-23 15:48:24 +02:00
2022-10-13 18:12:54 +08:00
#ifdef _WIN32
SetErrorMode(SEM_FAILCRITICALERRORS);
2023-12-17 19:04:26 +08:00
if (instance.config.display.noBuffer)
setvbuf(stdout, NULL, _IONBF, 0);
else
setvbuf(stdout, NULL, _IOFBF, 4096);
2022-10-13 18:12:54 +08:00
SetConsoleCtrlHandler(consoleHandler, TRUE);
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hStdout, &mode);
SetConsoleMode(hStdout, mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
2022-12-10 22:21:11 +08:00
SetConsoleOutputCP(CP_UTF8);
2022-10-13 18:12:54 +08:00
#else
2023-10-26 10:26:15 +08:00
if (instance.config.display.noBuffer) setvbuf(stdout, NULL, _IONBF, 0);
2022-10-13 18:12:54 +08:00
struct sigaction action = { .sa_handler = exitSignalHandler };
2021-10-23 15:48:24 +02:00
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigset_t newmask;
sigemptyset(&newmask);
sigaddset(&newmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &newmask, NULL);
2022-10-13 18:12:54 +08:00
#endif
2021-10-23 15:48:24 +02:00
2022-04-02 14:35:43 +02:00
//reset everything to default before we start printing
2023-10-26 10:26:15 +08:00
if(!instance.config.display.pipe && !instance.state.resultDoc)
2022-06-06 14:02:21 +02:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2022-04-02 14:35:43 +02:00
2022-06-06 14:02:21 +02:00
if(ffHideCursor)
2021-06-17 13:49:25 +02:00
fputs("\033[?25l", stdout);
2022-06-06 14:02:21 +02:00
if(ffDisableLinewrap)
2021-06-17 13:49:25 +02:00
fputs("\033[?7l", stdout);
2022-04-02 14:35:43 +02:00
if(instance.state.dynamicInterval > 0)
{
fputs("\033[?1049h\033[H", stdout); // Enable alternate buffer
fflush(stdout);
}
ffLogoPrint();
2021-06-17 13:49:25 +02:00
}
void ffFinish(void)
2021-04-09 14:07:05 +02:00
{
2022-06-06 14:02:21 +02:00
resetConsole();
2022-09-22 22:02:58 +02:00
}
2022-09-22 21:28:20 +08:00
static void destroyConfig(void)
2022-10-09 13:34:55 +02:00
{
2023-10-25 14:43:46 +08:00
ffOptionsDestroyLogo(&instance.config.logo);
ffOptionsDestroyGeneral(&instance.config.general);
2023-10-26 10:26:15 +08:00
ffOptionsDestroyDisplay(&instance.config.display);
2022-10-09 13:34:55 +02:00
}
static void destroyState(void)
2022-10-09 13:34:55 +02:00
{
ffPlatformDestroy(&instance.state.platform);
yyjson_doc_free(instance.state.configDoc);
2023-08-29 11:13:55 +08:00
yyjson_mut_doc_free(instance.state.resultDoc);
ffStrbufDestroy(&instance.state.genConfigPath);
2022-10-09 13:34:55 +02:00
}
void ffDestroyInstance(void)
2022-10-09 13:34:55 +02:00
{
destroyConfig();
destroyState();
2021-04-09 14:07:05 +02:00
}
2022-02-13 23:51:13 +01:00
2022-04-02 14:35:43 +02:00
//Must be in a file compiled with the libfastfetch target, because the FF_HAVE* macros are not defined for the executable targets
void ffListFeatures(void)
2022-02-13 23:51:13 +01:00
{
fputs(
2024-07-30 11:06:27 +08:00
#if FF_HAVE_THREADS
"threads\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_VULKAN
2022-02-13 23:51:13 +01:00
"vulkan\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_WAYLAND
2022-02-13 23:51:13 +01:00
"wayland\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_XCB_RANDR
2022-02-13 23:51:13 +01:00
"xcb-randr\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_XRANDR
2022-02-13 23:51:13 +01:00
"xrandr\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_DRM
2023-11-29 14:55:40 +08:00
"drm\n"
#endif
#if FF_HAVE_DRM_AMDGPU
"drm_amdgpu\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_GIO
2022-02-13 23:51:13 +01:00
"gio\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_DCONF
2022-02-13 23:51:13 +01:00
"dconf\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_DBUS
2022-02-13 23:51:13 +01:00
"dbus\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_IMAGEMAGICK7
"imagemagick7\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_IMAGEMAGICK6
"imagemagick6\n"
2022-04-06 14:54:39 +02:00
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_CHAFA
2022-04-29 14:11:22 +02:00
"chafa\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_ZLIB
2022-04-14 15:11:14 +02:00
"zlib\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_SQLITE3
2022-05-23 19:30:02 +02:00
"sqlite3\n"
2022-02-13 23:51:13 +01:00
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_RPM
2022-05-24 13:36:33 +02:00
"rpm\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_EGL
2022-06-04 15:21:34 +02:00
"egl\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_GLX
2022-06-04 15:21:34 +02:00
"glx\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_OPENCL
2022-06-05 00:18:40 +02:00
"opencl\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_FREETYPE
"freetype\n"
#endif
2025-02-12 22:22:35 +00:00
#if FF_HAVE_PCIACCESS
"libpciaccess\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_PULSE
2023-01-26 15:48:28 +01:00
"libpulse\n"
2023-01-26 00:49:43 +08:00
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_DDCUTIL
"libddcutil\n"
#endif
2024-11-03 08:35:11 +08:00
#if FF_HAVE_ELF || __sun || (__FreeBSD__ && !__DragonFly__) || __OpenBSD__ || __NetBSD__
2024-08-08 15:03:10 +08:00
"libelf\n"
#endif
2025-12-05 21:09:00 +08:00
#if FF_HAVE_LIBZFS
"libzfs\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_DIRECTX_HEADERS
"Directx Headers\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_USE_SYSTEM_YYJSON
"System yyjson\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_LINUX_VIDEODEV2
2024-04-27 16:36:33 +08:00
"linux/videodev2\n"
#endif
2024-07-30 11:06:27 +08:00
#if FF_HAVE_LINUX_WIRELESS
2024-04-27 16:36:33 +08:00
"linux/wireless\n"
#endif
2024-09-25 16:41:55 +08:00
#if FF_HAVE_EMBEDDED_PCIIDS
"Embedded pciids\n"
#endif
2022-02-13 23:51:13 +01:00
""
, stdout);
}