summaryrefslogtreecommitdiffstats
path: root/src/modules/opengl/opengl.c
blob: 0754cb20cbf705b2ff8c0fcfc3bb3f1bde549f34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/strutil.h"
#include "detection/opengl/opengl.h"
#include "modules/opengl/opengl.h"

bool ffPrintOpenGL(FFOpenGLOptions* options) {
    bool success = false;
    FFOpenGLResult result;
    ffStrbufInit(&result.version);
    ffStrbufInit(&result.renderer);
    ffStrbufInit(&result.vendor);
    ffStrbufInit(&result.slv);
    ffStrbufInit(&result.library);

    const char* error = ffDetectOpenGL(options, &result);
    if (error) {
        ffPrintError(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
    } else {
        if (options->moduleArgs.outputFormat.length == 0) {
            ffPrintLogoAndKey(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
            puts(result.version.chars);
        } else {
            FF_PRINT_FORMAT_CHECKED(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
                                                                                                               FF_ARG(result.version, "version"),
                                                                                                               FF_ARG(result.renderer, "renderer"),
                                                                                                               FF_ARG(result.vendor, "vendor"),
                                                                                                               FF_ARG(result.slv, "slv"),
                                                                                                               FF_ARG(result.library, "library"),
                                                                                                           }));
        }
        success = true;
    }

    ffStrbufDestroy(&result.version);
    ffStrbufDestroy(&result.renderer);
    ffStrbufDestroy(&result.vendor);
    ffStrbufDestroy(&result.slv);
    ffStrbufDestroy(&result.library);

    return success;
}

void ffParseOpenGLJsonObject(FFOpenGLOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        if (unsafe_yyjson_equals_str(key, "library")) {
            int value;
            const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
                                                                       { "auto", FF_OPENGL_LIBRARY_AUTO },
                                                                       { "egl", FF_OPENGL_LIBRARY_EGL },
                                                                       { "glx", FF_OPENGL_LIBRARY_GLX },
                                                                       {},
                                                                   });
            if (error) {
                ffPrintError(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Invalid %s value: %s", unsafe_yyjson_get_str(key), error);
            } else {
                options->library = (FFOpenGLLibrary) value;
            }
            continue;
        }

        ffPrintError(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

void ffGenerateOpenGLJsonConfig(FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);

    switch (options->library) {
        case FF_OPENGL_LIBRARY_AUTO:
            yyjson_mut_obj_add_str(doc, module, "library", "auto");
            break;
        case FF_OPENGL_LIBRARY_EGL:
            yyjson_mut_obj_add_str(doc, module, "library", "egl");
            break;
        case FF_OPENGL_LIBRARY_GLX:
            yyjson_mut_obj_add_str(doc, module, "library", "glx");
            break;
    }
}

bool ffGenerateOpenGLJsonResult(FF_A_UNUSED FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    bool success = false;
    FFOpenGLResult result;
    ffStrbufInit(&result.version);
    ffStrbufInit(&result.renderer);
    ffStrbufInit(&result.vendor);
    ffStrbufInit(&result.slv);
    ffStrbufInit(&result.library);

    const char* error = ffDetectOpenGL(options, &result);
    if (error != NULL) {
        yyjson_mut_obj_add_str(doc, module, "error", error);
    } else {
        yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
        yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
        yyjson_mut_obj_add_strbuf(doc, obj, "renderer", &result.renderer);
        yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor);
        yyjson_mut_obj_add_strbuf(doc, obj, "slv", &result.slv);
        yyjson_mut_obj_add_strbuf(doc, obj, "library", &result.library);
        success = true;
    }

    ffStrbufDestroy(&result.version);
    ffStrbufDestroy(&result.renderer);
    ffStrbufDestroy(&result.vendor);
    ffStrbufDestroy(&result.slv);
    ffStrbufDestroy(&result.library);

    return success;
}

void ffInitOpenGLOptions(FFOpenGLOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "");

    options->library = FF_OPENGL_LIBRARY_AUTO;
}

void ffDestroyOpenGLOptions(FFOpenGLOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
}

FFModuleBaseInfo ffOpenGLModuleInfo = {
    .name = FF_OPENGL_MODULE_NAME,
    .description = "Print the highest OpenGL version supported by the GPU",
    .initOptions = (void*) ffInitOpenGLOptions,
    .destroyOptions = (void*) ffDestroyOpenGLOptions,
    .parseJsonObject = (void*) ffParseOpenGLJsonObject,
    .printModule = (void*) ffPrintOpenGL,
    .generateJsonResult = (void*) ffGenerateOpenGLJsonResult,
    .generateJsonConfig = (void*) ffGenerateOpenGLJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "OpenGL version", "version" },
        { "OpenGL renderer", "renderer" },
        { "OpenGL vendor", "vendor" },
        { "OpenGL shading language version", "slv" },
        { "OpenGL library used", "library" },
    }))
};