summaryrefslogtreecommitdiffstats
path: root/src/detection/opengl/opengl_windows.c
blob: 01bd9869038faf3e52fe10d55f17ac5deb2ef9cf (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
#include "opengl.h"
#include "common/library.h"
#include "common/printing.h"
#include "common/windows/nt.h"

#include <windows.h>
#include <GL/gl.h>

typedef struct WGLData {
    FF_LIBRARY_SYMBOL(glGetString)
    FF_LIBRARY_SYMBOL(wglMakeCurrent)
    FF_LIBRARY_SYMBOL(wglCreateContext)
    FF_LIBRARY_SYMBOL(wglDeleteContext)
} WGLData;

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

static const char* wglHandleContext(WGLData* wglData, FFOpenGLResult* result, HDC hdc, HGLRC context) {
    if (wglData->ffwglMakeCurrent(hdc, context) == FALSE) {
        return "wglMakeCurrent() failed";
    }
    ffOpenGLHandleResult(result, wglData->ffglGetString);
    ffStrbufSetStatic(&result->library, "WGL 1.0");
    if (wglData->ffwglMakeCurrent(NULL, NULL) == FALSE) {
        return "wglMakeCurrent(NULL, NULL) failed";
    }
    return NULL;
}

static const char* wglHandlePixelFormat(WGLData* wglData, FFOpenGLResult* result, HWND hWnd) {
    HDC hdc = GetDC(hWnd);

    if (hdc == NULL) {
        return "GetDC() failed";
    }

    PIXELFORMATDESCRIPTOR pfd = {
        .nSize = sizeof(PIXELFORMATDESCRIPTOR),
        .nVersion = 1,
        .dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        .iPixelType = PFD_TYPE_RGBA,
        .cColorBits = 32,
        .cDepthBits = 24,
        .iLayerType = PFD_MAIN_PLANE
    };
    int pixelFormat = ChoosePixelFormat(hdc, &pfd);
    if (pixelFormat == 0) {
        ReleaseDC(hWnd, hdc);
        return "ChoosePixelFormat() failed";
    }

    if (SetPixelFormat(hdc, pixelFormat, &pfd) == FALSE) {
        ReleaseDC(hWnd, hdc);
        return "SetPixelFormat() failed";
    }

    HGLRC context = wglData->ffwglCreateContext(hdc);
    if (context == NULL) {
        ReleaseDC(hWnd, hdc);
        return "wglCreateContext() failed";
    }

    const char* error = wglHandleContext(wglData, result, hdc, context);
    wglData->ffwglDeleteContext(context);

    ReleaseDC(hWnd, hdc);

    return error;
}

static const char* wglDetectOpenGL(FFOpenGLResult* result) {
    FF_LIBRARY_LOAD_MESSAGE(opengl32, "opengl32" FF_LIBRARY_EXTENSION, 1);

    WGLData data = {};

    FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglMakeCurrent);
    FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglCreateContext);
    FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, wglDeleteContext);
    FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opengl32, data, glGetString);

    HINSTANCE hInstance = ffGetPeb()->ImageBaseAddress;

    WNDCLASSW wc = {
        .lpfnWndProc = DefWindowProcW,
        .hInstance = hInstance,
        .hbrBackground = (HBRUSH) COLOR_BACKGROUND,
        .lpszClassName = L"ogl_version_check",
        .style = CS_OWNDC,
    };
    if (!RegisterClassW(&wc)) {
        return "RegisterClassW() failed";
    }

    HWND hWnd = CreateWindowW(wc.lpszClassName, L"ogl_version_check", 0, 0, 0, FF_OPENGL_BUFFER_WIDTH, FF_OPENGL_BUFFER_HEIGHT, NULL, NULL, hInstance, NULL);
    if (!hWnd) {
        return "CreateWindowW() failed";
    }

    const char* error = wglHandlePixelFormat(&data, result, hWnd);

    DestroyWindow(hWnd);
    UnregisterClassW(wc.lpszClassName, hInstance);

    return error;
}

const char* ffDetectOpenGL(FFOpenGLOptions* options, FFOpenGLResult* result) {
    if (options->library == FF_OPENGL_LIBRARY_AUTO) {
        return wglDetectOpenGL(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";
    }
}