aboutsummaryrefslogtreecommitdiff
path: root/src/glx.c
blob: 6712947edecd6ccd417e1f9e76a52ffba03d0f87 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "../include/mgl/glx.h"
#include <dlfcn.h>
/*#include <GL/glx.h>*/
#include <stdio.h>

typedef struct {
    void **func;
    const char *name;
} dlsym_assign;

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;
    }

    const dlsym_assign assign[] = {
        { &self->glXChooseVisual, "glXChooseVisual" },
        { &self->glXCreateContext, "glXCreateContext" },
        { &self->glXDestroyContext, "glXDestroyContext" },
        { &self->glXMakeCurrent, "glXMakeCurrent" },
        { &self->glXSwapBuffers, "glXSwapBuffers" },
        { NULL, NULL }
    };

    for(int i = 0; assign[i].func; ++i) {
        *assign[i].func = dlsym_print_fail(self->handle, assign[i].name);
        if(!assign[i].func) {
            mgl_glx_unload(self);
            return -1;
        }
    }

    return 0;
}

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