aboutsummaryrefslogtreecommitdiff
path: root/src/mgl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mgl.c')
-rw-r--r--src/mgl.c48
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;
+}