diff --git a/CHANGELOG.md b/CHANGELOG.md index 8429c1ac1..47476d0f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Features: * Add `--no-buffer` option for easier debugging. CMake option `ENABLE_BUFFER` is removed and always enabled. * Support `--*-key-color` option to change the key color of specified module * Support `--colors-symbol` and `--colors-padding-left` +* Add LM (Login Manager) module # 1.11.3 diff --git a/CMakeLists.txt b/CMakeLists.txt index a0af3392e..36d44d31a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -293,6 +293,7 @@ set(LIBFASTFETCH_SRC src/modules/icons/icons.c src/modules/gamepad/gamepad.c src/modules/kernel/kernel.c + src/modules/lm/lm.c src/modules/locale/locale.c src/modules/localip/localip.c src/modules/memory/memory.c @@ -354,6 +355,7 @@ if(LINUX) src/detection/gtk_qt/gtk.c src/detection/host/host_linux.c src/detection/icons/icons_linux.c + src/detection/lm/lm_linux.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_linux.c src/detection/media/media_linux.c @@ -397,6 +399,7 @@ elseif(ANDROID) src/detection/gpu/gpu_nosupport.c src/detection/host/host_android.c src/detection/icons/icons_nosupport.c + src/detection/lm/lm_nosupport.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_nosupport.c src/detection/media/media_nosupport.c @@ -445,6 +448,7 @@ elseif(BSD) src/detection/gpu/gpu_linux.c src/detection/gtk_qt/gtk.c src/detection/host/host_bsd.c + src/detection/lm/lm_linux.c src/detection/icons/icons_linux.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_bsd.c @@ -490,6 +494,7 @@ elseif(APPLE) src/detection/font/font_apple.m src/detection/gpu/gpu_apple.c src/detection/host/host_apple.c + src/detection/lm/lm_nosupport.c src/detection/icons/icons_nosupport.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_apple.c @@ -535,6 +540,7 @@ elseif(WIN32) src/detection/gpu/gpu_windows.c src/detection/host/host_windows.c src/detection/icons/icons_windows.c + src/detection/lm/lm_nosupport.c src/detection/localip/localip_windows.c src/detection/gamepad/gamepad_windows.c src/detection/media/media_nosupport.c diff --git a/README.md b/README.md index a1d654210..48377ed83 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ All categories not listed here should work without needing a specific implementa ##### Available Modules ``` -Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, Gamepad, GPU, Host, Icons, Kernel, Locale, LocalIP, Media, Memory, OpenCL, OpenGL, Packages, Player, Power Adapter, Processes, PublicIP, Separator, OS, Shell, Sound, Swap, Terminal, Terminal Font, Theme, Time, Title, Uptime, Vulkan, Wallpaper, Wifi, WM, WMTheme +Battery, Bios, Bluetooth, Board, Break, Brightness, Colors, Command, CPU, CPUUsage, Cursor, Custom, Date, DateTime, DE, Disk, Display, Font, Gamepad, GPU, Host, Icons, Kernel, LM, Locale, LocalIP, Media, Memory, OpenCL, OpenGL, Packages, Player, Power Adapter, Processes, PublicIP, Separator, OS, Shell, Sound, Swap, Terminal, Terminal Font, Theme, Time, Title, Uptime, Vulkan, Wallpaper, Wifi, WM, WMTheme ``` ##### Builtin logos diff --git a/presets/all b/presets/all index 7079c84af..b669e6643 100644 --- a/presets/all +++ b/presets/all @@ -1 +1 @@ ---structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Wallpaper:Terminal:TerminalFont:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Gamepad:Weather:Break:Colors +--structure Title:Separator:OS:Host:Bios:Board:Chassis:Kernel:Uptime:Processes:Packages:Shell:Display:Brightness:LM:DE:WM:WMTheme:Theme:Icons:Font:Cursor:Wallpaper:Terminal:TerminalFont:CPU:CPUUsage:GPU:Memory:Swap:Disk:Battery:PowerAdapter:Player:Media:PublicIP:LocalIP:Wifi:DateTime:Locale:Vulkan:OpenGL:OpenCL:Users:Bluetooth:Sound:Gamepad:Weather:Break:Colors diff --git a/src/common/init.c b/src/common/init.c index 94883b468..ed522d8ff 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -85,6 +85,7 @@ static void defaultConfig(FFinstance* instance) ffInitDiskOptions(&instance->config.disk); ffInitBatteryOptions(&instance->config.battery); ffInitPowerAdapterOptions(&instance->config.powerAdapter); + ffInitLMOptions(&instance->config.lm); ffInitLocaleOptions(&instance->config.locale); ffInitLocalIpOptions(&instance->config.localIP); ffInitPublicIpOptions(&instance->config.publicIP); @@ -289,6 +290,7 @@ static void destroyConfig(FFinstance* instance) ffDestroyDiskOptions(&instance->config.disk); ffDestroyBatteryOptions(&instance->config.battery); ffDestroyPowerAdapterOptions(&instance->config.powerAdapter); + ffDestroyLMOptions(&instance->config.lm); ffDestroyLocaleOptions(&instance->config.locale); ffDestroyLocalIpOptions(&instance->config.localIP); ffDestroyPublicIpOptions(&instance->config.publicIP); diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 377aa3da7..c2ee05cf9 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -144,6 +144,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, yyjson case 'L': { return + tryModule(instance, type, module, FF_LM_MODULE_NAME, ffParseLMJsonObject) || tryModule(instance, type, module, FF_LOCALE_MODULE_NAME, ffParseLocaleJsonObject) || tryModule(instance, type, module, FF_LOCALIP_MODULE_NAME, ffParseLocalIpJsonObject) || false; diff --git a/src/data/config_user.txt b/src/data/config_user.txt index a05ca7ad8..c2842ad67 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -314,6 +314,7 @@ #--disk-key Disk ({1}) #--battery-key Battery {1} #--poweradapter-key Power Adapter {1} +#--lm-key LM #--locale-key Locale #--localip-key Local IP ({1}) #--publicip-key Public IP @@ -363,6 +364,7 @@ #--disk-format #--battery-format #--poweradapter-format +#--lm-format #--locale-format #--localip-format #--publicip-format @@ -408,6 +410,7 @@ #--disk-key-color #--battery-key-color #--poweradapter-key-color +#--lm-key-color #--locale-key-color #--localip-key-color #--publicip-key-color diff --git a/src/data/json_schema.jsonc b/src/data/json_schema.jsonc index e3c335810..37def9a95 100644 --- a/src/data/json_schema.jsonc +++ b/src/data/json_schema.jsonc @@ -492,6 +492,7 @@ "host", "icons", "kernel", + "lm", "locale", "localip", "media", @@ -551,6 +552,7 @@ "host", "icons", "kernel", + "lm", "locale", "media", "memory", diff --git a/src/detection/lm/lm.h b/src/detection/lm/lm.h new file mode 100644 index 000000000..5fd1919d5 --- /dev/null +++ b/src/detection/lm/lm.h @@ -0,0 +1,18 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_lm_lm +#define FF_INCLUDED_detection_lm_lm + +#include "fastfetch.h" + +typedef struct FFLMResult +{ + FFstrbuf service; + FFstrbuf type; + // bool isDisplay; + // bool remote; +} FFLMResult; + +const char* ffDetectLM(FFLMResult* result); + +#endif diff --git a/src/detection/lm/lm_linux.c b/src/detection/lm/lm_linux.c new file mode 100644 index 000000000..b5893c497 --- /dev/null +++ b/src/detection/lm/lm_linux.c @@ -0,0 +1,23 @@ +#include "lm.h" +#include "common/properties.h" + +#define FF_SYSTEMD_SESSIONS_PATH "/var/run/systemd/sessions/" + +const char* ffDetectLM(FFLMResult* result) +{ + const char* sessionId = getenv("XDG_SESSION_ID"); + if (!sessionId) return "$XDG_SESSION_ID is not set"; + + char path[64] = FF_SYSTEMD_SESSIONS_PATH; + strncpy(path + strlen(FF_SYSTEMD_SESSIONS_PATH), sessionId, sizeof(path) - strlen(FF_SYSTEMD_SESSIONS_PATH) - 1); + + // The file reads `This is private data. Do not parse`. + // What does it mean? But let's ignore it for now. + if (!ffParsePropFileValues(path, 2, (FFpropquery[]) { + {"SERVICE=", &result->service}, + {"TYPE=", &result->type}, + })) + return "Failed to parse /run/systemd/sessions/$XDG_SESSION_ID"; + + return NULL; +} diff --git a/src/detection/lm/lm_nosupport.c b/src/detection/lm/lm_nosupport.c new file mode 100644 index 000000000..9a0a816a1 --- /dev/null +++ b/src/detection/lm/lm_nosupport.c @@ -0,0 +1,6 @@ +#include "lm.h" + +const char* ffDetectLM(FFLMResult* result) +{ + return "Not supported on this platform"; +} diff --git a/src/fastfetch.c b/src/fastfetch.c index c601ed647..8de3e4f14 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -348,6 +348,13 @@ static inline void printCommandHelp(const char* command) "PowerAdapter description" ); } + else if(ffStrEqualsIgnCase(command, "lm-format")) + { + constructAndPrintCommandHelpFormat("lm", "{} ({})", 2, + "LM service", + "LM type" + ); + } else if(ffStrEqualsIgnCase(command, "locale-format")) { constructAndPrintCommandHelpFormat("locale", "{}", 1, @@ -1130,6 +1137,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line) ffPrintBattery(instance, &instance->config.battery); else if(ffStrEqualsIgnCase(line, FF_POWERADAPTER_MODULE_NAME)) ffPrintPowerAdapter(instance, &instance->config.powerAdapter); + else if(ffStrEqualsIgnCase(line, FF_LM_MODULE_NAME)) + ffPrintLM(instance, &instance->config.lm); else if(ffStrEqualsIgnCase(line, FF_LOCALE_MODULE_NAME)) ffPrintLocale(instance, &instance->config.locale); else if(ffStrEqualsIgnCase(line, FF_LOCALIP_MODULE_NAME)) diff --git a/src/fastfetch.h b/src/fastfetch.h index 7f2991db2..48d7dfa37 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -89,6 +89,7 @@ typedef struct FFconfig FFDiskOptions disk; FFBatteryOptions battery; FFPowerAdapterOptions powerAdapter; + FFLMOptions lm; FFLocaleOptions locale; FFLocalIpOptions localIP; FFPublicIpOptions publicIP; diff --git a/src/modules/lm/lm.c b/src/modules/lm/lm.c new file mode 100644 index 000000000..cfb1ac72b --- /dev/null +++ b/src/modules/lm/lm.c @@ -0,0 +1,87 @@ +#include "common/printing.h" +#include "common/jsonconfig.h" +#include "detection/lm/lm.h" +#include "modules/lm/lm.h" +#include "util/stringUtils.h" + +#define FF_LM_NUM_FORMAT_ARGS 2 + +void ffPrintLM(FFinstance* instance, FFLMOptions* options) +{ + FFLMResult result; + const char* error = ffDetectLM(&result); + + if(error) + { + ffPrintError(instance, FF_LM_MODULE_NAME, 0, &options->moduleArgs, "%s", error); + return; + } + + if(result.service.length == 0) + { + ffPrintError(instance, FF_LM_MODULE_NAME, 0, &options->moduleArgs, "No LM service found"); + return; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_LM_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor); + ffStrbufWriteTo(&result.service, stdout); + if(result.type.length) + printf(" (%s)", result.type.chars); + putchar('\n'); + } + else + { + ffPrintFormat(instance, FF_LM_MODULE_NAME, 0, &options->moduleArgs, FF_LM_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &result.service}, + {FF_FORMAT_ARG_TYPE_STRBUF, &result.type}, + }); + } +} + +void ffInitLMOptions(FFLMOptions* options) +{ + options->moduleName = FF_LM_MODULE_NAME; + ffOptionInitModuleArg(&options->moduleArgs); +} + +bool ffParseLMCommandOptions(FFLMOptions* options, const char* key, const char* value) +{ + const char* subKey = ffOptionTestPrefix(key, FF_LM_MODULE_NAME); + if (!subKey) return false; + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) + return true; + + return false; +} + +void ffDestroyLMOptions(FFLMOptions* options) +{ + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +void ffParseLMJsonObject(FFinstance* instance, yyjson_val* module) +{ + FFLMOptions __attribute__((__cleanup__(ffDestroyLMOptions))) options; + ffInitLMOptions(&options); + + if (module) + { + yyjson_val *key_, *val; + size_t idx, max; + yyjson_obj_foreach(module, idx, max, key_, val) + { + const char* key = yyjson_get_str(key_); + if(ffStrEqualsIgnCase(key, "type")) + continue; + + if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs)) + continue; + + ffPrintError(instance, FF_LM_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); + } + } + + ffPrintLM(instance, &options); +} diff --git a/src/modules/lm/lm.h b/src/modules/lm/lm.h new file mode 100644 index 000000000..862f474e0 --- /dev/null +++ b/src/modules/lm/lm.h @@ -0,0 +1,11 @@ +#pragma once + +#include "fastfetch.h" + +#define FF_LM_MODULE_NAME "LM" + +void ffPrintLM(FFinstance* instance, FFLMOptions* options); +void ffInitLMOptions(FFLMOptions* options); +bool ffParseLMCommandOptions(FFLMOptions* options, const char* key, const char* value); +void ffDestroyLMOptions(FFLMOptions* options); +void ffParseLMJsonObject(FFinstance* instance, yyjson_val* module); diff --git a/src/modules/lm/option.h b/src/modules/lm/option.h new file mode 100644 index 000000000..2a788186c --- /dev/null +++ b/src/modules/lm/option.h @@ -0,0 +1,11 @@ +#pragma once + +// This file will be included in "fastfetch.h", do NOT put unnecessary things here + +#include "common/option.h" + +typedef struct FFLMOptions +{ + const char* moduleName; + FFModuleArgs moduleArgs; +} FFLMOptions; diff --git a/src/modules/modules.h b/src/modules/modules.h index 3966b9500..6959ab815 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -25,6 +25,7 @@ #include "modules/host/host.h" #include "modules/icons/icons.h" #include "modules/kernel/kernel.h" +#include "modules/lm/lm.h" #include "modules/locale/locale.h" #include "modules/localip/localip.h" #include "modules/media/media.h" diff --git a/src/modules/options.h b/src/modules/options.h index 12aa53998..2fdc65c3e 100644 --- a/src/modules/options.h +++ b/src/modules/options.h @@ -25,6 +25,7 @@ #include "modules/icons/option.h" #include "modules/kernel/option.h" #include "modules/locale/option.h" +#include "modules/lm/option.h" #include "modules/localip/option.h" #include "modules/media/option.h" #include "modules/memory/option.h"