diff options
Diffstat (limited to 'src/glx.c')
-rw-r--r-- | src/glx.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/glx.c b/src/glx.c new file mode 100644 index 0000000..4a5af9d --- /dev/null +++ b/src/glx.c @@ -0,0 +1,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; + } +} |