]> git.alsa-project.org Git - alsa-utils.git/commitdiff
alsactl: add an iterator of registered instances of sound card
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Sun, 14 Oct 2018 14:36:27 +0000 (23:36 +0900)
committerJaroslav Kysela <perex@perex.cz>
Sun, 14 Oct 2018 14:57:15 +0000 (16:57 +0200)
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>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
alsactl/monitor.c

index fb3fc18ad7163bbfd911a4287f37e17b657496d9..008ceb303b24ca9ac14dbe0d8ce842e27a4b9b4d 100644 (file)
 #include <stdio.h>
 #include <alsa/asoundlib.h>
 
+#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;
@@ -113,8 +143,6 @@ static int run_dispatcher(snd_ctl_t **ctls, int ncards, int show_cards)
        return err;
 }
 
-#define MAX_CARDS      256
-
 int monitor(const char *name)
 {
        snd_ctl_t *ctls[MAX_CARDS];
@@ -123,15 +151,11 @@ int monitor(const char *name)
        int i, err = 0;
 
        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;