aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-11-26 18:21:33 +0100
committerdec05eba <dec05eba@protonmail.com>2024-11-26 18:21:33 +0100
commit25a962a8bf74b5a2484531e2095a219ab8ae7fe7 (patch)
tree9255f25d0c1356fa278dd5084dc4e6a268eec9fd
parent0fc77a5d2208b4649c8a274c34b79a6ce74580e4 (diff)
Add mgl_is_connected_to_display_server and mgl_ping_display_server to keep status of connection
-rw-r--r--include/mgl/mgl.h9
-rw-r--r--src/mgl.c23
2 files changed, 28 insertions, 4 deletions
diff --git a/include/mgl/mgl.h b/include/mgl/mgl.h
index e314e03..c5d1f2d 100644
--- a/include/mgl/mgl.h
+++ b/include/mgl/mgl.h
@@ -2,6 +2,7 @@
#define MGL_MGL_H
#include "gl.h"
+#include <stdbool.h>
/* Display* on x11 */
typedef void* mgl_connection;
@@ -38,5 +39,13 @@ int mgl_init(void);
void mgl_deinit(void);
mgl_context* mgl_get_context(void);
+/* Returns true is mgl_init has been setup successfully and the connection to the display server hasn't been severed */
+bool mgl_is_connected_to_display_server(void);
+/*
+ This can be used when no window has been created to update the connection status to the display server.
+ If the connection to the display server has been severed and mgl_ping_display_server has been called then
+ |mgl_is_connected_to_display_server| will return false.
+*/
+void mgl_ping_display_server(void);
#endif /* MGL_MGL_H */
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);
+ }
+}