From ffbc4a13d4c1756c371cb3ce518fb34a7ec16b36 Mon Sep 17 00:00:00 2001 From: tTh Date: Thu, 22 Aug 2024 00:49:48 +0200 Subject: [PATCH] you can now stop the wav player with the X key --- Ecoute/src/ecran.c | 41 +++++++++++++++++++++++++++++++++++++++++ Ecoute/src/playwav.c | 10 ++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Ecoute/src/ecran.c b/Ecoute/src/ecran.c index 8e4b705..a619e5b 100644 --- a/Ecoute/src/ecran.c +++ b/Ecoute/src/ecran.c @@ -6,9 +6,14 @@ * */ +#define _POSIX_C_SOURCE 600L /* for fileno(3) */ + +#include #include #include +#include + #include "ecoute.h" /*==------------------------------------------------------------------==*/ @@ -79,6 +84,42 @@ foo = 42; return foo; } /*==------------------------------------------------------------------==*/ +/* + * detection de la frappe d'une touche clavier -- remember kbhit() ? + */ +int is_a_key_hitted(void) +{ +int fd,retv; +fd_set in_fds; +struct timeval tv; + +fd = fileno(stdin); +#if DEBUG_LEVEL +fprintf(stderr, "%s: fileno -> %d\n", __func__, fd); +#endif + +FD_ZERO(&in_fds); FD_SET(fd, &in_fds); +tv.tv_sec = tv.tv_usec = 0; + +retv = select(1, &in_fds, NULL, NULL, &tv); +#if DEBUG_LEVEL +fprintf(stderr, "%s: select -> %d\n", __func__, retv); +#endif + +if (-1 == retv) { + perror("select()"); + } +else if (0 == retv) { + /* no key hit */ + } +else { + /* got a keypress */ + return 1; + } + +return 0; +} +/*==------------------------------------------------------------------==*/ void termine_ecran(void) { endwin(); exit(0); diff --git a/Ecoute/src/playwav.c b/Ecoute/src/playwav.c index 66dec77..add658f 100644 --- a/Ecoute/src/playwav.c +++ b/Ecoute/src/playwav.c @@ -20,7 +20,7 @@ ao_sample_format format; ao_device * device; char chaine[120]; short * samples; -int lu; +int lu, key; long total_lu; #if DEBUG_LEVEL @@ -103,7 +103,13 @@ while ( (lu=sf_read_short(sndf, samples, T_BUFFER)) > 0 ) wrefresh(popup); /* HERE WE HAVE TO DETECT A 'STOP LISTEN' FUNCTION. */ - + if (is_a_key_hitted()) { + key = getch(); + fprintf(stderr, "%s key pressed 0x%X\n", __func__, key); + if ('X' == key) { + break; + } + } } /*