aboutsummaryrefslogtreecommitdiff
path: root/src/glx.c
blob: 4a5af9dc18d98714a09f1903363ab52319f20f18 (plain)
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
#include "../include/mgl/glx.h"
#include <dlfcn.h>
#include <stdio.h>
#include <GL/glx.h>

static void* dlsym_print_fail(void *handle, const char *name) {
    dlerror();
    void *sym = dlsym(handle, name);
    char *err_str = dlerror();

    if(!sym)
        fprintf(stderr, "dlsym(handle, \"%s\") failed, error: %s\n", name, err_str ? err_str : "(null)");

    return sym;
}

int mgl_glx_load(mgl_glx *self) {
    const char *glx_path = "/usr/lib/libGLX.so.0";
    self->handle = dlopen(glx_path, RTLD_LAZY);
    if(!self->handle) {
        fprintf(stderr, "dlopen(\"%s\", RTLD_LAZY) failed\n", glx_path);
        return -1;
    }

    self->glXGetFBConfigs = (GLXFBConfig* (*)(Display*, int, int*))dlsym_print_fail(self->handle, "glXGetFBConfigs");
    if(!self->glXGetFBConfigs) {
        mgl_glx_unload(self);
        return -1;
    }

    return 0;
}

void mgl_glx_unload(mgl_glx *self) {
    if(self->handle) {
        dlclose(self->handle);
        self->handle = NULL;
    }
}