SDL/C++ OpenGL Program, how do I stop SDL from catching SIGINT

柔情痞子 提交于 2019-12-06 21:30:08

问题


I am using SDL for an OpenGL application, running on Linux. My problem is that SDL is catching SIGINT and ignoring it. This is a pain because I am developing through a screen session, and I can't kill the running program with CTRL-C (the program the computer is running on is connected to a projector and has no input devices).

Is there a flag or something I can pass to SDL so that it does not capture SIGINT? I really just want the program to stop when it receives the signal (ie when I press ctrl-c).


回答1:


Ctrl-C at the console generates an SDL_QUIT event. You can watch for this event using SDL_PollEvent or SDL_WaitEvent, and exit (cleanly) when it is detected.

Note that other actions can generate an SDL_QUIT event (e.g. attempting to close your main window via the window manager).




回答2:


I have found an answer:

The SDL_INIT_NOPARACHUTE flag will capture fatal signals so that SDL can clean up after itself. It works for things like SIGSEGV, but apparently SIGINT is not fatal enough.

My solution is to reset the signal handler to SIGINT after SDL has been initialised:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
signal(SIGINT, SIG_DFL); 

Thanks Cache for you input, it put me on the right track.

Michael




回答3:


Passing the SDL_INIT_NOPARACHUTE initialisation flag to SDL_Init "Prevents SDL from catching fatal signals".


See: http://www.libsdl.org/cgi/docwiki.cgi/SDL_Init




回答4:


If you're not actually using an event loop for some reason, you can use SDL_QuitRequested in your "poll stuff" loop.




回答5:


In SDL_quit.c, there's a check for hints to determine whether the signal handlers should not be used in SDL_QuitInit(). Not sure if this existed in older versions when the original question was asked, but may be handy for those coming here fresh.

Just tested on my Windows application, I can now receive all signals properly again, using:

SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
SDL_Init(...);


来源:https://stackoverflow.com/questions/675980/sdl-c-opengl-program-how-do-i-stop-sdl-from-catching-sigint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!