[alsa-devel] [PATCH][RFC][alsa-utils 2/9] alsactl: split event loop code to a function

Takashi Sakamoto o-takashi at sakamocchi.jp
Fri Oct 5 16:47:22 CEST 2018


In a mode of 'monitor', an event loop runs.

This commit applies a small refactoring to splits the loop into a
function for readability.

Signed-off-by: Takashi Sakamoto <o-takashi at sakamocchi.jp>
---
 alsactl/monitor.c | 78 ++++++++++++++++++++++++++---------------------
 1 file changed, 44 insertions(+), 34 deletions(-)

diff --git a/alsactl/monitor.c b/alsactl/monitor.c
index a2abacb..ae1653c 100644
--- a/alsactl/monitor.c
+++ b/alsactl/monitor.c
@@ -90,6 +90,47 @@ static int print_event(int card, snd_ctl_t *ctl)
 	return 0;
 }
 
+static int run_dispatcher(snd_ctl_t **ctls, int ncards, int show_cards)
+{
+	interrupted = false;
+	int err = 0;
+
+	for (;ncards > 0;) {
+		struct pollfd fds[ncards];
+		int i;
+
+		if (interrupted) {
+			printf("interrupted: %s\n", strsignal(signal_type));
+			break;
+		}
+
+		for (i = 0; i < ncards; i++)
+			snd_ctl_poll_descriptors(ctls[i], &fds[i], 1);
+
+		err = poll(fds, ncards, -1);
+		if (err <= 0) {
+			// Catch this case by an above condition statement to
+			// check value set by signal handler. I note that
+			// poll(2) returns EINTR even if configured with
+			// SA_RESTART.
+			if (errno == EINTR)
+				continue;
+			err = 0;
+			break;
+		}
+
+		for (i = 0; i < ncards; i++) {
+			unsigned short revents;
+			snd_ctl_poll_descriptors_revents(ctls[i], &fds[i], 1,
+							 &revents);
+			if (revents & POLLIN)
+				print_event(show_cards ? i : -1, ctls[i]);
+		}
+	}
+
+	return err;
+}
+
 static void handle_unix_signal_for_finish(int sig)
 {
 	signal_type = sig;
@@ -121,7 +162,8 @@ int monitor(const char *name)
 	snd_ctl_t *ctls[MAX_CARDS];
 	int ncards = 0;
 	int show_cards;
-	int i, err = 0;
+	int i;
+	int err = 0;
 
 	err = prepare_signal_handler();
 	if (err < 0)
@@ -151,39 +193,7 @@ int monitor(const char *name)
 		show_cards = 0;
 	}
 
-	interrupted = false;
-	for (;ncards > 0;) {
-		struct pollfd fds[ncards];
-
-		if (interrupted) {
-			printf("interrupted: %s\n", strsignal(signal_type));
-			break;
-		}
-
-		for (i = 0; i < ncards; i++)
-			snd_ctl_poll_descriptors(ctls[i], &fds[i], 1);
-
-		err = poll(fds, ncards, -1);
-		if (err <= 0) {
-			// Catch this case by an above condition statement to
-			// check value set by signal handler. I note that
-			// poll(2) returns EINTR even if configured with
-			// SA_RESTART.
-			if (errno == EINTR)
-				continue;
-			err = 0;
-			break;
-		}
-
-		for (i = 0; i < ncards; i++) {
-			unsigned short revents;
-			snd_ctl_poll_descriptors_revents(ctls[i], &fds[i], 1,
-							 &revents);
-			if (revents & POLLIN)
-				print_event(show_cards ? i : -1, ctls[i]);
-		}
-	}
-
+	err = run_dispatcher(ctls, ncards, show_cards);
  error:
 	for (i = 0; i < ncards; i++)
 		snd_ctl_close(ctls[i]);
-- 
2.19.0



More information about the Alsa-devel mailing list