master ohmu/fastfetch-openbsd / src / modules / lm / lm.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/lm/lm.h"
  5#include "modules/lm/lm.h"
  6
  7bool ffPrintLM(FFLMOptions* options) {
  8    bool success = false;
  9    FFLMResult result;
 10    ffStrbufInit(&result.service);
 11    ffStrbufInit(&result.prettyName);
 12    ffStrbufInit(&result.version);
 13    const char* error = ffDetectLM(&result);
 14
 15    if (error) {
 16        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(LM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 17        goto exit;
 18    }
 19
 20    if (result.service.length == 0) {
 21        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(LM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No LM service found");
 22        goto exit;
 23    }
 24
 25    if (options->moduleArgs.outputFormat.length == 0) {
 26        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(LM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 27        ffStrbufWriteTo(&result.prettyName, stdout);
 28        if (result.version.length) {
 29            printf(" %s", result.version.chars);
 30        }
 31        putchar('\n');
 32    } else {
 33        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(LM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 34                                                                                                       FF_ARG(result.service, "service"),
 35                                                                                                       FF_ARG(result.prettyName, "pretty-name"),
 36                                                                                                       FF_ARG(result.version, "version"),
 37                                                                                                   }));
 38    }
 39    success = true;
 40
 41exit:
 42    ffStrbufDestroy(&result.service);
 43    ffStrbufDestroy(&result.prettyName);
 44    ffStrbufDestroy(&result.version);
 45
 46    return success;
 47}
 48
 49void ffParseLMJsonObject(FFLMOptions* options, yyjson_val* module) {
 50    yyjson_val *key, *val;
 51    size_t idx, max;
 52    yyjson_obj_foreach (module, idx, max, key, val) {
 53        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 54            continue;
 55        }
 56
 57        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(LM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 58    }
 59}
 60
 61[[gnu::cold]]
 62void ffGenerateLMJsonConfig(FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 63    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 64}
 65
 66bool ffGenerateLMJsonResult([[maybe_unused]] FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 67    bool success = false;
 68    FFLMResult result;
 69    ffStrbufInit(&result.service);
 70    ffStrbufInit(&result.prettyName);
 71    ffStrbufInit(&result.version);
 72    const char* error = ffDetectLM(&result);
 73
 74    if (error) {
 75        yyjson_mut_obj_add_str(doc, module, "error", error);
 76        goto exit;
 77    }
 78
 79    if (result.service.length == 0) {
 80        yyjson_mut_obj_add_str(doc, module, "error", "No LM service found");
 81        goto exit;
 82    }
 83
 84    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 85    yyjson_mut_obj_add_strbuf(doc, obj, "service", &result.service);
 86    yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result.prettyName);
 87    yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
 88    success = true;
 89
 90exit:
 91    ffStrbufDestroy(&result.service);
 92    ffStrbufDestroy(&result.prettyName);
 93    ffStrbufDestroy(&result.version);
 94
 95    return success;
 96}
 97
 98void ffInitLMOptions(FFLMOptions* options) {
 99    ffOptionInitModuleArg(&options->moduleArgs, "󰧨");
100}
101
102void ffDestroyLMOptions(FFLMOptions* options) {
103    ffOptionDestroyModuleArg(&options->moduleArgs);
104}
105
106FFModuleBaseInfo ffLMModuleInfo = {
107    .name = "LM",
108    .description = "Print login manager (desktop manager) name and version",
109    .displayName = {
110        .en = "Login Manager",
111        .ar = "مدير تسجيل الدخول",
112        .cs = "Správce přihlášení",
113        .de = "Anmeldemanager",
114        .es = "Gestor de inicio de sesión",
115        .fi = "Sisäänkirjautumishallinta",
116        .fr = "Gestionnaire de connexion",
117        .gl = "Xestor de inicio de sesión",
118        .he = "מנהל התחברות",
119        .id = "Manajer Login",
120        .it = "Gestore di accesso",
121        .ja = "ログインマネージャー",
122        .ko = "로그인 관리자",
123        .pl = "Menedżer logowania",
124        .pt = "Gerenciador de login",
125        .ru = "Менеджер входа",
126        .tr = "Giriş Yöneticisi",
127        .uk = "Менеджер входу",
128        .vi = "Trình quản lý đăng nhập",
129        .zh_CN = "登录管理器",
130        .zh_TW = "登入管理器",
131    },
132    .initOptions = (void*) ffInitLMOptions,
133    .destroyOptions = (void*) ffDestroyLMOptions,
134    .parseJsonObject = (void*) ffParseLMJsonObject,
135    .printModule = (void*) ffPrintLM,
136    .generateJsonResult = (void*) ffGenerateLMJsonResult,
137    .generateJsonConfig = (void*) ffGenerateLMJsonConfig,
138    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
139        { "LM service / process name", "service" },
140        { "LM pretty name", "pretty-name" },
141        { "LM version", "version" },
142    })),
143    .defaultOrder = 20,
144};