NativeResolution: init code

This commit is contained in:
李通洲
2023-08-08 19:39:05 +08:00
parent 38044acf75
commit a58db92ab8
11 changed files with 162 additions and 0 deletions
+1
View File
@@ -30,6 +30,7 @@
#include "modules/localip/localip.h"
#include "modules/media/media.h"
#include "modules/memory/memory.h"
#include "modules/nativeresolution/nativeresolution.h"
#include "modules/opengl/opengl.h"
#include "modules/opencl/opencl.h"
#include "modules/os/os.h"
@@ -0,0 +1,106 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "detection/nativeresolution/nativeresolution.h"
#include "modules/nativeresolution/nativeresolution.h"
#include "util/stringUtils.h"
#define FF_NATIVERESOLUTION_DISPLAY_NAME "Native Resolution"
#define FF_NATIVERESOLUTION_NUM_FORMAT_ARGS 3
void ffPrintNativeResolution(FFNativeResolutionOptions* options)
{
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNativeResolutionResult));
const char* error = ffDetectNativeResolution(&result);
if(error)
{
ffPrintError(FF_NATIVERESOLUTION_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
if(!result.length)
{
ffPrintError(FF_NATIVERESOLUTION_DISPLAY_NAME, 0, &options->moduleArgs, "No devices detected");
return;
}
uint8_t index = 0;
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
FF_LIST_FOR_EACH(FFNativeResolutionResult, display, result)
{
if(options->moduleArgs.outputFormat.length == 0)
{
ffStrbufClear(&key);
if(options->moduleArgs.key.length == 0)
{
ffStrbufAppendF(&key, "%s (%s)", FF_NATIVERESOLUTION_DISPLAY_NAME, display->name.chars);
}
else
{
ffParseFormatString(&key, &options->moduleArgs.key, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &display->name},
});
}
ffPrintLogoAndKey(key.chars, 0, NULL, &options->moduleArgs.keyColor);
printf("%ux%u", display->width, display->height);
}
else
{
ffPrintFormat(FF_NATIVERESOLUTION_DISPLAY_NAME, index, &options->moduleArgs, FF_NATIVERESOLUTION_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &display->name},
{FF_FORMAT_ARG_TYPE_UINT, &display->width},
{FF_FORMAT_ARG_TYPE_UINT, &display->height},
});
}
ffStrbufDestroy(&display->name);
}
}
void ffInitNativeResolutionOptions(FFNativeResolutionOptions* options)
{
options->moduleName = FF_NATIVERESOLUTION_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseNativeResolutionCommandOptions(FFNativeResolutionOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_NATIVERESOLUTION_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyNativeResolutionOptions(FFNativeResolutionOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
void ffParseNativeResolutionJsonObject(yyjson_val* module)
{
FFNativeResolutionOptions __attribute__((__cleanup__(ffDestroyNativeResolutionOptions))) options;
ffInitNativeResolutionOptions(&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(FF_NATIVERESOLUTION_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintNativeResolution(&options);
}
@@ -0,0 +1,11 @@
#pragma once
#include "fastfetch.h"
#define FF_NATIVERESOLUTION_MODULE_NAME "NativeResolution"
void ffPrintNativeResolution(FFNativeResolutionOptions* options);
void ffInitNativeResolutionOptions(FFNativeResolutionOptions* options);
bool ffParseNativeResolutionCommandOptions(FFNativeResolutionOptions* options, const char* key, const char* value);
void ffDestroyNativeResolutionOptions(FFNativeResolutionOptions* options);
void ffParseNativeResolutionJsonObject(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 FFNativeResolutionOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFNativeResolutionOptions;
+1
View File
@@ -29,6 +29,7 @@
#include "modules/localip/option.h"
#include "modules/media/option.h"
#include "modules/memory/option.h"
#include "modules/nativeresolution/option.h"
#include "modules/opengl/option.h"
#include "modules/opencl/option.h"
#include "modules/os/option.h"