All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, perex@perex.cz
Cc: alsa-devel@alsa-project.org
Subject: [PATCH 02/35] axfer: add a sub-command to print list of PCMs/devices
Date: Tue, 13 Nov 2018 15:41:14 +0900	[thread overview]
Message-ID: <20181113064147.13577-2-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181113064147.13577-1-o-takashi@sakamocchi.jp>

Original aplay implementation has a feature to output two types of list;
devices and PCMs. The list of devices is a result to query sound card and
pcm component structured maintained in kernel land. The list of PCMs is a
result to parse runtime configuration files in alsa-lib. Entries in the
former list is corresponding to ALSA PCM character device
('/dev/snd/pcm%uC%uD[p|c]'), while entries in the latter list includes
some 'virtual' instances in application runtime.

This commit adds an implementation for the above functionality. This is
executed by taking 'list' sub-command. A 'device' option has the same
effect as '--list-devices' and '-L' of aplay. A 'pcm' option has the same
effect as '--list-pcms' and '-l' of aplay. In both cases, an additional
option is required for stream direction. Below is examples of new command
system for this sub-command.

$ axfer list device -C (= arecord --list-devices)
$ axfer list pcm -P    (= aplay -l)

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/Makefile.am   |   3 +-
 axfer/main.c        |   2 +-
 axfer/subcmd-list.c | 234 ++++++++++++++++++++++++++++++++++++++++++++
 axfer/subcmd.h      |   2 +
 4 files changed, 239 insertions(+), 2 deletions(-)
 create mode 100644 axfer/subcmd-list.c

diff --git a/axfer/Makefile.am b/axfer/Makefile.am
index b48b9c5..4e37b92 100644
--- a/axfer/Makefile.am
+++ b/axfer/Makefile.am
@@ -20,4 +20,5 @@ noinst_HEADERS = \
 axfer_SOURCES = \
 	misc.h \
 	subcmd.h \
-	main.c
+	main.c \
+	subcmd-list.c
diff --git a/axfer/main.c b/axfer/main.c
index 1e92941..c9cf104 100644
--- a/axfer/main.c
+++ b/axfer/main.c
@@ -181,7 +181,7 @@ int main(int argc, char *const *argv)
 	if (subcmd == SUBCMD_TRANSFER)
 		printf("execute 'transfer' subcmd.\n");
 	else if (subcmd == SUBCMD_LIST)
-		printf("execute 'list' subcmd.\n");
+		err = subcmd_list(argc, argv, direction);
 	else if (subcmd == SUBCMD_VERSION)
 		print_version(argv[0]);
 	else
diff --git a/axfer/subcmd-list.c b/axfer/subcmd-list.c
new file mode 100644
index 00000000..f4fd18b
--- /dev/null
+++ b/axfer/subcmd-list.c
@@ -0,0 +1,234 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// subcmd-list.c - operations for list sub command.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#include "subcmd.h"
+#include "misc.h"
+
+#include <getopt.h>
+
+static int dump_device(snd_ctl_t *handle, const char *id, const char *name,
+		       snd_pcm_stream_t direction, snd_pcm_info_t *info)
+{
+	unsigned int count;
+	int i;
+	int err;
+
+	printf("card %i: %s [%s], device %i: %s [%s]\n",
+	       snd_pcm_info_get_card(info), id, name,
+	       snd_pcm_info_get_device(info), snd_pcm_info_get_id(info),
+	       snd_pcm_info_get_name(info));
+
+	count = snd_pcm_info_get_subdevices_count(info);
+	printf("  Subdevices: %i/%i\n",
+	       snd_pcm_info_get_subdevices_avail(info), count);
+
+	for (i = 0; i < count; ++i) {
+		snd_pcm_info_set_subdevice(info, i);
+
+		err = snd_ctl_pcm_info(handle, info);
+		if (err < 0) {
+			printf("control digital audio playback info (%i): %s",
+			       snd_pcm_info_get_card(info), snd_strerror(err));
+			continue;
+		}
+
+		printf("  Subdevice #%i: %s\n",
+		       i, snd_pcm_info_get_subdevice_name(info));
+	}
+
+	return 0;
+}
+
+static int dump_devices(snd_ctl_t *handle, const char *id, const char *name,
+			snd_pcm_stream_t direction)
+{
+	snd_pcm_info_t *info;
+	int device = -1;
+	int err;
+
+	err = snd_pcm_info_malloc(&info);
+	if (err < 0)
+		return err;
+
+	while (1) {
+		err = snd_ctl_pcm_next_device(handle, &device);
+		if (err < 0)
+			break;
+		if (device < 0)
+			break;
+
+		snd_pcm_info_set_device(info, device);
+		snd_pcm_info_set_subdevice(info, 0);
+		snd_pcm_info_set_stream(info, direction);
+		err = snd_ctl_pcm_info(handle, info);
+		if (err < 0)
+			continue;
+
+		err = dump_device(handle, id, name, direction, info);
+		if (err < 0)
+			break;
+	}
+
+	free(info);
+	return err;
+}
+
+static int list_devices(snd_pcm_stream_t direction)
+{
+	int card = -1;
+	char name[32];
+	snd_ctl_t *handle;
+	snd_ctl_card_info_t *info;
+	int err;
+
+	err = snd_ctl_card_info_malloc(&info);
+	if (err < 0)
+		return err;
+
+	// Not found.
+	if (snd_card_next(&card) < 0 || card < 0)
+		goto end;
+
+	printf("**** List of %s Hardware Devices ****\n",
+	       snd_pcm_stream_name(direction));
+
+	while (card >= 0) {
+		sprintf(name, "hw:%d", card);
+		err = snd_ctl_open(&handle, name, 0);
+		if (err < 0) {
+			printf("control open (%i): %s",
+			       card, snd_strerror(err));
+		} else {
+			err = snd_ctl_card_info(handle, info);
+			if (err < 0) {
+				printf("control hardware info (%i): %s",
+				       card, snd_strerror(err));
+			} else {
+				err = dump_devices(handle,
+					snd_ctl_card_info_get_id(info),
+					snd_ctl_card_info_get_name(info),
+					direction);
+			}
+			snd_ctl_close(handle);
+		}
+
+		if (err < 0)
+			break;
+
+		// Go to next.
+		if (snd_card_next(&card) < 0) {
+			printf("snd_card_next");
+			break;
+		}
+	}
+end:
+	free(info);
+	return err;
+}
+
+static int list_pcms(snd_pcm_stream_t direction)
+{
+	static const char *const filters[] = {
+		[SND_PCM_STREAM_CAPTURE]	= "Input",
+		[SND_PCM_STREAM_PLAYBACK]	= "Output",
+	};
+	const char *filter;
+	void **hints;
+	void **n;
+	char *io;
+	char *name;
+	char *desc;
+
+	if (snd_device_name_hint(-1, "pcm", &hints) < 0)
+		return -EINVAL;
+
+	filter = filters[direction];
+
+	n = hints;
+	for (n = hints; *n != NULL; ++n) {
+		io = snd_device_name_get_hint(*n, "IOID");
+		if (io != NULL && strcmp(io, filter) != 0) {
+			free(io);
+			continue;
+		}
+
+		name = snd_device_name_get_hint(*n, "NAME");
+		desc = snd_device_name_get_hint(*n, "DESC");
+
+		printf("%s\n", name);
+		if (desc == NULL) {
+			free(name);
+			free(desc);
+			continue;
+		}
+
+
+		printf("    ");
+		while (*desc) {
+			if (*desc == '\n')
+				printf("\n    ");
+			else
+				putchar(*desc);
+			desc++;
+		}
+		putchar('\n');
+	}
+
+	snd_device_name_free_hint(hints);
+
+	return 0;
+}
+
+static void print_help(void)
+{
+	printf("help for list sub-command.\n");
+}
+
+int subcmd_list(int argc, char *const *argv, snd_pcm_stream_t direction)
+{
+	static const struct {
+		const char *const category;
+		int (*func)(snd_pcm_stream_t direction);
+	} ops[] = {
+		{"device",	list_devices},
+		{"pcm",		list_pcms},
+	};
+	int i;
+	static const char *s_opts = "hlL";
+	static const struct option l_opts[] = {
+		{"list-devices",	0, NULL, 'l'},
+		{"list-pcms",		0, NULL, 'L'},
+		{NULL,			0, NULL, 0}
+	};
+	int c;
+
+	// Renewed command system.
+	if (argc > 2 && !strcmp(argv[1], "list")) {
+		for (i = 0; i < ARRAY_SIZE(ops); ++i) {
+			if (!strcmp(ops[i].category, argv[2]))
+				return ops[i].func(direction);
+		}
+	}
+
+	// Original command system.
+	optind = 0;
+	opterr = 0;
+	while (1) {
+		c = getopt_long(argc, argv, s_opts, l_opts, NULL);
+		if (c < 0)
+			break;
+		if (c == 'l')
+			return list_devices(direction);
+		if (c == 'L')
+			return list_pcms(direction);
+	}
+
+	print_help();
+
+	return 0;
+}
diff --git a/axfer/subcmd.h b/axfer/subcmd.h
index 470a415..f78b766 100644
--- a/axfer/subcmd.h
+++ b/axfer/subcmd.h
@@ -11,4 +11,6 @@
 
 #include <alsa/asoundlib.h>
 
+int subcmd_list(int argc, char *const *argv, snd_pcm_stream_t direction);
+
 #endif
-- 
2.19.1

  reply	other threads:[~2018-11-13  6:41 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1542090296222634077-webhooks-bot@alsa-project.org>
2018-11-13  6:24 ` alsa-utils: axfer: rewrite aplay, adding 'timer-based scheduling' option GitHub pull_request - opened
2018-11-13  6:41   ` [PATCH 01/35] axfer: add an entry point for this command Takashi Sakamoto
2018-11-13  6:41     ` Takashi Sakamoto [this message]
2018-11-13  6:41     ` [PATCH 03/35] axfer: add a common interface to handle a file with audio-specific data format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 04/35] axfer: add support for a container of Microsoft/IBM RIFF/Wave format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 05/35] axfer: add support for a container of Sparc AU format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 06/35] axfer: add support for a container of Creative Tech. voice format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 07/35] axfer: add support for a container of raw data Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 08/35] axfer: add unit test for container interface Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 09/35] axfer: add a common interface to align data frames on different layout Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 10/35] axfer: add support for a mapper for single target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 11/35] axfer: add support for a mapper for multiple target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 12/35] axfer: add a unit test for mapper interface Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 13/35] axfer: add a common interface to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 14/35] axfer: add a parser for command-line options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 15/35] axfer: add support to transfer data frames by alsa-lib PCM APIs Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 16/35] axfer: add support for blocking data transmission operation of alsa-lib PCM API Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 17/35] axfer: add a sub-command to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 18/35] axfer: add informative output and an option to suppress it Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 19/35] axfer: add an option to dump available hardware parameters Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 20/35] axfer: add options related to duration and obsolete '--max-file-size' option Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 21/35] axfer: add an option to finish transmission at XRUN Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 22/35] axfer: add support for non-blocking operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 23/35] axfer: add support for MMAP PCM operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 24/35] axfer: add an option to suppress event waiting Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 25/35] axfer: add options for buffer arrangement Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 26/35] axfer: add options for software parameters of PCM substream Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 27/35] axfer: add options for plugins in alsa-lib Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 28/35] axfer: add a common interface of waiter for I/O event notification Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 29/35] axfer: add an option for waiter type Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 30/35] axfer: add an implementation of waiter for poll(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 31/35] axfer: add an implementation of waiter for select(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 32/35] axfer: add an implementation of waiter for epoll(7) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 33/35] axfer: add support for timer-based scheduling model with MMAP operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 34/35] axfer: obsolete some unimplemented options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 35/35] axfer: add support for libffado transmission backend Takashi Sakamoto
2018-11-13 11:27     ` [PATCH 01/35] axfer: add an entry point for this command Takashi Iwai
2018-11-14 16:05       ` Takashi Sakamoto
2018-11-14 16:23         ` Takashi Iwai

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=20181113064147.13577-2-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=perex@perex.cz \
    --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.