diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-10 10:59:43 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-10 10:59:43 +0200 |
commit | e57daa001cc74682cdb905d8e0c6c8c3a2c29372 (patch) | |
tree | c057e00453a3d46df494d75882285ef35a47825a /src/mgl.c |
Initial commit, skeleton
Diffstat (limited to 'src/mgl.c')
-rw-r--r-- | src/mgl.c | 48 |
1 files changed, 48 insertions, 0 deletions
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; +} |