master ohmu/fastfetch-openbsd / src / detection / opengl / opengl_apple.c
ohmu  ·  2026-10-15
 1
 2#include "fastfetch.h"
 3#include "opengl.h"
 4#include "common/debug.h"
 5
 6#define GL_SILENCE_DEPRECATION
 7#include <OpenGL/gl.h>
 8#include <OpenGL/OpenGL.h> // This brings in CGL, not GL
 9
10void ffOpenGLHandleResult(FFOpenGLResult* result, typeof(&glGetString) ffglGetString);
11
12static const char* cglHandleContext(FFOpenGLResult* result, CGLContextObj context) {
13    CGLError setContextError = CGLSetCurrentContext(context);
14    if (setContextError != kCGLNoError) {
15        FF_DEBUG("CGLSetCurrentContext() failed: CGLError %d", (int) setContextError);
16        return "CGLSetCurrentContext() failed";
17    }
18
19    ffOpenGLHandleResult(result, &glGetString);
20
21    GLint major, minor;
22    CGLGetVersion(&major, &minor);
23    ffStrbufSetF(&result->library, "CGL %d.%d", major, minor);
24
25    return nullptr;
26}
27
28static const char* cglHandlePixelFormat(FFOpenGLResult* result, CGLPixelFormatObj pixelFormat) {
29    CGLContextObj context;
30
31    CGLError createContextError = CGLCreateContext(pixelFormat, nullptr, &context);
32    if (createContextError != kCGLNoError) {
33        FF_DEBUG("CGLCreateContext() failed: CGLError %d", (int) createContextError);
34        return "CGLCreateContext() failed";
35    }
36
37    const char* error = cglHandleContext(result, context);
38    CGLDestroyContext(context);
39    return error;
40}
41
42const char* cglDetectOpenGL(FFOpenGLResult* result) {
43    CGLPixelFormatObj pixelFormat;
44    CGLPixelFormatAttribute attrs[] = {
45        kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, kCGLPFAAccelerated, 0
46    };
47
48    GLint num;
49    CGLError choosePixelFormatError = CGLChoosePixelFormat(attrs, &pixelFormat, &num);
50    if (choosePixelFormatError != kCGLNoError) {
51        FF_DEBUG("CGLChoosePixelFormat() failed: CGLError %d", (int) choosePixelFormatError);
52        return "CGLChoosePixelFormat() failed";
53    }
54
55    const char* error = cglHandlePixelFormat(result, pixelFormat);
56    CGLDestroyPixelFormat(pixelFormat);
57    return error;
58}
59
60const char* ffDetectOpenGL(FFOpenGLOptions* options, FFOpenGLResult* result) {
61    if (options->library == FF_OPENGL_LIBRARY_AUTO) {
62        return cglDetectOpenGL(result);
63    } else if (options->library == FF_OPENGL_LIBRARY_EGL) {
64#if __has_include(<EGL/egl.h>)
65        const char* ffOpenGLDetectByEGL(FFOpenGLResult * result);
66        return ffOpenGLDetectByEGL(result);
67#else
68        return "fastfetch was compiled without egl support";
69#endif
70    } else {
71        FF_DEBUG("Unsupported OpenGL library requested: %d", (int) options->library);
72        return "Unsupported OpenGL library";
73    }
74}