All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, thomas@coldfix.de
Cc: alsa-devel@alsa-project.org
Subject: [PATCH][RFC][alsa-utils 3/9] alsactl: add an iterator of registered instances of sound card
Date: Fri,  5 Oct 2018 23:47:23 +0900	[thread overview]
Message-ID: <20181005144729.21388-4-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181005144729.21388-1-o-takashi@sakamocchi.jp>

In a mode of 'monitor', when given no argument, all of available control
node is observed for their events. At present, discovering the nodes is
done according to sound card number, instead of listing nodes in
configuration space of alsa-lib.

This commit adds a structure to discover sound cards with a simple
interface.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 alsactl/monitor.c | 48 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/alsactl/monitor.c b/alsactl/monitor.c
index ae1653c..79e8fd9 100644
--- a/alsactl/monitor.c
+++ b/alsactl/monitor.c
@@ -28,6 +28,36 @@
 static int signal_type;
 static bool interrupted;
 
+#define MAX_CARDS	256
+
+struct snd_card_iterator {
+        int card;
+        char name[16];
+};
+
+void snd_card_iterator_init(struct snd_card_iterator *iter)
+{
+        iter->card = -1;
+        memset(iter->name, 0, sizeof(iter->name));
+}
+
+static const char *snd_card_iterator_next(struct snd_card_iterator *iter)
+{
+        if (snd_card_next(&iter->card) < 0)
+                return NULL;
+        if (iter->card < 0)
+                return NULL;
+	if (iter->card >= MAX_CARDS) {
+		fprintf(stderr, "alsactl: too many cards\n");
+		return NULL;
+	}
+
+
+        snprintf(iter->name, sizeof(iter->name), "hw:%d", iter->card);
+
+        return (const char *)iter->name;
+}
+
 static int open_ctl(const char *name, snd_ctl_t **ctlp)
 {
 	snd_ctl_t *ctl;
@@ -155,8 +185,6 @@ static int prepare_signal_handler(void)
 	return 0;
 }
 
-#define MAX_CARDS	256
-
 int monitor(const char *name)
 {
 	snd_ctl_t *ctls[MAX_CARDS];
@@ -170,19 +198,15 @@ int monitor(const char *name)
 		return err;
 
 	if (!name) {
-		int card = -1;
-		while (snd_card_next(&card) >= 0 && card >= 0) {
-			char cardname[16];
-			if (ncards >= MAX_CARDS) {
-				fprintf(stderr, "alsactl: too many cards\n");
-				err = -E2BIG;
-				goto error;
-			}
-			sprintf(cardname, "hw:%d", card);
+		struct snd_card_iterator iter;
+		const char *cardname;
+
+		snd_card_iterator_init(&iter);
+		while ((cardname = snd_card_iterator_next(&iter))) {
 			err = open_ctl(cardname, &ctls[ncards]);
 			if (err < 0)
 				goto error;
-			ncards++;
+			++ncards;
 		}
 		show_cards = 1;
 	} else {
-- 
2.19.0

  parent reply	other threads:[~2018-10-05 14:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-05 14:47 [PATCH][RFC][alsa-utils 0/9] alsactl: monitor mode friendly for Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 1/9] alsactl: install signal handler Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 2/9] alsactl: split event loop code to a function Takashi Sakamoto
2018-10-05 14:47 ` Takashi Sakamoto [this message]
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 4/9] alsactl: use epoll(7) instead of poll(2) Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 5/9] alsactl: use link list to maintain source of events Takashi Sakamoto
2018-10-05 17:14   ` Jaroslav Kysela
2018-10-07  3:04     ` Takashi Sakamoto
2018-10-08 15:06       ` Takashi Iwai
2018-10-08 15:14       ` Jaroslav Kysela
2018-10-09  3:15         ` Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 6/9] alsactl: use a list of source for event dispatcher instead of an array of source Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 7/9] alsactl: obsolete array for maintenance of handlers Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 8/9] alsactl: handle disconnection of sound card Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 9/9] alsactl: handle detection of new " Takashi Sakamoto
2018-10-05 14:51 ` [PATCH][RFC][alsa-utils 0/9] alsactl: monitor mode friendly for Takashi Sakamoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181005144729.21388-4-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=thomas@coldfix.de \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.