From 5cfca3e55a49e8656c987c1e8b15da3c4380d6ff Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 26 Nov 2024 18:27:56 +0100 Subject: Properly close program when disconnected from the X server --- depends/mglpp | 2 +- src/Overlay.cpp | 24 ++++++++++++------------ src/main.cpp | 21 +++++---------------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/depends/mglpp b/depends/mglpp index 9800cff..7f0a03d 160000 --- a/depends/mglpp +++ b/depends/mglpp @@ -1 +1 @@ -Subproject commit 9800cff631f1a82ee87005aabdefb3f5da7fadb5 +Subproject commit 7f0a03d90e2d13632d22eb43447f553a695ddd9c diff --git a/src/Overlay.cpp b/src/Overlay.cpp index f19e460..25817f5 100644 --- a/src/Overlay.cpp +++ b/src/Overlay.cpp @@ -642,11 +642,11 @@ namespace gsr { make_window_sticky(display, window->get_system_handle()); hide_window_from_taskbar(display, window->get_system_handle()); - if(default_cursor) { - XFreeCursor(display, default_cursor); - default_cursor = 0; - } - default_cursor = XCreateFontCursor(display, XC_arrow); + // if(default_cursor) { + // XFreeCursor(display, default_cursor); + // default_cursor = 0; + // } + // default_cursor = XCreateFontCursor(display, XC_arrow); // TODO: Retry if these fail. // TODO: Hmm, these dont work in owlboy. Maybe owlboy uses xi2 and that breaks this (does it?). @@ -701,14 +701,14 @@ namespace gsr { page_stack.pop(); } - if(default_cursor) { - XFreeCursor(display, default_cursor); - default_cursor = 0; - } + // if(default_cursor) { + // XFreeCursor(display, default_cursor); + // default_cursor = 0; + // } - XUngrabKeyboard(display, CurrentTime); - XUngrabPointer(display, CurrentTime); - XFlush(display); + // XUngrabKeyboard(display, CurrentTime); + // XUngrabPointer(display, CurrentTime); + // XFlush(display); window_texture_deinit(&window_texture); window_texture_sprite.set_texture(nullptr); diff --git a/src/main.cpp b/src/main.cpp index 4a36fe7..a030dbb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -39,13 +38,6 @@ static void disable_prime_run() { unsetenv("__VK_LAYER_NV_optimus"); } -static bool is_socket_disconnected(int socket) { - char buf = '\0'; - errno = 0; - const ssize_t bytes_read = recv(socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT); - return bytes_read == 0 || (bytes_read == -1 && (errno == EBADF || errno == ENOTCONN)); -} - int main(void) { setlocale(LC_ALL, "C"); // Sigh... stupid C @@ -70,7 +62,7 @@ int main(void) { signal(SIGINT, sigint_handler); if(mgl_init() != 0) { - fprintf(stderr, "error: failed to initialize mgl. Either failed to connec to the X11 server or failed to setup opengl\n"); + fprintf(stderr, "error: failed to initialize mgl. Failed to either connect to the X11 server or setup opengl\n"); exit(1); } @@ -199,19 +191,16 @@ int main(void) { fprintf(stderr, "error: failed to register hotkey alt+f10 for saving replay because the hotkey is registered by another program\n"); mgl::Clock frame_delta_clock; - while(running) { - if(is_socket_disconnected(x11_socket)) { - fprintf(stderr, "info: the X11 server has shutdown\n"); - break; - } - + while(running && mgl_is_connected_to_display_server()) { const double frame_delta_seconds = frame_delta_clock.restart(); gsr::set_frame_delta_seconds(frame_delta_seconds); global_hotkeys.poll_events(); overlay->handle_events(); - if(!overlay->draw()) + if(!overlay->draw()) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); + mgl_ping_display_server(); + } } fprintf(stderr, "info: shutting down!\n"); -- cgit v1.2.3