master ohmu/fastfetch-openbsd / src / modules / opengl / opengl.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/opengl/opengl.h"
  5#include "modules/opengl/opengl.h"
  6
  7bool ffPrintOpenGL(FFOpenGLOptions* options) {
  8    bool success = false;
  9    FFOpenGLResult result;
 10    ffStrbufInit(&result.version);
 11    ffStrbufInit(&result.renderer);
 12    ffStrbufInit(&result.vendor);
 13    ffStrbufInit(&result.slv);
 14    ffStrbufInit(&result.library);
 15
 16    const char* error = ffDetectOpenGL(options, &result);
 17    if (error) {
 18        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(OpenGL), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 19    } else {
 20        if (options->moduleArgs.outputFormat.length == 0) {
 21            ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(OpenGL), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 22            puts(result.version.chars);
 23        } else {
 24            FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(OpenGL), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 25                                                                                                               FF_ARG(result.version, "version"),
 26                                                                                                               FF_ARG(result.renderer, "renderer"),
 27                                                                                                               FF_ARG(result.vendor, "vendor"),
 28                                                                                                               FF_ARG(result.slv, "slv"),
 29                                                                                                               FF_ARG(result.library, "library"),
 30                                                                                                           }));
 31        }
 32        success = true;
 33    }
 34
 35    ffStrbufDestroy(&result.version);
 36    ffStrbufDestroy(&result.renderer);
 37    ffStrbufDestroy(&result.vendor);
 38    ffStrbufDestroy(&result.slv);
 39    ffStrbufDestroy(&result.library);
 40
 41    return success;
 42}
 43
 44void ffParseOpenGLJsonObject(FFOpenGLOptions* options, yyjson_val* module) {
 45    yyjson_val *key, *val;
 46    size_t idx, max;
 47    yyjson_obj_foreach (module, idx, max, key, val) {
 48        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 49            continue;
 50        }
 51
 52        if (unsafe_yyjson_equals_str(key, "library")) {
 53            int value;
 54            const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
 55                                                                       { "auto", FF_OPENGL_LIBRARY_AUTO },
 56                                                                       { "egl", FF_OPENGL_LIBRARY_EGL },
 57                                                                       { "glx", FF_OPENGL_LIBRARY_GLX },
 58                                                                       {},
 59                                                                   });
 60            if (error) {
 61                ffPrintError(FF_MODULE_GET_DISPLAY_NAME(OpenGL), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Invalid %s value: %s", unsafe_yyjson_get_str(key), error);
 62            } else {
 63                options->library = (FFOpenGLLibrary) value;
 64            }
 65            continue;
 66        }
 67
 68        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(OpenGL), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 69    }
 70}
 71
 72[[gnu::cold]]
 73void ffGenerateOpenGLJsonConfig(FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 74    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 75
 76    switch (options->library) {
 77        case FF_OPENGL_LIBRARY_AUTO:
 78            yyjson_mut_obj_add_str(doc, module, "library", "auto");
 79            break;
 80        case FF_OPENGL_LIBRARY_EGL:
 81            yyjson_mut_obj_add_str(doc, module, "library", "egl");
 82            break;
 83        case FF_OPENGL_LIBRARY_GLX:
 84            yyjson_mut_obj_add_str(doc, module, "library", "glx");
 85            break;
 86    }
 87}
 88
 89bool ffGenerateOpenGLJsonResult([[maybe_unused]] FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 90    bool success = false;
 91    FFOpenGLResult result;
 92    ffStrbufInit(&result.version);
 93    ffStrbufInit(&result.renderer);
 94    ffStrbufInit(&result.vendor);
 95    ffStrbufInit(&result.slv);
 96    ffStrbufInit(&result.library);
 97
 98    const char* error = ffDetectOpenGL(options, &result);
 99    if (error != nullptr) {
100        yyjson_mut_obj_add_str(doc, module, "error", error);
101    } else {
102        yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
103        yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
104        yyjson_mut_obj_add_strbuf(doc, obj, "renderer", &result.renderer);
105        yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor);
106        yyjson_mut_obj_add_strbuf(doc, obj, "slv", &result.slv);
107        yyjson_mut_obj_add_strbuf(doc, obj, "library", &result.library);
108        success = true;
109    }
110
111    ffStrbufDestroy(&result.version);
112    ffStrbufDestroy(&result.renderer);
113    ffStrbufDestroy(&result.vendor);
114    ffStrbufDestroy(&result.slv);
115    ffStrbufDestroy(&result.library);
116
117    return success;
118}
119
120void ffInitOpenGLOptions(FFOpenGLOptions* options) {
121    ffOptionInitModuleArg(&options->moduleArgs, "");
122
123    options->library = FF_OPENGL_LIBRARY_AUTO;
124}
125
126void ffDestroyOpenGLOptions(FFOpenGLOptions* options) {
127    ffOptionDestroyModuleArg(&options->moduleArgs);
128}
129
130FFModuleBaseInfo ffOpenGLModuleInfo = {
131    .name = "OpenGL",
132    .description = "Print the highest OpenGL version supported by the GPU",
133    .displayName = {
134        .en = "OpenGL",
135        .ar = "OpenGL",
136        .cs = "OpenGL",
137        .de = "OpenGL",
138        .es = "OpenGL",
139        .fi = "OpenGL",
140        .fr = "OpenGL",
141        .gl = "OpenGL",
142        .he = "OpenGL",
143        .id = "OpenGL",
144        .it = "OpenGL",
145        .ja = "OpenGL",
146        .ko = "OpenGL",
147        .pl = "OpenGL",
148        .pt = "OpenGL",
149        .ru = "OpenGL",
150        .tr = "OpenGL",
151        .uk = "OpenGL",
152        .vi = "OpenGL",
153        .zh_CN = "OpenGL",
154        .zh_TW = "OpenGL",
155    },
156    .initOptions = (void*) ffInitOpenGLOptions,
157    .destroyOptions = (void*) ffDestroyOpenGLOptions,
158    .parseJsonObject = (void*) ffParseOpenGLJsonObject,
159    .printModule = (void*) ffPrintOpenGL,
160    .generateJsonResult = (void*) ffGenerateOpenGLJsonResult,
161    .generateJsonConfig = (void*) ffGenerateOpenGLJsonConfig,
162    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
163        { "OpenGL version", "version" },
164        { "OpenGL renderer", "renderer" },
165        { "OpenGL vendor", "vendor" },
166        { "OpenGL shading language version", "slv" },
167        { "OpenGL library used", "library" },
168    })),
169    .defaultOrder = 56,
170};