diff --git a/CMakeLists.txt b/CMakeLists.txt index 920f98d72..e85996d78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,7 +265,7 @@ set(LIBFASTFETCH_SRC src/modules/bios/bios.c src/modules/bluetooth/bluetooth.c src/modules/board/board.c - src/modules/brightness.c + src/modules/brightness/brightness.c src/modules/break/break.c src/modules/chassis.c src/modules/colors.c diff --git a/src/common/init.c b/src/common/init.c index 0bfbeb164..a09e34fa8 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -62,7 +62,7 @@ static void defaultConfig(FFinstance* instance) ffInitHostOptions(&instance->config.host); ffInitBiosOptions(&instance->config.bios); ffInitBoardOptions(&instance->config.board); - initModuleArg(&instance->config.brightness); + ffInitBrightnessOptions(&instance->config.brightness); initModuleArg(&instance->config.chassis); ffInitCommandOptions(&instance->config.command); ffInitKernelOptions(&instance->config.kernel); @@ -298,6 +298,7 @@ static void destroyConfig(FFinstance* instance) ffDestroyHostOptions(&instance->config.host); ffDestroyBiosOptions(&instance->config.bios); ffDestroyBoardOptions(&instance->config.board); + ffDestroyBrightnessOptions(&instance->config.brightness); destroyModuleArg(&instance->config.chassis); ffDestroyCommandOptions(&instance->config.command); ffDestroyKernelOptions(&instance->config.kernel); diff --git a/src/fastfetch.c b/src/fastfetch.c index 650a30923..6f28ca97c 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1021,7 +1021,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(optionParseModuleArgs(key, value, "packages", &instance->config.packages)) {} else if(optionParseModuleArgs(key, value, "shell", &instance->config.shell)) {} else if(ffParseDisplayCommandOptions(&instance->config.display, key, value)) {} - else if(optionParseModuleArgs(key, value, "brightness", &instance->config.brightness)) {} + else if(ffParseBrightnessCommandOptions(&instance->config.brightness, key, value)) {} else if(optionParseModuleArgs(key, value, "de", &instance->config.de)) {} else if(optionParseModuleArgs(key, value, "wifi", &instance->config.wifi)) {} else if(optionParseModuleArgs(key, value, "wm", &instance->config.wm)) {} @@ -1265,7 +1265,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char else if(strcasecmp(line, "board") == 0) ffPrintBoard(instance, &instance->config.board); else if(strcasecmp(line, "brightness") == 0) - ffPrintBrightness(instance); + ffPrintBrightness(instance, &instance->config.brightness); else if(strcasecmp(line, "chassis") == 0) ffPrintChassis(instance); else if(strcasecmp(line, "kernel") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 16975ef34..3fe6d3f54 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -96,7 +96,7 @@ typedef struct FFconfig FFHostOptions host; FFBiosOptions bios; FFBoardOptions board; - FFModuleArgs brightness; + FFBrightnessOptions brightness; FFModuleArgs chassis; FFCommandOptions command; FFKernelOptions kernel; @@ -255,7 +255,6 @@ void ffPrintUptime(FFinstance* instance); void ffPrintProcesses(FFinstance* instance); void ffPrintPackages(FFinstance* instance); void ffPrintShell(FFinstance* instance); -void ffPrintBrightness(FFinstance* instance); void ffPrintDesktopEnvironment(FFinstance* instance); void ffPrintWM(FFinstance* instance); void ffPrintWMTheme(FFinstance* instance); diff --git a/src/modules/brightness.c b/src/modules/brightness.c deleted file mode 100644 index 9c1cde499..000000000 --- a/src/modules/brightness.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "fastfetch.h" -#include "common/printing.h" -#include "detection/brightness/brightness.h" - -#define FF_BRIGHTNESS_MODULE_NAME "Brightness" -#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2 - -void ffPrintBrightness(FFinstance* instance) -{ - FF_LIST_AUTO_DESTROY result; - ffListInit(&result, sizeof(FFBrightnessResult)); - const char* error = ffDetectBrightness(&result); - - if(error) - { - ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "%s", error); - return; - } - - if(result.length == 0) - { - ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "No result is detected."); - return; - } - - FF_STRBUF_AUTO_DESTROY key; - ffStrbufInit(&key); - - FF_LIST_FOR_EACH(FFBrightnessResult, item, result) - { - if(instance->config.brightness.key.length == 0) - { - ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars); - } - else - { - ffParseFormatString(&key, &instance->config.brightness.key, 1, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &item->name} - }); - } - - if(instance->config.brightness.outputFormat.length == 0) - { - ffPrintLogoAndKey(instance, key.chars, 0, NULL); - printf("%.0f%%\n", item->value); - } - else - { - ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.brightness.outputFormat, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) { - {FF_FORMAT_ARG_TYPE_STRBUF, &item->value}, - {FF_FORMAT_ARG_TYPE_FLOAT, &item->name} - }); - } - - ffStrbufClear(&key); - ffStrbufDestroy(&item->name); - } -} diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c new file mode 100644 index 000000000..fd85ea459 --- /dev/null +++ b/src/modules/brightness/brightness.c @@ -0,0 +1,111 @@ +#include "fastfetch.h" +#include "common/printing.h" +#include "detection/brightness/brightness.h" +#include "modules/brightness/brightness.h" + +#define FF_BRIGHTNESS_MODULE_NAME "Brightness" +#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2 + +void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options) +{ + FF_LIST_AUTO_DESTROY result; + ffListInit(&result, sizeof(FFBrightnessResult)); + const char* error = ffDetectBrightness(&result); + + if(error) + { + ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "%s", error); + return; + } + + if(result.length == 0) + { + ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "No result is detected."); + return; + } + + FF_STRBUF_AUTO_DESTROY key; + ffStrbufInit(&key); + + FF_LIST_FOR_EACH(FFBrightnessResult, item, result) + { + if(options->moduleArgs.key.length == 0) + { + ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars); + } + else + { + ffParseFormatString(&key, &options->moduleArgs.key, 1, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &item->name} + }); + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, key.chars, 0, NULL); + printf("%.0f%%\n", item->value); + } + else + { + ffPrintFormatString(instance, key.chars, 0, NULL, &options->moduleArgs.outputFormat, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) { + {FF_FORMAT_ARG_TYPE_STRBUF, &item->value}, + {FF_FORMAT_ARG_TYPE_FLOAT, &item->name} + }); + } + + ffStrbufClear(&key); + ffStrbufDestroy(&item->name); + } +} + +void ffInitBrightnessOptions(FFBrightnessOptions* options) +{ + options->moduleName = FF_BRIGHTNESS_MODULE_NAME; + ffOptionInitModuleArg(&options->moduleArgs); +} + +bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value) +{ + const char* subKey = ffOptionTestPrefix(key, FF_BRIGHTNESS_MODULE_NAME); + if (!subKey) return false; + if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) + return true; + + return false; +} + +void ffDestroyBrightnessOptions(FFBrightnessOptions* options) +{ + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +#ifdef FF_HAVE_JSONC +bool ffParseBrightnessJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module) +{ + if (strcasecmp(type, FF_BRIGHTNESS_MODULE_NAME) != 0) + return false; + + FFBrightnessOptions __attribute__((__cleanup__(ffDestroyBrightnessOptions))) options; + ffInitBrightnessOptions(&options); + + if (module) + { + struct lh_entry* entry; + lh_foreach(data->ffjson_object_get_object(module), entry) + { + const char* key = (const char *)lh_entry_k(entry); + if (strcasecmp(key, "type") == 0) + continue; + json_object* val = (struct json_object *)lh_entry_v(entry); + + if (ffJsonConfigParseModuleArgs(data, key, val, &options.moduleArgs)) + continue; + + ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); + } + } + + ffPrintBrightness(instance, &options); + return true; +} +#endif diff --git a/src/modules/brightness/brightness.h b/src/modules/brightness/brightness.h new file mode 100644 index 000000000..5f32ddb6f --- /dev/null +++ b/src/modules/brightness/brightness.h @@ -0,0 +1,13 @@ +#pragma once + +#include "fastfetch.h" + +void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options); +void ffInitBrightnessOptions(FFBrightnessOptions* options); +bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value); +void ffDestroyBrightnessOptions(FFBrightnessOptions* options); + +#ifdef FF_HAVE_JSONC +#include "common/config.h" +bool ffParseBrightnessJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module); +#endif diff --git a/src/modules/brightness/option.h b/src/modules/brightness/option.h new file mode 100644 index 000000000..803ea4792 --- /dev/null +++ b/src/modules/brightness/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 FFBrightnessOptions +{ + const char* moduleName; + FFModuleArgs moduleArgs; +} FFBrightnessOptions; diff --git a/src/modules/jsonconfig/jsonconfig.c b/src/modules/jsonconfig/jsonconfig.c index 30c0f2451..dd2d2d157 100644 --- a/src/modules/jsonconfig/jsonconfig.c +++ b/src/modules/jsonconfig/jsonconfig.c @@ -18,6 +18,7 @@ static inline bool parseModuleJsonObject(FFinstance* instance, const char* type, ffParseBluetoothJsonObject(instance, type, data, module) || ffParseBoardJsonObject(instance, type, data, module) || ffParseBreakJsonObject(instance, type, data, module) || + ffParseBrightnessJsonObject(instance, type, data, module) || ffParseCommandJsonObject(instance, type, data, module) || ffParseDateTimeJsonObject(instance, type, data, module) || ffParseDisplayJsonObject(instance, type, data, module) || diff --git a/src/modules/modules.h b/src/modules/modules.h index bd5a75acf..46ab35e4a 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -6,6 +6,7 @@ #include "modules/battery/battery.h" #include "modules/bios/bios.h" #include "modules/bluetooth/bluetooth.h" +#include "modules/brightness/brightness.h" #include "modules/board/board.h" #include "modules/break/break.h" #include "modules/command/command.h" diff --git a/src/modules/options.h b/src/modules/options.h index 3872909fb..0777c7487 100644 --- a/src/modules/options.h +++ b/src/modules/options.h @@ -6,6 +6,7 @@ #include "modules/bios/option.h" #include "modules/bluetooth/option.h" #include "modules/board/option.h" +#include "modules/brightness/option.h" #include "modules/command/option.h" #include "modules/datetime/option.h" #include "modules/display/option.h"