Skip to content

Commit 81c18e7

Browse files
committed
glfw: glfwTerminate() everywhere
whenever oshot was not exited normally, it would not call glfwTerminate() and thus create display problems on NVIDIA drivers PCs
1 parent 66493ac commit 81c18e7

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <thread>
1515
#include <utility>
1616

17-
#include "socket.hpp"
18-
1917
#ifndef _WIN32
2018
# include <netdb.h>
2119
# include <netinet/in.h>
@@ -31,6 +29,7 @@
3129
#include "oshot_png.hpp"
3230
#include "screen_capture.hpp"
3331
#include "screenshot_tool.hpp"
32+
#include "socket.hpp"
3433
#include "switch_fnv1a.hpp"
3534
#include "tray.hpp"
3635
#include "util.hpp"
@@ -215,6 +214,9 @@ static std::atomic<bool> quit{ false };
215214
static bool do_capture = false;
216215
struct GLFWwindow;
217216

217+
// Avoid dragging glfw headers
218+
void extern_glfw_terminate();
219+
218220
void exit_handler(int _)
219221
{
220222
quit.store(true);
@@ -223,6 +225,7 @@ void exit_handler(int _)
223225
if (g_sock > 0)
224226
shutdown(g_sock, SHUT_RDWR);
225227
#endif
228+
extern_glfw_terminate();
226229
trayMaker.Exit();
227230
}
228231

@@ -350,6 +353,17 @@ int main(int argc, char* argv[])
350353

351354
signal(SIGINT, exit_handler);
352355
signal(SIGTERM, exit_handler);
356+
signal(SIGABRT, exit_handler);
357+
358+
#ifndef _WIN32
359+
// SIGSEGV handler: restore display then re-raise so the OS
360+
// still generates a core dump.
361+
signal(SIGSEGV, [](int sig) {
362+
extern_glfw_terminate(); // restore display mode
363+
signal(sig, SIG_DFL); // reset to default
364+
raise(sig); // re-raise for core dump
365+
});
366+
#endif
353367

354368
const std::string& configDir = get_config_dir().string();
355369
const std::string& configFile = parse_config_path(argc, argv, configDir).string();

src/main_tool_metal.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
void glfw_error_callback(int error, const char* description);
2727
void glfw_drop_callback(GLFWwindow*, int count, const char** paths);
28+
void extern_glfw_terminate()
29+
{
30+
glfwTerminate();
31+
}
2832

2933
GLFWwindow* window = nullptr;
3034

src/main_tool_opengl3.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ GLFWwindow* window = nullptr;
2525
void glfw_error_callback(int i_error, const char* description);
2626
void glfw_drop_callback(GLFWwindow*, int count, const char** paths);
2727

28+
void extern_glfw_terminate()
29+
{
30+
glfwTerminate();
31+
}
32+
33+
// RAII guard: ensures glfwTerminate() runs even on crash/signal.
34+
// Without this, NVIDIA's driver is left in the implicit mode it switched
35+
// to when we created a full-resolution window, permanently showing 1024x768.
36+
struct GlfwGuard
37+
{
38+
~GlfwGuard() { glfwTerminate(); }
39+
} glfw_guard;
40+
2841
// Returns the GLFW monitor that currently contains the cursor.
2942
// Falls back to the primary monitor if the cursor position cannot be
3043
// determined (e.g. on a pure Wayland session without XWayland).

0 commit comments

Comments
 (0)