From 436a1d4012553a2f33d0e3a5180b3b5ae0378bdd Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 23 Oct 2015 10:27:31 +0200 Subject: Interruptible requests ^C now works as expected: It interrupts API requests, input prompts and audio streaming. Timeouts have been removed. --- src/main.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index b113f4e..5d0e91b 100644 --- a/src/main.c +++ b/src/main.c @@ -199,7 +199,7 @@ static void BarMainGetInitialStation (BarApp_t *app) { static void BarMainHandleUserInput (BarApp_t *app) { char buf[2]; if (BarReadline (buf, sizeof (buf), NULL, &app->input, - BAR_RL_FULLRETURN | BAR_RL_NOECHO, 1) > 0) { + BAR_RL_FULLRETURN | BAR_RL_NOECHO | BAR_RL_NOINT, 1) > 0) { BarUiDispatch (app, buf[0], app->curStation, app->playlist, true, BAR_DC_GLOBAL); } @@ -259,6 +259,9 @@ static void BarMainStartPlayback (BarApp_t *app, pthread_t *playerThread) { pthread_mutex_init (&app->player.pauseMutex, NULL); pthread_cond_init (&app->player.pauseCond, NULL); + assert (interrupted == NULL); + interrupted = &app->player.interrupted; + /* throw event */ BarUiStartEventCmd (&app->settings, "songstart", app->curStation, curSong, &app->player, app->ph.stations, @@ -301,6 +304,9 @@ static void BarMainPlayerCleanup (BarApp_t *app, pthread_t *playerThread) { } memset (&app->player, 0, sizeof (app->player)); + + assert (interrupted == &app->player.interrupted); + interrupted = NULL; } /* print song duration @@ -384,6 +390,23 @@ static void BarMainLoop (BarApp_t *app) { } } +sig_atomic_t *interrupted = NULL; + +static void intHandler (int signal) { + if (interrupted != NULL) { + *interrupted = 1; + } +} + +static void BarMainSetupSigaction () { + struct sigaction act = { + .sa_handler = intHandler, + .sa_flags = 0, + }; + sigemptyset (&act.sa_mask); + sigaction (SIGINT, &act, NULL); +} + int main (int argc, char **argv) { static BarApp_t app; @@ -391,6 +414,7 @@ int main (int argc, char **argv) { /* save terminal attributes, before disabling echoing */ BarTermInit (); + BarMainSetupSigaction (); /* signals */ signal (SIGPIPE, SIG_IGN); -- cgit v1.2.3