From 25a962a8bf74b5a2484531e2095a219ab8ae7fe7 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Tue, 26 Nov 2024 18:21:33 +0100
Subject: Add mgl_is_connected_to_display_server and mgl_ping_display_server to
 keep status of connection

---
 src/mgl.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/mgl.c b/src/mgl.c
index 5a43427..63512ef 100644
--- a/src/mgl.c
+++ b/src/mgl.c
@@ -12,15 +12,17 @@ static mgl_context context;
 static int init_count = 0;
 static XErrorHandler prev_xerror = NULL;
 static XIOErrorHandler prev_xioerror = NULL;
+static bool connected_to_x_server = false;
 
-static int ignore_xerror(Display *display, XErrorEvent *ee) {
+static int mgl_x_error_handler(Display *display, XErrorEvent *ee) {
     (void)display;
     (void)ee;
     return 0;
 }
 
-static int ignore_xioerror(Display *display) {
+static int mgl_x_io_error_handler(Display *display) {
     (void)display;
+    connected_to_x_server = false;
     return 0;
 }
 
@@ -64,9 +66,10 @@ int mgl_init(void) {
             mgl_deinit();
             return -1;
         }
+        connected_to_x_server = true;
 
-        prev_xerror = XSetErrorHandler(ignore_xerror);
-        prev_xioerror = XSetIOErrorHandler(ignore_xioerror);
+        prev_xerror = XSetErrorHandler(mgl_x_error_handler);
+        prev_xioerror = XSetIOErrorHandler(mgl_x_io_error_handler);
 
         if(!xrender_is_supported(context.connection, &context.render_event_base, &context.render_error_base)) {
             fprintf(stderr, "mgl error: x11 render extension is not supported by your X server\n");
@@ -102,6 +105,7 @@ void mgl_deinit(void) {
         if(context.connection) {
             XCloseDisplay(context.connection);
             context.connection = NULL;
+            connected_to_x_server = false;
 
             /*
                 GLX needs to be unloaded after closing the display on nvidia because
@@ -136,3 +140,14 @@ mgl_context* mgl_get_context(void) {
 #endif
     return &context;
 }
+
+bool mgl_is_connected_to_display_server(void) {
+    return connected_to_x_server;
+}
+
+void mgl_ping_display_server(void) {
+    if(context.connection) {
+        XNoOp(context.connection);
+        XFlush(context.connection);
+    }
+}
-- 
cgit v1.2.3-70-g09d2