summaryrefslogtreecommitdiffstats
path: root/src/detection/opengl/opengl_apple.c
blob: 91285938d110033d3bd5708bfe45974e956110e6 (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

#include "fastfetch.h"
#include "opengl.h"

#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <OpenGL/OpenGL.h> // This brings in CGL, not GL

void ffOpenGLHandleResult(FFOpenGLResult* result, __typeof__(&glGetString) ffglGetString);

static const char* cglHandleContext(FFOpenGLResult* result, CGLContextObj context) {
    if (CGLSetCurrentContext(context) != kCGLNoError) {
        return "CGLSetCurrentContext() failed";
    }

    ffOpenGLHandleResult(result, &glGetString);

    GLint major, minor;
    CGLGetVersion(&major, &minor);
    ffStrbufSetF(&result->library, "CGL %d.%d", major, minor);

    return NULL;
}

static const char* cglHandlePixelFormat(FFOpenGLResult* result, CGLPixelFormatObj pixelFormat) {
    CGLContextObj context;

    if (CGLCreateContext(pixelFormat, NULL, &context) != kCGLNoError) {
        return "CGLCreateContext() failed";
    }

    const char* error = cglHandleContext(result, context);
    CGLDestroyContext(context);
    return error;
}

const char* cglDetectOpenGL(FFOpenGLResult* result) {
    CGLPixelFormatObj pixelFormat;
    CGLPixelFormatAttribute attrs[] = {
        kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, kCGLPFAAccelerated, 0
    };

    GLint num;
    if (CGLChoosePixelFormat(attrs, &pixelFormat, &num) != kCGLNoError) {
        return "CGLChoosePixelFormat() failed";
    }

    const char* error = cglHandlePixelFormat(result, pixelFormat);
    CGLDestroyPixelFormat(pixelFormat);
    return error;
}

const char* ffDetectOpenGL(FFOpenGLOptions* options, FFOpenGLResult* result) {
    if (options->library == FF_OPENGL_LIBRARY_AUTO) {
        return cglDetectOpenGL(result);
    } else if (options->library == FF_OPENGL_LIBRARY_EGL) {
#if __has_include(<EGL/egl.h>)
        const char* ffOpenGLDetectByEGL(FFOpenGLResult * result);
        return ffOpenGLDetectByEGL(result);
#else
        return "fastfetch was compiled without egl support";
#endif
    } else {
        return "Unsupported OpenGL library";
    }
}