Skip to content

Commit

Permalink
main: properly handle SINGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei8l committed Aug 27, 2024
1 parent 83b5393 commit d31f06e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,9 @@ bool game::try_get_right_click_action( action_id &act, const tripoint_bub_ms &mo

bool game::is_game_over()
{
if( uquit == QUIT_EXIT ) {
return true;
}
if( uquit == QUIT_DIED || uquit == QUIT_WATCH ) {
events().send<event_type::avatar_dies>();
}
Expand Down
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum quit_status {
QUIT_NOSAVED, // Quit without saving
QUIT_DIED, // Actual death
QUIT_WATCH, // Died, and watching aftermath
QUIT_EXIT, // Skip main menu and quit directly to OS
};

enum safe_mode_type {
Expand Down
66 changes: 36 additions & 30 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,42 @@ namespace
// Used only if AttachConsole() works
FILE *CONOUT;
#endif
void exit_handler( int s )

#if !defined(_WIN32)
void sigint_handler( int /* s */ )
{
const int old_timeout = inp_mngr.get_timeout();
inp_mngr.reset_timeout();
if( s != 2 || query_yn( _( "Really Quit? All unsaved changes will be lost." ) ) ) {
deinitDebug();
if( query_yn( _( "Really Quit? All unsaved changes will be lost." ) ) ) {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = SIG_DFL;
sigemptyset( &sigIntHandler.sa_mask );
sigIntHandler.sa_flags = 0;
sigaction( SIGINT, &sigIntHandler, nullptr );
g->uquit = QUIT_EXIT;
} else {
inp_mngr.set_timeout( old_timeout );
ui_manager::redraw_invalidated();
catacurses::doupdate();
}
}
#endif

void exit_handler( int /* s */ )
{
deinitDebug();

int exit_status = 0;
g.reset();
g.reset();

catacurses::endwin();
catacurses::endwin();
imclient.reset();

#if defined(__ANDROID__)
// Avoid capturing SIGABRT on exit on Android in crash report
// Can be removed once the SIGABRT on exit problem is fixed
signal( SIGABRT, SIG_DFL );
// Avoid capturing SIGABRT on exit on Android in crash report
// Can be removed once the SIGABRT on exit problem is fixed
signal( SIGABRT, SIG_DFL );
#endif

#if !defined(_WIN32)
if( s == 2 ) {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = SIG_DFL;
sigemptyset( &sigIntHandler.sa_mask );
sigIntHandler.sa_flags = 0;
sigaction( SIGINT, &sigIntHandler, nullptr );
kill( getpid(), s );
} else
#endif
{
imclient.reset();
exit( exit_status );
}
}
inp_mngr.set_timeout( old_timeout );
ui_manager::redraw_invalidated();
catacurses::doupdate();
}

struct arg_handler {
Expand Down Expand Up @@ -838,7 +838,7 @@ int main( int argc, const char *argv[] )

#if !defined(_WIN32)
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = exit_handler;
sigIntHandler.sa_handler = sigint_handler;
sigemptyset( &sigIntHandler.sa_mask );
sigIntHandler.sa_flags = 0;
sigaction( SIGINT, &sigIntHandler, nullptr );
Expand All @@ -862,15 +862,21 @@ int main( int argc, const char *argv[] )

get_help().load();

while( true ) {
bool running = true;
while( running ) {
main_menu menu;
if( !menu.opening_screen() ) {
break;
}

shared_ptr_fast<ui_adaptor> ui = g->create_or_get_main_ui_adaptor();
get_event_bus().send<event_type::game_begin>( getVersionString() );
while( !do_turn() ) {}
while( !do_turn() ) {
if( g->uquit == QUIT_EXIT ) {
running = false;
break;
}
}
}

exit_handler( -999 );
Expand Down

0 comments on commit d31f06e

Please sign in to comment.