LM: new module

This commit is contained in:
李通洲
2023-06-20 23:01:18 +08:00
parent a88f603e6d
commit 68feaa2800
18 changed files with 185 additions and 2 deletions
+1
View File
@@ -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
+6
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+2
View File
@@ -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);
+1
View File
@@ -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;
+3
View File
@@ -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
+2
View File
@@ -492,6 +492,7 @@
"host",
"icons",
"kernel",
"lm",
"locale",
"localip",
"media",
@@ -551,6 +552,7 @@
"host",
"icons",
"kernel",
"lm",
"locale",
"media",
"memory",
+18
View File
@@ -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
+23
View File
@@ -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;
}
+6
View File
@@ -0,0 +1,6 @@
#include "lm.h"
const char* ffDetectLM(FFLMResult* result)
{
return "Not supported on this platform";
}
+9
View File
@@ -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))
+1
View File
@@ -89,6 +89,7 @@ typedef struct FFconfig
FFDiskOptions disk;
FFBatteryOptions battery;
FFPowerAdapterOptions powerAdapter;
FFLMOptions lm;
FFLocaleOptions locale;
FFLocalIpOptions localIP;
FFPublicIpOptions publicIP;
+87
View File
@@ -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);
}
+11
View File
@@ -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);
+11
View File
@@ -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;
+1
View File
@@ -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"
+1
View File
@@ -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"