diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/glx.c | 39 | ||||
-rw-r--r-- | src/mgl.c | 48 | ||||
-rw-r--r-- | src/window.c | 13 |
3 files changed, 100 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; + } +} diff --git a/src/mgl.c b/src/mgl.c new file mode 100644 index 0000000..a7221d0 --- /dev/null +++ b/src/mgl.c @@ -0,0 +1,48 @@ +#include "../include/mgl/mgl.h" +#include <X11/Xlib.h> +#include <stdio.h> + +static mgl_context context; +static int init_count = 0; +static XErrorHandler prev_xerror = NULL; + +static int ignore_xerror(Display *display, XErrorEvent *ee) { + (void)display; + (void)ee; + return 0; +} + +int mgl_init(void) { + ++init_count; + if(init_count == 1) { + context.connection = XOpenDisplay(NULL); + if(!context.connection) { + fprintf(stderr, "XOpenDisplay(NULL) failed\n"); + mgl_deinit(); + return -1; + } + + prev_xerror = XSetErrorHandler(ignore_xerror); + if(mgl_glx_load(&context.glx) != 0) { + mgl_deinit(); + return -1; + } + } + return 0; +} + +void mgl_deinit(void) { + if(init_count == 1) { + mgl_glx_unload(&context.glx); + XSetErrorHandler(prev_xerror); + XCloseDisplay(context.connection); + context.connection = NULL; + } + + if(init_count > 0) + --init_count; +} + +mgl_context* mgl_get_context(void) { + return &context; +} diff --git a/src/window.c b/src/window.c new file mode 100644 index 0000000..39855da --- /dev/null +++ b/src/window.c @@ -0,0 +1,13 @@ +#include "../include/mgl/window.h" + +void mgl_window_init(mgl_window *self) { + +} + +void mgl_window_deinit(mgl_window *self) { + +} + +void mgl_window_show(mgl_window *self) { + +} |