OpenGL: add JSON config support

This commit is contained in:
李通洲
2023-06-07 14:44:27 +08:00
committed by 李通洲
parent d731051cb7
commit e484c6cdfc
13 changed files with 171 additions and 74 deletions
+1
View File
@@ -103,6 +103,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
case 'O': {
return
tryModule(instance, type, module, FF_OPENGL_MODULE_NAME, ffParseOpenGLJsonObject) ||
tryModule(instance, type, module, FF_OS_MODULE_NAME, ffParseOSJsonObject) ||
false;
}
+1
View File
@@ -27,6 +27,7 @@
#include "modules/locale/locale.h"
#include "modules/localip/localip.h"
#include "modules/memory/memory.h"
#include "modules/opengl/opengl.h"
#include "modules/os/os.h"
#include "modules/packages/packages.h"
#include "modules/poweradapter/poweradapter.h"
-42
View File
@@ -1,42 +0,0 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/opengl/opengl.h"
#define FF_OPENGL_MODULE_NAME "OpenGL"
#define FF_OPENGL_NUM_FORMAT_ARGS 4
void ffPrintOpenGL(FFinstance* instance)
{
FFOpenGLResult result;
ffStrbufInit(&result.version);
ffStrbufInit(&result.renderer);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.slv);
const char* error = ffDetectOpenGL(instance, &result);
if(error)
{
ffPrintError(instance, FF_OPENGL_MODULE_NAME, 0, &instance->config.openGL, "%s", error);
return;
}
if(instance->config.openGL.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_OPENGL_MODULE_NAME, 0, &instance->config.openGL.key);
puts(result.version.chars);
}
else
{
ffPrintFormat(instance, FF_OPENGL_MODULE_NAME, 0, &instance->config.openGL, FF_OPENGL_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result.version},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.renderer},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.slv}
});
}
ffStrbufDestroy(&result.version);
ffStrbufDestroy(&result.renderer);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.slv);
}
+117
View File
@@ -0,0 +1,117 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/opengl/opengl.h"
#include "modules/opengl/opengl.h"
#define FF_OPENGL_NUM_FORMAT_ARGS 4
void ffPrintOpenGL(FFinstance* instance, FFOpenGLOptions* options)
{
FFOpenGLResult result;
ffStrbufInit(&result.version);
ffStrbufInit(&result.renderer);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.slv);
const char* error = ffDetectOpenGL(instance, &result);
if(error)
{
ffPrintError(instance, FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs.key);
puts(result.version.chars);
}
else
{
ffPrintFormat(instance, FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_OPENGL_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result.version},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.renderer},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.slv}
});
}
ffStrbufDestroy(&result.version);
ffStrbufDestroy(&result.renderer);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.slv);
}
void ffInitOpenGLOptions(FFOpenGLOptions* options)
{
options->moduleName = FF_OPENGL_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
#if defined(__linux__) || defined(__FreeBSD__)
options->type = FF_OPENGL_TYPE_AUTO;
#endif
}
bool ffParseOpenGLCommandOptions(FFOpenGLOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_OPENGL_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
#if defined(__linux__) || defined(__FreeBSD__)
options->type = (FFOpenGLType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
{ "auto", FF_OPENGL_TYPE_AUTO },
{ "egl", FF_OPENGL_TYPE_EGL },
{ "glx", FF_OPENGL_TYPE_GLX },
{ "osmesa", FF_OPENGL_TYPE_OSMESA },
{}
});
#endif
return false;
}
void ffDestroyOpenGLOptions(FFOpenGLOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseOpenGLJsonObject(FFinstance* instance, json_object* module)
{
FFOpenGLOptions __attribute__((__cleanup__(ffDestroyOpenGLOptions))) options;
ffInitOpenGLOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
#if defined(__linux__) || defined(__FreeBSD__)
if (strcasecmp(key, "type") == 0)
{
int value;
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
{ "auto", FF_OPENGL_TYPE_AUTO },
{ "egl", FF_OPENGL_TYPE_EGL },
{ "glx", FF_OPENGL_TYPE_GLX },
{ "osmesa", FF_OPENGL_TYPE_OSMESA },
{},
});
if (error)
ffPrintError(instance, FF_OPENGL_MODULE_NAME, 0, &options.moduleArgs, "Invalid %s value: %s", key, error);
else
options.type = (FFOpenGLType) value;
continue;
}
#endif
ffPrintError(instance, FF_OPENGL_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintOpenGL(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_OPENGL_MODULE_NAME "OpenGL"
void ffPrintOpenGL(FFinstance* instance, FFOpenGLOptions* options);
void ffInitOpenGLOptions(FFOpenGLOptions* options);
bool ffParseOpenGLCommandOptions(FFOpenGLOptions* options, const char* key, const char* value);
void ffDestroyOpenGLOptions(FFOpenGLOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseOpenGLJsonObject(FFinstance* instance, json_object* module);
#endif
+25
View File
@@ -0,0 +1,25 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
#if defined(__linux__) || defined(__FreeBSD__)
typedef enum FFOpenGLType
{
FF_OPENGL_TYPE_AUTO,
FF_OPENGL_TYPE_EGL,
FF_OPENGL_TYPE_GLX,
FF_OPENGL_TYPE_OSMESA
} FFOpenGLType;
#endif
typedef struct FFOpenGLOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
#if defined(__linux__) || defined(__FreeBSD__)
FFOpenGLType type;
#endif
} FFOpenGLOptions;
+1
View File
@@ -25,6 +25,7 @@
#include "modules/locale/option.h"
#include "modules/localip/option.h"
#include "modules/memory/option.h"
#include "modules/opengl/option.h"
#include "modules/os/option.h"
#include "modules/packages/option.h"
#include "modules/poweradapter/option.h"